Skip to content

Commit 55a608b

Browse files
committed
testing all (u)intX_t types
1 parent 18b6265 commit 55a608b

File tree

2 files changed

+103
-64
lines changed

2 files changed

+103
-64
lines changed

.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ module.exports = {
77
extends: [
88
'standard'
99
],
10+
globals: {
11+
BigInt: true
12+
},
1013
parserOptions: {
1114
ecmaVersion: 11
1215
},

test/test.js

Lines changed: 100 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,47 @@ const { getNativeFunction, getBufferPointer } = require('../lib/index');
77

88
const test = pitesti();
99

10-
let add;
11-
let addPtr;
12-
let addAsync;
13-
let addTwiceAsync;
10+
const add = {};
11+
const addPtr = {};
12+
const addAsync = {};
13+
const addTwiceAsync = {};
14+
15+
const sizes = [8, 16, 32, 64];
1416

1517
const libAdder = path.join(__dirname, 'adder', 'libadder.so');
1618

1719
test`get functions`(() => {
18-
add = getNativeFunction(
19-
libAdder,
20-
'test_add_uint32_t',
21-
'uint32_t',
22-
['uint32_t', 'uint32_t']
23-
);
24-
25-
addPtr = getNativeFunction(
26-
libAdder,
27-
'test_add_ptr_uint32_t',
28-
'void',
29-
['uint32_t *', 'uint32_t *', 'uint32_t *']
30-
);
31-
32-
addAsync = getNativeFunction(
33-
libAdder,
34-
'test_add_async_uint32_t',
35-
'void',
36-
['uint32_t', 'uint32_t', ['void', ['uint32_t']]]
37-
);
38-
39-
addTwiceAsync = getNativeFunction(
40-
libAdder,
41-
'test_add_async_twice_uint32_t',
42-
'void',
43-
['uint32_t', 'uint32_t', ['void', ['uint32_t']]]
44-
);
45-
});
20+
for (const size of sizes) {
21+
for (const typ of [`int${size}_t`, `uint${size}_t`]) {
22+
add[typ] = getNativeFunction(
23+
libAdder,
24+
`test_add_${typ}`,
25+
typ,
26+
[typ, typ]
27+
);
28+
29+
addPtr[typ] = getNativeFunction(
30+
libAdder,
31+
`test_add_ptr_${typ}`,
32+
'void',
33+
[typ + ' *', typ + ' *', typ + ' *']
34+
);
35+
36+
addAsync[typ] = getNativeFunction(
37+
libAdder,
38+
`test_add_async_${typ}`,
39+
'void',
40+
[typ, typ, ['void', [typ]]]
41+
);
4642

47-
test`basic adding`(() => {
48-
assert.strictEqual(add(0, 2), 2);
49-
assert.strictEqual(add(4, 5), 9);
50-
assert.strictEqual(add(11, 22), 33);
51-
assert.strictEqual(add(Math.pow(2, 32) - 1, 5), 4);
43+
addTwiceAsync[typ] = getNativeFunction(
44+
libAdder,
45+
`test_add_async_twice_${typ}`,
46+
'void',
47+
[typ, typ, ['void', [typ]]]
48+
);
49+
}
50+
}
5251
});
5352

5453
test`getBufferPointer`(() => {
@@ -58,35 +57,72 @@ test`getBufferPointer`(() => {
5857
assert(testBufPtr > 0n);
5958
});
6059

61-
test`adding via pointers`(() => {
62-
const addingBuf = Buffer.alloc(12);
63-
addingBuf.writeUInt32LE(4);
64-
addingBuf.writeUInt32LE(3, 4);
65-
const addingBufPtr = getBufferPointer(addingBuf);
66-
addPtr(addingBufPtr, addingBufPtr + 4n, addingBufPtr + 8n);
67-
assert.strictEqual(addingBuf.readUInt32LE(8), 7);
68-
});
60+
function num (size, n) {
61+
return size === 64 ? BigInt(n) : n;
62+
}
6963

70-
test`async adding`((done) => {
71-
addAsync(4, 5, (result) => {
72-
assert.strictEqual(result, 9);
73-
done();
74-
});
75-
});
64+
function bufAccess (typ, size) {
65+
const u = typ.startsWith('u') ? 'U' : '';
66+
const big = size === 64 ? 'Big' : '';
67+
const endian = size === 8 ? '' : 'LE';
68+
return `${big}${u}Int${size}${endian}`;
69+
}
7670

77-
test`promisified adding`(async () => {
78-
const addPromise = (a, b) => new Promise(resolve => addAsync(a, b, resolve));
79-
assert.strictEqual(await addPromise(5, 3), 8);
80-
});
71+
function read (typ, size, buf, n) {
72+
return buf[`read${bufAccess(typ, size)}`](n);
73+
}
8174

82-
test`calling callback more than once`((done) => {
83-
let counter = 0;
84-
addTwiceAsync(4, 5, (result) => {
85-
assert.strictEqual(result, 9);
86-
if (++counter === 2) {
87-
done();
88-
}
89-
});
90-
});
75+
function write (typ, size, buf, n, i) {
76+
return buf[`write${bufAccess(typ, size)}`](n, i);
77+
}
78+
79+
for (const size of sizes) {
80+
for (const typ of [`int${size}_t`, `uint${size}_t`]) {
81+
test.context(typ, () => {
82+
const n = num.bind(null, size);
83+
test`basic adding`(() => {
84+
assert.strictEqual(add[typ](n(0), n(2)), n(2));
85+
assert.strictEqual(add[typ](n(4), n(5)), n(9));
86+
assert.strictEqual(add[typ](n(11), n(22)), n(33));
87+
if (typ.startsWith('u') && size !== 64) {
88+
assert.strictEqual(add[typ](Math.pow(2, size) - 1, 5), 4);
89+
}
90+
});
91+
92+
const r = read.bind(null, typ, size);
93+
const w = write.bind(null, typ, size);
94+
test`adding via pointers`(() => {
95+
const addingBuf = Buffer.alloc(size * 3);
96+
w(addingBuf, n(4), 0);
97+
w(addingBuf, n(3), size / 8);
98+
const addingBufPtr = getBufferPointer(addingBuf);
99+
addPtr[typ](addingBufPtr, addingBufPtr + BigInt(size / 8), addingBufPtr + BigInt((size / 8) * 2));
100+
assert.strictEqual(r(addingBuf, (size / 8) * 2), n(7));
101+
});
102+
103+
test`async adding`((done) => {
104+
addAsync[typ](n(4), n(5), (result) => {
105+
assert.strictEqual(result, n(9));
106+
done();
107+
});
108+
});
109+
110+
test`promisified adding`(async () => {
111+
const addPromise = (a, b) => new Promise(resolve => addAsync[typ](a, b, resolve));
112+
assert.strictEqual(await addPromise(n(5), n(3)), n(8));
113+
});
114+
115+
test`calling callback more than once`((done) => {
116+
let counter = 0;
117+
addTwiceAsync[typ](n(4), n(5), (result) => {
118+
assert.strictEqual(result, n(9));
119+
if (++counter === 2) {
120+
done();
121+
}
122+
});
123+
});
124+
});
125+
}
126+
}
91127

92128
test();

0 commit comments

Comments
 (0)