Skip to content

Commit 5509b62

Browse files
committed
test: 增加单元测试
1 parent 4d6c4a5 commit 5509b62

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using BootstrapBlazor.Components.Utility;
6+
7+
namespace UnitTestTcpSocket;
8+
9+
public class ModbusCrcTest
10+
{
11+
[Fact]
12+
public void Computer_Ok()
13+
{
14+
// 06 00 00 01 7F C9 BA
15+
var data = new byte[] { 0x01, 0x06, 0x00, 0x00, 0x01, 0x7F };
16+
17+
var crc = ModbusCrc16.Compute(data);
18+
Assert.Equal("BAC9", crc.ToString("X4"));
19+
Assert.Equal("01060000017FC9BA", HexConverter.ToString(ModbusCrc16.Append(data), ""));
20+
}
21+
22+
[Fact]
23+
public void Validate_Ok()
24+
{
25+
var result = ModbusCrc16.Validate([0x01]);
26+
Assert.False(result);
27+
28+
result = ModbusCrc16.Validate([0x01, 0x06, 0x00, 0x00, 0x01, 0x7F, 0xC9, 0xBA]);
29+
Assert.True(result);
30+
31+
result = false;
32+
var data = Enumerable.Range(0, 300).Select(i => (byte)Random.Shared.Next(0, 255));
33+
result = ModbusCrc16.Validate(ModbusCrc16.Append(data.ToArray()));
34+
Assert.True(result);
35+
}
36+
}

0 commit comments

Comments
 (0)