-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
优化Crc32/Crc16,增加Span计算,测试用例全覆盖;Binary增加Span写入;
- Loading branch information
Showing
5 changed files
with
241 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using NewLife.Security; | ||
using Xunit; | ||
|
||
namespace XUnitTest.Security; | ||
|
||
public class Crc16Tests | ||
{ | ||
[Fact] | ||
public void TestComputeWithByteArray() | ||
{ | ||
var data = "123456789"u8.ToArray(); | ||
var crc = Crc16.Compute(data); | ||
Assert.Equal(0x31C3, crc); | ||
} | ||
|
||
[Fact] | ||
public void TestComputeWithStream() | ||
{ | ||
var data = "123456789"u8.ToArray(); | ||
using var stream = new MemoryStream(data); | ||
var crc = Crc16.Compute(stream, -1); | ||
Assert.Equal(0x31C3, crc); | ||
} | ||
|
||
[Fact] | ||
public void TestComputeWithReadOnlySpan() | ||
{ | ||
var data = "123456789"u8.ToArray(); | ||
var crc = Crc16.Compute(new ReadOnlySpan<Byte>(data)); | ||
Assert.Equal(0x31C3, crc); | ||
} | ||
|
||
[Fact] | ||
public void TestComputeModbusWithByteArray() | ||
{ | ||
var data = "123456789"u8.ToArray(); | ||
var crc = Crc16.ComputeModbus(data, 0); | ||
Assert.Equal(0x4B37, crc); | ||
} | ||
|
||
[Fact] | ||
public void TestComputeModbusWithStream() | ||
{ | ||
var data = "123456789"u8.ToArray(); | ||
using var stream = new MemoryStream(data); | ||
var crc = Crc16.ComputeModbus(stream); | ||
Assert.Equal(0x4B37, crc); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using NewLife.Security; | ||
using Xunit; | ||
|
||
namespace XUnitTest.Security; | ||
|
||
public class Crc32Tests | ||
{ | ||
[Fact] | ||
public void Compute_ByteArray_ReturnsExpectedCrc() | ||
{ | ||
Byte[] data = { 1, 2, 3, 4, 5 }; | ||
var expectedCrc = 0x470B99F4u; | ||
|
||
var actualCrc = Crc32.Compute(data); | ||
|
||
Assert.Equal(expectedCrc, actualCrc); | ||
} | ||
|
||
[Fact] | ||
public void Compute_ReadOnlySpan_ReturnsExpectedCrc() | ||
{ | ||
Byte[] data = { 1, 2, 3, 4, 5 }; | ||
var expectedCrc = 0x470B99F4u; | ||
|
||
var actualCrc = Crc32.Compute(new ReadOnlySpan<Byte>(data)); | ||
|
||
Assert.Equal(expectedCrc, actualCrc); | ||
} | ||
|
||
[Fact] | ||
public void Compute_Stream_ReturnsExpectedCrc() | ||
{ | ||
Byte[] data = { 1, 2, 3, 4, 5 }; | ||
var expectedCrc = 0x470B99F4u; | ||
|
||
using var stream = new MemoryStream(data); | ||
var actualCrc = Crc32.Compute(stream); | ||
|
||
Assert.Equal(expectedCrc, actualCrc); | ||
} | ||
|
||
[Fact] | ||
public void ComputeRange_Stream_ReturnsExpectedCrc() | ||
{ | ||
Byte[] data = { 1, 2, 3, 4, 5 }; | ||
var expectedCrc = 0x470B99F4u; | ||
|
||
using var stream = new MemoryStream(data); | ||
var actualCrc = Crc32.ComputeRange(stream, 0, data.Length); | ||
|
||
Assert.Equal(expectedCrc, actualCrc); | ||
} | ||
|
||
[Fact] | ||
public void Update_ByteArray_ReturnsExpectedCrc() | ||
{ | ||
Byte[] data = { 1, 2, 3, 4, 5 }; | ||
var expectedCrc = 0x470B99F4u; | ||
|
||
var crc32 = new Crc32(); | ||
crc32.Update(data); | ||
|
||
Assert.Equal(expectedCrc, crc32.Value); | ||
} | ||
|
||
[Fact] | ||
public void Update_ReadOnlySpan_ReturnsExpectedCrc() | ||
{ | ||
Byte[] data = { 1, 2, 3, 4, 5 }; | ||
var expectedCrc = 0x470B99F4u; | ||
|
||
var crc32 = new Crc32(); | ||
crc32.Update(new ReadOnlySpan<Byte>(data)); | ||
|
||
Assert.Equal(expectedCrc, crc32.Value); | ||
} | ||
|
||
[Fact] | ||
public void Update_Stream_ReturnsExpectedCrc() | ||
{ | ||
Byte[] data = { 1, 2, 3, 4, 5 }; | ||
var expectedCrc = 0x470B99F4u; | ||
|
||
using var stream = new MemoryStream(data); | ||
var crc32 = new Crc32(); | ||
crc32.Update(stream); | ||
|
||
Assert.Equal(expectedCrc, crc32.Value); | ||
} | ||
} |