|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const buildType = process.config.target_defaults.default_configuration; |
| 4 | +const assert = require('assert'); |
| 5 | + |
| 6 | +test(require(`../build/${buildType}/binding.node`)); |
| 7 | +test(require(`../build/${buildType}/binding_noexcept.node`)); |
| 8 | + |
| 9 | +function test(binding) { |
| 10 | + function expected(type, value) { |
| 11 | + return eval(`(new ${type}Array([${value}]))[0]`); |
| 12 | + } |
| 13 | + |
| 14 | + function nativeReadDataView(dataview, type, offset, value) { |
| 15 | + return eval(`binding.dataview_read_write.get${type}(dataview, offset)`); |
| 16 | + } |
| 17 | + |
| 18 | + function nativeWriteDataView(dataview, type, offset, value) { |
| 19 | + eval(`binding.dataview_read_write.set${type}(dataview, offset, value)`); |
| 20 | + } |
| 21 | + |
| 22 | + function jsReadDataView(dataview, type, offset, value) { |
| 23 | + return eval(`dataview.get${type}(offset, true)`); |
| 24 | + } |
| 25 | + |
| 26 | + function jsWriteDataView(dataview, type, offset, value) { |
| 27 | + eval(`dataview.set${type}(offset, value, true)`); |
| 28 | + } |
| 29 | + |
| 30 | + function testReadData(dataview, type, offset, value) { |
| 31 | + jsWriteDataView(dataview, type, offset, 0); |
| 32 | + assert.strictEqual(jsReadDataView(dataview, type, offset), 0); |
| 33 | + |
| 34 | + jsWriteDataView(dataview, type, offset, value); |
| 35 | + assert.strictEqual( |
| 36 | + nativeReadDataView(dataview, type, offset), expected(type, value)); |
| 37 | + } |
| 38 | + |
| 39 | + function testWriteData(dataview, type, offset, value) { |
| 40 | + jsWriteDataView(dataview, type, offset, 0); |
| 41 | + assert.strictEqual(jsReadDataView(dataview, type, offset), 0); |
| 42 | + |
| 43 | + nativeWriteDataView(dataview, type, offset, value); |
| 44 | + assert.strictEqual( |
| 45 | + jsReadDataView(dataview, type, offset), expected(type, value)); |
| 46 | + } |
| 47 | + |
| 48 | + function testInvalidOffset(dataview, type, offset, value) { |
| 49 | + assert.throws(() => { |
| 50 | + nativeReadDataView(dataview, type, offset); |
| 51 | + }, RangeError); |
| 52 | + |
| 53 | + assert.throws(() => { |
| 54 | + nativeWriteDataView(dataview, type, offset, value); |
| 55 | + }, RangeError); |
| 56 | + } |
| 57 | + |
| 58 | + const dataview = new DataView(new ArrayBuffer(22)); |
| 59 | + |
| 60 | + testReadData(dataview, 'Float32', 0, 10.2); |
| 61 | + testReadData(dataview, 'Float64', 4, 20.3); |
| 62 | + testReadData(dataview, 'Int8', 5, 120); |
| 63 | + testReadData(dataview, 'Int16', 7, 15000); |
| 64 | + testReadData(dataview, 'Int32', 11, 200000); |
| 65 | + testReadData(dataview, 'Uint8', 12, 128); |
| 66 | + testReadData(dataview, 'Uint16', 14, 32768); |
| 67 | + testReadData(dataview, 'Uint32', 18, 1000000); |
| 68 | + |
| 69 | + testWriteData(dataview, 'Float32', 0, 10.2); |
| 70 | + testWriteData(dataview, 'Float64', 4, 20.3); |
| 71 | + testWriteData(dataview, 'Int8', 5, 120); |
| 72 | + testWriteData(dataview, 'Int16', 7, 15000); |
| 73 | + testWriteData(dataview, 'Int32', 11, 200000); |
| 74 | + testWriteData(dataview, 'Uint8', 12, 128); |
| 75 | + testWriteData(dataview, 'Uint16', 14, 32768); |
| 76 | + testWriteData(dataview, 'Uint32', 18, 1000000); |
| 77 | + |
| 78 | + testInvalidOffset(dataview, 'Float32', 22, 10.2); |
| 79 | + testInvalidOffset(dataview, 'Float64', 22, 20.3); |
| 80 | + testInvalidOffset(dataview, 'Int8', 22, 120); |
| 81 | + testInvalidOffset(dataview, 'Int16', 22, 15000); |
| 82 | + testInvalidOffset(dataview, 'Int32', 22, 200000); |
| 83 | + testInvalidOffset(dataview, 'Uint8', 22, 128); |
| 84 | + testInvalidOffset(dataview, 'Uint16', 22, 32768); |
| 85 | + testInvalidOffset(dataview, 'Uint32', 22, 1000000); |
| 86 | +} |
0 commit comments