mirror of
https://github.com/claunia/cuetools.net.git
synced 2025-12-16 18:14:25 +00:00
Calculating CRC32 with offsets and fast
This commit is contained in:
@@ -20,8 +20,6 @@ namespace CUETools.TestCodecs
|
||||
|
||||
private TestContext testContextInstance;
|
||||
|
||||
private int[,] testSamples = new int[,] { { 0, 0 }, { -2, -3 }, { 32767, 32766 }, { -32765, -32764 } };
|
||||
|
||||
/// <summary>
|
||||
///Gets or sets the test context which provides
|
||||
///information about and functionality for the current test run.
|
||||
@@ -79,23 +77,39 @@ namespace CUETools.TestCodecs
|
||||
public void CalculateCRCsTest()
|
||||
{
|
||||
CDImageLayout toc = new CDImageLayout();
|
||||
toc.AddTrack(new CDTrack(1, 0, 10, true, false));
|
||||
toc.AddTrack(new CDTrack(1, 03, 20, true, false));
|
||||
toc.AddTrack(new CDTrack(2, 23, 20, true, false));
|
||||
toc.AddTrack(new CDTrack(3, 43, 20, true, false));
|
||||
AccurateRipVerify target = new AccurateRipVerify(toc);
|
||||
AudioBuffer buff = new AudioBuffer(AudioPCMConfig.RedBook, testSamples, testSamples.GetLength(0));
|
||||
target.CalculateCRCs(buff, 0, testSamples.GetLength(0));
|
||||
Crc32 crc32 = new Crc32();
|
||||
uint crc1 = crc32.ComputeChecksum(0xffffffff, buff.Bytes, 0, buff.ByteLength) ^ 0xffffffff;
|
||||
Assert.AreEqual<uint>(3856971150, crc1, "CRC32 was not set correctly.");
|
||||
|
||||
uint crc2 = crc32.ComputeChecksum(0, buff.Bytes, 0, buff.ByteLength);
|
||||
uint crc3 = crc32.ComputeChecksum(0, buff.Bytes, 0, 7);
|
||||
uint crc4 = crc32.ComputeChecksum(0, buff.Bytes, 7, buff.ByteLength - 7);
|
||||
uint crc5 = crc32.Combine(crc3, crc4, buff.ByteLength - 7);
|
||||
Assert.AreEqual<uint>(crc2, crc5, "CRC32 was not combined correctly.");
|
||||
uint crc6 = crc2 ^ crc32.Combine(crc3, 0, buff.ByteLength - 7);
|
||||
Assert.AreEqual<uint>(crc4, crc6, "CRC32 was not substracted correctly.");
|
||||
Assert.AreEqual<uint>(3856971150, target.CRC32(0), "CRC32 was not set correctly.");
|
||||
Assert.AreEqual<uint>(1921661108, target.CRCWONULL(0), "CRC32WONULL was not set correctly.");
|
||||
for (int sector = 0; sector < toc.AudioLength; sector++)
|
||||
{
|
||||
AudioBuffer buff = new AudioBuffer(AudioPCMConfig.RedBook, 588);
|
||||
buff.Length = 588;
|
||||
for (int i = 0; i < buff.Length; i++)
|
||||
{
|
||||
buff.Samples[i, 0] = sector * 588 + i;
|
||||
buff.Samples[i, 1] = sector * 588;
|
||||
}
|
||||
target.Write(buff);
|
||||
}
|
||||
Assert.AreEqual<uint>(3762425816, target.CRC(0), "CRC[0] was not set correctly.");
|
||||
Assert.AreEqual<uint>(3103217968, target.CRC(1), "CRC[1] was not set correctly.");
|
||||
Assert.AreEqual<uint>(3206462296, target.CRC(1, 11), "CRC[1][11] was not set correctly.");
|
||||
Assert.AreEqual<uint>(3068174852, target.CRC(2), "CRC[2] was not set correctly.");
|
||||
Assert.AreEqual<uint>(3233779629, target.CRC32(0), "CRC32[0] was not set correctly.");
|
||||
Assert.AreEqual<uint>(3753760724, target.CRC32(0, 13), "CRC32[0][13] was not set correctly.");
|
||||
Assert.AreEqual<uint>(3153592639, target.CRC32(0, -7), "CRC32[0][-7] was not set correctly.");
|
||||
Assert.AreEqual<uint>(0408974480, target.CRC32(1), "CRC32[1] was not set correctly.");
|
||||
Assert.AreEqual<uint>(4123211700, target.CRC32(2), "CRC32[2] was not set correctly.");
|
||||
Assert.AreEqual<uint>(0297562037, target.CRC32(2, 15), "CRC32[2][15] was not set correctly.");
|
||||
Assert.AreEqual<uint>(0398293317, target.CRC32(2, -1), "CRC32[2][-1] was not set correctly.");
|
||||
Assert.AreEqual<uint>(0564210909, target.CRC32(3), "CRC32[3] was not set correctly.");
|
||||
Assert.AreEqual<uint>(2395016718, target.CRCWONULL(0), "CRC32WONULL[0] was not set correctly.");
|
||||
Assert.AreEqual<uint>(0834934371, target.CRCWONULL(1), "CRC32WONULL[1] was not set correctly.");
|
||||
Assert.AreEqual<uint>(4123211700, target.CRCWONULL(2), "CRC32WONULL[2] was not set correctly.");
|
||||
Assert.AreEqual<uint>(0062860870, target.CRCWONULL(2, 19), "CRC32WONULL[2][19] was not set correctly.");
|
||||
Assert.AreEqual<uint>(0950746738, target.CRCWONULL(2, -2), "CRC32WONULL[2][-2] was not set correctly.");
|
||||
Assert.AreEqual<uint>(0564210909, target.CRCWONULL(3), "CRC32WONULL[3] was not set correctly.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
<Compile Include="AccurateRipTest.cs" />
|
||||
<Compile Include="ALACWriterTest.cs" />
|
||||
<Compile Include="CodecsTest.cs" />
|
||||
<Compile Include="Crc32Test.cs" />
|
||||
<Compile Include="FlakeWriterTest.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
94
CUETools/CUETools.TestCodecs/Crc32Test.cs
Normal file
94
CUETools/CUETools.TestCodecs/Crc32Test.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using CUETools.Codecs;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
namespace CUETools.TestCodecs
|
||||
{
|
||||
|
||||
|
||||
/// <summary>
|
||||
///This is a test class for Crc32Test and is intended
|
||||
///to contain all Crc32Test Unit Tests
|
||||
///</summary>
|
||||
[TestClass()]
|
||||
public class Crc32Test
|
||||
{
|
||||
|
||||
private byte[] testBytes = new byte[] { 0, 0, 1, 0, 254, 255, 253, 255, 255, 127, 254, 127, 3, 128, 4, 128 };
|
||||
|
||||
private TestContext testContextInstance;
|
||||
|
||||
/// <summary>
|
||||
///Gets or sets the test context which provides
|
||||
///information about and functionality for the current test run.
|
||||
///</summary>
|
||||
public TestContext TestContext
|
||||
{
|
||||
get
|
||||
{
|
||||
return testContextInstance;
|
||||
}
|
||||
set
|
||||
{
|
||||
testContextInstance = value;
|
||||
}
|
||||
}
|
||||
|
||||
#region Additional test attributes
|
||||
//
|
||||
//You can use the following additional attributes as you write your tests:
|
||||
//
|
||||
//Use ClassInitialize to run code before running the first test in the class
|
||||
//[ClassInitialize()]
|
||||
//public static void MyClassInitialize(TestContext testContext)
|
||||
//{
|
||||
//}
|
||||
//
|
||||
//Use ClassCleanup to run code after all tests in a class have run
|
||||
//[ClassCleanup()]
|
||||
//public static void MyClassCleanup()
|
||||
//{
|
||||
//}
|
||||
//
|
||||
//Use TestInitialize to run code before running each test
|
||||
//[TestInitialize()]
|
||||
//public void MyTestInitialize()
|
||||
//{
|
||||
//}
|
||||
//
|
||||
//Use TestCleanup to run code after each test has run
|
||||
//[TestCleanup()]
|
||||
//public void MyTestCleanup()
|
||||
//{
|
||||
//}
|
||||
//
|
||||
#endregion
|
||||
|
||||
|
||||
/// <summary>
|
||||
///A test for Combine
|
||||
///</summary>
|
||||
[TestMethod()]
|
||||
public void CombineTest()
|
||||
{
|
||||
Crc32 crc32 = new Crc32();
|
||||
int lenAB = testBytes.Length;
|
||||
int lenA = 7;
|
||||
int lenB = lenAB - lenA;
|
||||
uint crcAB = crc32.ComputeChecksum(0, testBytes, 0, lenAB);
|
||||
uint crcA = crc32.ComputeChecksum(0, testBytes, 0, lenA);
|
||||
uint crcB = crc32.ComputeChecksum(0, testBytes, lenA, lenB);
|
||||
Assert.AreEqual<uint>(crcAB, crc32.Combine(crcA, crcB, lenB), "CRC32 was not combined correctly.");
|
||||
Assert.AreEqual<uint>(crcB, crc32.Combine(crcA, crcAB, lenB), "CRC32 was not substracted correctly.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///A test for ComputeChecksum
|
||||
///</summary>
|
||||
[TestMethod()]
|
||||
public void ComputeChecksumTest()
|
||||
{
|
||||
Crc32 crc32 = new Crc32();
|
||||
uint actual = crc32.ComputeChecksum(0xffffffff, testBytes, 0, testBytes.Length) ^ 0xffffffff;
|
||||
Assert.AreEqual<uint>(2028688632, actual, "CRC32 was not combined correctly.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user