-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathtest-util.ts
More file actions
84 lines (65 loc) · 2.16 KB
/
test-util.ts
File metadata and controls
84 lines (65 loc) · 2.16 KB
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
import { assert } from 'chai';
import * as util from '../lib/common/Util.js';
import { FourCcToken } from '../lib/common/FourCC.js';
import { latin1toString } from '@exodus/bytes/single-byte.js';
const t = assert;
describe('shared utility functionality', () => {
describe('find zero', () => {
const findZero = util.findZero;
it('should find terminator in ascii encoded string', () => {
const buf = Uint8Array.from([0xFF, 0xFF, 0xFF, 0x00]);
t.equal(findZero(buf, 'ascii'), 3);
});
it('find terminator in middle of ascii encoded string', () => {
const buf = Uint8Array.from([0xFF, 0xFF, 0x00, 0xFF, 0xFF]);
t.equal(findZero(buf, 'ascii'), 2);
});
it('return offset to end if nothing is found', () => {
const buf = Uint8Array.from([0xFF, 0xFF, 0xFF, 0xFF, 0xFF]);
t.equal(findZero(buf, 'ascii'), buf.length);
});
it('find terminator in utf16le encoded string', () => {
const buf = Uint8Array.from([0x68, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00, 0x00, 0x00]);
t.equal(findZero(buf, 'utf-16le'), 10);
});
it('find terminator in utf16be encoded string', () => {
const buf = Uint8Array.from([0x00, 0x68, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x00]);
t.equal(findZero(buf, 'utf-16le'), 8);
});
});
describe('stripNulls', () => {
it('should strip nulls', () => {
const tests = [
{
str: 'foo',
expected: 'foo'
},
{
str: 'derp\x00\x00',
expected: 'derp'
},
{
str: '\x00\x00harkaaa\x00',
expected: 'harkaaa'
},
{
str: '\x00joystick',
expected: 'joystick'
}
];
tests.forEach(test => {
t.strictEqual(util.stripNulls(test.str), test.expected);
});
});
});
describe('FourCC token', () => {
it('should be able to encode FourCC token', () => {
const buffer = new Uint8Array(4);
FourCcToken.put(buffer, 0, 'abcd');
t.deepEqual(latin1toString(buffer), 'abcd');
});
});
it('a2hex', () => {
t.equal(util.a2hex('\x00\x01ABC\x02'), '00 01 41 42 43 02');
});
});