|
| 1 | +'use strict'; |
| 2 | +require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | + |
| 5 | +// testing basic buffer read functions |
| 6 | +const buf = Buffer.from([0xa4, 0xfd, 0x48, 0xea, 0xcf, 0xff, 0xd9, 0x01, 0xde]); |
| 7 | + |
| 8 | +function read(buff, funx, args, expected) { |
| 9 | + |
| 10 | + assert.strictEqual(buff[funx](...args), expected); |
| 11 | + assert.throws( |
| 12 | + () => buff[funx](-1), |
| 13 | + /^RangeError: Index out of range$/ |
| 14 | + ); |
| 15 | + |
| 16 | + assert.doesNotThrow( |
| 17 | + () => assert.strictEqual(buff[funx](...args, true), expected), |
| 18 | + 'noAssert does not change return value for valid ranges' |
| 19 | +); |
| 20 | + |
| 21 | +} |
| 22 | + |
| 23 | +// testing basic functionality of readDoubleBE() and readDOubleLE() |
| 24 | +read(buf, 'readDoubleBE', [1], -3.1827727774563287e+295); |
| 25 | +read(buf, 'readDoubleLE', [1], -6.966010051009108e+144); |
| 26 | + |
| 27 | +// testing basic functionality of readFLoatBE() and readFloatLE() |
| 28 | +read(buf, 'readFloatBE', [1], -1.6691549692541768e+37); |
| 29 | +read(buf, 'readFloatLE', [1], -7861303808); |
| 30 | + |
| 31 | +// testing basic functionality of readInt8() |
| 32 | +read(buf, 'readInt8', [1], -3); |
| 33 | + |
| 34 | +// testing basic functionality of readInt16BE() and readInt16LE() |
| 35 | +read(buf, 'readInt16BE', [1], -696); |
| 36 | +read(buf, 'readInt16LE', [1], 0x48fd); |
| 37 | + |
| 38 | +// testing basic functionality of readInt32BE() and readInt32LE() |
| 39 | +read(buf, 'readInt32BE', [1], -45552945); |
| 40 | +read(buf, 'readInt32LE', [1], -806729475); |
| 41 | + |
| 42 | +// testing basic functionality of readIntBE() and readIntLE() |
| 43 | +read(buf, 'readIntBE', [1, 1], -3); |
| 44 | +read(buf, 'readIntLE', [2, 1], 0x48); |
| 45 | + |
| 46 | +// testing basic functionality of readUInt8() |
| 47 | +read(buf, 'readUInt8', [1], 0xfd); |
| 48 | + |
| 49 | +// testing basic functionality of readUInt16BE() and readUInt16LE() |
| 50 | +read(buf, 'readUInt16BE', [2], 0x48ea); |
| 51 | +read(buf, 'readUInt16LE', [2], 0xea48); |
| 52 | + |
| 53 | +// testing basic functionality of readUInt32BE() and readUInt32LE() |
| 54 | +read(buf, 'readUInt32BE', [1], 0xfd48eacf); |
| 55 | +read(buf, 'readUInt32LE', [1], 0xcfea48fd); |
| 56 | + |
| 57 | +// testing basic functionality of readUIntBE() and readUIntLE() |
| 58 | +read(buf, 'readUIntBE', [2, 0], 0xfd); |
| 59 | +read(buf, 'readUIntLE', [2, 0], 0x48); |
0 commit comments