mer unit test

This commit is contained in:
Geir Okkenhaug Jerstad 2025-01-03 13:23:45 +01:00
parent 8ff1f69ea3
commit 5b6d47da85
15 changed files with 386 additions and 0 deletions

22
Emne 3/Calc/Calc.sln Normal file
View file

@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Calc", "Calc\Calc.csproj", "{316D55FE-FAD0-4686-BD29-68E4B6B8DBE8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Calculator.Test", "Calculator.Test\Calculator.Test.csproj", "{57206669-909C-4445-B3B9-2444902E1C20}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{316D55FE-FAD0-4686-BD29-68E4B6B8DBE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{316D55FE-FAD0-4686-BD29-68E4B6B8DBE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{316D55FE-FAD0-4686-BD29-68E4B6B8DBE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{316D55FE-FAD0-4686-BD29-68E4B6B8DBE8}.Release|Any CPU.Build.0 = Release|Any CPU
{57206669-909C-4445-B3B9-2444902E1C20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{57206669-909C-4445-B3B9-2444902E1C20}.Debug|Any CPU.Build.0 = Debug|Any CPU
{57206669-909C-4445-B3B9-2444902E1C20}.Release|Any CPU.ActiveCfg = Release|Any CPU
{57206669-909C-4445-B3B9-2444902E1C20}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View file

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View file

@ -0,0 +1,29 @@
namespace Calc;
public class Calculator
{
public int LeggSammen(int a, int b)
{
return a + b;
}
public int TrekkFra(int a, int b)
{
return a - b;
}
public int Multipliser(int a, int b)
{
return a * b;
}
public int Divider(int a, int b)
{
if (b == 0)
{
throw new ArgumentException("Cannot divide by zero.");
}
return a / b;
}
}

View file

@ -0,0 +1,3 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

View file

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="NUnit" Version="3.14.0"/>
<PackageReference Include="NUnit.Analyzers" Version="3.9.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Calc\Calc.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,43 @@
namespace Calculator.Test;
public class Tests
{
[Test]
public void TestLeggSammen()
{
var calc = new Calc.Calculator();
var sum = calc.LeggSammen(1, 2);
Assert.That(sum, Is.EqualTo(3));
}
[Test]
public void TestTrekkFra()
{
var calc = new Calc.Calculator();
var sum = calc.TrekkFra(2, 1);
Assert.That(sum, Is.EqualTo(1));
}
[Test]
public void TestMultipliser()
{
var calc = new Calc.Calculator();
var sum = calc.Multipliser(2, 2);
Assert.That(sum, Is.EqualTo(4));
}
[Test]
public void TestDivision()
{
var calc = new Calc.Calculator();
var sum = calc.Divider(4, 2);
Assert.That(sum, Is.EqualTo(2));
}
[Test]
public void TestDivisionByZero()
{
var calc = new Calc.Calculator();
Assert.Throws<ArgumentException>(() => calc.Divider(4, 0));
}
}

View file

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="NUnit" Version="3.14.0"/>
<PackageReference Include="NUnit.Analyzers" Version="3.9.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\StigespilletTDD\StigespilletTDD.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,12 @@
namespace GameTest;
using StigespilletTDD
public class Tests
{
[Test]
public void ()
{
Assert.Pass();
}
}

View file

@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StigespilletTDD", "StigespilletTDD\StigespilletTDD.csproj", "{EA04112E-4F91-4EFF-98A6-09C05202F318}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StigespilletTDD.test", "StigespilletTDD.test\StigespilletTDD.test.csproj", "{BCE58F6B-442E-4977-A26E-10E39AE516BD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EA04112E-4F91-4EFF-98A6-09C05202F318}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EA04112E-4F91-4EFF-98A6-09C05202F318}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA04112E-4F91-4EFF-98A6-09C05202F318}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA04112E-4F91-4EFF-98A6-09C05202F318}.Release|Any CPU.Build.0 = Release|Any CPU
{BCE58F6B-442E-4977-A26E-10E39AE516BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BCE58F6B-442E-4977-A26E-10E39AE516BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BCE58F6B-442E-4977-A26E-10E39AE516BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BCE58F6B-442E-4977-A26E-10E39AE516BD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View file

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="NUnit" Version="3.14.0"/>
<PackageReference Include="NUnit.Analyzers" Version="3.9.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\StigespilletTDD\StigespilletTDD.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,72 @@
namespace StigespilletTDD.test;
using StigespilletTDD;
public class Tests
{
[Test]
public void TestStartAt0()
{
var game = new Game(4);
int position = game.GetPlayerPosition(0);
Assert.AreEqual(0, position);
}
[Test]
public void TestInitialMove()
{
var game = new Game(4);
game.Move(1, 4);
var position = game.GetPlayerPosition(0);
Assert.AreEqual(4, position);
}
[Test]
public void TestMultiplePlayerPosition()
{
var game = new Game(2);
game.Move(0, 3);
game.Move(1, 4);
Assert.AreEqual(3, game.GetPlayerPosition(0));
Assert.AreEqual(4, game.GetPlayerPosition(1));
}
[TestCase(1,40)]
[TestCase(36,52)]
[TestCase(24,5)]
public void TestLadder(int posFrom, int posTo)
{
var game = new Game(1);
game.Move(0,posFrom);
Assert.AreEqual(posTo, game.GetPlayerPosition(0));
}
[Test]
public void TestDiceMax6()
{
}
[Test]
public void TestNotWinning()
{
var game = new Game(1);
game.Move(0, 2);
var winner = game.GetWinner();
Assert.IsNull(winner);
}
[Test]
public void TestWinning()
{
var game = new Game(1);
game.Move(0, 1);
game.Move(0, 3);
game.Move(0, 3);
game.Move(0, 6);
game.Move(0, 2);
var winner = game.GetWinner();
Assert.AreEqual(0, winner);
}
}

View file

@ -0,0 +1,63 @@
namespace StigespilletTDD;
public class Game
{
private int[] _positions;
private readonly Ladder[] _ladders;
public Game(int playerCount)
{
_positions = new int[playerCount];
_ladders = new[]
{
new Ladder(1, 40),
new Ladder(8, 10),
new Ladder(36, 52),
new Ladder(43, 62),
new Ladder(49, 79),
new Ladder(65, 82),
new Ladder(68, 85),
new Ladder(87, 70),
new Ladder(74, 12),
new Ladder(64, 27),
new Ladder(56, 37),
new Ladder(42, 30),
new Ladder(33, 3),
new Ladder(24, 5),
};
}
public int GetPlayerPosition(int playerIndex)
{
return _positions[playerIndex];
}
public void Move(int playerIndex, int moveCount)
{
var pos = _positions[playerIndex] += moveCount;
var ladder = FindLadder(pos);
if (ladder != null)
{
_positions[playerIndex] = ladder.PositionTo;
}
}
private Ladder FindLadder(int pos)
{
return _ladders.FirstOrDefault(l => l.PositionFrom == pos);
}
public int? GetWinner()
{
if (GetPlayerPosition(0) == 90)
{
return 0;
}
else
{
return null;
}
}
}

View file

@ -0,0 +1,13 @@
namespace StigespilletTDD;
public class Ladder
{
public int PositionFrom { get; set; }
public int PositionTo { get; set; }
public Ladder(int positionFrom, int positionTo)
{
PositionFrom = positionFrom;
PositionTo = positionTo;
}
}

View file

@ -0,0 +1,3 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

View file

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>