buffer: refactor all read/write functions

There are a lot of changes in this commit:

1) Remove the `noAssert` argument from all read and write functions.
2) Improve the performance of all read floating point functions
   significantly. This is done by switching to TypedArrays as the
   write floating point write functions.
3) No implicit type coercion for offset and byteLength anymore.
4) Adds a lot of tests.
5) Moves the read and write functions to the internal buffer file
   to split the files in smaller chunks.
6) Reworked a lot of existing tests.
7) Improve the performane of all all read write functions by using
   a faster input validation and by improving function logic.
8) Significantly improved the performance of all read int functions.
   This is done by using a implementation without a loop.
9) Improved error handling.
10) Rename test file to use the correct subsystem.

PR-URL: https://github.com/nodejs/node/pull/18395
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
Ruben Bridgewater committed 2018-03-02 19:29:46 +00:00
1 parent a6c490cc8e
commit e8bb1f35df
21 files changed
+2015 -1839

No files matched your search

-7
View File
@@ -42,13 +42,6 @@ assert.throws(function() {
Buffer.from(new AB());
}, TypeError);
// write{Double,Float}{LE,BE} with noAssert should not crash, cf. #3766
const b = Buffer.allocUnsafe(1);
b.writeFloatLE(11.11, 0, true);
b.writeFloatBE(11.11, 0, true);
b.writeDoubleLE(11.11, 0, true);
b.writeDoubleBE(11.11, 0, true);
// Test the byteOffset and length arguments
{
const ab = new Uint8Array(5);
+2 -23
View File
@@ -6,16 +6,11 @@ const assert = require('assert');
const buf = Buffer.from([0xa4, 0xfd, 0x48, 0xea, 0xcf, 0xff, 0xd9, 0x01, 0xde]);
function read(buff, funx, args, expected) {
assert.strictEqual(buff[funx](...args), expected);
common.expectsError(
() => buff[funx](-1, args[1]),
{
code: 'ERR_INDEX_OUT_OF_RANGE'
}
{ code: 'ERR_OUT_OF_RANGE' }
);
assert.strictEqual(buff[funx](...args, true), expected);
}
// testing basic functionality of readDoubleBE() and readDoubleLE()
@@ -123,7 +118,7 @@ assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(-1), RangeError);
(0xFFFFFFFF >> (32 - bits)));
});
// test for common read(U)IntLE/BE
// Test for common read(U)IntLE/BE
{
const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
@@ -144,19 +139,3 @@ assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(-1), RangeError);
assert.strictEqual(buf.readIntLE(0, 6), 0x060504030201);
assert.strictEqual(buf.readIntBE(0, 6), 0x010203040506);
}
// test for byteLength parameter not between 1 and 6 (inclusive)
common.expectsError(() => { buf.readIntLE(1); }, { code: 'ERR_OUT_OF_RANGE' });
common.expectsError(() => { buf.readIntLE(1, 'string'); },
{ code: 'ERR_OUT_OF_RANGE' });
common.expectsError(() => { buf.readIntLE(1, 0); },
{ code: 'ERR_OUT_OF_RANGE' });
common.expectsError(() => { buf.readIntLE(1, 7); },
{ code: 'ERR_OUT_OF_RANGE' });
common.expectsError(() => { buf.readIntBE(1); }, { code: 'ERR_OUT_OF_RANGE' });
common.expectsError(() => { buf.readIntBE(1, 'string'); },
{ code: 'ERR_OUT_OF_RANGE' });
common.expectsError(() => { buf.readIntBE(1, 0); },
{ code: 'ERR_OUT_OF_RANGE' });
common.expectsError(() => { buf.readIntBE(1, 7); },
{ code: 'ERR_OUT_OF_RANGE' });
+138
View File
@@ -0,0 +1,138 @@
'use strict';
require('../common');
const assert = require('assert');
// Test (64 bit) double
const buffer = Buffer.allocUnsafe(8);
buffer[0] = 0x55;
buffer[1] = 0x55;
buffer[2] = 0x55;
buffer[3] = 0x55;
buffer[4] = 0x55;
buffer[5] = 0x55;
buffer[6] = 0xd5;
buffer[7] = 0x3f;
assert.strictEqual(buffer.readDoubleBE(0), 1.1945305291680097e+103);
assert.strictEqual(buffer.readDoubleLE(0), 0.3333333333333333);
buffer[0] = 1;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0;
buffer[4] = 0;
buffer[5] = 0;
buffer[6] = 0xf0;
buffer[7] = 0x3f;
assert.strictEqual(buffer.readDoubleBE(0), 7.291122019655968e-304);
assert.strictEqual(buffer.readDoubleLE(0), 1.0000000000000002);
buffer[0] = 2;
assert.strictEqual(buffer.readDoubleBE(0), 4.778309726801735e-299);
assert.strictEqual(buffer.readDoubleLE(0), 1.0000000000000004);
buffer[0] = 1;
buffer[6] = 0;
buffer[7] = 0;
assert.strictEqual(buffer.readDoubleBE(0), 7.291122019556398e-304);
assert.strictEqual(buffer.readDoubleLE(0), 5e-324);
buffer[0] = 0xff;
buffer[1] = 0xff;
buffer[2] = 0xff;
buffer[3] = 0xff;
buffer[4] = 0xff;
buffer[5] = 0xff;
buffer[6] = 0x0f;
buffer[7] = 0x00;
assert.ok(Number.isNaN(buffer.readDoubleBE(0)));
assert.strictEqual(buffer.readDoubleLE(0), 2.225073858507201e-308);
buffer[6] = 0xef;
buffer[7] = 0x7f;
assert.ok(Number.isNaN(buffer.readDoubleBE(0)));
assert.strictEqual(buffer.readDoubleLE(0), 1.7976931348623157e+308);
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0;
buffer[4] = 0;
buffer[5] = 0;
buffer[6] = 0xf0;
buffer[7] = 0x3f;
assert.strictEqual(buffer.readDoubleBE(0), 3.03865e-319);
assert.strictEqual(buffer.readDoubleLE(0), 1);
buffer[6] = 0;
buffer[7] = 0x40;
assert.strictEqual(buffer.readDoubleBE(0), 3.16e-322);
assert.strictEqual(buffer.readDoubleLE(0), 2);
buffer[7] = 0xc0;
assert.strictEqual(buffer.readDoubleBE(0), 9.5e-322);
assert.strictEqual(buffer.readDoubleLE(0), -2);
buffer[6] = 0x10;
buffer[7] = 0;
assert.strictEqual(buffer.readDoubleBE(0), 2.0237e-320);
assert.strictEqual(buffer.readDoubleLE(0), 2.2250738585072014e-308);
buffer[6] = 0;
assert.strictEqual(buffer.readDoubleBE(0), 0);
assert.strictEqual(buffer.readDoubleLE(0), 0);
assert.ok(1 / buffer.readDoubleLE(0) >= 0);
buffer[7] = 0x80;
assert.strictEqual(buffer.readDoubleBE(0), 6.3e-322);
assert.strictEqual(buffer.readDoubleLE(0), -0);
assert.ok(1 / buffer.readDoubleLE(0) < 0);
buffer[6] = 0xf0;
buffer[7] = 0x7f;
assert.strictEqual(buffer.readDoubleBE(0), 3.0418e-319);
assert.strictEqual(buffer.readDoubleLE(0), Infinity);
buffer[7] = 0xff;
assert.strictEqual(buffer.readDoubleBE(0), 3.04814e-319);
assert.strictEqual(buffer.readDoubleLE(0), -Infinity);
['readDoubleLE', 'readDoubleBE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => buffer[fn](off),
{ code: 'ERR_INVALID_ARG_TYPE' }
);
});
[Infinity, -1, 1].forEach((offset) => {
assert.throws(
() => buffer[fn](offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be >= 0 and <= 0. Received ${offset}`
});
});
assert.throws(
() => Buffer.alloc(1)[fn](1),
{
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
name: 'RangeError [ERR_BUFFER_OUT_OF_BOUNDS]',
message: 'Attempt to write outside buffer bounds'
});
[NaN, 1.01].forEach((offset) => {
assert.throws(
() => buffer[fn](offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be an integer. Received ${offset}`
});
});
});
+101
View File
@@ -0,0 +1,101 @@
'use strict';
require('../common');
const assert = require('assert');
// Test 32 bit float
const buffer = Buffer.alloc(4);
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0x80;
buffer[3] = 0x3f;
assert.strictEqual(buffer.readFloatBE(0), 4.600602988224807e-41);
assert.strictEqual(buffer.readFloatLE(0), 1);
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0xc0;
assert.strictEqual(buffer.readFloatBE(0), 2.6904930515036488e-43);
assert.strictEqual(buffer.readFloatLE(0), -2);
buffer[0] = 0xff;
buffer[1] = 0xff;
buffer[2] = 0x7f;
buffer[3] = 0x7f;
assert.ok(Number.isNaN(buffer.readFloatBE(0)));
assert.strictEqual(buffer.readFloatLE(0), 3.4028234663852886e+38);
buffer[0] = 0xab;
buffer[1] = 0xaa;
buffer[2] = 0xaa;
buffer[3] = 0x3e;
assert.strictEqual(buffer.readFloatBE(0), -1.2126478207002966e-12);
assert.strictEqual(buffer.readFloatLE(0), 0.3333333432674408);
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0;
assert.strictEqual(buffer.readFloatBE(0), 0);
assert.strictEqual(buffer.readFloatLE(0), 0);
assert.ok(1 / buffer.readFloatLE(0) >= 0);
buffer[3] = 0x80;
assert.strictEqual(buffer.readFloatBE(0), 1.793662034335766e-43);
assert.strictEqual(buffer.readFloatLE(0), -0);
assert.ok(1 / buffer.readFloatLE(0) < 0);
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0x80;
buffer[3] = 0x7f;
assert.strictEqual(buffer.readFloatBE(0), 4.609571298396486e-41);
assert.strictEqual(buffer.readFloatLE(0), Infinity);
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0x80;
buffer[3] = 0xff;
assert.strictEqual(buffer.readFloatBE(0), 4.627507918739843e-41);
assert.strictEqual(buffer.readFloatLE(0), -Infinity);
['readFloatLE', 'readFloatBE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => buffer[fn](off),
{ code: 'ERR_INVALID_ARG_TYPE' }
);
});
[Infinity, -1, 1].forEach((offset) => {
assert.throws(
() => buffer[fn](offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be >= 0 and <= 0. Received ${offset}`
});
});
assert.throws(
() => Buffer.alloc(1)[fn](1),
{
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
name: 'RangeError [ERR_BUFFER_OUT_OF_BOUNDS]',
message: 'Attempt to write outside buffer bounds'
});
[NaN, 1.01].forEach((offset) => {
assert.throws(
() => buffer[fn](offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be an integer. Received ${offset}`
});
});
});
+195
View File
@@ -0,0 +1,195 @@
'use strict';
require('../common');
const assert = require('assert');
// Test OOB
{
const buffer = Buffer.alloc(4);
['Int8', 'Int16BE', 'Int16LE', 'Int32BE', 'Int32LE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
assert.throws(
() => buffer[`read${fn}`](o),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError [ERR_INVALID_ARG_TYPE]'
});
});
[Infinity, -1, -4294967295].forEach((offset) => {
assert.throws(
() => buffer[`read${fn}`](offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]'
});
});
[NaN, 1.01].forEach((offset) => {
assert.throws(
() => buffer[`read${fn}`](offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be an integer. Received ${offset}`
});
});
});
}
// Test 8 bit signed integers
{
const data = Buffer.alloc(4);
data[0] = 0x23;
assert.strictEqual(data.readInt8(0), 0x23);
data[0] = 0xff;
assert.strictEqual(data.readInt8(0), -1);
data[0] = 0x87;
data[1] = 0xab;
data[2] = 0x7c;
data[3] = 0xef;
assert.strictEqual(data.readInt8(0), -121);
assert.strictEqual(data.readInt8(1), -85);
assert.strictEqual(data.readInt8(2), 124);
assert.strictEqual(data.readInt8(3), -17);
}
// Test 16 bit integers
{
const buffer = Buffer.alloc(6);
buffer[0] = 0x16;
buffer[1] = 0x79;
assert.strictEqual(buffer.readInt16BE(0), 0x1679);
assert.strictEqual(buffer.readInt16LE(0), 0x7916);
buffer[0] = 0xff;
buffer[1] = 0x80;
assert.strictEqual(buffer.readInt16BE(0), -128);
assert.strictEqual(buffer.readInt16LE(0), -32513);
buffer[0] = 0x77;
buffer[1] = 0x65;
buffer[2] = 0x65;
buffer[3] = 0x6e;
buffer[4] = 0x69;
buffer[5] = 0x78;
assert.strictEqual(buffer.readInt16BE(0), 0x7765);
assert.strictEqual(buffer.readInt16BE(1), 0x6565);
assert.strictEqual(buffer.readInt16BE(2), 0x656e);
assert.strictEqual(buffer.readInt16BE(3), 0x6e69);
assert.strictEqual(buffer.readInt16BE(4), 0x6978);
assert.strictEqual(buffer.readInt16LE(0), 0x6577);
assert.strictEqual(buffer.readInt16LE(1), 0x6565);
assert.strictEqual(buffer.readInt16LE(2), 0x6e65);
assert.strictEqual(buffer.readInt16LE(3), 0x696e);
assert.strictEqual(buffer.readInt16LE(4), 0x7869);
}
// Test 32 bit integers
{
const buffer = Buffer.alloc(6);
buffer[0] = 0x43;
buffer[1] = 0x53;
buffer[2] = 0x16;
buffer[3] = 0x79;
assert.strictEqual(buffer.readInt32BE(0), 0x43531679);
assert.strictEqual(buffer.readInt32LE(0), 0x79165343);
buffer[0] = 0xff;
buffer[1] = 0xfe;
buffer[2] = 0xef;
buffer[3] = 0xfa;
assert.strictEqual(buffer.readInt32BE(0), -69638);
assert.strictEqual(buffer.readInt32LE(0), -84934913);
buffer[0] = 0x42;
buffer[1] = 0xc3;
buffer[2] = 0x95;
buffer[3] = 0xa9;
buffer[4] = 0x36;
buffer[5] = 0x17;
assert.strictEqual(buffer.readInt32BE(0), 0x42c395a9);
assert.strictEqual(buffer.readInt32BE(1), -1013601994);
assert.strictEqual(buffer.readInt32BE(2), -1784072681);
assert.strictEqual(buffer.readInt32LE(0), -1449802942);
assert.strictEqual(buffer.readInt32LE(1), 917083587);
assert.strictEqual(buffer.readInt32LE(2), 389458325);
}
// Test Int
{
const buffer = Buffer.alloc(8);
// Check byteLength.
['readIntBE', 'readIntLE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((len) => {
assert.throws(
() => buffer[fn](0, len),
{ code: 'ERR_INVALID_ARG_TYPE' });
});
[Infinity, -1].forEach((byteLength) => {
assert.throws(
() => buffer[fn](0, byteLength),
{
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "byteLength" is out of range. ' +
`It must be >= 1 and <= 6. Received ${byteLength}`
});
});
[NaN, 1.01].forEach((byteLength) => {
assert.throws(
() => buffer[fn](0, byteLength),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "byteLength" is out of range. ' +
`It must be an integer. Received ${byteLength}`
});
});
});
// Test 1 to 6 bytes.
for (let i = 1; i < 6; i++) {
['readIntBE', 'readIntLE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
assert.throws(
() => buffer[fn](o, i),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError [ERR_INVALID_ARG_TYPE]'
});
});
[Infinity, -1, -4294967295].forEach((offset) => {
assert.throws(
() => buffer[fn](offset, i),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be >= 0 and <= ${8 - i}. Received ${offset}`
});
});
[NaN, 1.01].forEach((offset) => {
assert.throws(
() => buffer[fn](offset, i),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be an integer. Received ${offset}`
});
});
});
}
}
@@ -1,95 +0,0 @@
'use strict';
require('../common');
const assert = require('assert');
// testing buffer write functions
const outOfRange = /^RangeError\b.*\bIndex out of range$/;
function write(funx, args, result, res) {
{
const buf = Buffer.alloc(9);
assert.strictEqual(buf[funx](...args), result);
assert.deepStrictEqual(buf, res);
}
writeInvalidOffset(-1);
writeInvalidOffset(9);
{
const buf2 = Buffer.alloc(9);
assert.strictEqual(buf2[funx](...args, true), result);
assert.deepStrictEqual(buf2, res);
}
function writeInvalidOffset(offset) {
const newArgs = Array.from(args);
newArgs[1] = offset;
assert.throws(() => Buffer.alloc(9)[funx](...newArgs), outOfRange);
const buf = Buffer.alloc(9);
buf[funx](...newArgs, true);
assert.deepStrictEqual(buf, Buffer.alloc(9));
}
}
write('writeInt8', [1, 0], 1, Buffer.from([1, 0, 0, 0, 0, 0, 0, 0, 0]));
write('writeIntBE', [1, 1, 4], 5, Buffer.from([0, 0, 0, 0, 1, 0, 0, 0, 0]));
write('writeIntLE', [1, 1, 4], 5, Buffer.from([0, 1, 0, 0, 0, 0, 0, 0, 0]));
write('writeInt16LE', [1, 1], 3, Buffer.from([0, 1, 0, 0, 0, 0, 0, 0, 0]));
write('writeInt16BE', [1, 1], 3, Buffer.from([0, 0, 1, 0, 0, 0, 0, 0, 0]));
write('writeInt32LE', [1, 1], 5, Buffer.from([0, 1, 0, 0, 0, 0, 0, 0, 0]));
write('writeInt32BE', [1, 1], 5, Buffer.from([0, 0, 0, 0, 1, 0, 0, 0, 0]));
write('writeUInt8', [1, 0], 1, Buffer.from([1, 0, 0, 0, 0, 0, 0, 0, 0]));
write('writeUIntLE', [1, 1, 4], 5, Buffer.from([0, 1, 0, 0, 0, 0, 0, 0, 0]));
write('writeUIntBE', [1, 1, 4], 5, Buffer.from([0, 0, 0, 0, 1, 0, 0, 0, 0]));
write('writeUInt16LE', [1, 1], 3, Buffer.from([0, 1, 0, 0, 0, 0, 0, 0, 0]));
write('writeUInt16BE', [1, 1], 3, Buffer.from([0, 0, 1, 0, 0, 0, 0, 0, 0]));
write('writeUInt32LE', [1, 1], 5, Buffer.from([0, 1, 0, 0, 0, 0, 0, 0, 0]));
write('writeUInt32BE', [1, 1], 5, Buffer.from([0, 0, 0, 0, 1, 0, 0, 0, 0]));
write('writeDoubleBE', [1, 1], 9, Buffer.from([0, 63, 240, 0, 0, 0, 0, 0, 0]));
write('writeDoubleLE', [1, 1], 9, Buffer.from([0, 0, 0, 0, 0, 0, 0, 240, 63]));
write('writeFloatBE', [1, 1], 5, Buffer.from([0, 63, 128, 0, 0, 0, 0, 0, 0]));
write('writeFloatLE', [1, 1], 5, Buffer.from([0, 0, 0, 128, 63, 0, 0, 0, 0]));
function writePartial(funx, args, result, res) {
assert.throws(() => Buffer.alloc(9)[funx](...args), outOfRange);
const buf = Buffer.alloc(9);
assert.strictEqual(buf[funx](...args, true), result);
assert.deepStrictEqual(buf, res);
}
// Test partial writes (cases where the buffer isn't large enough to hold the
// entire value, but is large enough to hold parts of it).
writePartial('writeIntBE', [0x0eadbeef, 6, 4], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0x0e, 0xad, 0xbe]));
writePartial('writeIntLE', [0x0eadbeef, 6, 4], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0xef, 0xbe, 0xad]));
writePartial('writeInt16BE', [0x1234, 8], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0x12]));
writePartial('writeInt16LE', [0x1234, 8], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0x34]));
writePartial('writeInt32BE', [0x0eadbeef, 6], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0x0e, 0xad, 0xbe]));
writePartial('writeInt32LE', [0x0eadbeef, 6], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0xef, 0xbe, 0xad]));
writePartial('writeUIntBE', [0xdeadbeef, 6, 4], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0xde, 0xad, 0xbe]));
writePartial('writeUIntLE', [0xdeadbeef, 6, 4], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0xef, 0xbe, 0xad]));
writePartial('writeUInt16BE', [0x1234, 8], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0x12]));
writePartial('writeUInt16LE', [0x1234, 8], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0x34]));
writePartial('writeUInt32BE', [0xdeadbeef, 6], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0xde, 0xad, 0xbe]));
writePartial('writeUInt32LE', [0xdeadbeef, 6], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0xef, 0xbe, 0xad]));
writePartial('writeDoubleBE', [1, 2], 10,
Buffer.from([0, 0, 63, 240, 0, 0, 0, 0, 0]));
writePartial('writeDoubleLE', [1, 2], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 240]));
writePartial('writeFloatBE', [1, 6], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 63, 128, 0]));
writePartial('writeFloatLE', [1, 6], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 128]));
+119
View File
@@ -0,0 +1,119 @@
'use strict';
// Tests to verify doubles are correctly written
require('../common');
const assert = require('assert');
const buffer = Buffer.allocUnsafe(16);
buffer.writeDoubleBE(2.225073858507201e-308, 0);
buffer.writeDoubleLE(2.225073858507201e-308, 8);
assert.ok(buffer.equals(new Uint8Array([
0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00
])));
buffer.writeDoubleBE(1.0000000000000004, 0);
buffer.writeDoubleLE(1.0000000000000004, 8);
assert.ok(buffer.equals(new Uint8Array([
0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f
])));
buffer.writeDoubleBE(-2, 0);
buffer.writeDoubleLE(-2, 8);
assert.ok(buffer.equals(new Uint8Array([
0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0
])));
buffer.writeDoubleBE(1.7976931348623157e+308, 0);
buffer.writeDoubleLE(1.7976931348623157e+308, 8);
assert.ok(buffer.equals(new Uint8Array([
0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0x7f
])));
buffer.writeDoubleBE(0 * -1, 0);
buffer.writeDoubleLE(0 * -1, 8);
assert.ok(buffer.equals(new Uint8Array([
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80
])));
buffer.writeDoubleBE(Infinity, 0);
buffer.writeDoubleLE(Infinity, 8);
assert.ok(buffer.equals(new Uint8Array([
0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x7F
])));
assert.strictEqual(buffer.readDoubleBE(0), Infinity);
assert.strictEqual(buffer.readDoubleLE(8), Infinity);
buffer.writeDoubleBE(-Infinity, 0);
buffer.writeDoubleLE(-Infinity, 8);
assert.ok(buffer.equals(new Uint8Array([
0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xFF
])));
assert.strictEqual(buffer.readDoubleBE(0), -Infinity);
assert.strictEqual(buffer.readDoubleLE(8), -Infinity);
buffer.writeDoubleBE(NaN, 0);
buffer.writeDoubleLE(NaN, 8);
assert.ok(buffer.equals(new Uint8Array([
0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x7F
])));
assert.ok(Number.isNaN(buffer.readDoubleBE(0)));
assert.ok(Number.isNaN(buffer.readDoubleLE(8)));
// OOB in writeDouble{LE,BE} should throw.
{
const small = Buffer.allocUnsafe(1);
['writeDoubleLE', 'writeDoubleBE'].forEach((fn) => {
assert.throws(
() => small[fn](11.11, 0),
{
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
name: 'RangeError [ERR_BUFFER_OUT_OF_BOUNDS]',
message: 'Attempt to write outside buffer bounds'
});
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => small[fn](23, off),
{ code: 'ERR_INVALID_ARG_TYPE' });
});
[Infinity, -1, 9].forEach((offset) => {
assert.throws(
() => buffer[fn](23, offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be >= 0 and <= 8. Received ${offset}`
});
});
[NaN, 1.01].forEach((offset) => {
assert.throws(
() => buffer[fn](42, offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be an integer. Received ${offset}`
});
});
});
}
+102
View File
@@ -0,0 +1,102 @@
'use strict';
// Tests to verify floats are correctly written
require('../common');
const assert = require('assert');
const buffer = Buffer.allocUnsafe(8);
buffer.writeFloatBE(1, 0);
buffer.writeFloatLE(1, 4);
assert.ok(buffer.equals(
new Uint8Array([ 0x3f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f ])));
buffer.writeFloatBE(1 / 3, 0);
buffer.writeFloatLE(1 / 3, 4);
assert.ok(buffer.equals(
new Uint8Array([ 0x3e, 0xaa, 0xaa, 0xab, 0xab, 0xaa, 0xaa, 0x3e ])));
buffer.writeFloatBE(3.4028234663852886e+38, 0);
buffer.writeFloatLE(3.4028234663852886e+38, 4);
assert.ok(buffer.equals(
new Uint8Array([ 0x7f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x7f ])));
buffer.writeFloatLE(1.1754943508222875e-38, 0);
buffer.writeFloatBE(1.1754943508222875e-38, 4);
assert.ok(buffer.equals(
new Uint8Array([ 0x00, 0x00, 0x80, 0x00, 0x00, 0x80, 0x00, 0x00 ])));
buffer.writeFloatBE(0 * -1, 0);
buffer.writeFloatLE(0 * -1, 4);
assert.ok(buffer.equals(
new Uint8Array([ 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 ])));
buffer.writeFloatBE(Infinity, 0);
buffer.writeFloatLE(Infinity, 4);
assert.ok(buffer.equals(
new Uint8Array([ 0x7F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x7F ])));
assert.strictEqual(buffer.readFloatBE(0), Infinity);
assert.strictEqual(buffer.readFloatLE(4), Infinity);
buffer.writeFloatBE(-Infinity, 0);
buffer.writeFloatLE(-Infinity, 4);
assert.ok(buffer.equals(
new Uint8Array([ 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF ])));
assert.strictEqual(buffer.readFloatBE(0), -Infinity);
assert.strictEqual(buffer.readFloatLE(4), -Infinity);
buffer.writeFloatBE(NaN, 0);
buffer.writeFloatLE(NaN, 4);
assert.ok(buffer.equals(
new Uint8Array([ 0x7F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x7F ])));
assert.ok(Number.isNaN(buffer.readFloatBE(0)));
assert.ok(Number.isNaN(buffer.readFloatLE(4)));
// OOB in writeFloat{LE,BE} should throw.
{
const small = Buffer.allocUnsafe(1);
['writeFloatLE', 'writeFloatBE'].forEach((fn) => {
assert.throws(
() => small[fn](11.11, 0),
{
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
name: 'RangeError [ERR_BUFFER_OUT_OF_BOUNDS]',
message: 'Attempt to write outside buffer bounds'
});
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => small[fn](23, off),
{ code: 'ERR_INVALID_ARG_TYPE' }
);
});
[Infinity, -1, 5].forEach((offset) => {
assert.throws(
() => buffer[fn](23, offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be >= 0 and <= 4. Received ${offset}`
}
);
});
[NaN, 1.01].forEach((offset) => {
assert.throws(
() => buffer[fn](42, offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be an integer. Received ${offset}`
});
});
});
}
+235
View File
@@ -0,0 +1,235 @@
'use strict';
// Tests to verify signed integers are correctly written
const common = require('../common');
const assert = require('assert');
const errorOutOfBounds = common.expectsError({
code: 'ERR_OUT_OF_RANGE',
type: RangeError,
message: new RegExp('^The value of "value" is out of range\\. ' +
'It must be >= -\\d+ and <= \\d+\\. Received .+$')
}, 10);
// Test 8 bit
{
const buffer = Buffer.alloc(2);
buffer.writeInt8(0x23, 0);
buffer.writeInt8(-5, 1);
assert.ok(buffer.equals(new Uint8Array([ 0x23, 0xfb ])));
/* Make sure we handle min/max correctly */
buffer.writeInt8(0x7f, 0);
buffer.writeInt8(-0x80, 1);
assert.ok(buffer.equals(new Uint8Array([ 0x7f, 0x80 ])));
assert.throws(() => {
buffer.writeInt8(0x7f + 1, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer.writeInt8(-0x80 - 1, 0);
}, errorOutOfBounds);
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => buffer.writeInt8(23, off),
{ code: 'ERR_INVALID_ARG_TYPE' });
});
[NaN, Infinity, -1, 1.01].forEach((off) => {
assert.throws(
() => buffer.writeInt8(23, off),
{ code: 'ERR_OUT_OF_RANGE' });
});
}
// Test 16 bit
{
const buffer = Buffer.alloc(4);
buffer.writeInt16BE(0x0023, 0);
buffer.writeInt16LE(0x0023, 2);
assert.ok(buffer.equals(new Uint8Array([ 0x00, 0x23, 0x23, 0x00 ])));
buffer.writeInt16BE(-5, 0);
buffer.writeInt16LE(-5, 2);
assert.ok(buffer.equals(new Uint8Array([ 0xff, 0xfb, 0xfb, 0xff ])));
buffer.writeInt16BE(-1679, 0);
buffer.writeInt16LE(-1679, 2);
assert.ok(buffer.equals(new Uint8Array([ 0xf9, 0x71, 0x71, 0xf9 ])));
/* Make sure we handle min/max correctly */
buffer.writeInt16BE(0x7fff, 0);
buffer.writeInt16BE(-0x8000, 2);
assert.ok(buffer.equals(new Uint8Array([ 0x7f, 0xff, 0x80, 0x00 ])));
buffer.writeInt16LE(0x7fff, 0);
buffer.writeInt16LE(-0x8000, 2);
assert.ok(buffer.equals(new Uint8Array([ 0xff, 0x7f, 0x00, 0x80 ])));
['writeInt16BE', 'writeInt16LE'].forEach((fn) => {
assert.throws(() => {
buffer[fn](0x7fff + 1, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer[fn](-0x8000 - 1, 0);
}, errorOutOfBounds);
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => buffer[fn](23, off),
{ code: 'ERR_INVALID_ARG_TYPE' });
});
[NaN, Infinity, -1, 1.01].forEach((off) => {
assert.throws(
() => buffer[fn](23, off),
{ code: 'ERR_OUT_OF_RANGE' });
});
});
}
// Test 32 bit
{
const buffer = Buffer.alloc(8);
buffer.writeInt32BE(0x23, 0);
buffer.writeInt32LE(0x23, 4);
assert.ok(buffer.equals(new Uint8Array([
0x00, 0x00, 0x00, 0x23, 0x23, 0x00, 0x00, 0x00
])));
buffer.writeInt32BE(-5, 0);
buffer.writeInt32LE(-5, 4);
assert.ok(buffer.equals(new Uint8Array([
0xff, 0xff, 0xff, 0xfb, 0xfb, 0xff, 0xff, 0xff
])));
buffer.writeInt32BE(-805306713, 0);
buffer.writeInt32LE(-805306713, 4);
assert.ok(buffer.equals(new Uint8Array([
0xcf, 0xff, 0xfe, 0xa7, 0xa7, 0xfe, 0xff, 0xcf
])));
/* Make sure we handle min/max correctly */
buffer.writeInt32BE(0x7fffffff, 0);
buffer.writeInt32BE(-0x80000000, 4);
assert.ok(buffer.equals(new Uint8Array([
0x7f, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00
])));
buffer.writeInt32LE(0x7fffffff, 0);
buffer.writeInt32LE(-0x80000000, 4);
assert.ok(buffer.equals(new Uint8Array([
0xff, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x80
])));
['writeInt32BE', 'writeInt32LE'].forEach((fn) => {
assert.throws(() => {
buffer[fn](0x7fffffff + 1, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer[fn](-0x80000000 - 1, 0);
}, errorOutOfBounds);
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => buffer[fn](23, off),
{ code: 'ERR_INVALID_ARG_TYPE' });
});
[NaN, Infinity, -1, 1.01].forEach((off) => {
assert.throws(
() => buffer[fn](23, off),
{ code: 'ERR_OUT_OF_RANGE' });
});
});
}
// Test Int
{
const data = Buffer.alloc(8);
// Check byteLength.
['writeIntBE', 'writeIntLE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
assert.throws(
() => data[fn](23, 0, o),
{ code: 'ERR_INVALID_ARG_TYPE' });
});
[Infinity, -1].forEach((offset) => {
assert.throws(
() => data[fn](23, 0, offset),
{
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "byteLength" is out of range. ' +
`It must be >= 1 and <= 6. Received ${offset}`
}
);
});
[NaN, 1.01].forEach((byteLength) => {
assert.throws(
() => data[fn](42, 0, byteLength),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "byteLength" is out of range. ' +
`It must be an integer. Received ${byteLength}`
});
});
});
// Test 1 to 6 bytes.
for (let i = 1; i < 6; i++) {
['writeIntBE', 'writeIntLE'].forEach((fn) => {
const min = -(2 ** (i * 8 - 1));
const max = 2 ** (i * 8 - 1) - 1;
[min - 1, max + 1].forEach((val) => {
assert.throws(() => {
data[fn](val, 0, i);
}, {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "value" is out of range. ' +
`It must be >= ${min} and <= ${max}. Received ${val}`
});
});
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
assert.throws(
() => data[fn](min, o, i),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError [ERR_INVALID_ARG_TYPE]'
});
});
[Infinity, -1, -4294967295].forEach((offset) => {
assert.throws(
() => data[fn](min, offset, i),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be >= 0 and <= ${8 - i}. Received ${offset}`
});
});
[NaN, 1.01].forEach((offset) => {
assert.throws(
() => data[fn](max, offset, i),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be an integer. Received ${offset}`
});
});
});
}
}
+190
View File
@@ -0,0 +1,190 @@
'use strict';
require('../common');
const assert = require('assert');
/*
* We need to check the following things:
* - We are correctly resolving big endian (doesn't mean anything for 8 bit)
* - Correctly resolving little endian (doesn't mean anything for 8 bit)
* - Correctly using the offsets
* - Correctly interpreting values that are beyond the signed range as unsigned
*/
{ // OOB
const data = Buffer.alloc(8);
['UInt8', 'UInt16BE', 'UInt16LE', 'UInt32BE', 'UInt32LE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
assert.throws(
() => data[`write${fn}`](23, o),
{ code: 'ERR_INVALID_ARG_TYPE' });
});
[NaN, Infinity, -1, 1.01].forEach((o) => {
assert.throws(
() => data[`write${fn}`](23, o),
{ code: 'ERR_OUT_OF_RANGE' });
});
});
}
{ // Test 8 bit
const data = Buffer.alloc(4);
data.writeUInt8(23, 0);
data.writeUInt8(23, 1);
data.writeUInt8(23, 2);
data.writeUInt8(23, 3);
assert.ok(data.equals(new Uint8Array([23, 23, 23, 23])));
data.writeUInt8(23, 0);
data.writeUInt8(23, 1);
data.writeUInt8(23, 2);
data.writeUInt8(23, 3);
assert.ok(data.equals(new Uint8Array([23, 23, 23, 23])));
data.writeUInt8(255, 0);
assert.strictEqual(data[0], 255);
data.writeUInt8(255, 0);
assert.strictEqual(data[0], 255);
}
// Test 16 bit
{
let value = 0x2343;
const data = Buffer.alloc(4);
data.writeUInt16BE(value, 0);
assert.ok(data.equals(new Uint8Array([0x23, 0x43, 0, 0])));
data.writeUInt16BE(value, 1);
assert.ok(data.equals(new Uint8Array([0x23, 0x23, 0x43, 0])));
data.writeUInt16BE(value, 2);
assert.ok(data.equals(new Uint8Array([0x23, 0x23, 0x23, 0x43])));
data.writeUInt16LE(value, 0);
assert.ok(data.equals(new Uint8Array([0x43, 0x23, 0x23, 0x43])));
data.writeUInt16LE(value, 1);
assert.ok(data.equals(new Uint8Array([0x43, 0x43, 0x23, 0x43])));
data.writeUInt16LE(value, 2);
assert.ok(data.equals(new Uint8Array([0x43, 0x43, 0x43, 0x23])));
value = 0xff80;
data.writeUInt16LE(value, 0);
assert.ok(data.equals(new Uint8Array([0x80, 0xff, 0x43, 0x23])));
data.writeUInt16BE(value, 0);
assert.ok(data.equals(new Uint8Array([0xff, 0x80, 0x43, 0x23])));
}
// Test 32 bit
{
const data = Buffer.alloc(6);
const value = 0xe7f90a6d;
data.writeUInt32BE(value, 0);
assert.ok(data.equals(new Uint8Array([0xe7, 0xf9, 0x0a, 0x6d, 0, 0])));
data.writeUInt32BE(value, 1);
assert.ok(data.equals(new Uint8Array([0xe7, 0xe7, 0xf9, 0x0a, 0x6d, 0])));
data.writeUInt32BE(value, 2);
assert.ok(data.equals(new Uint8Array([0xe7, 0xe7, 0xe7, 0xf9, 0x0a, 0x6d])));
data.writeUInt32LE(value, 0);
assert.ok(data.equals(new Uint8Array([0x6d, 0x0a, 0xf9, 0xe7, 0x0a, 0x6d])));
data.writeUInt32LE(value, 1);
assert.ok(data.equals(new Uint8Array([0x6d, 0x6d, 0x0a, 0xf9, 0xe7, 0x6d])));
data.writeUInt32LE(value, 2);
assert.ok(data.equals(new Uint8Array([0x6d, 0x6d, 0x6d, 0x0a, 0xf9, 0xe7])));
}
// Test UInt
{
const data = Buffer.alloc(8);
let val = 0x100;
// Check byteLength.
['writeUIntBE', 'writeUIntLE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
assert.throws(
() => data[fn](23, 0, o),
{ code: 'ERR_INVALID_ARG_TYPE' });
});
[Infinity, -1].forEach((offset) => {
assert.throws(
() => data[fn](23, 0, offset),
{
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "byteLength" is out of range. ' +
`It must be >= 1 and <= 6. Received ${offset}`
}
);
});
[NaN, 1.01].forEach((byteLength) => {
assert.throws(
() => data[fn](42, 0, byteLength),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "byteLength" is out of range. ' +
`It must be an integer. Received ${byteLength}`
});
});
});
// Test 1 to 6 bytes.
for (let i = 1; i < 6; i++) {
['writeUIntBE', 'writeUIntLE'].forEach((fn) => {
assert.throws(() => {
data[fn](val, 0, i);
}, {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "value" is out of range. ' +
`It must be >= 0 and <= ${val - 1}. Received ${val}`
});
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
assert.throws(
() => data[fn](23, o, i),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError [ERR_INVALID_ARG_TYPE]'
});
});
[Infinity, -1, -4294967295].forEach((offset) => {
assert.throws(
() => data[fn](val - 1, offset, i),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be >= 0 and <= ${8 - i}. Received ${offset}`
});
});
[NaN, 1.01].forEach((offset) => {
assert.throws(
() => data[fn](val - 1, offset, i),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be an integer. Received ${offset}`
});
});
});
val *= 0x100;
}
}
-129
View File
@@ -1,129 +0,0 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
/*
* Tests to verify we're reading in doubles correctly
*/
require('../common');
const assert = require('assert');
/*
* Test (64 bit) double
*/
const buffer = Buffer.allocUnsafe(8);
buffer[0] = 0x55;
buffer[1] = 0x55;
buffer[2] = 0x55;
buffer[3] = 0x55;
buffer[4] = 0x55;
buffer[5] = 0x55;
buffer[6] = 0xd5;
buffer[7] = 0x3f;
assert.strictEqual(1.1945305291680097e+103, buffer.readDoubleBE(0));
assert.strictEqual(0.3333333333333333, buffer.readDoubleLE(0));
buffer[0] = 1;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0;
buffer[4] = 0;
buffer[5] = 0;
buffer[6] = 0xf0;
buffer[7] = 0x3f;
assert.strictEqual(7.291122019655968e-304, buffer.readDoubleBE(0));
assert.strictEqual(1.0000000000000002, buffer.readDoubleLE(0));
buffer[0] = 2;
assert.strictEqual(4.778309726801735e-299, buffer.readDoubleBE(0));
assert.strictEqual(1.0000000000000004, buffer.readDoubleLE(0));
buffer[0] = 1;
buffer[6] = 0;
buffer[7] = 0;
assert.strictEqual(7.291122019556398e-304, buffer.readDoubleBE(0));
assert.strictEqual(5e-324, buffer.readDoubleLE(0));
buffer[0] = 0xff;
buffer[1] = 0xff;
buffer[2] = 0xff;
buffer[3] = 0xff;
buffer[4] = 0xff;
buffer[5] = 0xff;
buffer[6] = 0x0f;
buffer[7] = 0x00;
assert.ok(Number.isNaN(buffer.readDoubleBE(0)));
assert.strictEqual(2.225073858507201e-308, buffer.readDoubleLE(0));
buffer[6] = 0xef;
buffer[7] = 0x7f;
assert.ok(Number.isNaN(buffer.readDoubleBE(0)));
assert.strictEqual(1.7976931348623157e+308, buffer.readDoubleLE(0));
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0;
buffer[4] = 0;
buffer[5] = 0;
buffer[6] = 0xf0;
buffer[7] = 0x3f;
assert.strictEqual(3.03865e-319, buffer.readDoubleBE(0));
assert.strictEqual(1, buffer.readDoubleLE(0));
buffer[6] = 0;
buffer[7] = 0x40;
assert.strictEqual(3.16e-322, buffer.readDoubleBE(0));
assert.strictEqual(2, buffer.readDoubleLE(0));
buffer[7] = 0xc0;
assert.strictEqual(9.5e-322, buffer.readDoubleBE(0));
assert.strictEqual(-2, buffer.readDoubleLE(0));
buffer[6] = 0x10;
buffer[7] = 0;
assert.strictEqual(2.0237e-320, buffer.readDoubleBE(0));
assert.strictEqual(2.2250738585072014e-308, buffer.readDoubleLE(0));
buffer[6] = 0;
assert.strictEqual(0, buffer.readDoubleBE(0));
assert.strictEqual(0, buffer.readDoubleLE(0));
assert.strictEqual(false, 1 / buffer.readDoubleLE(0) < 0);
buffer[7] = 0x80;
assert.strictEqual(6.3e-322, buffer.readDoubleBE(0));
assert.strictEqual(-0, buffer.readDoubleLE(0));
assert.strictEqual(true, 1 / buffer.readDoubleLE(0) < 0);
buffer[6] = 0xf0;
buffer[7] = 0x7f;
assert.strictEqual(3.0418e-319, buffer.readDoubleBE(0));
assert.strictEqual(Infinity, buffer.readDoubleLE(0));
buffer[7] = 0xff;
assert.strictEqual(3.04814e-319, buffer.readDoubleBE(0));
assert.strictEqual(-Infinity, buffer.readDoubleLE(0));
buffer.writeDoubleBE(246800);
assert.strictEqual(buffer.readDoubleBE(), 246800);
assert.strictEqual(buffer.readDoubleBE(0.7), 246800);
assert.strictEqual(buffer.readDoubleBE(NaN), 246800);
-92
View File
@@ -1,92 +0,0 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
/*
* Tests to verify we're reading in floats correctly
*/
require('../common');
const assert = require('assert');
/*
* Test (32 bit) float
*/
function test(clazz) {
const buffer = new clazz(4);
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0x80;
buffer[3] = 0x3f;
assert.strictEqual(4.600602988224807e-41, buffer.readFloatBE(0));
assert.strictEqual(1, buffer.readFloatLE(0));
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0xc0;
assert.strictEqual(2.6904930515036488e-43, buffer.readFloatBE(0));
assert.strictEqual(-2, buffer.readFloatLE(0));
buffer[0] = 0xff;
buffer[1] = 0xff;
buffer[2] = 0x7f;
buffer[3] = 0x7f;
assert.ok(Number.isNaN(buffer.readFloatBE(0)));
assert.strictEqual(3.4028234663852886e+38, buffer.readFloatLE(0));
buffer[0] = 0xab;
buffer[1] = 0xaa;
buffer[2] = 0xaa;
buffer[3] = 0x3e;
assert.strictEqual(-1.2126478207002966e-12, buffer.readFloatBE(0));
assert.strictEqual(0.3333333432674408, buffer.readFloatLE(0));
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0;
assert.strictEqual(0, buffer.readFloatBE(0));
assert.strictEqual(0, buffer.readFloatLE(0));
assert.strictEqual(false, 1 / buffer.readFloatLE(0) < 0);
buffer[3] = 0x80;
assert.strictEqual(1.793662034335766e-43, buffer.readFloatBE(0));
assert.strictEqual(-0, buffer.readFloatLE(0));
assert.strictEqual(true, 1 / buffer.readFloatLE(0) < 0);
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0x80;
buffer[3] = 0x7f;
assert.strictEqual(4.609571298396486e-41, buffer.readFloatBE(0));
assert.strictEqual(Infinity, buffer.readFloatLE(0));
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0x80;
buffer[3] = 0xff;
assert.strictEqual(4.627507918739843e-41, buffer.readFloatBE(0));
assert.strictEqual(-Infinity, buffer.readFloatLE(0));
}
test(Buffer);
-119
View File
@@ -1,119 +0,0 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
/*
* Tests to verify we're reading in signed integers correctly
*/
require('../common');
const assert = require('assert');
/*
* Test 8 bit signed integers
*/
function test8(clazz) {
const data = new clazz(4);
data[0] = 0x23;
assert.strictEqual(0x23, data.readInt8(0));
data[0] = 0xff;
assert.strictEqual(-1, data.readInt8(0));
data[0] = 0x87;
data[1] = 0xab;
data[2] = 0x7c;
data[3] = 0xef;
assert.strictEqual(-121, data.readInt8(0));
assert.strictEqual(-85, data.readInt8(1));
assert.strictEqual(124, data.readInt8(2));
assert.strictEqual(-17, data.readInt8(3));
}
function test16(clazz) {
const buffer = new clazz(6);
buffer[0] = 0x16;
buffer[1] = 0x79;
assert.strictEqual(0x1679, buffer.readInt16BE(0));
assert.strictEqual(0x7916, buffer.readInt16LE(0));
buffer[0] = 0xff;
buffer[1] = 0x80;
assert.strictEqual(-128, buffer.readInt16BE(0));
assert.strictEqual(-32513, buffer.readInt16LE(0));
/* test offset with weenix */
buffer[0] = 0x77;
buffer[1] = 0x65;
buffer[2] = 0x65;
buffer[3] = 0x6e;
buffer[4] = 0x69;
buffer[5] = 0x78;
assert.strictEqual(0x7765, buffer.readInt16BE(0));
assert.strictEqual(0x6565, buffer.readInt16BE(1));
assert.strictEqual(0x656e, buffer.readInt16BE(2));
assert.strictEqual(0x6e69, buffer.readInt16BE(3));
assert.strictEqual(0x6978, buffer.readInt16BE(4));
assert.strictEqual(0x6577, buffer.readInt16LE(0));
assert.strictEqual(0x6565, buffer.readInt16LE(1));
assert.strictEqual(0x6e65, buffer.readInt16LE(2));
assert.strictEqual(0x696e, buffer.readInt16LE(3));
assert.strictEqual(0x7869, buffer.readInt16LE(4));
}
function test32(clazz) {
const buffer = new clazz(6);
buffer[0] = 0x43;
buffer[1] = 0x53;
buffer[2] = 0x16;
buffer[3] = 0x79;
assert.strictEqual(0x43531679, buffer.readInt32BE(0));
assert.strictEqual(0x79165343, buffer.readInt32LE(0));
buffer[0] = 0xff;
buffer[1] = 0xfe;
buffer[2] = 0xef;
buffer[3] = 0xfa;
assert.strictEqual(-69638, buffer.readInt32BE(0));
assert.strictEqual(-84934913, buffer.readInt32LE(0));
buffer[0] = 0x42;
buffer[1] = 0xc3;
buffer[2] = 0x95;
buffer[3] = 0xa9;
buffer[4] = 0x36;
buffer[5] = 0x17;
assert.strictEqual(0x42c395a9, buffer.readInt32BE(0));
assert.strictEqual(-1013601994, buffer.readInt32BE(1));
assert.strictEqual(-1784072681, buffer.readInt32BE(2));
assert.strictEqual(-1449802942, buffer.readInt32LE(0));
assert.strictEqual(917083587, buffer.readInt32LE(1));
assert.strictEqual(389458325, buffer.readInt32LE(2));
}
test8(Buffer);
test16(Buffer);
test32(Buffer);
-197
View File
@@ -1,197 +0,0 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
/*
* Tests to verify we're writing doubles correctly
*/
require('../common');
const assert = require('assert');
function test(clazz) {
const buffer = new clazz(16);
buffer.writeDoubleBE(2.225073858507201e-308, 0);
buffer.writeDoubleLE(2.225073858507201e-308, 8);
assert.strictEqual(0x00, buffer[0]);
assert.strictEqual(0x0f, buffer[1]);
assert.strictEqual(0xff, buffer[2]);
assert.strictEqual(0xff, buffer[3]);
assert.strictEqual(0xff, buffer[4]);
assert.strictEqual(0xff, buffer[5]);
assert.strictEqual(0xff, buffer[6]);
assert.strictEqual(0xff, buffer[7]);
assert.strictEqual(0xff, buffer[8]);
assert.strictEqual(0xff, buffer[9]);
assert.strictEqual(0xff, buffer[10]);
assert.strictEqual(0xff, buffer[11]);
assert.strictEqual(0xff, buffer[12]);
assert.strictEqual(0xff, buffer[13]);
assert.strictEqual(0x0f, buffer[14]);
assert.strictEqual(0x00, buffer[15]);
buffer.writeDoubleBE(1.0000000000000004, 0);
buffer.writeDoubleLE(1.0000000000000004, 8);
assert.strictEqual(0x3f, buffer[0]);
assert.strictEqual(0xf0, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x02, buffer[7]);
assert.strictEqual(0x02, buffer[8]);
assert.strictEqual(0x00, buffer[9]);
assert.strictEqual(0x00, buffer[10]);
assert.strictEqual(0x00, buffer[11]);
assert.strictEqual(0x00, buffer[12]);
assert.strictEqual(0x00, buffer[13]);
assert.strictEqual(0xf0, buffer[14]);
assert.strictEqual(0x3f, buffer[15]);
buffer.writeDoubleBE(-2, 0);
buffer.writeDoubleLE(-2, 8);
assert.strictEqual(0xc0, buffer[0]);
assert.strictEqual(0x00, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x00, buffer[7]);
assert.strictEqual(0x00, buffer[8]);
assert.strictEqual(0x00, buffer[9]);
assert.strictEqual(0x00, buffer[10]);
assert.strictEqual(0x00, buffer[11]);
assert.strictEqual(0x00, buffer[12]);
assert.strictEqual(0x00, buffer[13]);
assert.strictEqual(0x00, buffer[14]);
assert.strictEqual(0xc0, buffer[15]);
buffer.writeDoubleBE(1.7976931348623157e+308, 0);
buffer.writeDoubleLE(1.7976931348623157e+308, 8);
assert.strictEqual(0x7f, buffer[0]);
assert.strictEqual(0xef, buffer[1]);
assert.strictEqual(0xff, buffer[2]);
assert.strictEqual(0xff, buffer[3]);
assert.strictEqual(0xff, buffer[4]);
assert.strictEqual(0xff, buffer[5]);
assert.strictEqual(0xff, buffer[6]);
assert.strictEqual(0xff, buffer[7]);
assert.strictEqual(0xff, buffer[8]);
assert.strictEqual(0xff, buffer[9]);
assert.strictEqual(0xff, buffer[10]);
assert.strictEqual(0xff, buffer[11]);
assert.strictEqual(0xff, buffer[12]);
assert.strictEqual(0xff, buffer[13]);
assert.strictEqual(0xef, buffer[14]);
assert.strictEqual(0x7f, buffer[15]);
buffer.writeDoubleBE(0 * -1, 0);
buffer.writeDoubleLE(0 * -1, 8);
assert.strictEqual(0x80, buffer[0]);
assert.strictEqual(0x00, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x00, buffer[7]);
assert.strictEqual(0x00, buffer[8]);
assert.strictEqual(0x00, buffer[9]);
assert.strictEqual(0x00, buffer[10]);
assert.strictEqual(0x00, buffer[11]);
assert.strictEqual(0x00, buffer[12]);
assert.strictEqual(0x00, buffer[13]);
assert.strictEqual(0x00, buffer[14]);
assert.strictEqual(0x80, buffer[15]);
buffer.writeDoubleBE(Infinity, 0);
buffer.writeDoubleLE(Infinity, 8);
assert.strictEqual(0x7F, buffer[0]);
assert.strictEqual(0xF0, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x00, buffer[7]);
assert.strictEqual(0x00, buffer[8]);
assert.strictEqual(0x00, buffer[9]);
assert.strictEqual(0x00, buffer[10]);
assert.strictEqual(0x00, buffer[11]);
assert.strictEqual(0x00, buffer[12]);
assert.strictEqual(0x00, buffer[13]);
assert.strictEqual(0xF0, buffer[14]);
assert.strictEqual(0x7F, buffer[15]);
assert.strictEqual(Infinity, buffer.readDoubleBE(0));
assert.strictEqual(Infinity, buffer.readDoubleLE(8));
buffer.writeDoubleBE(-Infinity, 0);
buffer.writeDoubleLE(-Infinity, 8);
assert.strictEqual(0xFF, buffer[0]);
assert.strictEqual(0xF0, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x00, buffer[7]);
assert.strictEqual(0x00, buffer[8]);
assert.strictEqual(0x00, buffer[9]);
assert.strictEqual(0x00, buffer[10]);
assert.strictEqual(0x00, buffer[11]);
assert.strictEqual(0x00, buffer[12]);
assert.strictEqual(0x00, buffer[13]);
assert.strictEqual(0xF0, buffer[14]);
assert.strictEqual(0xFF, buffer[15]);
assert.strictEqual(-Infinity, buffer.readDoubleBE(0));
assert.strictEqual(-Infinity, buffer.readDoubleLE(8));
buffer.writeDoubleBE(NaN, 0);
buffer.writeDoubleLE(NaN, 8);
// Darwin ia32 does the other kind of NaN.
// Compiler bug. No one really cares.
assert(0x7F === buffer[0] || 0xFF === buffer[0]);
// mips processors use a slightly different NaN
assert(0xF8 === buffer[1] || 0xF7 === buffer[1]);
assert(0x00 === buffer[2] || 0xFF === buffer[2]);
assert(0x00 === buffer[3] || 0xFF === buffer[3]);
assert(0x00 === buffer[4] || 0xFF === buffer[4]);
assert(0x00 === buffer[5] || 0xFF === buffer[5]);
assert(0x00 === buffer[6] || 0xFF === buffer[6]);
assert(0x00 === buffer[7] || 0xFF === buffer[7]);
assert(0x00 === buffer[8] || 0xFF === buffer[8]);
assert(0x00 === buffer[9] || 0xFF === buffer[9]);
assert(0x00 === buffer[10] || 0xFF === buffer[10]);
assert(0x00 === buffer[11] || 0xFF === buffer[11]);
assert(0x00 === buffer[12] || 0xFF === buffer[12]);
assert(0x00 === buffer[13] || 0xFF === buffer[13]);
assert(0xF8 === buffer[14] || 0xF7 === buffer[14]);
// Darwin ia32 does the other kind of NaN.
// Compiler bug. No one really cares.
assert(0x7F === buffer[15] || 0xFF === buffer[15]);
assert.ok(Number.isNaN(buffer.readDoubleBE(0)));
assert.ok(Number.isNaN(buffer.readDoubleLE(8)));
}
test(Buffer);
-135
View File
@@ -1,135 +0,0 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
/*
* Tests to verify we're writing floats correctly
*/
require('../common');
const assert = require('assert');
function test(clazz) {
const buffer = new clazz(8);
buffer.writeFloatBE(1, 0);
buffer.writeFloatLE(1, 4);
assert.strictEqual(0x3f, buffer[0]);
assert.strictEqual(0x80, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x80, buffer[6]);
assert.strictEqual(0x3f, buffer[7]);
buffer.writeFloatBE(1 / 3, 0);
buffer.writeFloatLE(1 / 3, 4);
assert.strictEqual(0x3e, buffer[0]);
assert.strictEqual(0xaa, buffer[1]);
assert.strictEqual(0xaa, buffer[2]);
assert.strictEqual(0xab, buffer[3]);
assert.strictEqual(0xab, buffer[4]);
assert.strictEqual(0xaa, buffer[5]);
assert.strictEqual(0xaa, buffer[6]);
assert.strictEqual(0x3e, buffer[7]);
buffer.writeFloatBE(3.4028234663852886e+38, 0);
buffer.writeFloatLE(3.4028234663852886e+38, 4);
assert.strictEqual(0x7f, buffer[0]);
assert.strictEqual(0x7f, buffer[1]);
assert.strictEqual(0xff, buffer[2]);
assert.strictEqual(0xff, buffer[3]);
assert.strictEqual(0xff, buffer[4]);
assert.strictEqual(0xff, buffer[5]);
assert.strictEqual(0x7f, buffer[6]);
assert.strictEqual(0x7f, buffer[7]);
buffer.writeFloatLE(1.1754943508222875e-38, 0);
buffer.writeFloatBE(1.1754943508222875e-38, 4);
assert.strictEqual(0x00, buffer[0]);
assert.strictEqual(0x00, buffer[1]);
assert.strictEqual(0x80, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x80, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x00, buffer[7]);
buffer.writeFloatBE(0 * -1, 0);
buffer.writeFloatLE(0 * -1, 4);
assert.strictEqual(0x80, buffer[0]);
assert.strictEqual(0x00, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x80, buffer[7]);
buffer.writeFloatBE(Infinity, 0);
buffer.writeFloatLE(Infinity, 4);
assert.strictEqual(0x7F, buffer[0]);
assert.strictEqual(0x80, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x80, buffer[6]);
assert.strictEqual(0x7F, buffer[7]);
assert.strictEqual(Infinity, buffer.readFloatBE(0));
assert.strictEqual(Infinity, buffer.readFloatLE(4));
buffer.writeFloatBE(-Infinity, 0);
buffer.writeFloatLE(-Infinity, 4);
// Darwin ia32 does the other kind of NaN.
// Compiler bug. No one really cares.
assert(0xFF === buffer[0] || 0x7F === buffer[0]);
assert.strictEqual(0x80, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x80, buffer[6]);
assert.strictEqual(0xFF, buffer[7]);
assert.strictEqual(-Infinity, buffer.readFloatBE(0));
assert.strictEqual(-Infinity, buffer.readFloatLE(4));
buffer.writeFloatBE(NaN, 0);
buffer.writeFloatLE(NaN, 4);
// Darwin ia32 does the other kind of NaN.
// Compiler bug. No one really cares.
assert(0x7F === buffer[0] || 0xFF === buffer[0]);
// mips processors use a slightly different NaN
assert(0xC0 === buffer[1] || 0xBF === buffer[1]);
assert(0x00 === buffer[2] || 0xFF === buffer[2]);
assert(0x00 === buffer[3] || 0xFF === buffer[3]);
assert(0x00 === buffer[4] || 0xFF === buffer[4]);
assert(0x00 === buffer[5] || 0xFF === buffer[5]);
assert(0xC0 === buffer[6] || 0xBF === buffer[6]);
// Darwin ia32 does the other kind of NaN.
// Compiler bug. No one really cares.
assert(0x7F === buffer[7] || 0xFF === buffer[7]);
assert.ok(Number.isNaN(buffer.readFloatBE(0)));
assert.ok(Number.isNaN(buffer.readFloatLE(4)));
}
test(Buffer);
-194
View File
@@ -1,194 +0,0 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
/*
* Tests to verify we're writing signed integers correctly
*/
const common = require('../common');
const assert = require('assert');
const errorOutOfBounds = common.expectsError({
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError,
message: /^The value "[^"]*" is invalid for option "value"$/
}, 12);
function test8(clazz) {
const buffer = new clazz(2);
buffer.writeInt8(0x23, 0);
buffer.writeInt8(-5, 1);
assert.strictEqual(0x23, buffer[0]);
assert.strictEqual(0xfb, buffer[1]);
/* Make sure we handle truncation correctly */
assert.throws(() => {
buffer.writeInt8(0xabc, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer.writeInt8(0xabc, 0);
}, errorOutOfBounds);
/* Make sure we handle min/max correctly */
buffer.writeInt8(0x7f, 0);
buffer.writeInt8(-0x80, 1);
assert.strictEqual(0x7f, buffer[0]);
assert.strictEqual(0x80, buffer[1]);
assert.throws(() => {
buffer.writeInt8(0x7f + 1, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer.writeInt8(-0x80 - 1, 0);
}, errorOutOfBounds);
}
function test16(clazz) {
const buffer = new clazz(6);
buffer.writeInt16BE(0x0023, 0);
buffer.writeInt16LE(0x0023, 2);
assert.strictEqual(0x00, buffer[0]);
assert.strictEqual(0x23, buffer[1]);
assert.strictEqual(0x23, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
buffer.writeInt16BE(-5, 0);
buffer.writeInt16LE(-5, 2);
assert.strictEqual(0xff, buffer[0]);
assert.strictEqual(0xfb, buffer[1]);
assert.strictEqual(0xfb, buffer[2]);
assert.strictEqual(0xff, buffer[3]);
buffer.writeInt16BE(-1679, 1);
buffer.writeInt16LE(-1679, 3);
assert.strictEqual(0xf9, buffer[1]);
assert.strictEqual(0x71, buffer[2]);
assert.strictEqual(0x71, buffer[3]);
assert.strictEqual(0xf9, buffer[4]);
/* Make sure we handle min/max correctly */
buffer.writeInt16BE(0x7fff, 0);
buffer.writeInt16BE(-0x8000, 2);
assert.strictEqual(0x7f, buffer[0]);
assert.strictEqual(0xff, buffer[1]);
assert.strictEqual(0x80, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.throws(() => {
buffer.writeInt16BE(0x7fff + 1, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer.writeInt16BE(-0x8000 - 1, 0);
}, errorOutOfBounds);
buffer.writeInt16LE(0x7fff, 0);
buffer.writeInt16LE(-0x8000, 2);
assert.strictEqual(0xff, buffer[0]);
assert.strictEqual(0x7f, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x80, buffer[3]);
assert.throws(() => {
buffer.writeInt16LE(0x7fff + 1, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer.writeInt16LE(-0x8000 - 1, 0);
}, errorOutOfBounds);
}
function test32(clazz) {
const buffer = new clazz(8);
buffer.writeInt32BE(0x23, 0);
buffer.writeInt32LE(0x23, 4);
assert.strictEqual(0x00, buffer[0]);
assert.strictEqual(0x00, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x23, buffer[3]);
assert.strictEqual(0x23, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x00, buffer[7]);
buffer.writeInt32BE(-5, 0);
buffer.writeInt32LE(-5, 4);
assert.strictEqual(0xff, buffer[0]);
assert.strictEqual(0xff, buffer[1]);
assert.strictEqual(0xff, buffer[2]);
assert.strictEqual(0xfb, buffer[3]);
assert.strictEqual(0xfb, buffer[4]);
assert.strictEqual(0xff, buffer[5]);
assert.strictEqual(0xff, buffer[6]);
assert.strictEqual(0xff, buffer[7]);
buffer.writeInt32BE(-805306713, 0);
buffer.writeInt32LE(-805306713, 4);
assert.strictEqual(0xcf, buffer[0]);
assert.strictEqual(0xff, buffer[1]);
assert.strictEqual(0xfe, buffer[2]);
assert.strictEqual(0xa7, buffer[3]);
assert.strictEqual(0xa7, buffer[4]);
assert.strictEqual(0xfe, buffer[5]);
assert.strictEqual(0xff, buffer[6]);
assert.strictEqual(0xcf, buffer[7]);
/* Make sure we handle min/max correctly */
buffer.writeInt32BE(0x7fffffff, 0);
buffer.writeInt32BE(-0x80000000, 4);
assert.strictEqual(0x7f, buffer[0]);
assert.strictEqual(0xff, buffer[1]);
assert.strictEqual(0xff, buffer[2]);
assert.strictEqual(0xff, buffer[3]);
assert.strictEqual(0x80, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x00, buffer[7]);
assert.throws(() => {
buffer.writeInt32BE(0x7fffffff + 1, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer.writeInt32BE(-0x80000000 - 1, 0);
}, errorOutOfBounds);
buffer.writeInt32LE(0x7fffffff, 0);
buffer.writeInt32LE(-0x80000000, 4);
assert.strictEqual(0xff, buffer[0]);
assert.strictEqual(0xff, buffer[1]);
assert.strictEqual(0xff, buffer[2]);
assert.strictEqual(0x7f, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x80, buffer[7]);
assert.throws(() => {
buffer.writeInt32LE(0x7fffffff + 1, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer.writeInt32LE(-0x80000000 - 1, 0);
}, errorOutOfBounds);
}
test8(Buffer);
test16(Buffer);
test32(Buffer);
-171
View File
@@ -1,171 +0,0 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
/*
* A battery of tests to help us read a series of uints
*/
const common = require('../common');
const assert = require('assert');
/*
* We need to check the following things:
* - We are correctly resolving big endian (doesn't mean anything for 8 bit)
* - Correctly resolving little endian (doesn't mean anything for 8 bit)
* - Correctly using the offsets
* - Correctly interpreting values that are beyond the signed range as unsigned
*/
function test8(clazz) {
const data = new clazz(4);
data.writeUInt8(23, 0);
data.writeUInt8(23, 1);
data.writeUInt8(23, 2);
data.writeUInt8(23, 3);
assert.strictEqual(23, data[0]);
assert.strictEqual(23, data[1]);
assert.strictEqual(23, data[2]);
assert.strictEqual(23, data[3]);
data.writeUInt8(23, 0);
data.writeUInt8(23, 1);
data.writeUInt8(23, 2);
data.writeUInt8(23, 3);
assert.strictEqual(23, data[0]);
assert.strictEqual(23, data[1]);
assert.strictEqual(23, data[2]);
assert.strictEqual(23, data[3]);
data.writeUInt8(255, 0);
assert.strictEqual(255, data[0]);
data.writeUInt8(255, 0);
assert.strictEqual(255, data[0]);
}
function test16(clazz) {
let value = 0x2343;
const data = new clazz(4);
data.writeUInt16BE(value, 0);
assert.strictEqual(0x23, data[0]);
assert.strictEqual(0x43, data[1]);
data.writeUInt16BE(value, 1);
assert.strictEqual(0x23, data[1]);
assert.strictEqual(0x43, data[2]);
data.writeUInt16BE(value, 2);
assert.strictEqual(0x23, data[2]);
assert.strictEqual(0x43, data[3]);
data.writeUInt16LE(value, 0);
assert.strictEqual(0x23, data[1]);
assert.strictEqual(0x43, data[0]);
data.writeUInt16LE(value, 1);
assert.strictEqual(0x23, data[2]);
assert.strictEqual(0x43, data[1]);
data.writeUInt16LE(value, 2);
assert.strictEqual(0x23, data[3]);
assert.strictEqual(0x43, data[2]);
value = 0xff80;
data.writeUInt16LE(value, 0);
assert.strictEqual(0xff, data[1]);
assert.strictEqual(0x80, data[0]);
data.writeUInt16BE(value, 0);
assert.strictEqual(0xff, data[0]);
assert.strictEqual(0x80, data[1]);
}
function test32(clazz) {
const data = new clazz(6);
const value = 0xe7f90a6d;
data.writeUInt32BE(value, 0);
assert.strictEqual(0xe7, data[0]);
assert.strictEqual(0xf9, data[1]);
assert.strictEqual(0x0a, data[2]);
assert.strictEqual(0x6d, data[3]);
data.writeUInt32BE(value, 1);
assert.strictEqual(0xe7, data[1]);
assert.strictEqual(0xf9, data[2]);
assert.strictEqual(0x0a, data[3]);
assert.strictEqual(0x6d, data[4]);
data.writeUInt32BE(value, 2);
assert.strictEqual(0xe7, data[2]);
assert.strictEqual(0xf9, data[3]);
assert.strictEqual(0x0a, data[4]);
assert.strictEqual(0x6d, data[5]);
data.writeUInt32LE(value, 0);
assert.strictEqual(0xe7, data[3]);
assert.strictEqual(0xf9, data[2]);
assert.strictEqual(0x0a, data[1]);
assert.strictEqual(0x6d, data[0]);
data.writeUInt32LE(value, 1);
assert.strictEqual(0xe7, data[4]);
assert.strictEqual(0xf9, data[3]);
assert.strictEqual(0x0a, data[2]);
assert.strictEqual(0x6d, data[1]);
data.writeUInt32LE(value, 2);
assert.strictEqual(0xe7, data[5]);
assert.strictEqual(0xf9, data[4]);
assert.strictEqual(0x0a, data[3]);
assert.strictEqual(0x6d, data[2]);
}
function testUint(clazz) {
const data = new clazz(8);
let val = 1;
// Test 0 to 5 bytes.
for (let i = 0; i <= 5; i++) {
const errMsg = common.expectsError({
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError,
message: /^The value "[^"]*" is invalid for option "value"$/
}, 2);
assert.throws(() => {
data.writeUIntBE(val, 0, i);
}, errMsg);
assert.throws(() => {
data.writeUIntLE(val, 0, i);
}, errMsg);
val *= 0x100;
}
}
test8(Buffer);
test16(Buffer);
test32(Buffer);
testUint(Buffer);