|
21 | 21 | import static org.junit.jupiter.api.Assertions.assertNotSame; |
22 | 22 |
|
23 | 23 | import java.nio.charset.StandardCharsets; |
| 24 | +import java.util.function.Supplier; |
24 | 25 | import java.util.zip.Checksum; |
25 | 26 |
|
| 27 | +import org.apache.commons.codec.binary.StringUtils; |
26 | 28 | import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.params.ParameterizedTest; |
| 30 | +import org.junit.jupiter.params.provider.MethodSource; |
27 | 31 |
|
28 | 32 | /** |
29 | 33 | * Tests {@link CRC16}. |
30 | 34 | */ |
31 | 35 | class CRC16Test { |
32 | 36 |
|
33 | | - private static final byte[] BYTES_123456789 = "123456789".getBytes(StandardCharsets.US_ASCII); |
| 37 | + private static final byte[] TEST_BYTES = "123456789".getBytes(StandardCharsets.US_ASCII); |
| 38 | + private static final int TEST_BYTES_LEN = TEST_BYTES.length; |
34 | 39 |
|
35 | | - @Test |
36 | | - void testDefaultCcitt() { |
37 | | - final Checksum crc16 = CRC16.ccitt(); |
38 | | - crc16.update(BYTES_123456789, 0, 9); |
39 | | - assertEquals(0x2189, crc16.getValue()); |
40 | | - crc16.update(BYTES_123456789, 0, 9); |
41 | | - assertEquals(0xE026, crc16.getValue()); |
42 | | - crc16.reset(); |
43 | | - crc16.update(BYTES_123456789, 0, 9); |
44 | | - assertEquals(0x2189, crc16.getValue()); |
| 40 | + static Object[][] testCcittDefault() { |
| 41 | + // @formatter:off |
| 42 | + return new Object[][] { |
| 43 | + { "", 0x0000 }, |
| 44 | + { "1", 0x200A }, |
| 45 | + { "12", 0xBDEB }, |
| 46 | + { "123", 0x5A78 }, |
| 47 | + { "1234", 0x8832 }, |
| 48 | + { "12345", 0x7437 }, |
| 49 | + { "123456", 0x11FD }, |
| 50 | + { "1234567", 0x6947 }, |
| 51 | + { "12345678", 0x8B19 }, |
| 52 | + { "123456789", 0x2189 } |
| 53 | + }; |
| 54 | + // @formatter:on |
45 | 55 | } |
46 | 56 |
|
47 | | - @Test |
48 | | - void testDefaultModbus() { |
49 | | - final Checksum crc16 = CRC16.modbus(); |
50 | | - crc16.update(BYTES_123456789, 0, 9); |
51 | | - assertEquals(0x4B37, crc16.getValue()); |
52 | | - crc16.update(BYTES_123456789, 0, 9); |
53 | | - assertEquals(0x090A, crc16.getValue()); |
54 | | - crc16.reset(); |
55 | | - crc16.update(BYTES_123456789, 0, 9); |
56 | | - assertEquals(0x4B37, crc16.getValue()); |
| 57 | + static Object[][] testModbusDefault() { |
| 58 | + // @formatter:off |
| 59 | + return new Object[][] { |
| 60 | + { "", 0xFFFF }, |
| 61 | + { "1", 0x947E }, |
| 62 | + { "12", 0xF595 }, |
| 63 | + { "123", 0x7A75 }, |
| 64 | + { "1234", 0x30BA }, |
| 65 | + { "12345", 0xA471 }, |
| 66 | + { "123456", 0x32E4 }, |
| 67 | + { "1234567", 0x9D73 }, |
| 68 | + { "12345678", 0x37DD }, |
| 69 | + { "123456789", 0x4B37 } |
| 70 | + }; |
| 71 | + // @formatter:on |
| 72 | + } |
| 73 | + |
| 74 | + private Supplier<String> messageSupplier(final Checksum crc16, final long expected) { |
| 75 | + return () -> String.format("Expected %X, actual %s", expected, crc16); |
| 76 | + } |
| 77 | + |
| 78 | + void stdUpdate(final Checksum crc16) { |
| 79 | + crc16.update(TEST_BYTES, 0, TEST_BYTES_LEN); |
| 80 | + } |
| 81 | + |
| 82 | + @ParameterizedTest |
| 83 | + @MethodSource |
| 84 | + void testCcittDefault(final String source, final long expected) { |
| 85 | + testUpdateReset(source, expected, CRC16.ccitt()); |
57 | 86 | } |
58 | 87 |
|
59 | 88 | @Test |
60 | 89 | void testGetTables() { |
61 | 90 | assertNotSame(CRC16.getCcittTable(), CRC16.getCcittTable()); |
62 | 91 | assertNotSame(CRC16.getModbusTable(), CRC16.getModbusTable()); |
| 92 | + assertNotSame(CRC16.getCdma2000Table(), CRC16.getCdma2000Table()); |
63 | 93 | } |
64 | 94 |
|
65 | 95 | @Test |
66 | 96 | void testInit() { |
67 | 97 | final Checksum crc16 = CRC16.builder().setTable(CRC16.getModbusTable()).setInit(0xFFFF).get(); |
68 | | - crc16.update(BYTES_123456789, 0, 9); |
| 98 | + stdUpdate(crc16); |
69 | 99 | assertEquals(0x4B37, crc16.getValue()); |
70 | | - crc16.update(BYTES_123456789, 0, 9); |
| 100 | + stdUpdate(crc16); |
71 | 101 | assertEquals(0x090A, crc16.getValue()); |
72 | 102 | crc16.reset(); |
73 | | - crc16.update(BYTES_123456789, 0, 9); |
| 103 | + stdUpdate(crc16); |
74 | 104 | assertEquals(0x4B37, crc16.getValue()); |
75 | 105 | } |
76 | 106 |
|
77 | 107 | @Test |
78 | 108 | void testModbusCustom() { |
79 | 109 | final Checksum crc16 = CRC16.builder().setTable(CRC16.getModbusTable()).setInit(0xFFFF).get(); |
80 | | - crc16.update(BYTES_123456789, 0, 9); |
| 110 | + stdUpdate(crc16); |
81 | 111 | assertEquals(0x4B37, crc16.getValue()); |
82 | | - crc16.update(BYTES_123456789, 0, 9); |
| 112 | + stdUpdate(crc16); |
83 | 113 | assertEquals(0x090A, crc16.getValue()); |
84 | 114 | crc16.reset(); |
85 | | - crc16.update(BYTES_123456789, 0, 9); |
| 115 | + stdUpdate(crc16); |
86 | 116 | assertEquals(0x4B37, crc16.getValue()); |
87 | 117 | } |
88 | 118 |
|
| 119 | + @ParameterizedTest |
| 120 | + @MethodSource |
| 121 | + void testModbusDefault(final String source, final long expected) { |
| 122 | + testUpdateReset(source, expected, CRC16.modbus()); |
| 123 | + } |
| 124 | + |
89 | 125 | @Test |
90 | 126 | void testReset() { |
91 | 127 | final Checksum crc16 = CRC16.modbus(); |
92 | | - crc16.update(BYTES_123456789, 0, 9); |
| 128 | + stdUpdate(crc16); |
93 | 129 | assertEquals(0x4B37, crc16.getValue()); |
94 | | - crc16.update(BYTES_123456789, 0, 9); |
| 130 | + stdUpdate(crc16); |
95 | 131 | assertEquals(0x090A, crc16.getValue()); |
96 | 132 | crc16.reset(); |
97 | | - crc16.update(BYTES_123456789, 0, 9); |
| 133 | + stdUpdate(crc16); |
98 | 134 | assertEquals(0x4B37, crc16.getValue()); |
99 | 135 | } |
100 | 136 |
|
101 | 137 | @Test |
102 | 138 | void testResetCustomModbus() { |
103 | 139 | final Checksum crc16 = CRC16.builder().setTable(CRC16.getModbusTable()).setInit(0x0000).get(); |
104 | | - crc16.update(BYTES_123456789, 0, 9); |
| 140 | + stdUpdate(crc16); |
105 | 141 | assertEquals(0xBB3D, crc16.getValue()); |
106 | | - crc16.update(BYTES_123456789, 0, 9); |
| 142 | + stdUpdate(crc16); |
107 | 143 | assertEquals(0xED7B, crc16.getValue()); |
108 | 144 | crc16.reset(); |
109 | | - crc16.update(BYTES_123456789, 0, 9); |
| 145 | + stdUpdate(crc16); |
110 | 146 | assertEquals(0xbb3d, crc16.getValue()); |
111 | 147 | } |
112 | 148 |
|
113 | 149 | @Test |
114 | 150 | void testUpdateArray() { |
115 | 151 | final Checksum crc16 = CRC16.builder().setTable(CRC16.getModbusTable()).setInit(0x0000).get(); |
116 | | - crc16.update(BYTES_123456789, 0, 9); |
| 152 | + stdUpdate(crc16); |
117 | 153 | assertEquals(0xBB3D, crc16.getValue()); |
118 | 154 | } |
119 | 155 |
|
120 | 156 | @Test |
121 | 157 | void testUpdateInt() { |
122 | 158 | final Checksum crc16 = CRC16.builder().setTable(CRC16.getModbusTable()).setInit(0x0000).get(); |
123 | | - final byte[] bytes = BYTES_123456789; |
| 159 | + final byte[] bytes = TEST_BYTES; |
124 | 160 | for (final byte element : bytes) { |
125 | 161 | crc16.update(element); |
126 | 162 | } |
127 | 163 | assertEquals(0xBB3D, crc16.getValue()); |
128 | 164 | } |
| 165 | + |
| 166 | + void testUpdateReset(final String source, final long expected, final Checksum crc16) { |
| 167 | + final byte[] bytes = StringUtils.getBytesUsAscii(source); |
| 168 | + crc16.update(bytes, 0, bytes.length); |
| 169 | + long actual = crc16.getValue(); |
| 170 | + assertEquals(expected, actual, messageSupplier(crc16, expected)); |
| 171 | + crc16.reset(); |
| 172 | + crc16.update(bytes, 0, bytes.length); |
| 173 | + actual = crc16.getValue(); |
| 174 | + assertEquals(expected, actual, messageSupplier(crc16, expected)); |
| 175 | + } |
| 176 | + |
129 | 177 | } |
0 commit comments