From 484a1d8fa4092833904ef914c21ac2dbc0c26794 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Mon, 4 Oct 2021 09:53:49 +1000 Subject: [PATCH] Add unit tests for CRC32 impls --- .../Common/checksums/CRC32Tests.cs | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/BizHawk.Tests/Common/checksums/CRC32Tests.cs diff --git a/src/BizHawk.Tests/Common/checksums/CRC32Tests.cs b/src/BizHawk.Tests/Common/checksums/CRC32Tests.cs new file mode 100644 index 0000000000..f90c01f2ca --- /dev/null +++ b/src/BizHawk.Tests/Common/checksums/CRC32Tests.cs @@ -0,0 +1,49 @@ +using BizHawk.Common; +using BizHawk.Emulation.DiscSystem; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace BizHawk.Tests.Common.checksums +{ + [TestClass] + public sealed class CRC32Tests + { + private const uint EXPECTED = 0xDA3BA10AU; + + private const uint EXPECTED_COMBINED = 0xED182BB0U; + + private const uint EXPECTED_EXTRA = 0x29058C73U; + + [TestMethod] + public void TestCRC32Stability() + { + static byte[] InitialiseArray() + { + var a = new byte[0x100]; + for (var i = 0; i < 0x100; i++) a[i] = (byte) ~i; + return a; + } + static byte[] InitialiseArrayExtra() + { + var a = new byte[0x100]; + for (var i = 0; i < 0x100; i++) a[i] = (byte) i; + return a; + } + + var data = InitialiseArray(); + Assert.AreEqual(EXPECTED, (uint) CRC32.Calculate(data)); + + data = InitialiseArray(); + DiscHasher.SpecialCRC32 crc32 = new(); + crc32.Add(data, 0, data.Length); + Assert.AreEqual(EXPECTED, crc32.Result); + + var dataExtra = InitialiseArrayExtra(); + DiscHasher.SpecialCRC32 crc32Extra = new(); + crc32Extra.Add(dataExtra, 0, dataExtra.Length); + Assert.AreEqual(EXPECTED_EXTRA, crc32Extra.Result); + crc32.Incorporate(crc32Extra.Result, dataExtra.Length); + Assert.AreEqual(EXPECTED_COMBINED, crc32.Result); + } + } +}