Add unit tests for `IMemoryApi.HashRegion`
This commit is contained in:
parent
32e36e28bb
commit
a8bffab1b6
|
@ -17,6 +17,8 @@ namespace BizHawk.Common
|
||||||
|
|
||||||
internal const string PREFIX = "SHA256";
|
internal const string PREFIX = "SHA256";
|
||||||
|
|
||||||
|
public /*static readonly*/const string EmptyFile = "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855";
|
||||||
|
|
||||||
#if NET6_0
|
#if NET6_0
|
||||||
public static byte[] Compute(ReadOnlySpan<byte> data)
|
public static byte[] Compute(ReadOnlySpan<byte> data)
|
||||||
=> SHA256.HashData(data);
|
=> SHA256.HashData(data);
|
||||||
|
|
|
@ -3,6 +3,7 @@ using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
|
using BizHawk.Common;
|
||||||
using BizHawk.Client.Common;
|
using BizHawk.Client.Common;
|
||||||
using BizHawk.Emulation.Common;
|
using BizHawk.Emulation.Common;
|
||||||
|
|
||||||
|
@ -15,7 +16,12 @@ namespace BizHawk.Tests.Client.Common.Api
|
||||||
{
|
{
|
||||||
/// <remarks><see cref="CollectionAssert.AreEqual(ICollection, ICollection)"/> and its overloads can't take <see cref="IReadOnlyCollection{T}"/> -_-</remarks>
|
/// <remarks><see cref="CollectionAssert.AreEqual(ICollection, ICollection)"/> and its overloads can't take <see cref="IReadOnlyCollection{T}"/> -_-</remarks>
|
||||||
private static void AssertAreSequenceEqual<T>(IReadOnlyList<T> expected, IReadOnlyList<T> actual, string message)
|
private static void AssertAreSequenceEqual<T>(IReadOnlyList<T> expected, IReadOnlyList<T> actual, string message)
|
||||||
=> Assert.IsTrue(actual.SequenceEqual(expected), message);
|
{
|
||||||
|
if (actual.SequenceEqual(expected)) return;
|
||||||
|
Console.WriteLine($"ex[{expected.Count}]: {string.Join(", ", expected.Select(static o => o?.ToString() ?? "null"))}");
|
||||||
|
Console.WriteLine($"ac[{actual.Count}]: {string.Join(", ", actual.Select(static o => o?.ToString() ?? "null"))}");
|
||||||
|
Assert.Fail(message);
|
||||||
|
}
|
||||||
|
|
||||||
private IMemoryApi CreateDummyApi(byte[] memDomainContents)
|
private IMemoryApi CreateDummyApi(byte[] memDomainContents)
|
||||||
=> new MemoryApi(Console.WriteLine) //TODO capture and check for error messages?
|
=> new MemoryApi(Console.WriteLine) //TODO capture and check for error messages?
|
||||||
|
@ -106,5 +112,38 @@ namespace BizHawk.Tests.Client.Common.Api
|
||||||
memApi => memApi.WriteByteRange(9, new byte[] { 0x01, 0x23, 0x45, 0x67 }),
|
memApi => memApi.WriteByteRange(9, new byte[] { 0x01, 0x23, 0x45, 0x67 }),
|
||||||
"fully above upper boundary");
|
"fully above upper boundary");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void TestHash()
|
||||||
|
{
|
||||||
|
var memApi = CreateDummyApi(new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF });
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(
|
||||||
|
() => memApi.HashRegion(addr: -5, count: 3),
|
||||||
|
"fully below lower boundary");
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(
|
||||||
|
() => memApi.HashRegion(addr: -2, count: 4),
|
||||||
|
"crosses lower boundary");
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(
|
||||||
|
() => memApi.HashRegion(addr: -1, count: 10),
|
||||||
|
"crosses both boundaries");
|
||||||
|
Assert.AreEqual(
|
||||||
|
"55C53F5D490297900CEFA825D0C8E8E9532EE8A118ABE7D8570762CD38BE9818",
|
||||||
|
memApi.HashRegion(addr: 0, count: 8),
|
||||||
|
"whole domain");
|
||||||
|
Assert.AreEqual(
|
||||||
|
"233ABF8F525463C943A10F4DC3080B1BDA19D2EEAA4ACF4676F44689CED29232",
|
||||||
|
memApi.HashRegion(addr: 1, count: 5),
|
||||||
|
"strict contains");
|
||||||
|
Assert.AreEqual(
|
||||||
|
SHA256Checksum.EmptyFile,
|
||||||
|
memApi.HashRegion(addr: 3, count: 0),
|
||||||
|
"empty");
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(
|
||||||
|
() => memApi.HashRegion(addr: 6, count: 3),
|
||||||
|
"crosses upper boundary");
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(
|
||||||
|
() => memApi.HashRegion(addr: 9, count: 4),
|
||||||
|
"fully above upper boundary");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue