summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/buffers
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /addon-sdk/source/test/buffers
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'addon-sdk/source/test/buffers')
-rw-r--r--addon-sdk/source/test/buffers/test-read-types.js368
-rw-r--r--addon-sdk/source/test/buffers/test-write-types.js602
2 files changed, 970 insertions, 0 deletions
diff --git a/addon-sdk/source/test/buffers/test-read-types.js b/addon-sdk/source/test/buffers/test-read-types.js
new file mode 100644
index 000000000..35aca16ad
--- /dev/null
+++ b/addon-sdk/source/test/buffers/test-read-types.js
@@ -0,0 +1,368 @@
+// 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.
+
+const { Buffer } = require('sdk/io/buffer');
+
+exports.testReadDouble = helper('readDoubleLE/readDoubleBE', function (assert) {
+ var buffer = new Buffer(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.equal(1.1945305291680097e+103, buffer.readDoubleBE(0));
+ assert.equal(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.equal(7.291122019655968e-304, buffer.readDoubleBE(0));
+ assert.equal(1.0000000000000002, buffer.readDoubleLE(0));
+
+ buffer[0] = 2;
+ assert.equal(4.778309726801735e-299, buffer.readDoubleBE(0));
+ assert.equal(1.0000000000000004, buffer.readDoubleLE(0));
+
+ buffer[0] = 1;
+ buffer[6] = 0;
+ buffer[7] = 0;
+ assert.equal(7.291122019556398e-304, buffer.readDoubleBE(0));
+ assert.equal(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(isNaN(buffer.readDoubleBE(0)));
+ assert.equal(2.225073858507201e-308, buffer.readDoubleLE(0));
+
+ buffer[6] = 0xef;
+ buffer[7] = 0x7f;
+ assert.ok(isNaN(buffer.readDoubleBE(0)));
+ assert.equal(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.equal(3.03865e-319, buffer.readDoubleBE(0));
+ assert.equal(1, buffer.readDoubleLE(0));
+
+ buffer[6] = 0;
+ buffer[7] = 0x40;
+ assert.equal(3.16e-322, buffer.readDoubleBE(0));
+ assert.equal(2, buffer.readDoubleLE(0));
+
+ buffer[7] = 0xc0;
+ assert.equal(9.5e-322, buffer.readDoubleBE(0));
+ assert.equal(-2, buffer.readDoubleLE(0));
+
+ buffer[6] = 0x10;
+ buffer[7] = 0;
+ assert.equal(2.0237e-320, buffer.readDoubleBE(0));
+ assert.equal(2.2250738585072014e-308, buffer.readDoubleLE(0));
+
+ buffer[6] = 0;
+ assert.equal(0, buffer.readDoubleBE(0));
+ assert.equal(0, buffer.readDoubleLE(0));
+ assert.equal(false, 1 / buffer.readDoubleLE(0) < 0);
+
+ buffer[7] = 0x80;
+ assert.equal(6.3e-322, buffer.readDoubleBE(0));
+ assert.equal(0, buffer.readDoubleLE(0));
+ assert.equal(true, 1 / buffer.readDoubleLE(0) < 0);
+
+ buffer[6] = 0xf0;
+ buffer[7] = 0x7f;
+ assert.equal(3.0418e-319, buffer.readDoubleBE(0));
+ assert.equal(Infinity, buffer.readDoubleLE(0));
+
+ buffer[6] = 0xf0;
+ buffer[7] = 0xff;
+ assert.equal(3.04814e-319, buffer.readDoubleBE(0));
+ assert.equal(-Infinity, buffer.readDoubleLE(0));
+});
+
+
+exports.testReadFloat = helper('readFloatLE/readFloatBE', function (assert) {
+ var buffer = new Buffer(4);
+
+ buffer[0] = 0;
+ buffer[1] = 0;
+ buffer[2] = 0x80;
+ buffer[3] = 0x3f;
+ assert.equal(4.600602988224807e-41, buffer.readFloatBE(0));
+ assert.equal(1, buffer.readFloatLE(0));
+
+ buffer[0] = 0;
+ buffer[1] = 0;
+ buffer[2] = 0;
+ buffer[3] = 0xc0;
+ assert.equal(2.6904930515036488e-43, buffer.readFloatBE(0));
+ assert.equal(-2, buffer.readFloatLE(0));
+
+ buffer[0] = 0xff;
+ buffer[1] = 0xff;
+ buffer[2] = 0x7f;
+ buffer[3] = 0x7f;
+ assert.ok(isNaN(buffer.readFloatBE(0)));
+ assert.equal(3.4028234663852886e+38, buffer.readFloatLE(0));
+
+ buffer[0] = 0xab;
+ buffer[1] = 0xaa;
+ buffer[2] = 0xaa;
+ buffer[3] = 0x3e;
+ assert.equal(-1.2126478207002966e-12, buffer.readFloatBE(0));
+ assert.equal(0.3333333432674408, buffer.readFloatLE(0));
+
+ buffer[0] = 0;
+ buffer[1] = 0;
+ buffer[2] = 0;
+ buffer[3] = 0;
+ assert.equal(0, buffer.readFloatBE(0));
+ assert.equal(0, buffer.readFloatLE(0));
+ assert.equal(false, 1 / buffer.readFloatLE(0) < 0);
+
+ buffer[3] = 0x80;
+ assert.equal(1.793662034335766e-43, buffer.readFloatBE(0));
+ assert.equal(0, buffer.readFloatLE(0));
+ assert.equal(true, 1 / buffer.readFloatLE(0) < 0);
+
+ buffer[0] = 0;
+ buffer[1] = 0;
+ buffer[2] = 0x80;
+ buffer[3] = 0x7f;
+ assert.equal(4.609571298396486e-41, buffer.readFloatBE(0));
+ assert.equal(Infinity, buffer.readFloatLE(0));
+
+ buffer[0] = 0;
+ buffer[1] = 0;
+ buffer[2] = 0x80;
+ buffer[3] = 0xff;
+ assert.equal(4.627507918739843e-41, buffer.readFloatBE(0));
+ assert.equal(-Infinity, buffer.readFloatLE(0));
+});
+
+
+exports.testReadInt8 = helper('readInt8', function (assert) {
+ var data = new Buffer(4);
+
+ data[0] = 0x23;
+ assert.equal(0x23, data.readInt8(0));
+
+ data[0] = 0xff;
+ assert.equal(-1, data.readInt8(0));
+
+ data[0] = 0x87;
+ data[1] = 0xab;
+ data[2] = 0x7c;
+ data[3] = 0xef;
+ assert.equal(-121, data.readInt8(0));
+ assert.equal(-85, data.readInt8(1));
+ assert.equal(124, data.readInt8(2));
+ assert.equal(-17, data.readInt8(3));
+});
+
+
+exports.testReadInt16 = helper('readInt16BE/readInt16LE', function (assert) {
+ var buffer = new Buffer(6);
+
+ buffer[0] = 0x16;
+ buffer[1] = 0x79;
+ assert.equal(0x1679, buffer.readInt16BE(0));
+ assert.equal(0x7916, buffer.readInt16LE(0));
+
+ buffer[0] = 0xff;
+ buffer[1] = 0x80;
+ assert.equal(-128, buffer.readInt16BE(0));
+ assert.equal(-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.equal(0x7765, buffer.readInt16BE(0));
+ assert.equal(0x6565, buffer.readInt16BE(1));
+ assert.equal(0x656e, buffer.readInt16BE(2));
+ assert.equal(0x6e69, buffer.readInt16BE(3));
+ assert.equal(0x6978, buffer.readInt16BE(4));
+ assert.equal(0x6577, buffer.readInt16LE(0));
+ assert.equal(0x6565, buffer.readInt16LE(1));
+ assert.equal(0x6e65, buffer.readInt16LE(2));
+ assert.equal(0x696e, buffer.readInt16LE(3));
+ assert.equal(0x7869, buffer.readInt16LE(4));
+});
+
+
+exports.testReadInt32 = helper('readInt32BE/readInt32LE', function (assert) {
+ var buffer = new Buffer(6);
+
+ buffer[0] = 0x43;
+ buffer[1] = 0x53;
+ buffer[2] = 0x16;
+ buffer[3] = 0x79;
+ assert.equal(0x43531679, buffer.readInt32BE(0));
+ assert.equal(0x79165343, buffer.readInt32LE(0));
+
+ buffer[0] = 0xff;
+ buffer[1] = 0xfe;
+ buffer[2] = 0xef;
+ buffer[3] = 0xfa;
+ assert.equal(-69638, buffer.readInt32BE(0));
+ assert.equal(-84934913, buffer.readInt32LE(0));
+
+ buffer[0] = 0x42;
+ buffer[1] = 0xc3;
+ buffer[2] = 0x95;
+ buffer[3] = 0xa9;
+ buffer[4] = 0x36;
+ buffer[5] = 0x17;
+ assert.equal(0x42c395a9, buffer.readInt32BE(0));
+ assert.equal(-1013601994, buffer.readInt32BE(1));
+ assert.equal(-1784072681, buffer.readInt32BE(2));
+ assert.equal(-1449802942, buffer.readInt32LE(0));
+ assert.equal(917083587, buffer.readInt32LE(1));
+ assert.equal(389458325, buffer.readInt32LE(2));
+});
+
+
+/*
+ * 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
+ */
+exports.testReadUInt8 = helper('readUInt8', function (assert) {
+ var data = new Buffer(4);
+
+ data[0] = 23;
+ data[1] = 23;
+ data[2] = 23;
+ data[3] = 23;
+ assert.equal(23, data.readUInt8(0));
+ assert.equal(23, data.readUInt8(1));
+ assert.equal(23, data.readUInt8(2));
+ assert.equal(23, data.readUInt8(3));
+
+ data[0] = 255; /* If it became a signed int, would be -1 */
+ assert.equal(255, data.readUInt8(0));
+});
+
+
+/*
+ * Test 16 bit unsigned integers. We need to verify the same set as 8 bit, only
+ * now some of the issues actually matter:
+ * - We are correctly resolving big endian
+ * - Correctly resolving little endian
+ * - Correctly using the offsets
+ * - Correctly interpreting values that are beyond the signed range as unsigned
+ */
+exports.testReadUInt16 = helper('readUInt16LE/readUInt16BE', function (assert) {
+ var data = new Buffer(4);
+
+ data[0] = 0;
+ data[1] = 0x23;
+ data[2] = 0x42;
+ data[3] = 0x3f;
+ assert.equal(0x23, data.readUInt16BE(0));
+ assert.equal(0x2342, data.readUInt16BE(1));
+ assert.equal(0x423f, data.readUInt16BE(2));
+ assert.equal(0x2300, data.readUInt16LE(0));
+ assert.equal(0x4223, data.readUInt16LE(1));
+ assert.equal(0x3f42, data.readUInt16LE(2));
+
+ data[0] = 0xfe;
+ data[1] = 0xfe;
+ assert.equal(0xfefe, data.readUInt16BE(0));
+ assert.equal(0xfefe, data.readUInt16LE(0));
+});
+
+
+/*
+ * Test 32 bit unsigned integers. We need to verify the same set as 8 bit, only
+ * now some of the issues actually matter:
+ * - We are correctly resolving big endian
+ * - Correctly using the offsets
+ * - Correctly interpreting values that are beyond the signed range as unsigned
+ */
+exports.testReadUInt32 = helper('readUInt32LE/readUInt32BE', function (assert) {
+ var data = new Buffer(8);
+
+ data[0] = 0x32;
+ data[1] = 0x65;
+ data[2] = 0x42;
+ data[3] = 0x56;
+ data[4] = 0x23;
+ data[5] = 0xff;
+ assert.equal(0x32654256, data.readUInt32BE(0));
+ assert.equal(0x65425623, data.readUInt32BE(1));
+ assert.equal(0x425623ff, data.readUInt32BE(2));
+ assert.equal(0x56426532, data.readUInt32LE(0));
+ assert.equal(0x23564265, data.readUInt32LE(1));
+ assert.equal(0xff235642, data.readUInt32LE(2));
+});
+
+function helper (description, fn) {
+ let bulkAssert = {
+ equal: function (a, b) {
+ if (a !== b) throw new Error('Error found in ' + description);
+ },
+ ok: function (value) {
+ if (!value) throw new Error('Error found in ' + description);
+ },
+ throws: function (shouldThrow) {
+ let didItThrow = false;
+ try {
+ shouldThrow();
+ } catch (e) {
+ didItThrow = e;
+ }
+ if (!didItThrow)
+ throw new Error('Error found in ' + description + ': ' + shouldThrow + ' should have thrown');
+ }
+ };
+ return function (assert) {
+ fn(bulkAssert);
+ // If we get here, no errors thrown
+ assert.pass('All tests passed for ' + description);
+ };
+}
diff --git a/addon-sdk/source/test/buffers/test-write-types.js b/addon-sdk/source/test/buffers/test-write-types.js
new file mode 100644
index 000000000..f7872aaaa
--- /dev/null
+++ b/addon-sdk/source/test/buffers/test-write-types.js
@@ -0,0 +1,602 @@
+// 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.
+
+const { Buffer } = require('sdk/io/buffer');
+
+exports.testWriteDouble = helper('writeDoubleBE/writeDoubleLE', function (assert) {
+ var buffer = new Buffer(16);
+
+ buffer.writeDoubleBE(2.225073858507201e-308, 0);
+ buffer.writeDoubleLE(2.225073858507201e-308, 8);
+ assert.equal(0x00, buffer[0]);
+ assert.equal(0x0f, buffer[1]);
+ assert.equal(0xff, buffer[2]);
+ assert.equal(0xff, buffer[3]);
+ assert.equal(0xff, buffer[4]);
+ assert.equal(0xff, buffer[5]);
+ assert.equal(0xff, buffer[6]);
+ assert.equal(0xff, buffer[7]);
+ assert.equal(0xff, buffer[8]);
+ assert.equal(0xff, buffer[9]);
+ assert.equal(0xff, buffer[10]);
+ assert.equal(0xff, buffer[11]);
+ assert.equal(0xff, buffer[12]);
+ assert.equal(0xff, buffer[13]);
+ assert.equal(0x0f, buffer[14]);
+ assert.equal(0x00, buffer[15]);
+
+ buffer.writeDoubleBE(1.0000000000000004, 0);
+ buffer.writeDoubleLE(1.0000000000000004, 8);
+ assert.equal(0x3f, buffer[0]);
+ assert.equal(0xf0, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x02, buffer[7]);
+ assert.equal(0x02, buffer[8]);
+ assert.equal(0x00, buffer[9]);
+ assert.equal(0x00, buffer[10]);
+ assert.equal(0x00, buffer[11]);
+ assert.equal(0x00, buffer[12]);
+ assert.equal(0x00, buffer[13]);
+ assert.equal(0xf0, buffer[14]);
+ assert.equal(0x3f, buffer[15]);
+
+ buffer.writeDoubleBE(-2, 0);
+ buffer.writeDoubleLE(-2, 8);
+ assert.equal(0xc0, buffer[0]);
+ assert.equal(0x00, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x00, buffer[7]);
+ assert.equal(0x00, buffer[8]);
+ assert.equal(0x00, buffer[9]);
+ assert.equal(0x00, buffer[10]);
+ assert.equal(0x00, buffer[11]);
+ assert.equal(0x00, buffer[12]);
+ assert.equal(0x00, buffer[13]);
+ assert.equal(0x00, buffer[14]);
+ assert.equal(0xc0, buffer[15]);
+
+ buffer.writeDoubleBE(1.7976931348623157e+308, 0);
+ buffer.writeDoubleLE(1.7976931348623157e+308, 8);
+ assert.equal(0x7f, buffer[0]);
+ assert.equal(0xef, buffer[1]);
+ assert.equal(0xff, buffer[2]);
+ assert.equal(0xff, buffer[3]);
+ assert.equal(0xff, buffer[4]);
+ assert.equal(0xff, buffer[5]);
+ assert.equal(0xff, buffer[6]);
+ assert.equal(0xff, buffer[7]);
+ assert.equal(0xff, buffer[8]);
+ assert.equal(0xff, buffer[9]);
+ assert.equal(0xff, buffer[10]);
+ assert.equal(0xff, buffer[11]);
+ assert.equal(0xff, buffer[12]);
+ assert.equal(0xff, buffer[13]);
+ assert.equal(0xef, buffer[14]);
+ assert.equal(0x7f, buffer[15]);
+
+ buffer.writeDoubleBE(0 * -1, 0);
+ buffer.writeDoubleLE(0 * -1, 8);
+ assert.equal(0x80, buffer[0]);
+ assert.equal(0x00, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x00, buffer[7]);
+ assert.equal(0x00, buffer[8]);
+ assert.equal(0x00, buffer[9]);
+ assert.equal(0x00, buffer[10]);
+ assert.equal(0x00, buffer[11]);
+ assert.equal(0x00, buffer[12]);
+ assert.equal(0x00, buffer[13]);
+ assert.equal(0x00, buffer[14]);
+ assert.equal(0x80, buffer[15]);
+
+ buffer.writeDoubleBE(Infinity, 0);
+ buffer.writeDoubleLE(Infinity, 8);
+ assert.equal(0x7F, buffer[0]);
+ assert.equal(0xF0, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x00, buffer[7]);
+ assert.equal(0x00, buffer[8]);
+ assert.equal(0x00, buffer[9]);
+ assert.equal(0x00, buffer[10]);
+ assert.equal(0x00, buffer[11]);
+ assert.equal(0x00, buffer[12]);
+ assert.equal(0x00, buffer[13]);
+ assert.equal(0xF0, buffer[14]);
+ assert.equal(0x7F, buffer[15]);
+ assert.equal(Infinity, buffer.readDoubleBE(0));
+ assert.equal(Infinity, buffer.readDoubleLE(8));
+
+ buffer.writeDoubleBE(-Infinity, 0);
+ buffer.writeDoubleLE(-Infinity, 8);
+ assert.equal(0xFF, buffer[0]);
+ assert.equal(0xF0, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x00, buffer[7]);
+ assert.equal(0x00, buffer[8]);
+ assert.equal(0x00, buffer[9]);
+ assert.equal(0x00, buffer[10]);
+ assert.equal(0x00, buffer[11]);
+ assert.equal(0x00, buffer[12]);
+ assert.equal(0x00, buffer[13]);
+ assert.equal(0xF0, buffer[14]);
+ assert.equal(0xFF, buffer[15]);
+ assert.equal(-Infinity, buffer.readDoubleBE(0));
+ assert.equal(-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.ok(0x7F === buffer[0] || 0xFF === buffer[0]);
+ assert.equal(0xF8, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x00, buffer[7]);
+ assert.equal(0x00, buffer[8]);
+ assert.equal(0x00, buffer[9]);
+ assert.equal(0x00, buffer[10]);
+ assert.equal(0x00, buffer[11]);
+ assert.equal(0x00, buffer[12]);
+ assert.equal(0x00, buffer[13]);
+ assert.equal(0xF8, buffer[14]);
+ // Darwin ia32 does the other kind of NaN.
+ // Compiler bug. No one really cares.
+ assert.ok(0x7F === buffer[15] || 0xFF === buffer[15]);
+ assert.ok(isNaN(buffer.readDoubleBE(0)));
+ assert.ok(isNaN(buffer.readDoubleLE(8)));
+});
+
+exports.testWriteFloat = helper('writeFloatBE/writeFloatLE', function (assert) {
+ var buffer = new Buffer(8);
+
+ buffer.writeFloatBE(1, 0);
+ buffer.writeFloatLE(1, 4);
+ assert.equal(0x3f, buffer[0]);
+ assert.equal(0x80, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x80, buffer[6]);
+ assert.equal(0x3f, buffer[7]);
+
+ buffer.writeFloatBE(1 / 3, 0);
+ buffer.writeFloatLE(1 / 3, 4);
+ assert.equal(0x3e, buffer[0]);
+ assert.equal(0xaa, buffer[1]);
+ assert.equal(0xaa, buffer[2]);
+ assert.equal(0xab, buffer[3]);
+ assert.equal(0xab, buffer[4]);
+ assert.equal(0xaa, buffer[5]);
+ assert.equal(0xaa, buffer[6]);
+ assert.equal(0x3e, buffer[7]);
+
+ buffer.writeFloatBE(3.4028234663852886e+38, 0);
+ buffer.writeFloatLE(3.4028234663852886e+38, 4);
+ assert.equal(0x7f, buffer[0]);
+ assert.equal(0x7f, buffer[1]);
+ assert.equal(0xff, buffer[2]);
+ assert.equal(0xff, buffer[3]);
+ assert.equal(0xff, buffer[4]);
+ assert.equal(0xff, buffer[5]);
+ assert.equal(0x7f, buffer[6]);
+ assert.equal(0x7f, buffer[7]);
+
+ buffer.writeFloatLE(1.1754943508222875e-38, 0);
+ buffer.writeFloatBE(1.1754943508222875e-38, 4);
+ assert.equal(0x00, buffer[0]);
+ assert.equal(0x00, buffer[1]);
+ assert.equal(0x80, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x80, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x00, buffer[7]);
+
+ buffer.writeFloatBE(0 * -1, 0);
+ buffer.writeFloatLE(0 * -1, 4);
+ assert.equal(0x80, buffer[0]);
+ assert.equal(0x00, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x80, buffer[7]);
+
+ buffer.writeFloatBE(Infinity, 0);
+ buffer.writeFloatLE(Infinity, 4);
+ assert.equal(0x7F, buffer[0]);
+ assert.equal(0x80, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x80, buffer[6]);
+ assert.equal(0x7F, buffer[7]);
+ assert.equal(Infinity, buffer.readFloatBE(0));
+ assert.equal(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.ok(0xFF === buffer[0] || 0x7F === buffer[0]);
+ assert.equal(0x80, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x80, buffer[6]);
+ assert.equal(0xFF, buffer[7]);
+ assert.equal(-Infinity, buffer.readFloatBE(0));
+ assert.equal(-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.ok(0x7F === buffer[0] || 0xFF === buffer[0]);
+ assert.equal(0xc0, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0xc0, buffer[6]);
+ // Darwin ia32 does the other kind of NaN.
+ // Compiler bug. No one really cares.
+ assert.ok(0x7F === buffer[7] || 0xFF === buffer[7]);
+ assert.ok(isNaN(buffer.readFloatBE(0)));
+ assert.ok(isNaN(buffer.readFloatLE(4)));
+});
+
+
+exports.testWriteInt8 = helper('writeInt8', function (assert) {
+ var buffer = new Buffer(2);
+
+ buffer.writeInt8(0x23, 0);
+ buffer.writeInt8(-5, 1);
+
+ assert.equal(0x23, buffer[0]);
+ assert.equal(0xfb, buffer[1]);
+
+ /* Make sure we handle truncation correctly */
+ assert.throws(function() {
+ buffer.writeInt8(0xabc, 0);
+ });
+ assert.throws(function() {
+ buffer.writeInt8(0xabc, 0);
+ });
+
+ /* Make sure we handle min/max correctly */
+ buffer.writeInt8(0x7f, 0);
+ buffer.writeInt8(-0x80, 1);
+
+ assert.equal(0x7f, buffer[0]);
+ assert.equal(0x80, buffer[1]);
+ assert.throws(function() {
+ buffer.writeInt8(0x7f + 1, 0);
+ });
+ assert.throws(function() {
+ buffer.writeInt8(-0x80 - 1, 0);
+ });
+});
+
+
+exports.testWriteInt16 = helper('writeInt16LE/writeInt16BE', function (assert) {
+ var buffer = new Buffer(6);
+
+ buffer.writeInt16BE(0x0023, 0);
+ buffer.writeInt16LE(0x0023, 2);
+ assert.equal(0x00, buffer[0]);
+ assert.equal(0x23, buffer[1]);
+ assert.equal(0x23, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+
+ buffer.writeInt16BE(-5, 0);
+ buffer.writeInt16LE(-5, 2);
+ assert.equal(0xff, buffer[0]);
+ assert.equal(0xfb, buffer[1]);
+ assert.equal(0xfb, buffer[2]);
+ assert.equal(0xff, buffer[3]);
+
+ buffer.writeInt16BE(-1679, 1);
+ buffer.writeInt16LE(-1679, 3);
+ assert.equal(0xf9, buffer[1]);
+ assert.equal(0x71, buffer[2]);
+ assert.equal(0x71, buffer[3]);
+ assert.equal(0xf9, buffer[4]);
+
+ /* Make sure we handle min/max correctly */
+ buffer.writeInt16BE(0x7fff, 0);
+ buffer.writeInt16BE(-0x8000, 2);
+ assert.equal(0x7f, buffer[0]);
+ assert.equal(0xff, buffer[1]);
+ assert.equal(0x80, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.throws(function() {
+ buffer.writeInt16BE(0x7fff + 1, 0);
+ });
+ assert.throws(function() {
+ buffer.writeInt16BE(-0x8000 - 1, 0);
+ });
+
+ buffer.writeInt16LE(0x7fff, 0);
+ buffer.writeInt16LE(-0x8000, 2);
+ assert.equal(0xff, buffer[0]);
+ assert.equal(0x7f, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x80, buffer[3]);
+ assert.throws(function() {
+ buffer.writeInt16LE(0x7fff + 1, 0);
+ });
+ assert.throws(function() {
+ buffer.writeInt16LE(-0x8000 - 1, 0);
+ });
+});
+
+exports.testWriteInt32 = helper('writeInt32BE/writeInt32LE', function (assert) {
+ var buffer = new Buffer(8);
+
+ buffer.writeInt32BE(0x23, 0);
+ buffer.writeInt32LE(0x23, 4);
+ assert.equal(0x00, buffer[0]);
+ assert.equal(0x00, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x23, buffer[3]);
+ assert.equal(0x23, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x00, buffer[7]);
+
+ buffer.writeInt32BE(-5, 0);
+ buffer.writeInt32LE(-5, 4);
+ assert.equal(0xff, buffer[0]);
+ assert.equal(0xff, buffer[1]);
+ assert.equal(0xff, buffer[2]);
+ assert.equal(0xfb, buffer[3]);
+ assert.equal(0xfb, buffer[4]);
+ assert.equal(0xff, buffer[5]);
+ assert.equal(0xff, buffer[6]);
+ assert.equal(0xff, buffer[7]);
+
+ buffer.writeInt32BE(-805306713, 0);
+ buffer.writeInt32LE(-805306713, 4);
+ assert.equal(0xcf, buffer[0]);
+ assert.equal(0xff, buffer[1]);
+ assert.equal(0xfe, buffer[2]);
+ assert.equal(0xa7, buffer[3]);
+ assert.equal(0xa7, buffer[4]);
+ assert.equal(0xfe, buffer[5]);
+ assert.equal(0xff, buffer[6]);
+ assert.equal(0xcf, buffer[7]);
+
+ /* Make sure we handle min/max correctly */
+ buffer.writeInt32BE(0x7fffffff, 0);
+ buffer.writeInt32BE(-0x80000000, 4);
+ assert.equal(0x7f, buffer[0]);
+ assert.equal(0xff, buffer[1]);
+ assert.equal(0xff, buffer[2]);
+ assert.equal(0xff, buffer[3]);
+ assert.equal(0x80, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x00, buffer[7]);
+ assert.throws(function() {
+ buffer.writeInt32BE(0x7fffffff + 1, 0);
+ });
+ assert.throws(function() {
+ buffer.writeInt32BE(-0x80000000 - 1, 0);
+ });
+
+ buffer.writeInt32LE(0x7fffffff, 0);
+ buffer.writeInt32LE(-0x80000000, 4);
+ assert.equal(0xff, buffer[0]);
+ assert.equal(0xff, buffer[1]);
+ assert.equal(0xff, buffer[2]);
+ assert.equal(0x7f, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x80, buffer[7]);
+ assert.throws(function() {
+ buffer.writeInt32LE(0x7fffffff + 1, 0);
+ });
+ assert.throws(function() {
+ buffer.writeInt32LE(-0x80000000 - 1, 0);
+ });
+});
+
+/*
+ * 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
+ */
+exports.testWriteUInt8 = helper('writeUInt8', function (assert) {
+ var data = new Buffer(4);
+
+ data.writeUInt8(23, 0);
+ data.writeUInt8(23, 1);
+ data.writeUInt8(23, 2);
+ data.writeUInt8(23, 3);
+ assert.equal(23, data[0]);
+ assert.equal(23, data[1]);
+ assert.equal(23, data[2]);
+ assert.equal(23, data[3]);
+
+ data.writeUInt8(23, 0);
+ data.writeUInt8(23, 1);
+ data.writeUInt8(23, 2);
+ data.writeUInt8(23, 3);
+ assert.equal(23, data[0]);
+ assert.equal(23, data[1]);
+ assert.equal(23, data[2]);
+ assert.equal(23, data[3]);
+
+ data.writeUInt8(255, 0);
+ assert.equal(255, data[0]);
+
+ data.writeUInt8(255, 0);
+ assert.equal(255, data[0]);
+});
+
+
+exports.testWriteUInt16 = helper('writeUInt16BE/writeUInt16LE', function (assert) {
+ var value = 0x2343;
+ var data = new Buffer(4);
+
+ data.writeUInt16BE(value, 0);
+ assert.equal(0x23, data[0]);
+ assert.equal(0x43, data[1]);
+
+ data.writeUInt16BE(value, 1);
+ assert.equal(0x23, data[1]);
+ assert.equal(0x43, data[2]);
+
+ data.writeUInt16BE(value, 2);
+ assert.equal(0x23, data[2]);
+ assert.equal(0x43, data[3]);
+
+ data.writeUInt16LE(value, 0);
+ assert.equal(0x23, data[1]);
+ assert.equal(0x43, data[0]);
+
+ data.writeUInt16LE(value, 1);
+ assert.equal(0x23, data[2]);
+ assert.equal(0x43, data[1]);
+
+ data.writeUInt16LE(value, 2);
+ assert.equal(0x23, data[3]);
+ assert.equal(0x43, data[2]);
+
+ value = 0xff80;
+ data.writeUInt16LE(value, 0);
+ assert.equal(0xff, data[1]);
+ assert.equal(0x80, data[0]);
+
+ data.writeUInt16BE(value, 0);
+ assert.equal(0xff, data[0]);
+ assert.equal(0x80, data[1]);
+});
+
+
+exports.testWriteUInt32 = helper('writeUInt32BE/writeUInt32LE', function (assert) {
+ var data = new Buffer(6);
+ var value = 0xe7f90a6d;
+
+ data.writeUInt32BE(value, 0);
+ assert.equal(0xe7, data[0]);
+ assert.equal(0xf9, data[1]);
+ assert.equal(0x0a, data[2]);
+ assert.equal(0x6d, data[3]);
+
+ data.writeUInt32BE(value, 1);
+ assert.equal(0xe7, data[1]);
+ assert.equal(0xf9, data[2]);
+ assert.equal(0x0a, data[3]);
+ assert.equal(0x6d, data[4]);
+
+ data.writeUInt32BE(value, 2);
+ assert.equal(0xe7, data[2]);
+ assert.equal(0xf9, data[3]);
+ assert.equal(0x0a, data[4]);
+ assert.equal(0x6d, data[5]);
+
+ data.writeUInt32LE(value, 0);
+ assert.equal(0xe7, data[3]);
+ assert.equal(0xf9, data[2]);
+ assert.equal(0x0a, data[1]);
+ assert.equal(0x6d, data[0]);
+
+ data.writeUInt32LE(value, 1);
+ assert.equal(0xe7, data[4]);
+ assert.equal(0xf9, data[3]);
+ assert.equal(0x0a, data[2]);
+ assert.equal(0x6d, data[1]);
+
+ data.writeUInt32LE(value, 2);
+ assert.equal(0xe7, data[5]);
+ assert.equal(0xf9, data[4]);
+ assert.equal(0x0a, data[3]);
+ assert.equal(0x6d, data[2]);
+});
+
+function helper (description, fn) {
+ return function (assert) {
+ let bulkAssert = {
+ equal: function (a, b) {
+ if (a !== b) throw new Error('Error found in ' + description);
+ },
+ ok: function (value) {
+ if (!value) throw new Error('Error found in ' + description);
+ },
+ /*
+ * TODO
+ * There should be errors when setting outside of the value range
+ * of the data type (like writeInt8 with value of 1000), but DataView
+ * does not throw; seems to just grab the appropriate max bits.
+ * So ignoring this test for now
+ */
+ throws: function (shouldThrow) {
+ assert.pass(description + ': Need to implement error handling for setting buffer values ' +
+ 'outside of the data types\' range.');
+ /*
+ let didItThrow = false;
+ try {
+ shouldThrow();
+ } catch (e) {
+ didItThrow = e;
+ }
+ if (!didItThrow)
+ throw new Error('Error found in ' + description + ': ' + shouldThrow + ' should have thrown');
+ */
+ }
+ };
+ fn(bulkAssert);
+ // If we get here, no errors thrown
+ assert.pass('All tests passed for ' + description);
+ };
+}