Skip to content

Commit 8d99e9b

Browse files
committed
test: number utils
1 parent 922792a commit 8d99e9b

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

test/node/utils/number_utils.test.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import { expect } from 'chai';
2+
import { NumberUtils } from '../../../src/utils/number_utils';
3+
4+
describe.only('NumberUtils', () => {
5+
/** Make a Uint8Array in a less verbose way */
6+
const b = (...values) => new Uint8Array(values);
7+
8+
context('getInt32LE()', () => {
9+
it('parses an int32 little endian', () => {
10+
expect(NumberUtils.getInt32LE(b(0, 0, 0, 1), 0)).to.equal(1 << 24);
11+
});
12+
13+
it('parses an signed int32 little endian', () => {
14+
expect(NumberUtils.getInt32LE(b(255, 255, 255, 255), 0)).to.equal(-1);
15+
});
16+
17+
it('parses an int32 little endian at offset', () => {
18+
expect(NumberUtils.getInt32LE(b(0, 0, 0, 0, 0, 1), 2)).to.equal(1 << 24);
19+
});
20+
21+
it('does not check bounds of offset', () => {
22+
expect(NumberUtils.getInt32LE(b(0, 0, 0, 1), 4)).to.equal(0);
23+
});
24+
});
25+
26+
context('getUint32LE()', () => {
27+
it('parses an unsigned int32 little endian', () => {
28+
expect(NumberUtils.getUint32LE(b(255, 255, 255, 255), 0)).to.equal(0xffff_ffff);
29+
});
30+
31+
it('parses an int32 little endian at offset', () => {
32+
expect(NumberUtils.getUint32LE(b(0, 0, 255, 255, 255, 255), 2)).to.equal(0xffff_ffff);
33+
});
34+
35+
it('does not check bounds of offset', () => {
36+
expect(NumberUtils.getUint32LE(b(0, 0, 0, 1), 4)).to.be.NaN;
37+
});
38+
});
39+
40+
context('getUint32BE()', () => {
41+
it('parses an int32 big endian', () => {
42+
expect(NumberUtils.getUint32BE(b(0, 0, 0, 1), 0)).to.equal(1);
43+
});
44+
45+
it('parses an unsigned int32 big endian', () => {
46+
expect(NumberUtils.getUint32LE(b(255, 255, 255, 255), 0)).to.equal(0xffff_ffff);
47+
});
48+
49+
it('parses an int32 big endian at offset', () => {
50+
expect(NumberUtils.getUint32BE(b(0, 0, 0, 0, 0, 1), 2)).to.equal(1);
51+
});
52+
53+
it('does not check bounds of offset', () => {
54+
expect(NumberUtils.getUint32BE(b(0, 0, 0, 1), 4)).to.be.NaN;
55+
});
56+
});
57+
58+
context('getBigInt64LE()', () => {
59+
it('parses an int64 little endian', () => {
60+
expect(NumberUtils.getBigInt64LE(b(0, 0, 0, 0, 0, 0, 0, 1), 0)).to.equal(1n << 56n);
61+
});
62+
63+
it('parses an int64 little endian at offset', () => {
64+
expect(NumberUtils.getBigInt64LE(b(0, 0, 0, 0, 0, 0, 0, 0, 0, 1), 2)).to.equal(1n << 56n);
65+
});
66+
67+
it('throws if offset is out of bounds', () => {
68+
expect(() => NumberUtils.getBigInt64LE(b(0, 0, 0, 0, 0, 0, 0, 1), 4)).to.throw(RangeError);
69+
});
70+
});
71+
72+
context('getFloat64LE()', () => {
73+
/** 2.3 in bytes */
74+
const num = [0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x02, 0x40];
75+
76+
it('parses an float64 little endian', () => {
77+
expect(NumberUtils.getFloat64LE(b(...num), 0)).to.equal(2.3);
78+
});
79+
80+
it('parses an float64 little endian at offset', () => {
81+
expect(NumberUtils.getFloat64LE(b(0, 0, ...num), 2)).to.equal(2.3);
82+
});
83+
84+
it('does not check bounds of offset', () => {
85+
expect(NumberUtils.getFloat64LE(b(...num), 4)).to.not.equal(2.3);
86+
});
87+
});
88+
89+
context('setInt32BE()', () => {
90+
it('writes an int32 big endian', () => {
91+
const space = new Uint8Array(4);
92+
expect(NumberUtils.setInt32BE(space, 0, 1)).to.equal(4);
93+
expect(space).to.deep.equal(b(0, 0, 0, 1));
94+
});
95+
96+
it('writes an int32 big endian at offset', () => {
97+
const space = new Uint8Array(6);
98+
expect(NumberUtils.setInt32BE(space, 2, 1)).to.equal(4);
99+
expect(space).to.deep.equal(b(0, 0, 0, 0, 0, 1));
100+
});
101+
102+
it('does not bound or type check', () => {
103+
const space = {};
104+
// @ts-expect-error: testing incorrect type
105+
expect(NumberUtils.setInt32BE(space, 'a', 1)).to.equal(4);
106+
expect(space).to.deep.equal({ a: 0, a1: 0, a2: 0, a3: 1 });
107+
});
108+
});
109+
110+
context('setInt32LE()', () => {
111+
it('writes an int32 big endian', () => {
112+
const space = new Uint8Array(4);
113+
expect(NumberUtils.setInt32LE(space, 0, 1)).to.equal(4);
114+
expect(space).to.deep.equal(b(1, 0, 0, 0));
115+
});
116+
117+
it('writes an int32 big endian at offset', () => {
118+
const space = new Uint8Array(6);
119+
expect(NumberUtils.setInt32LE(space, 2, 1)).to.equal(4);
120+
expect(space).to.deep.equal(b(0, 0, 1, 0, 0, 0));
121+
});
122+
123+
it('does not bound or type check', () => {
124+
const space = {};
125+
// @ts-expect-error: testing incorrect type
126+
expect(NumberUtils.setInt32LE(space, 'a', 1)).to.equal(4);
127+
expect(space).to.deep.equal({ a: 1, a1: 0, a2: 0, a3: 0 });
128+
});
129+
});
130+
});

0 commit comments

Comments
 (0)