Skip to content

Commit

Permalink
added Crc32C support
Browse files Browse the repository at this point in the history
  • Loading branch information
force-net committed Jul 3, 2017
1 parent 128fc86 commit 6bdb7a9
Show file tree
Hide file tree
Showing 28 changed files with 8,199 additions and 36 deletions.
4 changes: 3 additions & 1 deletion Crc32.NET.Core.sln
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Crc32.NET.Core", "Crc32.NET\Crc32.NET.Core.xproj", "{4889B52E-03E0-4632-9655-B209FBCD6EBA}"
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Crc32.NET", "Crc32.NET\Crc32.NET.xproj", "{4889B52E-03E0-4632-9655-B209FBCD6EBA}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Crc32.NET.Tests", "Crc32.NET.Tests\Crc32.NET.Tests.xproj", "{561519E1-4C48-4934-B56D-042835CD2D29}"
EndProject
5 changes: 4 additions & 1 deletion Crc32.NET.Tests/BytePatternsTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#if !NETCORE
using System;
using System.Linq;
using Force.Crc32.Tests.Crc32Implementations;
using NUnit.Framework;
Expand Down Expand Up @@ -89,3 +90,5 @@ private void TestByteSequence(byte[] data)
private readonly CrcCalculator _referenceImplementation = new System_Data_HashFunction_CRC();
}
}

#endif
7 changes: 7 additions & 0 deletions Crc32.NET.Tests/Crc32.NET.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Crc32.1.0.0\lib\net40\Crc32.dll</HintPath>
</Reference>
<Reference Include="Crc32C.NET, Version=1.0.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Crc32C.NET.1.0.5.0\lib\net20\Crc32C.NET.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dexiom.QuickCrc32, Version=1.0.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dexiom.QuickCrc32.1.0.3\lib\net40\Dexiom.QuickCrc32.dll</HintPath>
<Private>True</Private>
Expand All @@ -68,6 +72,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BytePatternsTest.cs" />
<Compile Include="Crc32Implementations\Crc32C_Crc32CAlgorithm.cs" />
<Compile Include="Crc32Implementations\Force_Crc32_Crc32CAlgorithm.cs" />
<Compile Include="ImplementationCTest.cs" />
<Compile Include="Crc32Implementations\CH_Crc32_Crc.cs" />
<Compile Include="Crc32Implementations\Dexiom_Quick_Crc32.cs" />
<Compile Include="Crc32Implementations\Crc32_Crc32Algorithm.cs" />
Expand Down
21 changes: 21 additions & 0 deletions Crc32.NET.Tests/Crc32.NET.Tests.xproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>

<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>{561519E1-4C48-4934-B56D-042835CD2D29}</ProjectGuid>
<RootNamespace>Crc32.NET.Tests</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
</PropertyGroup>

<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
5 changes: 4 additions & 1 deletion Crc32.NET.Tests/Crc32Implementations/CH_Crc32_Crc.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Force.Crc32.Tests.Crc32Implementations
#if !NETCORE

namespace Force.Crc32.Tests.Crc32Implementations
{
public class CH_Crc32_Crc : CrcCalculator
{
Expand All @@ -12,3 +14,4 @@ public override uint Calculate(byte[] data)
}
}
}
#endif
16 changes: 16 additions & 0 deletions Crc32.NET.Tests/Crc32Implementations/Crc32C_Crc32CAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#if !NETCORE
namespace Force.Crc32.Tests.Crc32Implementations
{
public class Crc32C_Crc32CAlgorithm : CrcCalculator
{
public Crc32C_Crc32CAlgorithm() : base("Crc32C.Crc32CAlgorithm")
{
}

public override uint Calculate(byte[] data)
{
return Crc32C.Crc32CAlgorithm.Compute(data);
}
}
}
#endif
4 changes: 3 additions & 1 deletion Crc32.NET.Tests/Crc32Implementations/Crc32_Crc32Algorithm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Crc = Crc32.Crc32Algorithm;
#if !NETCORE
using Crc = Crc32.Crc32Algorithm;

namespace Force.Crc32.Tests.Crc32Implementations
{
Expand All @@ -14,3 +15,4 @@ public override uint Calculate(byte[] data)
}
}
}
#endif
4 changes: 3 additions & 1 deletion Crc32.NET.Tests/Crc32Implementations/Dexiom_Quick_Crc32.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Force.Crc32.Tests.Crc32Implementations
#if !NETCORE
namespace Force.Crc32.Tests.Crc32Implementations
{
public class Dexiom_Quick_Crc32 : CrcCalculator
{
Expand All @@ -13,3 +14,4 @@ public override uint Calculate(byte[] data)
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Force.Crc32.Tests.Crc32Implementations
{
public class Force_Crc32_Crc32CAlgorithm : CrcCalculator
{
public Force_Crc32_Crc32CAlgorithm() : base("Force.Crc32.Crc32CAlgorithm")
{
}

public override uint Calculate(byte[] data)
{
return Crc32CAlgorithm.Compute(data);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Force.Crc32.Tests.Crc32Implementations
#if !NETCORE
namespace Force.Crc32.Tests.Crc32Implementations
{
public class Klinkby_Checkum_Crc32 : CrcCalculator
{
Expand All @@ -12,3 +13,4 @@ public override uint Calculate(byte[] data)
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#if !NETCORE
using System;
using System.Data.HashFunction;

namespace Force.Crc32.Tests.Crc32Implementations
Expand All @@ -18,3 +19,4 @@ public override uint Calculate(byte[] data)
private readonly CRC _crc;
}
}
#endif
79 changes: 79 additions & 0 deletions Crc32.NET.Tests/ImplementationCTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Linq;
using System.Text;

using NUnit.Framework;

#if !NETCORE
using E = Crc32C.Crc32CAlgorithm;
#endif

namespace Force.Crc32.Tests
{
[TestFixture]
public class ImplementationCTest
{
#if !NETCORE
[TestCase("Hello", 3)]
[TestCase("Nazdar", 0)]
[TestCase("Ahoj", 1)]
[TestCase("Very long text.Very long text.Very long text.Very long text.Very long text.Very long text.Very long text", 0)]
[TestCase("Very long text.Very long text.Very long text.Very long text.Very long text.Very long text.Very long text", 3)]
public void ResultConsistency(string text, int offset)
{
var bytes = Encoding.ASCII.GetBytes(text);

var crc1 = E.Compute(bytes.Skip(offset).ToArray());
var crc2 = Crc32CAlgorithm.Append(0, bytes, offset, bytes.Length - offset);
Assert.That(crc2, Is.EqualTo(crc1));
}
#endif

[Test]
public void ResultConsistency2()
{
Assert.That(Crc32CAlgorithm.Compute(new byte[] { 1 }), Is.EqualTo(0xA016D052));
Assert.That(Crc32CAlgorithm.Compute(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }), Is.EqualTo(0xB219DB69));
}

#if !NETCORE
[Test]
public void ResultConsistencyAsHashAlgorithm()
{
var bytes = new byte[30000];
new Random().NextBytes(bytes);
var e = new E();
// other implementation is little-endian
var c = new Crc32CAlgorithm(false);
var crc1 = BitConverter.ToInt32(e.ComputeHash(bytes), 0);
var crc2 = BitConverter.ToInt32(c.ComputeHash(bytes), 0);
Console.WriteLine(crc1.ToString("X8"));
Console.WriteLine(crc2.ToString("X8"));
Assert.That(crc1, Is.EqualTo(crc2));
}
#endif

[Test]
public void PartIsWhole()
{
var bytes = new byte[30000];
new Random().NextBytes(bytes);
var r1 = Crc32CAlgorithm.Append(0, bytes, 0, 15000);
var r2 = Crc32CAlgorithm.Append(r1, bytes, 15000, 15000);
var r3 = Crc32CAlgorithm.Append(0, bytes, 0, 30000);
Assert.That(r2, Is.EqualTo(r3));
}

[Test]
public void Result_Is_BigEndian()
{
var bytes = new byte[30000];
new Random().NextBytes(bytes);
var crc1 = Crc32CAlgorithm.Append(0, bytes, 0, bytes.Length);
var crc2Bytes = new Crc32CAlgorithm().ComputeHash(bytes);
if (BitConverter.IsLittleEndian) crc2Bytes = crc2Bytes.Reverse().ToArray();
var crc2 = BitConverter.ToUInt32(crc2Bytes, 0);
Assert.That(crc2, Is.EqualTo(crc1));
}
}
}
22 changes: 21 additions & 1 deletion Crc32.NET.Tests/ImplementationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

using NUnit.Framework;

#if !NETCORE
using E = Crc32.Crc32Algorithm;
#endif


namespace Force.Crc32.Tests
{
[TestFixture]
public class ImplementationTest
{

#if !NETCORE
[TestCase("Hello", 3)]
[TestCase("Nazdar", 0)]
[TestCase("Ahoj", 1)]
Expand All @@ -24,14 +29,16 @@ public void ResultConsistency(string text, int offset)
var crc2 = Crc32Algorithm.Append(0, bytes, offset, bytes.Length - offset);
Assert.That(crc2, Is.EqualTo(crc1));
}

#endif

[Test]
public void ResultConsistency2()
{
Assert.That(Crc32Algorithm.Compute(new byte[] { 1 }), Is.EqualTo(2768625435));
Assert.That(Crc32Algorithm.Compute(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }), Is.EqualTo(622876539));
}

#if !NETCORE
[Test]
public void ResultConsistencyAsHashAlgorithm()
{
Expand All @@ -45,6 +52,7 @@ public void ResultConsistencyAsHashAlgorithm()
Console.WriteLine(crc2.ToString("X8"));
Assert.That(crc1, Is.EqualTo(crc2));
}
#endif

[Test]
public void PartIsWhole()
Expand All @@ -68,5 +76,17 @@ public void Result_Is_BigEndian()
var crc2 = BitConverter.ToUInt32(crc2Bytes, 0);
Assert.That(crc2, Is.EqualTo(crc1));
}

[Test]
public void Result_Is_LittleEndian_IF_Specified()
{
var bytes = new byte[30000];
new Random().NextBytes(bytes);
var crc1 = Crc32Algorithm.Append(0, bytes, 0, bytes.Length);
var crc2Bytes = new Crc32Algorithm(false).ComputeHash(bytes);
if (!BitConverter.IsLittleEndian) crc2Bytes = crc2Bytes.Reverse().ToArray();
var crc2 = BitConverter.ToUInt32(crc2Bytes, 0);
Assert.That(crc2, Is.EqualTo(crc1));
}
}
}
16 changes: 15 additions & 1 deletion Crc32.NET.Tests/PerformanceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Force.Crc32.Tests
[TestFixture]
public class PerformanceTest
{
#if !NETCORE
[Test]
public void ThroughputCHCrc32_By_tanglebones()
{
Expand Down Expand Up @@ -37,7 +38,14 @@ public void ThroughputCrc32_By_Dexiom()
{
Calculate(new Dexiom_Quick_Crc32());
}


[Test]
public void ThroughputCrc32C_Crc32C()
{
Calculate(new Crc32C_Crc32CAlgorithm());
}
#endif

[Test]
public void ThroughputCrc32_By_Me()
{
Expand All @@ -50,6 +58,12 @@ public void ThroughputCrc32_By_Me_Unaligned()
Calculate(new Force_Crc32_Crc32Algorithm(), 60);
}

[Test]
public void ThroughputCrc32C_By_Me()
{
Calculate(new Force_Crc32_Crc32CAlgorithm());
}

private void Calculate(CrcCalculator implementation, int size = 65536)
{
var data = new byte[size];
Expand Down
2 changes: 2 additions & 0 deletions Crc32.NET.Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ public static class Program
public static void Main()
{
var pt = new PerformanceTest();
#if !NETCORE
pt.ThroughputCrc32_By_dariogriffo();
pt.ThroughputCHCrc32_By_tanglebones();
pt.ThroughputKlinkby_Checksum();
pt.ThroughputCrc32_By_Data_HashFunction_Crc();
pt.ThroughputCrc32_By_Me();
#endif
}
}
}
1 change: 1 addition & 0 deletions Crc32.NET.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<packages>
<package id="CH.Crc32" version="1.0" targetFramework="net40" />
<package id="Crc32" version="1.0.0" targetFramework="net40" />
<package id="Crc32C.NET" version="1.0.5.0" targetFramework="net40" />
<package id="Dexiom.QuickCrc32" version="1.0.3" targetFramework="net40" />
<package id="Klinkby.Checksum" version="1.0.2.1" targetFramework="net40" />
<package id="NUnit" version="2.6.4" targetFramework="net20" />
Expand Down
29 changes: 29 additions & 0 deletions Crc32.NET.Tests/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable"
},
"dependencies": {
"Crc32.NET": "1.1.0",
"NUnit": "3.6.0"
},

"testRunner": "nunit",

"frameworks": {
"netcoreapp1.0": {
"imports": "portable-net45+win8",
"buildOptions": {
"define": ["NETCORE"]
},
"dependencies": {
"NETStandard.Library": "1.6.1",
"dotnet-test-nunit": "3.4.0-beta-3",
"Microsoft.NETCore.App": {
"version": "1.1.0-*",
"type": "platform"
}
}
}
}
}
Loading

0 comments on commit 6bdb7a9

Please sign in to comment.