|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +using Crc32C; |
| 6 | + |
| 7 | +using NUnit.Framework; |
| 8 | + |
| 9 | +using E = Force.Blazer.Algorithms.Crc32C.Crc32C; |
| 10 | + |
| 11 | +namespace Blazer.Net.Tests |
| 12 | +{ |
| 13 | + [TestFixture] |
| 14 | + public class Crc32Tests |
| 15 | + { |
| 16 | + [TestCase("Hello", 3)] |
| 17 | + [TestCase("Nazdar", 0)] |
| 18 | + [TestCase("Ahoj", 1)] |
| 19 | + [TestCase("Very long text.Very long text.Very long text.Very long text.Very long text.Very long text.Very long text", 0)] |
| 20 | + [TestCase("Very long text.Very long text.Very long text.Very long text.Very long text.Very long text.Very long text", 3)] |
| 21 | + public void ResultConsistency(string text, int offset) |
| 22 | + { |
| 23 | + var bytes = Encoding.ASCII.GetBytes(text); |
| 24 | + |
| 25 | + var crc1 = E.Calculate(bytes.Skip(offset).ToArray()); |
| 26 | + var crc2 = Crc32CAlgorithm.Append(0, bytes, offset, bytes.Length - offset); |
| 27 | + Assert.That(crc2, Is.EqualTo(crc1)); |
| 28 | + } |
| 29 | + |
| 30 | + [Test] |
| 31 | + public void ResultConsistencyLong() |
| 32 | + { |
| 33 | + var bytes = new byte[30000]; |
| 34 | + new Random().NextBytes(bytes); |
| 35 | + var crc1 = E.Calculate(bytes, 0, bytes.Length); |
| 36 | + var crc2 = Crc32CAlgorithm.Append(0, bytes, 0, bytes.Length); |
| 37 | + Assert.That(crc2, Is.EqualTo(crc1)); |
| 38 | + } |
| 39 | + |
| 40 | + [Test] |
| 41 | + public void ResultConsistency2() |
| 42 | + { |
| 43 | + Assert.That(E.Calculate(new byte[] { 1 }), Is.EqualTo(Crc32CAlgorithm.Compute(new byte[] { 1 }))); |
| 44 | + Assert.That(E.Calculate(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }), Is.EqualTo(Crc32CAlgorithm.Compute(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }))); |
| 45 | + } |
| 46 | + |
| 47 | + [Test] |
| 48 | + public void PartIsWhole() |
| 49 | + { |
| 50 | + var bytes = new byte[30000]; |
| 51 | + new Random().NextBytes(bytes); |
| 52 | + var r1 = E.Calculate(0, bytes, 0, 15000); |
| 53 | + var r2 = E.Calculate(r1, bytes, 15000, 15000); |
| 54 | + var r3 = E.Calculate(0, bytes, 0, 30000); |
| 55 | + Assert.That(r2, Is.EqualTo(r3)); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments