More unit test stuff. New tests generated by copilot

This commit is contained in:
Adrian Conlon
2025-05-13 09:52:12 +01:00
parent 12053fd076
commit caca3467d9
6 changed files with 257 additions and 1 deletions
+105
View File
@@ -0,0 +1,105 @@
namespace EightBit.UnitTest
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EightBit;
[TestClass]
public class ChipTests
{
[TestMethod]
public void Bit_ReturnsCorrectBit()
{
Assert.AreEqual(0x01, Chip.Bit(0));
Assert.AreEqual(0x02, Chip.Bit(1));
Assert.AreEqual(0x80, Chip.Bit(7));
Assert.AreEqual(0x08, Chip.Bit((byte)3));
}
[TestMethod]
public void SetBit_SetsBitCorrectly()
{
Assert.AreEqual(0b00001101, Chip.SetBit(0b00001001, 0b00000100));
Assert.AreEqual(0b00001101, Chip.SetBit(0b00001101, 0b00000100));
Assert.AreEqual(0b00001101, Chip.SetBit(0b00001101, 0b00000100, true));
Assert.AreEqual(0b00001001, Chip.SetBit(0b00001101, 0b00000100, false));
}
[TestMethod]
public void ClearBit_ClearsBitCorrectly()
{
Assert.AreEqual(0b00001001, Chip.ClearBit(0b00001101, 0b00000100));
Assert.AreEqual(0b00001101, Chip.ClearBit(0b00001101, 0b00000100, false));
Assert.AreEqual(0b00001001, Chip.ClearBit(0b00001101, 0b00000100, true));
}
[TestMethod]
public void HighByte_LowByte_WorkCorrectly()
{
ushort value = 0xABCD;
Assert.AreEqual(0xAB, Chip.HighByte(value));
Assert.AreEqual(0xCD, Chip.LowByte(value));
int intValue = 0x1234;
Assert.AreEqual(0x12, Chip.HighByte(intValue));
Assert.AreEqual(0x34, Chip.LowByte(intValue));
}
[TestMethod]
public void PromoteByte_DemoteByte_WorkCorrectly()
{
Assert.AreEqual(0x3400, Chip.PromoteByte(0x34));
Assert.AreEqual(0x12, Chip.DemoteByte(0x1234));
}
[TestMethod]
public void HigherPart_LowerPart_WorkCorrectly()
{
ushort value = 0xABCD;
Assert.AreEqual(0xAB00, Chip.HigherPart(value));
Assert.AreEqual(0xCD, Chip.LowerPart(value));
}
[TestMethod]
public void MakeWord_CreatesCorrectWord()
{
Assert.AreEqual(0x1234, Chip.MakeWord(0x34, 0x12));
}
[TestMethod]
public void NibbleMethods_WorkCorrectly()
{
byte value = 0xAB;
Assert.AreEqual(0xA, Chip.HighNibble(value));
Assert.AreEqual(0xB, Chip.LowNibble(value));
Assert.AreEqual(0xA0, Chip.HigherNibble(value));
Assert.AreEqual(0xB, Chip.LowerNibble(value));
Assert.AreEqual(0xB0, Chip.PromoteNibble(value));
Assert.AreEqual(0xA, Chip.DemoteNibble(value));
}
[TestMethod]
public void CountBits_ReturnsCorrectCount()
{
Assert.AreEqual(0, Chip.CountBits(0));
Assert.AreEqual(1, Chip.CountBits(1));
Assert.AreEqual(8, Chip.CountBits(0xFF));
}
[TestMethod]
public void EvenParity_ReturnsCorrectParity()
{
Assert.IsTrue(Chip.EvenParity(0)); // 0 bits set
Assert.IsFalse(Chip.EvenParity(1)); // 1 bit set
Assert.IsTrue(Chip.EvenParity(3)); // 2 bits set
}
[TestMethod]
public void FindFirstSet_ReturnsCorrectIndex()
{
Assert.AreEqual(0, Chip.FindFirstSet(0));
Assert.AreEqual(1, Chip.FindFirstSet(1));
Assert.AreEqual(2, Chip.FindFirstSet(2));
Assert.AreEqual(3, Chip.FindFirstSet(4));
Assert.AreEqual(5, Chip.FindFirstSet(0b10000));
}
}
}
+85
View File
@@ -0,0 +1,85 @@
namespace EightBit.UnitTest
{
using EightBit;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class DeviceTests
{
[TestMethod]
public void Device_InitialState_IsNotPowered()
{
var device = new Device();
Assert.IsFalse(device.Powered);
Assert.AreEqual(PinLevel.Low, device.POWER);
}
[TestMethod]
public void Device_RaisePOWER_SetsPoweredHigh_AndFiresEvents()
{
var device = new Device();
bool raising = false, raised = false;
device.RaisingPOWER += (s, e) => raising = true;
device.RaisedPOWER += (s, e) => raised = true;
device.RaisePOWER();
Assert.IsTrue(device.Powered);
Assert.AreEqual(PinLevel.High, device.POWER);
Assert.IsTrue(raising);
Assert.IsTrue(raised);
}
[TestMethod]
public void Device_LowerPOWER_SetsPoweredLow_AndFiresEvents()
{
var device = new Device();
device.RaisePOWER(); // Set to High first
bool lowering = false, lowered = false;
device.LoweringPOWER += (s, e) => lowering = true;
device.LoweredPOWER += (s, e) => lowered = true;
device.LowerPOWER();
Assert.IsFalse(device.Powered);
Assert.AreEqual(PinLevel.Low, device.POWER);
Assert.IsTrue(lowering);
Assert.IsTrue(lowered);
}
[TestMethod]
public void Device_RaisePOWER_DoesNothing_IfAlreadyHigh()
{
var device = new Device();
device.RaisePOWER();
bool raising = false, raised = false;
device.RaisingPOWER += (s, e) => raising = true;
device.RaisedPOWER += (s, e) => raised = true;
device.RaisePOWER();
Assert.IsTrue(device.Powered);
Assert.IsFalse(raising);
Assert.IsFalse(raised);
}
[TestMethod]
public void Device_LowerPOWER_DoesNothing_IfAlreadyLow()
{
var device = new Device();
bool lowering = false, lowered = false;
device.LoweringPOWER += (s, e) => lowering = true;
device.LoweredPOWER += (s, e) => lowered = true;
device.LowerPOWER();
Assert.IsFalse(device.Powered);
Assert.IsFalse(lowering);
Assert.IsFalse(lowered);
}
}
}
@@ -0,0 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnableMSTestRunner>true</EnableMSTestRunner>
<OutputType>Exe</OutputType>
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<AnalysisLevel>latest</AnalysisLevel>
<!--
Displays error on console in addition to the log file. Note that this feature comes with a performance impact.
For more information, visit https://learn.microsoft.com/dotnet/core/testing/unit-testing-platform-integration-dotnet-test#show-failure-per-test
-->
<TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<WarningLevel>9999</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<WarningLevel>9999</WarningLevel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" Version="17.12.6" />
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="1.4.3" />
<PackageReference Include="MSTest" Version="3.6.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EightBit.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>
</Project>
@@ -0,0 +1 @@
[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)]
+6
View File
@@ -43,6 +43,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SM83.HarteTest", "LR35902\S
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Z80.HarteTest", "Z80\Z80.HarteTest\Z80.HarteTest.csproj", "{4238F9DF-E58B-456D-86B4-A92381BC471D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EightBit.UnitTest", "EightBit\EightBit.UnitTest\EightBit.UnitTest.csproj", "{A8B3D819-0E84-4586-ADB1-D57AB1A8165B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -129,6 +131,10 @@ Global
{4238F9DF-E58B-456D-86B4-A92381BC471D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4238F9DF-E58B-456D-86B4-A92381BC471D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4238F9DF-E58B-456D-86B4-A92381BC471D}.Release|Any CPU.Build.0 = Release|Any CPU
{A8B3D819-0E84-4586-ADB1-D57AB1A8165B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A8B3D819-0E84-4586-ADB1-D57AB1A8165B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A8B3D819-0E84-4586-ADB1-D57AB1A8165B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A8B3D819-0E84-4586-ADB1-D57AB1A8165B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
+17 -1
View File
@@ -2,12 +2,21 @@
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnableMSTestRunner>true</EnableMSTestRunner>
<GenerateDocumentationFile>False</GenerateDocumentationFile>
<SignAssembly>False</SignAssembly>
<OutputType>Exe</OutputType>
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<AnalysisLevel>latest</AnalysisLevel>
<!--
Displays error on console in addition to the log file. Note that this feature comes with a performance impact.
For more information, visit https://learn.microsoft.com/dotnet/core/testing/unit-testing-platform-integration-dotnet-test#show-failure-per-test
-->
<TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@@ -19,7 +28,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MSTest" Version="3.8.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" Version="17.12.6" />
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="1.4.3" />
<PackageReference Include="MSTest" Version="3.6.4" />
</ItemGroup>
<ItemGroup>
@@ -27,4 +39,8 @@
<ProjectReference Include="..\MC6809.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>
</Project>