-
Notifications
You must be signed in to change notification settings - Fork 1
/
TestCanOverhead.js
396 lines (368 loc) · 17 KB
/
TestCanOverhead.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/**
* @file Unit tests of the CanOverhead.js library using simple console
* assertions that point to the failing line.
* @licence BSD 3-clause license. See LICENSE.md for details.
*/
let check = console.assert; // Shorter-name alias
/**
* Compares two arrays for equality of type, length and content.
* @param {Array|Uint8Array} a first array
* @param {Array|Uint8Array} b second array
* @returns {boolean} true if they are equal, false otherwise
*/
function arrayEqual(a, b) {
let bothArraysOfSameSize = (Array.isArray(a) && Array.isArray(b) && a.length === b.length);
let bothUint8ArraysOfSameSize = (a instanceof Uint8Array && b instanceof Uint8Array && a.length === b.length);
if (bothArraysOfSameSize || bothUint8ArraysOfSameSize) {
// Compare them field by field
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
} else {
return false;
}
}
console.log("Starting unit tests.");
// maxAmountOfStuffBits
check(BitSequence.maxAmountOfStuffBits(0) === 0);
check(BitSequence.maxAmountOfStuffBits(1) === 0);
check(BitSequence.maxAmountOfStuffBits(2) === 0);
check(BitSequence.maxAmountOfStuffBits(3) === 0);
check(BitSequence.maxAmountOfStuffBits(4) === 0);
check(BitSequence.maxAmountOfStuffBits(5) === 1);
check(BitSequence.maxAmountOfStuffBits(6) === 1);
check(BitSequence.maxAmountOfStuffBits(7) === 1);
check(BitSequence.maxAmountOfStuffBits(8) === 1);
check(BitSequence.maxAmountOfStuffBits(9) === 2);
check(BitSequence.maxAmountOfStuffBits(10) === 2);
check(BitSequence.maxAmountOfStuffBits(11) === 2);
check(BitSequence.maxAmountOfStuffBits(12) === 2);
check(BitSequence.maxAmountOfStuffBits(13) === 3);
check(BitSequence.maxAmountOfStuffBits(14) === 3);
check(BitSequence.maxAmountOfStuffBits(15) === 3);
check(BitSequence.maxAmountOfStuffBits(16) === 3);
check(BitSequence.maxAmountOfStuffBits(17) === 4);
check(BitSequence.maxAmountOfStuffBits(18) === 4);
check(BitSequence.maxAmountOfStuffBits(19) === 4);
check(BitSequence.maxAmountOfStuffBits(20) === 4);
check(BitSequence.maxAmountOfStuffBits(21) === 5);
// maxLengthAfterStuffing
check(BitSequence.maxLengthAfterStuffing(0) === 0);
check(BitSequence.maxLengthAfterStuffing(1) === 1);
check(BitSequence.maxLengthAfterStuffing(2) === 2);
check(BitSequence.maxLengthAfterStuffing(3) === 3);
check(BitSequence.maxLengthAfterStuffing(4) === 4);
check(BitSequence.maxLengthAfterStuffing(5) === 6);
check(BitSequence.maxLengthAfterStuffing(6) === 7);
check(BitSequence.maxLengthAfterStuffing(7) === 8);
check(BitSequence.maxLengthAfterStuffing(8) === 9);
check(BitSequence.maxLengthAfterStuffing(9) === 11);
check(BitSequence.maxLengthAfterStuffing(10) === 12);
check(BitSequence.maxLengthAfterStuffing(11) === 13);
check(BitSequence.maxLengthAfterStuffing(12) === 14);
check(BitSequence.maxLengthAfterStuffing(13) === 16);
check(BitSequence.maxLengthAfterStuffing(14) === 17);
check(BitSequence.maxLengthAfterStuffing(15) === 18);
check(BitSequence.maxLengthAfterStuffing(16) === 19);
check(BitSequence.maxLengthAfterStuffing(17) === 21);
check(BitSequence.maxLengthAfterStuffing(18) === 22);
check(BitSequence.maxLengthAfterStuffing(19) === 23);
check(BitSequence.maxLengthAfterStuffing(20) === 24);
check(BitSequence.maxLengthAfterStuffing(21) === 26);
// BitSequence constructor
let bits;
// Default
bits = new BitSequence();
check(arrayEqual(bits._sequence, []), bits._sequence);
check(bits._isStuffed === false);
// Binary string
bits = new BitSequence(" 1 ")
check(arrayEqual(bits._sequence, [1]));
check(bits._isStuffed === false);
bits = new BitSequence(" 0 1 ");
check(arrayEqual(bits._sequence, [0, 1]));
check(bits._isStuffed === false);
bits = new BitSequence(" 0 1 101 ");
check(arrayEqual(bits._sequence, [0, 1, 1, 0, 1]));
check(bits._isStuffed === false);
bits = new BitSequence(" 0 1 101 ", true);
check(arrayEqual(bits._sequence, [0, 1, 1, 0, 1]));
check(bits._isStuffed === true);
// Array of ints
bits = new BitSequence([0, 1, 0]);
check(arrayEqual(bits._sequence, [0, 1, 0]));
check(bits._isStuffed === false);
// Array of booleans
bits = new BitSequence([false, true, false]);
check(arrayEqual(bits._sequence, [0, 1, 0]));
check(bits._isStuffed === false);
// Hybrid array
bits = new BitSequence([1, false, "0"]);
check(arrayEqual(bits._sequence, [1, 0, 0]));
check(bits._isStuffed === false);
// Single bit integer
bits = new BitSequence(0);
check(arrayEqual(bits._sequence, [0]));
check(bits._isStuffed === false);
bits = new BitSequence(1);
check(arrayEqual(bits._sequence, [1]));
check(bits._isStuffed === false);
// Single bit boolean
bits = new BitSequence(false);
check(arrayEqual(bits._sequence, [0]));
check(bits._isStuffed === false);
bits = new BitSequence(true);
check(arrayEqual(bits._sequence, [1]));
check(bits._isStuffed === false);
// applyBitStuffing
check(new BitSequence().applyBitStuffing()
.equal(new BitSequence("", true)));
check(new BitSequence("1").applyBitStuffing()
.equal(new BitSequence("1", true)));
check(new BitSequence("1110101010").applyBitStuffing()
.equal(new BitSequence("1110101010", true)));
check(new BitSequence("11111").applyBitStuffing()
.equal(new BitSequence("111112", true)));
check(new BitSequence("111110000").applyBitStuffing()
.equal(new BitSequence("11111200003", true)));
// Cannot double-stuff
try {
new BitSequence("111110", true).applyBitStuffing();
check(false, "Error nor raised");
} catch (TypeError) {
// Error as expected
}
// length
check(new BitSequence().length() === 0);
check(new BitSequence("1").length() === 1);
check(new BitSequence("1110101010").length() === 10);
check(new BitSequence("11111").length() === 5);
check(new BitSequence("11111000001", true).length() === 11);
// toBinString
check(new BitSequence("").toBinString() === "");
check(new BitSequence("0").toBinString() === "0");
check(new BitSequence("10").toBinString() === "10");
check(new BitSequence("0010").toBinString() === "0010");
check(new BitSequence("0110101").toBinString() === "0110101");
check(new BitSequence("0110101").toBinString("[", "]") === "0110101");
check(new BitSequence("1111121", true).toBinString("[", "]") === "11111[0]1");
check(new BitSequence("0000031", true).toBinString("[", "]") === "00000[1]1");
// toBinStringWithSpacesRightAlign
check(new BitSequence("").toBinStringWithSpacesRightAlign() === "");
check(new BitSequence("0").toBinStringWithSpacesRightAlign() === "0");
check(new BitSequence("10").toBinStringWithSpacesRightAlign() === "10");
check(new BitSequence("0010").toBinStringWithSpacesRightAlign() === "0010");
check(new BitSequence("0110101").toBinStringWithSpacesRightAlign() === "011 0101");
check(new BitSequence("010110101").toBinStringWithSpacesRightAlign() === "0 1011 0101");
check(new BitSequence("010110101").toBinStringWithSpacesRightAlign("[", "]") === "0 1011 0101");
check(new BitSequence("0001111121", true).toBinStringWithSpacesRightAlign("[", "]") === "00 0111 11[0]1");
check(new BitSequence("0000031", true).toBinStringWithSpacesRightAlign("[", "]") === "000 00[1]1");
// toBinStringWithSpacesLeftAlign
check(new BitSequence("").toBinStringWithSpacesLeftAlign() === "");
check(new BitSequence("0").toBinStringWithSpacesLeftAlign() === "0");
check(new BitSequence("10").toBinStringWithSpacesLeftAlign() === "10");
check(new BitSequence("0010").toBinStringWithSpacesLeftAlign() === "0010");
check(new BitSequence("0110101").toBinStringWithSpacesLeftAlign() === "0110 101");
check(new BitSequence("010110101").toBinStringWithSpacesLeftAlign() === "0101 1010 1");
check(new BitSequence("010110101").toBinStringWithSpacesLeftAlign("[", "]") === "0101 1010 1");
check(new BitSequence("0001111121", true).toBinStringWithSpacesLeftAlign("[", "]") === "0001 1111 [0]1");
check(new BitSequence("0000031", true).toBinStringWithSpacesLeftAlign("[", "]") === "0000 0[1]1");
// toHexString
check(new BitSequence("").toHexString() === "");
check(new BitSequence("0").toHexString() === "0");
check(new BitSequence("00 0000 0000").toHexString() === "000");
check(new BitSequence("10").toHexString() === "2");
check(new BitSequence("0010").toHexString() === "2");
check(new BitSequence("0110101").toHexString() === "35");
check(new BitSequence("0 0001 1111").toHexString() === "01F");
check(new BitSequence("1 0001 1111").toHexString() === "11F");
check(new BitSequence("10 1111 1111").toHexString() === "2FF");
// Extend
check(new BitSequence("").extend("").toBinString() === "");
check(new BitSequence("").extend("0").toBinString() === "0");
check(new BitSequence("").extend("1").toBinString() === "1");
check(new BitSequence("").extend("01").toBinString() === "01");
check(new BitSequence("1").extend("00").toBinString() === "100");
check(new BitSequence("0").extend("11").toBinString() === "011");
check(new BitSequence("01").extend("00").toBinString() === "0100");
check(new BitSequence("01").extend(0).toBinString() === "010");
check(new BitSequence("01").extend(1).toBinString() === "011");
check(new BitSequence("01").extend(false).toBinString() === "010");
check(new BitSequence("01").extend(true).toBinString() === "011");
// Iterability
bits = new BitSequence("");
for (let b of bits) {
check(false, "This should not be executed.");
}
bits = new BitSequence("1");
for (let b of bits) {
check(b === 1);
}
bits = new BitSequence("101");
for (let b of bits) {
check(b === 1 || b === 0);
}
// CRC
// The tested values in hex are: 0x0, 0xF1, 0x833, 0x34ec
// Test expected results are computed with
// https://www.ghsi.de/pages/subpages/Online%20CRC%20Calculation/index.php
// CRC 15 bits for classic CAN
check(crc15([0]) === 0);
check(crc15([1, 1, 1, 1, 0, 0, 0, 1]) === 0b110001011110110);
check(crc15([true, true, true, true, 0, 0, false, 1]) === 0b110001011110110);
check(crc15([1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1]) === 0b000010001110100);
check(crc15([1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0]) === 0b010001010101010);
// CRC 17 bits for CAN FD
check(crc17([0]) === 0);
check(crc17([1, 1, 1, 1, 0, 0, 0, 1]) === 0b00000001000111100);
check(crc17([1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1]) === 0b00100010111100111);
check(crc17([1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0])
=== 0b00000111010101101);
// CRC 21 bits for CAN FD
check(crc21([0]) === 0);
check(crc21([1, 1, 1, 1, 0, 0, 0, 1]) === 0b000010111011100111001);
check(crc21([1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1]) === 0b100110110111010101101);
check(crc21([1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0])
=== 0b001001011010101011100);
// CanFrame11Bit fields and whole frame
let frame;
let toComputeCrcOn;
let expectedWholeFrame;
let expectedWholeFrameStuffed;
let crcBits;
// Case 1: simple ID and empty payload
frame = new CanFrame11Bit(0, new Uint8Array(0));
check(frame.id === 0);
check(arrayEqual(frame.payload, new Uint8Array(0)));
check(frame.field01_startOfFrame().equal(new BitSequence(0)));
check(frame.field02_identifier().equal(new BitSequence("000 0000 0000")));
check(frame.field03_remoteTransmissionRequest().equal(new BitSequence(0)));
check(frame.field04_identifierExtensionBit().equal(new BitSequence(0)));
check(frame.field05_reservedBit().equal(new BitSequence(0)));
check(frame.field06_dataLengthCode().equal(new BitSequence("0000")));
check(frame.field07_dataField().equal(new BitSequence()));
toComputeCrcOn = new BitSequence("0 000 0000 0000 0 0 0000");
check(frame.field08_crc().equal(new BitSequence("000 0000 0000 0000")));
check(frame.field09_crcDelimiter().equal(new BitSequence(1)));
check(frame.field10_ackSlot().equal(new BitSequence(1)));
check(frame.field11_ackDelimiter().equal(new BitSequence(1)));
check(frame.field12_endOfFrame().equal(new BitSequence("111 1111")));
check(frame.field13_interFrameSpace().equal(new BitSequence("111")));
expectedWholeFrame = new BitSequence(
"0 000 0000 0000 0 0 0 0000 " // Header
+ "" // Payload
+ "000 0000 0000 0000 " // CRC
+ "1 1 1 111 1111 111" // Rest of the trailer
);
check(frame.wholeFrame().equal(expectedWholeFrame));
expectedWholeFrameStuffed = new BitSequence(
"0 000 03000 00300 0 0 03 0000 " // Header
+ "" // Payload
+ "0300 00030 0000 30000 " // CRC
+ "1 1 1 111 1111 111", // Rest of the trailer (unstuffed)
true // This sequence is already stuffed
);
check(frame.wholeFrameStuffed().equal(expectedWholeFrameStuffed));
// Case 2: simple ID and payload with 1 byte
frame = new CanFrame11Bit(0, new Uint8Array(1));
check(frame.id === 0);
check(arrayEqual(frame.payload, new Uint8Array(1)));
check(frame.field01_startOfFrame().equal(new BitSequence(0)));
check(frame.field02_identifier().equal(new BitSequence("000 0000 0000")));
check(frame.field03_remoteTransmissionRequest().equal(new BitSequence(0)));
check(frame.field04_identifierExtensionBit().equal(new BitSequence(0)));
check(frame.field05_reservedBit().equal(new BitSequence(0)));
check(frame.field06_dataLengthCode().equal(new BitSequence("0001")));
check(frame.field07_dataField().equal(new BitSequence("0000 0000")));
toComputeCrcOn = new BitSequence("0 000 0000 0000 0 0 0001 0000 0000");
crcBits = new BitSequence(crc15(toComputeCrcOn).toString(2).padStart(15, "0"));
check(frame.field08_crc().equal(crcBits));
check(frame.field09_crcDelimiter().equal(new BitSequence(1)));
check(frame.field10_ackSlot().equal(new BitSequence(1)));
check(frame.field11_ackDelimiter().equal(new BitSequence(1)));
check(frame.field12_endOfFrame().equal(new BitSequence("111 1111")));
check(frame.field13_interFrameSpace().equal(new BitSequence("111")));
expectedWholeFrame = new BitSequence(
"0 000 0000 0000 0 0 0 0001 " // Header
+ "0000 0000" // Payload
+ "100 0100 0010 0110" // CRC
+ "1 1 1 111 1111 111" // Rest of the trailer
);
check(frame.wholeFrame().equal(expectedWholeFrame));
expectedWholeFrameStuffed = new BitSequence(
"0 000 03000 00300 0 0 03 0001 " // Header
+ "0000 03000" // Payload
+ "100 0100 0010 0110" // CRC
+ "1 1 1 111 1111 111", // Rest of the trailer (unstuffed)
true // This sequence is already stuffed
);
check(frame.wholeFrameStuffed().equal(expectedWholeFrameStuffed));
// Case 3: complex ID and payload with 8 bytes
frame = new CanFrame11Bit(0x133, new Uint8Array(8));
frame.payload[0] = 0xFF;
frame.payload[7] = 0xFF;
check(frame.id === 0x133);
check(frame.payload[0] === 0xFF);
check(frame.payload[1] === 0);
check(frame.payload[6] === 0);
check(frame.payload[7] === 0xFF);
check(frame.field01_startOfFrame().equal(new BitSequence(0)));
check(frame.field02_identifier().equal(new BitSequence("001 0011 0011")));
check(frame.field03_remoteTransmissionRequest().equal(new BitSequence(0)));
check(frame.field04_identifierExtensionBit().equal(new BitSequence(0)));
check(frame.field05_reservedBit().equal(new BitSequence(0)));
check(frame.field06_dataLengthCode().equal(new BitSequence("1000")));
check(frame.field07_dataField().equal(new BitSequence(
"1111 1111 0000 0000 0000 0000 0000 0000" +
"0000 0000 0000 0000 0000 0000 1111 1111")));
toComputeCrcOn = new BitSequence(
"0 001 0011 0011 0 0 0 1000 "
+ "1111 1111 0000 0000 0000 0000 0000 0000"
+ "0000 0000 0000 0000 0000 0000 1111 1111");
crcBits = new BitSequence(crc15(toComputeCrcOn).toString(2).padStart(15, "0"));
check(frame.field08_crc().equal(crcBits));
check(frame.field09_crcDelimiter().equal(new BitSequence(1)));
check(frame.field10_ackSlot().equal(new BitSequence(1)));
check(frame.field11_ackDelimiter().equal(new BitSequence(1)));
check(frame.field12_endOfFrame().equal(new BitSequence("111 1111")));
check(frame.field13_interFrameSpace().equal(new BitSequence("111")));
expectedWholeFrame = new BitSequence(
"0 001 0011 0011 0 0 0 1000 " // Header
+ "1111 1111 0000 0000 0000 0000 0000 0000" // Payload
+ "0000 0000 0000 0000 0000 0000 1111 1111" // Payload
+ "001 1000 0101 1111" // CRC
+ "1 1 1 111 1111 111" // Rest of the trailer
);
check(frame.wholeFrame().equal(expectedWholeFrame));
expectedWholeFrameStuffed = new BitSequence(
"0 001 0011 0011 0 0 0 1000 " // Header
+ "1111 12111 0000 03000 00300 00030 00003 0000" // Payload
+ "03000 00300 00030 00003 0000 03000 1111 12111" // Payload
+ "001 1000 0101 11112" // CRC
+ "1 1 1 111 1111 111", // Rest of the trailer (unstuffed)
true // This sequence is already stuffed
);
check(frame.wholeFrameStuffed().equal(expectedWholeFrameStuffed));
// CanFrame11Bit max frame length
// In the CanFrame11Bit class, the length is computed field by field. Here
// we compute the expected value manually. Note that the part after the CRC
// is not stuffed, so we must not include it in the computation of the amount
// of stuff bits.
// Header = SOF + ID + RTR + IDE + R0 + DLC
const headerLength = 1 + 11 + 1 + 1 + 1 + 4;
const crcLength = 15;
// Post CRC = CRC del + ACK slot + ACK del + EOF + pause
const postCrcLength = 1 + 1 + 1 + 7 + 3;
// Try it over all payload lengths
for (let i = 0; i <= 8; i++) {
let payload = new Uint8Array(i);
frame = new CanFrame11Bit(0, payload);
let expectedMaxFrameLenAfterStuffing =
BitSequence.maxLengthAfterStuffing(
headerLength + payload.length * 8 + crcLength)
+ postCrcLength;
check(frame.maxLengthAfterStuffing()
=== expectedMaxFrameLenAfterStuffing, payload);
}
// TODO tests for 29 bit ID frame