Skip to content

Commit

Permalink
Merge pull request #24 from nullfx/nuget-updates-endian-fixes
Browse files Browse the repository at this point in the history
Endian bug fixes, nuget package update, documentation additions
  • Loading branch information
nullfx committed Mar 22, 2023
2 parents a3c4b9a + 9f77a33 commit 82cbe69
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cicd-actions.yml
Expand Up @@ -55,6 +55,6 @@ jobs:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
shell: powershell
run: |
.\.sonar\scanner\dotnet-sonarscanner begin /k:"nullfx_NullFX.CRC" /o:"nullfx" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io"
.\.sonar\scanner\dotnet-sonarscanner begin /k:"nullfx_NullFX.CRC" /o:"nullfx" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.dotnet.excludeTestProjects=true
dotnet build -f net7.0 --no-restore
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -260,4 +260,7 @@ paket-files/

# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
*.pyc

# mac files
.DS_Store
5 changes: 3 additions & 2 deletions NullFX.CRC.Benchmarks/NullFX.CRC.Benchmarks.csproj
Expand Up @@ -4,8 +4,9 @@
<OutputType>Exe</OutputType>
<TargetFrameworks>net47;net48;netcoreapp3.1;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<ReleaseVersion>1.1.6</ReleaseVersion>
</PropertyGroup>
<ReleaseVersion>1.1.7</ReleaseVersion>
<SonarQubeExclude>true</SonarQubeExclude>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="System.Data.HashFunction.CRC" Version="2.0.0" />
Expand Down
82 changes: 42 additions & 40 deletions NullFX.CRC.Tests/CrcTests.cs
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Xunit;
using FluentAssertions;

namespace NullFX.CRC.Tests {
public class CrcTests {
Expand All @@ -15,7 +17,7 @@ public class CrcTests {
actual = Crc8.ComputeChecksum ( parameter.TestPayload, range.Start, range.Length );
}
var expected = (byte)(object)parameter.ExpectedCrc;
Assert.Equal ( expected, actual );
$"0x{actual:X2}".Should ( ).Be ( $"0x{expected:X2}" );
}

[Theory]
Expand All @@ -29,7 +31,7 @@ public class CrcTests {
actual = Crc16.ComputeChecksum ( parameter.Algorithm, parameter.TestPayload, range.Start, range.Length );
}
var expected = (ushort)(object)parameter.ExpectedCrc;
Assert.Equal ( expected, actual );
$"0x{actual:X4}".Should ( ).Be ( $"0x{expected:X4}" );
}

[Theory]
Expand All @@ -43,56 +45,56 @@ public class CrcTests {
actual = Crc32.ComputeChecksum ( parameter.TestPayload, range.Start, range.Length );
}
var expected = (uint)(object)parameter.ExpectedCrc;
Assert.Equal ( expected, actual );
$"0x{actual:X8}".Should ( ).Be ( $"0x{expected:X8}" );
}


public static IEnumerable<object[]> Crc8TestData ( ) {
yield return new object[] { new TestParameter<byte> ( buffer, TestRange.All, 0x49 ) };
yield return new object[] { new TestParameter<byte> ( buffer, new TestRange ( 3, 11 ), 0x66 ) };
yield return new object[] { new TestParameter<byte> ( buffer, new TestRange ( 9, 5 ), 0xB0 ) };
yield return new object[] { new TestParameter<byte> ( buffer, new TestRange ( 70, 6 ), 0x63 ) };
yield return new object[] { new TestParameter<byte> ( buffer, TestRange.All, 0x49 ) { TestScenario = "CRC 8, Checksum of all bytes" } };
yield return new object[] { new TestParameter<byte> ( buffer, new TestRange ( 3, 11 ), 0x66 ) { TestScenario = "CRC 8, Checksum of bytes 3-14" } };
yield return new object[] { new TestParameter<byte> ( buffer, new TestRange ( 9, 5 ), 0xB0 ) { TestScenario = "CRC 8, Checksum of bytes 9-14" } };
yield return new object[] { new TestParameter<byte> ( buffer, new TestRange ( 70, 6 ), 0x63 ) { TestScenario = "CRC 8, Checksum of bytes 70-76" } };
}

public static IEnumerable<object[]> Crc16TestData ( ) {
yield return new object[] { new TestParameter<ushort> ( buffer, TestRange.All, 0xFD78 ) { Algorithm = Crc16Algorithm.Standard } };
yield return new object[] { new TestParameter<ushort> ( buffer, TestRange.All, 0xF3C7 ) { Algorithm = Crc16Algorithm.Ccitt } };
yield return new object[] { new TestParameter<ushort> ( buffer, TestRange.All, 0x3ACE ) { Algorithm = Crc16Algorithm.CcittInitialValue0x1D0F } };
yield return new object[] { new TestParameter<ushort> ( buffer, TestRange.All, 0x2237 ) { Algorithm = Crc16Algorithm.CcittInitialValue0xFFFF } };
yield return new object[] { new TestParameter<ushort> ( buffer, TestRange.All, 0xC097 ) { Algorithm = Crc16Algorithm.CcittKermit } };
yield return new object[] { new TestParameter<ushort> ( buffer, TestRange.All, 0x8B02 ) { Algorithm = Crc16Algorithm.Dnp } };
yield return new object[] { new TestParameter<ushort> ( buffer, TestRange.All, 0x16E2 ) { Algorithm = Crc16Algorithm.Modbus } };
yield return new object[] { new TestParameter<ushort> ( buffer, TestRange.All, 0xFD78 ) { Algorithm = Crc16Algorithm.Standard ,TestScenario = "Standard CRC 16, Checksum of all bytes" } };
yield return new object[] { new TestParameter<ushort> ( buffer, TestRange.All, 0xF3C7 ) { Algorithm = Crc16Algorithm.Ccitt ,TestScenario = "CRC 16 CCITT, Checksum of all bytes" } };
yield return new object[] { new TestParameter<ushort> ( buffer, TestRange.All, 0x3ACE ) { Algorithm = Crc16Algorithm.CcittInitialValue0x1D0F ,TestScenario = "CRC 16 CCITT Initial Value: 0x1D0F, Checksum of all bytes" } };
yield return new object[] { new TestParameter<ushort> ( buffer, TestRange.All, 0x2237 ) { Algorithm = Crc16Algorithm.CcittInitialValue0xFFFF ,TestScenario = "CRC 16 CCITT Initial Value: 0xFFFF, Checksum of all bytes" } };
yield return new object[] { new TestParameter<ushort> ( buffer, TestRange.All, 0x97C0 ) { Algorithm = Crc16Algorithm.CcittKermit ,TestScenario = "CRC 16 CCITT Kermit, Checksum of all bytes" } };
yield return new object[] { new TestParameter<ushort> ( buffer, TestRange.All, 0x028B ) { Algorithm = Crc16Algorithm.Dnp ,TestScenario = "CRC 16 DNP, Checksum of all bytes" } };
yield return new object[] { new TestParameter<ushort> ( buffer, TestRange.All, 0x16E2 ) { Algorithm = Crc16Algorithm.Modbus ,TestScenario = "CRC 16 Modbus, Checksum of all bytes" } };

yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 3, 11 ), 0x0B3B ) { Algorithm = Crc16Algorithm.Standard } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 3, 11 ), 0x610B ) { Algorithm = Crc16Algorithm.Ccitt } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 3, 11 ), 0x4907 ) { Algorithm = Crc16Algorithm.CcittInitialValue0x1D0F } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 3, 11 ), 0xB504 ) { Algorithm = Crc16Algorithm.CcittInitialValue0xFFFF } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 3, 11 ), 0x9524 ) { Algorithm = Crc16Algorithm.CcittKermit } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 3, 11 ), 0x293E ) { Algorithm = Crc16Algorithm.Dnp } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 3, 11 ), 0xEF3D ) { Algorithm = Crc16Algorithm.Modbus } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 3, 11 ), 0x0B3B ) { Algorithm = Crc16Algorithm.Standard ,TestScenario = "Standard CRC 16, Checksum of bytes 3-14" } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 3, 11 ), 0x610B ) { Algorithm = Crc16Algorithm.Ccitt ,TestScenario = "CRC 16 CCITT, Checksum of bytes 3-14" } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 3, 11 ), 0x4907 ) { Algorithm = Crc16Algorithm.CcittInitialValue0x1D0F ,TestScenario = "CRC 16 CCITT Initial Value: 0x1D0F, Checksum of bytes 3-14" } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 3, 11 ), 0xB504 ) { Algorithm = Crc16Algorithm.CcittInitialValue0xFFFF ,TestScenario = "CRC 16 CCITT Initial Value: 0xFFFF, Checksum of bytes 3-14" } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 3, 11 ), 0x2495 ) { Algorithm = Crc16Algorithm.CcittKermit ,TestScenario = "CRC 16 CCITT Kermit, Checksum of bytes 3-14" } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 3, 11 ), 0x3E29 ) { Algorithm = Crc16Algorithm.Dnp ,TestScenario = "CRC 16 DNP, Checksum of bytes 3-14" } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 3, 11 ), 0xEF3D ) { Algorithm = Crc16Algorithm.Modbus ,TestScenario = "CRC 16 Modbus, Checksum of bytes 3-14" } };

yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 9, 5 ), 0xE86E ) { Algorithm = Crc16Algorithm.Standard } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 9, 5 ), 0x8917 ) { Algorithm = Crc16Algorithm.Ccitt } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 9, 5 ), 0x78D9 ) { Algorithm = Crc16Algorithm.CcittInitialValue0x1D0F } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 9, 5 ), 0x981B ) { Algorithm = Crc16Algorithm.CcittInitialValue0xFFFF } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 9, 5 ), 0xBECD ) { Algorithm = Crc16Algorithm.CcittKermit } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 9, 5 ), 0x43F9 ) { Algorithm = Crc16Algorithm.Dnp } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 9, 5 ), 0xE84A ) { Algorithm = Crc16Algorithm.Modbus } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 9, 5 ), 0xE86E ) { Algorithm = Crc16Algorithm.Standard ,TestScenario = "Standard CRC 16, Checksum of bytes 9-14" } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 9, 5 ), 0x8917 ) { Algorithm = Crc16Algorithm.Ccitt ,TestScenario = "CRC 16 CCITT, Checksum of bytes 9-14" } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 9, 5 ), 0x78D9 ) { Algorithm = Crc16Algorithm.CcittInitialValue0x1D0F ,TestScenario = "CRC 16 CCITT Initial Value: 0x1D0F, Checksum of bytes 9-14" } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 9, 5 ), 0x981B ) { Algorithm = Crc16Algorithm.CcittInitialValue0xFFFF ,TestScenario = "CRC 16 CCITT Initial Value: 0xFFFF, Checksum of bytes 9-14" } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 9, 5 ), 0xCDBE ) { Algorithm = Crc16Algorithm.CcittKermit ,TestScenario = "CRC 16 CCITT Kermit, Checksum of bytes 9-14" } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 9, 5 ), 0xF943 ) { Algorithm = Crc16Algorithm.Dnp ,TestScenario = "CRC 16 DNP, Checksum of bytes 9-14" } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 9, 5 ), 0xE84A ) { Algorithm = Crc16Algorithm.Modbus ,TestScenario = "CRC 16 Modbus, Checksum of bytes 9-14" } };

yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 70, 6 ), 0x24F6 ) { Algorithm = Crc16Algorithm.Standard } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 70, 6 ), 0x7545 ) { Algorithm = Crc16Algorithm.Ccitt } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 70, 6 ), 0x447B ) { Algorithm = Crc16Algorithm.CcittInitialValue0x1D0F } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 70, 6 ), 0x7B55 ) { Algorithm = Crc16Algorithm.CcittInitialValue0xFFFF } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 70, 6 ), 0x8842 ) { Algorithm = Crc16Algorithm.CcittKermit } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 70, 6 ), 0xE95B ) { Algorithm = Crc16Algorithm.Dnp } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 70, 6 ), 0x3FF6 ) { Algorithm = Crc16Algorithm.Modbus } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 70, 6 ), 0x24F6 ) { Algorithm = Crc16Algorithm.Standard ,TestScenario = "Standard CRC 16, Checksum of bytes 70-76" } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 70, 6 ), 0x7545 ) { Algorithm = Crc16Algorithm.Ccitt ,TestScenario = "CRC 16 CCITT, Checksum of bytes 70-76" } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 70, 6 ), 0x447B ) { Algorithm = Crc16Algorithm.CcittInitialValue0x1D0F ,TestScenario = "CRC 16 CCITT Initial Value: 0x1D0F, Checksum of bytes 70-76" } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 70, 6 ), 0x7B55 ) { Algorithm = Crc16Algorithm.CcittInitialValue0xFFFF ,TestScenario = "CRC 16 CCITT Initial Value: 0xFFFF, Checksum of bytes 70-76" } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 70, 6 ), 0x4288 ) { Algorithm = Crc16Algorithm.CcittKermit ,TestScenario = "CRC 16 CCITT Kermit, Checksum of bytes 70-76" } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 70, 6 ), 0x5BE9 ) { Algorithm = Crc16Algorithm.Dnp ,TestScenario = "CRC 16 DNP, Checksum of bytes 70-76" } };
yield return new object[] { new TestParameter<ushort> ( buffer, new TestRange ( 70, 6 ), 0x3FF6 ) { Algorithm = Crc16Algorithm.Modbus ,TestScenario = "CRC 16 Modbus, Checksum of bytes 70-76" } };
}

public static IEnumerable<object[]> Crc32TestData ( ) {
yield return new object[] { new TestParameter<uint> ( buffer, TestRange.All, 0xCBE25E99 ) };
yield return new object[] { new TestParameter<uint> ( buffer, new TestRange ( 3, 11 ), 0xA0D118A0 ) };
yield return new object[] { new TestParameter<uint> ( buffer, new TestRange ( 9, 5 ), 0xDEF4CFE9 ) };
yield return new object[] { new TestParameter<uint> ( buffer, new TestRange ( 70, 6 ), 0xF373B43B ) };
yield return new object[] { new TestParameter<uint> ( buffer, TestRange.All, 0xCBE25E99 ) { TestScenario = "CRC 32, Checksum of all bytes" } };
yield return new object[] { new TestParameter<uint> ( buffer, new TestRange ( 3, 11 ), 0xA0D118A0 ) { TestScenario = "CRC 32, Checksum of bytes 3-14" } };
yield return new object[] { new TestParameter<uint> ( buffer, new TestRange ( 9, 5 ), 0xDEF4CFE9 ) { TestScenario = "CRC 32, Checksum of bytes 9-14" } };
yield return new object[] { new TestParameter<uint> ( buffer, new TestRange ( 70, 6 ), 0xF373B43B ) { TestScenario = "CRC 32, Checksum of bytes 70-76" } };
}
}
}
Expand Down
49 changes: 25 additions & 24 deletions NullFX.CRC.Tests/NullFX.CRC.Tests.csproj
@@ -1,26 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable>
<IsPublishable>false</IsPublishable>
<ReleaseVersion>1.1.6</ReleaseVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="xunit.assert" Version="2.4.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NullFX.CRC\NullFX.CRC.csproj" />
</ItemGroup>
<ItemGroup>
<None Remove="xunit" />
<None Remove="xunit.runner.visualstudio" />
<None Remove="xunit.assert" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable>
<IsPublishable>false</IsPublishable>
<ReleaseVersion>1.1.7</ReleaseVersion>
<SonarQubeExclude>true</SonarQubeExclude>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="*" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="*" />
<PackageReference Include="xunit" Version="*" />
<PackageReference Include="xunit.runner.visualstudio" Version="*">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="xunit.assert" Version="*" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NullFX.CRC\NullFX.CRC.csproj" />
</ItemGroup>
<ItemGroup>
<None Remove="xunit" />
<None Remove="xunit.runner.visualstudio" />
<None Remove="xunit.assert" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions NullFX.CRC.Tests/TestParameter.cs
Expand Up @@ -5,10 +5,12 @@ public class TestParameter<ExpectedChecksum> {
public TestRange TestRange { get; set; }
public ExpectedChecksum ExpectedCrc { get; set; }
public Crc16Algorithm Algorithm { get; set; }
public string TestScenario { get; set; }
public TestParameter ( byte[] payload, TestRange range, ExpectedChecksum expected ) {
TestPayload = payload;
TestRange = range;
ExpectedCrc = expected;
TestScenario = "";
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions NullFX.CRC/Crc16.cs
Expand Up @@ -104,7 +104,7 @@ internal static class DnpCrc16Impl {
for ( int i = start; i <= end; ++i ) {
crc = ( ushort )( ( crc >> 8 ) ^ table[( byte )( crc ^ bytes[i] )] );
}
return crc.ByteSwapCompliment ( );
return (ushort)(~crc);
}
}

Expand Down Expand Up @@ -143,7 +143,7 @@ internal static class Crc16CcittKermitImpl {
for ( int i = start; i <= end; ++i ) {
crc = ( ushort )( ( crc >> 8 ) ^ table[( byte )( crc ^ bytes[i] )] );
}
return crc.ByteSwap ( );
return crc;
}
}

Expand Down
42 changes: 0 additions & 42 deletions NullFX.CRC/CrcExtensions.cs

This file was deleted.

0 comments on commit 82cbe69

Please sign in to comment.