-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc.test.ts
127 lines (117 loc) · 2.78 KB
/
crc.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { assertEquals } from '@std/assert';
import { crc } from './crc.ts';
import _00ff from './spec/00ff.ts';
import _123456789 from './spec/123456789.ts';
function* parseCoverage(
s: string,
): Generator<{
line: string;
size: bigint;
poly: bigint;
reflectIn: boolean;
xorIn: bigint;
reflectOut: boolean;
xorOut: bigint;
expected: bigint;
}> {
for (let line of s.split(/[\r\n]+/)) {
line = line.trim();
if (!line) {
continue;
}
const pieces = line.split(/\s+/);
yield {
line,
size: BigInt(+pieces[0]),
poly: BigInt(`0x${pieces[1]}`),
reflectIn: pieces[2] !== '0',
xorIn: BigInt(`0x${pieces[3]}`),
reflectOut: pieces[4] !== '0',
xorOut: BigInt(`0x${pieces[5]}`),
expected: BigInt(`0x${pieces[6]}`),
};
}
}
Deno.test('crc spec/123456789', () => {
const input = new TextEncoder().encode('123456789');
for (const cov of parseCoverage(_123456789)) {
const crcB = crc(
cov.size,
cov.poly,
cov.reflectIn,
cov.xorIn,
cov.reflectOut,
cov.xorOut,
);
let c = crcB.init();
c = crcB.update(c, input);
c = crcB.finalize(c);
assertEquals(c, cov.expected, cov.line);
if (cov.size <= 32) {
const crcI = crc(
Number(cov.size),
Number(cov.poly),
cov.reflectIn,
Number(cov.xorIn),
cov.reflectOut,
Number(cov.xorOut),
);
let c = crcI.init();
c = crcI.update(c, input);
c = crcI.finalize(c);
assertEquals(c, Number(cov.expected), cov.line);
}
}
});
Deno.test('crc spec/00ff', () => {
const input = new ArrayBuffer(256);
const inputA = new Uint8Array(input);
const inputB = new Int8Array(input);
for (let i = inputA.length; i--;) {
inputA[i] = i;
}
const inputA1 = inputA.subarray(0, 7);
const inputA2 = inputA.subarray(7);
const inputB1 = inputB.subarray(0, 7);
const inputB2 = inputB.subarray(7);
for (const cov of parseCoverage(_00ff)) {
const crcB = crc(
cov.size,
cov.poly,
cov.reflectIn,
cov.xorIn,
cov.reflectOut,
cov.xorOut,
);
let c1 = crcB.init();
c1 = crcB.update(c1, inputA1);
c1 = crcB.update(c1, inputA2);
c1 = crcB.finalize(c1);
assertEquals(c1, cov.expected, cov.line);
let c2 = crcB.init();
c2 = crcB.update(c2, inputB1);
c2 = crcB.update(c2, inputB2);
c2 = crcB.finalize(c2);
assertEquals(c2, cov.expected, cov.line);
if (cov.size <= 32) {
const crcI = crc(
Number(cov.size),
Number(cov.poly),
cov.reflectIn,
Number(cov.xorIn),
cov.reflectOut,
Number(cov.xorOut),
);
let c1 = crcI.init();
c1 = crcI.update(c1, inputA1);
c1 = crcI.update(c1, inputA2);
c1 = crcI.finalize(c1);
assertEquals(c1, Number(cov.expected), cov.line);
let c2 = crcI.init();
c2 = crcI.update(c2, inputB1);
c2 = crcI.update(c2, inputB2);
c2 = crcI.finalize(c2);
assertEquals(c1, Number(cov.expected), cov.line);
}
}
});