Gameboy single step tests converted to unit tests

This commit is contained in:
Adrian Conlon committed 2026-05-19 20:23:38 +01:00
1 parent d8181f5101
commit 5c2095b872
6 files changed
+98 -113

No files matched your search

@@ -1,19 +0,0 @@
namespace SM83.HarteTest
{
internal sealed class ProcessorTestSuite(string location)
{
public string Location { get; set; } = location;
public IEnumerable<OpcodeTestSuite> OpcodeTests()
{
foreach (var filename in Directory.EnumerateFiles(this.Location, "*.json"))
{
var fileInformation = new FileInfo(filename);
if (fileInformation.Length > 0)
{
yield return new OpcodeTestSuite(filename);
}
}
}
}
}
-87
View File
@@ -1,87 +0,0 @@
// <copyright file="Program.cs" company="Adrian Conlon">
// Copyright (c) Adrian Conlon. All rights reserved.
// </copyright>
namespace SM83.HarteTest
{
internal static class Program
{
public static async Task Main(string[] _)
{
var directory = @"C:\github\spectrum\libraries\EightBit\modules\sm83\v1";
await ProcessTestSuiteAsync(directory).ConfigureAwait(false);
}
private static async Task ProcessTestSuiteAsync(string directory)
{
var startTime = DateTime.Now;
var unimplemented_opcode_count = 0;
var invalid_opcode_count = 0;
var runner = new TestRunner();
runner.Initialize();
var checker = new Checker(runner);
checker.Initialise();
var testSuite = new ProcessorTestSuite(directory);
foreach (var opcode in testSuite.OpcodeTests())
{
Console.WriteLine($"Processing: {Path.GetFileName(opcode.Path)}");
List<string?> testNames = [];
var tests = opcode.TestsAsync;
await foreach (var test in tests)
{
if (test is null)
{
throw new InvalidOperationException("Test cannot be null");
}
checker.Check(test);
if (checker.Invalid)
{
++invalid_opcode_count;
// Was it just unimplemented?
if (checker.Unimplemented)
{
++unimplemented_opcode_count;
}
// Let's see if we had any successes!
if (testNames.Count > 0)
{
Console.WriteLine("**** The follow test variations succeeded");
foreach (var testName in testNames)
{
Console.WriteLine($"****** {testName}");
}
}
// OK, we've attempted an implementation, how did it fail?
foreach (var message in checker.Messages)
{
Console.WriteLine($"**** {message}");
}
// I'm not really interested in the remaining tests for this opcode
break;
}
testNames.Add(test.Name);
}
}
var finishTime = DateTime.Now;
var elapsedTime = finishTime - startTime;
Console.Write($"Elapsed time: {elapsedTime.TotalSeconds} seconds");
Console.Write($", unimplemented opcode count: {unimplemented_opcode_count}");
Console.Write($", invalid opcode count: {invalid_opcode_count - unimplemented_opcode_count}");
Console.WriteLine();
}
}
}
+25 -7
View File
@@ -1,12 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.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-all</AnalysisLevel>
<AnalysisLevel>latest-all</AnalysisLevel>
<TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@@ -17,9 +23,21 @@
<WarningLevel>9999</WarningLevel>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\EightBit\EightBit.csproj" />
<ProjectReference Include="..\LR35902.csproj" />
</ItemGroup>
<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\EightBit.csproj" />
<ProjectReference Include="..\LR35902.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="EightBit" />
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>
</Project>
+71
View File
@@ -0,0 +1,71 @@
// <copyright file="SingleStepTests.cs" company="Adrian Conlon">
// Copyright (c) Adrian Conlon. All rights reserved.
// </copyright>
namespace SM83.HarteTest
{
using System.Reflection;
[TestClass]
public class SingleStepTests
{
private const string TestDataDirectory = @"C:\github\spectrum\libraries\EightBit\modules\sm83\v1";
public static IEnumerable<object[]> OpcodeFiles
{
get
{
if (!Directory.Exists(TestDataDirectory))
{
yield break;
}
foreach (var file in Directory.EnumerateFiles(TestDataDirectory, "*.json").OrderBy(f => f))
{
if (new FileInfo(file).Length > 0)
{
yield return [file];
}
}
}
}
public static string GetDisplayName(MethodInfo _, object[] data)
{
ArgumentNullException.ThrowIfNull(data);
return System.IO.Path.GetFileNameWithoutExtension((string)data[0]);
}
[TestMethod]
[DynamicData(nameof(OpcodeFiles), DynamicDataSourceType.Property, DynamicDataDisplayName = nameof(GetDisplayName))]
public async Task Opcode_SingleStep(string filePath)
{
var runner = new TestRunner();
runner.Initialize();
var checker = new Checker(runner);
checker.Initialise();
using var suite = new OpcodeTestSuite(filePath);
await foreach (var test in suite.TestsAsync)
{
if (test is null)
{
throw new InvalidOperationException("Test cannot be null");
}
checker.Check(test);
if (checker.Unimplemented)
{
Assert.Inconclusive($"Opcode not implemented: {test.Name}");
return;
}
if (checker.Invalid)
{
Assert.Fail($"Test '{test.Name}' failed:{System.Environment.NewLine}{string.Join(System.Environment.NewLine, checker.Messages)}");
return;
}
}
}
}
}
+1
View File
@@ -1,5 +1,6 @@
namespace SM83.HarteTest
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "Instantiated by JSON deserializer")]
internal sealed class State
{
public ushort PC { get; set; }
+1
View File
@@ -1,5 +1,6 @@
namespace SM83.HarteTest
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "Instantiated by JSON deserializer")]
internal sealed class Test
{
public string? Name { get; set; }