MCPcopy Create free account
hub / github.com/nodejs/node-addon-api / test

Function test

test/dataview/dataview_read_write.js:8–92  ·  view source on GitHub ↗
(binding)

Source from the content-addressed store, hash-verified

6module.exports = require('../common').runTest(test);
7
8function test (binding) {
9 function expected (type, value) {
10 return eval(`(new ${type}Array([${value}]))[0]`);
11 }
12
13 function nativeReadDataView (dataview, type, offset, value) {
14 return eval(`binding.dataview_read_write.get${type}(dataview, offset)`);
15 }
16
17 function nativeWriteDataView (dataview, type, offset, value) {
18 eval(`binding.dataview_read_write.set${type}(dataview, offset, value)`);
19 }
20
21 // eslint-disable-next-line no-unused-vars
22 function isLittleEndian () {
23 const buffer = new ArrayBuffer(2);
24 new DataView(buffer).setInt16(0, 256, true /* littleEndian */);
25 return new Int16Array(buffer)[0] === 256;
26 }
27
28 function jsReadDataView (dataview, type, offset, value) {
29 return eval(`dataview.get${type}(offset, isLittleEndian())`);
30 }
31
32 function jsWriteDataView (dataview, type, offset, value) {
33 eval(`dataview.set${type}(offset, value, isLittleEndian())`);
34 }
35
36 function testReadData (dataview, type, offset, value) {
37 jsWriteDataView(dataview, type, offset, 0);
38 assert.strictEqual(jsReadDataView(dataview, type, offset), 0);
39
40 jsWriteDataView(dataview, type, offset, value);
41 assert.strictEqual(
42 nativeReadDataView(dataview, type, offset), expected(type, value));
43 }
44
45 function testWriteData (dataview, type, offset, value) {
46 jsWriteDataView(dataview, type, offset, 0);
47 assert.strictEqual(jsReadDataView(dataview, type, offset), 0);
48
49 nativeWriteDataView(dataview, type, offset, value);
50 assert.strictEqual(
51 jsReadDataView(dataview, type, offset), expected(type, value));
52 }
53
54 function testInvalidOffset (dataview, type, offset, value) {
55 assert.throws(() => {
56 nativeReadDataView(dataview, type, offset);
57 }, RangeError);
58
59 assert.throws(() => {
60 nativeWriteDataView(dataview, type, offset, value);
61 }, RangeError);
62 }
63
64 const dataview = new DataView(new ArrayBuffer(22));
65

Callers

nothing calls this directly

Calls 3

testReadDataFunction · 0.85
testWriteDataFunction · 0.85
testInvalidOffsetFunction · 0.85

Tested by

no test coverage detected