-
Notifications
You must be signed in to change notification settings - Fork 15
/
test.js
628 lines (482 loc) · 14.6 KB
/
test.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
var assert = require('assert'),
BitView = require('./bit-buffer').BitView,
BitStream = require('./bit-buffer').BitStream;
suite('BitBuffer', function () {
var array, bv, bsw, bsr;
setup(function () {
array = new ArrayBuffer(64);
bv = new BitView(array);
bsw = new BitStream(bv);
// Test initializing straight from the array.
bsr = new BitStream(array);
});
test('Min / max signed 5 bits', function () {
var signed_max = (1 << 4) - 1;
bsw.writeBits(signed_max, 5);
bsw.writeBits(-signed_max - 1, 5);
assert(bsr.readBits(5, true) === signed_max);
assert(bsr.readBits(5, true) === -signed_max - 1);
});
test('Min / max unsigned 5 bits', function () {
var unsigned_max = (1 << 5) - 1;
bsw.writeBits(unsigned_max, 5);
bsw.writeBits(-unsigned_max, 5);
assert.equal(bsr.readBits(5), unsigned_max);
assert.equal(bsr.readBits(5), 1);
});
test('Min / max int8', function () {
var signed_max = 0x7F;
bsw.writeInt8(signed_max);
bsw.writeInt8(-signed_max - 1);
assert.equal(bsr.readInt8(), signed_max);
assert.equal(bsr.readInt8(), -signed_max - 1);
});
test('Min / max uint8', function () {
var unsigned_max = 0xFF;
bsw.writeUint8(unsigned_max);
bsw.writeUint8(-unsigned_max);
assert.equal(bsr.readUint8(), unsigned_max);
assert.equal(bsr.readUint8(), 1);
});
test('Min / max int16', function () {
var signed_max = 0x7FFF;
bsw.writeInt16(signed_max);
bsw.writeInt16(-signed_max - 1);
assert.equal(bsr.readInt16(), signed_max);
assert.equal(bsr.readInt16(), -signed_max - 1);
});
test('Min / max uint16', function () {
var unsigned_max = 0xFFFF;
bsw.writeUint16(unsigned_max);
bsw.writeUint16(-unsigned_max);
assert.equal(bsr.readUint16(), unsigned_max);
assert.equal(bsr.readUint16(), 1);
});
test('Min / max int32', function () {
var signed_max = 0x7FFFFFFF;
bsw.writeInt32(signed_max);
bsw.writeInt32(-signed_max - 1);
assert.equal(bsr.readInt32(), signed_max);
assert.equal(bsr.readInt32(), -signed_max - 1);
});
test('Min / max uint32', function () {
var unsigned_max = 0xFFFFFFFF;
bsw.writeUint32(unsigned_max);
bsw.writeUint32(-unsigned_max);
assert.equal(bsr.readUint32(), unsigned_max);
assert.equal(bsr.readUint32(), 1);
});
test('Unaligned reads', function () {
bsw.writeBits(13, 5);
bsw.writeUint8(0xFF);
bsw.writeBits(14, 5);
assert.equal(bsr.readBits(5), 13);
assert.equal(bsr.readUint8(), 0xFF);
assert.equal(bsr.readBits(5), 14);
});
test('Min / max float32 (normal values)', function () {
var scratch = new DataView(new ArrayBuffer(8));
scratch.setUint32(0, 0x00800000);
scratch.setUint32(4, 0x7f7fffff);
var min = scratch.getFloat32(0);
var max = scratch.getFloat32(4);
bsw.writeFloat32(min);
bsw.writeFloat32(max);
assert.equal(bsr.readFloat32(), min);
assert.equal(bsr.readFloat32(), max);
});
test('Min / max float64 (normal values)', function () {
var scratch = new DataView(new ArrayBuffer(16));
scratch.setUint32(0, 0x00100000);
scratch.setUint32(4, 0x00000000);
scratch.setUint32(8, 0x7fefffff);
scratch.setUint32(12, 0xffffffff);
var min = scratch.getFloat64(0);
var max = scratch.getFloat64(8);
bsw.writeFloat64(min);
bsw.writeFloat64(max);
assert.equal(bsr.readFloat64(), min);
assert.equal(bsr.readFloat64(), max);
});
test('Overwrite previous value with 0', function () {
bv.setUint8(0, 13);
bv.setUint8(0, 0);
assert.equal(bv.getUint8(0), 0);
});
test('Read / write ASCII string, fixed length', function () {
var str = 'foobar';
var len = 16;
bsw.writeASCIIString(str, len);
assert.equal(bsw.byteIndex, len);
assert.equal(bsr.readASCIIString(len), str);
assert.equal(bsr.byteIndex, len);
});
test('Read / write ASCII string, unknown length', function () {
var str = 'foobar';
bsw.writeASCIIString(str);
assert.equal(bsw.byteIndex, str.length + 1); // +1 for 0x00
assert.equal(bsr.readASCIIString(), str);
assert.equal(bsr.byteIndex, str.length + 1);
});
test('Read ASCII string, 0 length', function () {
var str = 'foobar';
bsw.writeASCIIString(str);
assert.equal(bsw.byteIndex, str.length + 1); // +1 for 0x00
assert.equal(bsr.readASCIIString(0), '');
assert.equal(bsr.byteIndex, 0);
});
test('Read overflow', function () {
var exception = false;
try {
bsr.readASCIIString(128);
} catch (e) {
exception = true;
}
assert(exception);
});
test('Write overflow', function () {
var exception = false;
try {
bsw.writeASCIIString('foobar', 128);
} catch (e) {
exception = true;
}
assert(exception);
});
test('Get boolean', function () {
bv.setUint8(0, 1);
assert(bv.getBoolean(0));
bv.setUint8(0, 0);
assert(!bv.getBoolean(0));
});
test('Set boolean', function () {
bv.setBoolean(0, true);
assert(bv.getBoolean(0));
bv.setBoolean(0, false);
assert(!bv.getBoolean(0));
});
test('Read boolean', function () {
bv.setBits(0, 1, 1);
bv.setBits(1, 0, 1);
assert(bsr.readBoolean());
assert(!bsr.readBoolean());
});
test('Write boolean', function () {
bsr.writeBoolean(true);
assert.equal(bv.getBits(0, 1, false), 1);
bsr.writeBoolean(false);
assert.equal(bv.getBits(1, 1, false), 0);
});
test('Read / write UTF8 string, only ASCII characters', function () {
var str = 'foobar';
bsw.writeUTF8String(str);
assert(bsw.byteIndex === str.length + 1); // +1 for 0x00
assert.equal(bsr.readUTF8String(), str);
assert.equal(bsr.byteIndex, str.length + 1);
});
test('Read / write UTF8 string, non ASCII characters', function () {
var str = '日本語';
var bytes = [
0xE6,
0x97,
0xA5,
0xE6,
0x9C,
0xAC,
0xE8,
0xAA,
0x9E
];
bsw.writeUTF8String(str);
for (var i = 0; i < bytes.length; i++) {
assert.equal(bytes[i], bv.getBits(i * 8, 8));
}
assert.equal(bsw.byteIndex, bytes.length + 1); // +1 for 0x00
assert.equal(str, bsr.readUTF8String());
assert.equal(bsr.byteIndex, bytes.length + 1);
});
test('readBitStream', function () {
bsw.writeBits(0xF0, 8); //0b11110000
bsw.writeBits(0xF1, 8); //0b11110001
bsr.readBits(3); //offset
var slice = bsr.readBitStream(8);
assert.equal(slice.readBits(6), 0x3E); //0b111110
assert.equal(9, slice._index);
assert.equal(6, slice.index);
assert.equal(8, slice.length);
assert.equal(2, slice.bitsLeft);
assert.equal(bsr._index, 11);
assert.equal((64 * 8) - 11, bsr.bitsLeft);
});
test('readBitStream overflow', function () {
bsw.writeBits(0xF0, 8); //0b11110000
bsw.writeBits(0xF1, 8); //0b11110001
bsr.readBits(3); //offset
var slice = bsr.readBitStream(4);
var exception = false;
try {
slice.readUint8();
} catch (e) {
exception = true;
}
assert(exception);
});
test('writeBitStream', function () {
var buf = new ArrayBuffer(64);
var sourceStream = new BitStream(buf);
sourceStream.writeBits(0xF0, 8); //0b11110000
sourceStream.writeBits(0xF1, 8); //0b11110001
sourceStream.index = 0;
sourceStream.readBits(3); //offset
bsr.writeBitStream(sourceStream, 8);
assert.equal(8, bsr.index);
bsr.index = 0;
assert.equal(bsr.readBits(6), 0x3E); //0b00111110
assert.equal(11, sourceStream.index);
var bin = new Uint8Array(buf);
assert.equal(bin[0], 0xF0);
assert.equal(bin[1], 0xF1);
});
test('writeBitStream Buffer', function () {
var buf = Buffer.alloc(64);
var sourceStream = new BitStream(buf);
sourceStream.writeBits(0xF0, 8); //0b11110000
sourceStream.writeBits(0xF1, 8); //0b11110001
sourceStream.index = 0;
sourceStream.readBits(3); //offset
bsr.writeBitStream(sourceStream, 8);
assert.equal(8, bsr.index);
bsr.index = 0;
assert.equal(bsr.readBits(6), 0x3E); //0b00111110
assert.equal(11, sourceStream.index);
var bin = new Uint8Array(buf.buffer);
assert.equal(bin[0], 0xF0);
assert.equal(bin[1], 0xF1);
});
test('writeBitStream long', function () {
var sourceStream = new BitStream(new ArrayBuffer(64));
sourceStream.writeBits(0xF0, 8);
sourceStream.writeBits(0xF1, 8);
sourceStream.writeBits(0xF1, 8);
sourceStream.writeBits(0xF1, 8);
sourceStream.writeBits(0xF1, 8);
sourceStream.index = 0;
sourceStream.readBits(3); //offset
bsr.index = 3;
bsr.writeBitStream(sourceStream, 35);
assert.equal(38, bsr.index);
bsr.index = 3;
assert.equal(bsr.readBits(35), 1044266558);
assert.equal(38, sourceStream.index);
});
test('readArrayBuffer', function () {
bsw.writeBits(0xF0, 8); //0b11110000
bsw.writeBits(0xF1, 8); //0b11110001
bsw.writeBits(0xF0, 8); //0b11110000
bsr.readBits(3); //offset
var buffer = bsr.readArrayBuffer(2);
assert.equal(0x3E, buffer[0]); //0b00111110
assert.equal(0x1E, buffer[1]); //0b00011110
assert.equal(3 + (2 * 8), bsr._index);
});
test('writeArrayBuffer', function () {
var source = new Uint8Array(4);
source[0] = 0xF0;
source[1] = 0xF1;
source[2] = 0xF1;
bsr.readBits(3); //offset
bsr.writeArrayBuffer(source.buffer, 2);
assert.equal(19, bsr.index);
bsr.index = 0;
assert.equal(bsr.readBits(8), 128);
});
test('Get buffer from view', function () {
bv.setBits(0, 0xFFFFFFFF, 32);
var buffer = bv.buffer;
assert.equal(64, buffer.length);
assert.equal(0xFFFF, buffer.readUInt16LE(0));
});
test('Get buffer from stream', function () {
bsw.writeBits(0xFFFFFFFF, 32);
var buffer = bsr.buffer;
assert.equal(64, buffer.length);
assert.equal(0xFFFF, buffer.readUInt16LE(0));
});
});
suite('Reading big/little endian', function () {
var array, u8, bv, bsw, bsr;
setup(function () {
array = new ArrayBuffer(64);
u8 = new Uint8Array(array);
u8[0] = 0x01;
u8[1] = 0x02;
// Test initializing straight from the array.
bsr = new BitStream(array);
});
test('4b, little-endian', function () {
assert.equal(bsr.index, 0, 'BitStream didn\'t init at offset 0');
var result = [];
result.push(bsr.readBits(4));
result.push(bsr.readBits(4));
result.push(bsr.readBits(4));
result.push(bsr.readBits(4));
// 0000 0001 0000 0010 [01 02]
// [#2] [#1] [#4] [#3]
assert.deepEqual(result, [1, 0, 2, 0]);
});
test('8b, little-endian', function () {
assert.equal(bsr.index, 0, 'BitStream didn\'t init at offset 0');
var result = [];
result.push(bsr.readBits(8));
result.push(bsr.readBits(8));
// 0000 0001 0000 0010 [01 02]
// [ #1] [ #2]
assert.deepEqual(result, [1, 2]);
});
test('10b, little-endian', function () {
assert.equal(bsr.index, 0, 'BitStream didn\'t init at offset 0');
var result = [];
result.push(bsr.readBits(10));
// 0000 0001 0000 0010 [01 02]
// ... #1] [ #2][#1...
assert.deepEqual(result, [513]);
});
test('16b, little-endian', function () {
assert.equal(bsr.index, 0, 'BitStream didn\'t init at offset 0');
var result = [];
result.push(bsr.readBits(16));
// 0000 0001 0000 0010 [01 02]
// [ #1]
assert.deepEqual(result, [0x201]);
});
test('24b, little-endian', function () {
u8[2] = 0x03;
assert.equal(bsr.index, 0, 'BitStream didn\'t init at offset 0');
var result = [];
result.push(bsr.readBits(24));
// 0000 0001 0000 0010 0000 0011 [01 02 03]
// [ #1]
assert.deepEqual(result, [0x30201]);
});
test('4b, big-endian', function () {
bsr.bigEndian = true;
assert.equal(bsr.index, 0, 'BitStream didn\'t init at offset 0');
var result = [];
result.push(bsr.readBits(4));
result.push(bsr.readBits(4));
result.push(bsr.readBits(4));
result.push(bsr.readBits(4));
// 0000 0001 0000 0010 [01 02]
// [#1] [#2] [#3] [#4]
assert.deepEqual(result, [0, 1, 0, 2]);
});
test('8b, big-endian', function () {
bsr.bigEndian = true;
assert.equal(bsr.index, 0, 'BitStream didn\'t init at offset 0');
var result = [];
result.push(bsr.readBits(8));
result.push(bsr.readBits(8));
// 0000 0001 0000 0010 [01 02]
// [ #1] [ #2]
assert.deepEqual(result, [1, 2]);
});
test('10b, big-endian', function () {
bsr.bigEndian = true;
assert.equal(bsr.index, 0, 'BitStream didn\'t init at offset 0');
var result = [];
result.push(bsr.readBits(10));
result.push(bsr.readBits(6));
// 0000 0001 0000 0010 [01 02]
// [ #1][ #2]
assert.deepEqual(result, [4, 2]);
});
test('16b, big-endian', function () {
bsr.bigEndian = true;
assert.equal(bsr.index, 0, 'BitStream didn\'t init at offset 0');
var result = [];
result.push(bsr.readBits(16));
// 0000 0001 0000 0010 [01 02]
// [ #1]
assert.deepEqual(result, [0x102]);
});
test('24b, big-endian', function () {
u8[2] = 0x03;
bsr.bigEndian = true;
assert.equal(bsr.index, 0, 'BitStream didn\'t init at offset 0');
var result = [];
result.push(bsr.readBits(24));
// 0000 0001 0000 0010 0000 0011 [01 02 03]
// [ #1]
assert.deepEqual(result, [0x10203]);
});
});
suite('Writing big/little endian', function () {
var array, u8, bv, bsw, bsr;
setup(function () {
array = new ArrayBuffer(2);
u8 = new Uint8Array(array);
bv = new BitView(array);
bsw = new BitStream(bv);
});
test('4b, little-endian', function () {
// 0000 0001 0000 0010 [01 02]
// [#2] [#1] [#4] [#3]
bsw.writeBits(1, 4);
bsw.writeBits(0, 4);
bsw.writeBits(2, 4);
bsw.writeBits(0, 4);
assert.deepEqual(u8, new Uint8Array([0x01, 0x02]));
});
test('8b, little-endian', function () {
// 0000 0001 0000 0010 [01 02]
// [ #1] [ #2]
bsw.writeBits(1, 8);
bsw.writeBits(2, 8);
assert.deepEqual(u8, new Uint8Array([0x01, 0x02]));
});
test('10b, little-endian', function () {
// 0000 0001 0000 0010 [01 02]
// ... #1] [ #2][#1...
bsw.writeBits(513, 10);
assert.deepEqual(u8, new Uint8Array([0x01, 0x02]));
});
test('16b, little-endian', function () {
// 0000 0001 0000 0010 [01 02]
// [ #1]
bsw.writeBits(0x201, 16);
assert.deepEqual(u8, new Uint8Array([0x01, 0x02]));
});
test('4b, big-endian', function () {
bsw.bigEndian = true;
// 0000 0001 0000 0010 [01 02]
// [#1] [#2] [#3] [#4]
bsw.writeBits(0, 4);
bsw.writeBits(1, 4);
bsw.writeBits(0, 4);
bsw.writeBits(2, 4);
assert.deepEqual(u8, new Uint8Array([0x01, 0x02]));
});
test('8b, big-endian', function () {
bsw.bigEndian = true;
// 0000 0001 0000 0010 [01 02]
// [ #1] [ #2]
bsw.writeBits(1, 8);
bsw.writeBits(2, 8);
assert.deepEqual(u8, new Uint8Array([0x01, 0x02]));
});
test('10b, big-endian', function () {
bsw.bigEndian = true;
// 0000 0001 0000 0010 [01 02]
// [ #1][ #2]
bsw.writeBits(4, 10);
bsw.writeBits(2, 6);
assert.deepEqual(u8, new Uint8Array([0x01, 0x02]));
});
test('16b, big-endian', function () {
bsw.bigEndian = true;
// 0000 0001 0000 0010 [01 02]
// [ #1]
bsw.writeBits(0x102, 16);
assert.deepEqual(u8, new Uint8Array([0x01, 0x02]));
});
});