From 3cfe9c78f580c848993ba1a7074ed4ca380b6d84 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Fri, 12 Sep 2014 00:26:47 +0100 Subject: [PATCH] test object impl in node; write methods match node; bug fix for #41 --- index.js | 289 +++++++++++++++++++------------------------- package.json | 2 +- test/base64.js | 2 + test/basic.js | 20 +++ test/compare.js | 2 + test/constructor.js | 2 + test/deprecated.js | 4 +- test/methods.js | 83 +------------ test/slice.js | 2 + test/static.js | 2 + test/to-string.js | 2 + test/user-agent.js | 2 + test/utf16.js | 4 +- test/write.js | 112 +++++++++++++++++ 14 files changed, 280 insertions(+), 248 deletions(-) create mode 100644 test/write.js diff --git a/index.js b/index.js index 9b3d1c7..ad3f764 100644 --- a/index.js +++ b/index.js @@ -14,7 +14,7 @@ exports.INSPECT_MAX_BYTES = 50 Buffer.poolSize = 8192 /** - * If `TYPED_ARRAY_SUPPORT`: + * If `Buffer.TYPED_ARRAY_SUPPORT`: * === true Use Uint8Array implementation (fastest) * === false Use Object implementation (most compatible, even IE6) * @@ -32,10 +32,10 @@ Buffer.poolSize = 8192 * - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of * incorrect length in some situations. * - * We detect these buggy browsers and set `TYPED_ARRAY_SUPPORT` to `false` so they will + * We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they will * get the Object implementation, which is slower but will work correctly. */ -var TYPED_ARRAY_SUPPORT = Buffer.TYPED_ARRAY_SUPPORT = (function () { +Buffer.TYPED_ARRAY_SUPPORT = (function () { try { var buf = new ArrayBuffer(0) var arr = new Uint8Array(buf) @@ -82,7 +82,7 @@ function Buffer (subject, encoding, noZero) { throw new Error('First argument needs to be a number, array or string.') var buf - if (TYPED_ARRAY_SUPPORT) { + if (Buffer.TYPED_ARRAY_SUPPORT) { // Preferred: Return an augmented `Uint8Array` instance for best performance buf = Buffer._augment(new Uint8Array(length)) } else { @@ -93,7 +93,7 @@ function Buffer (subject, encoding, noZero) { } var i - if (TYPED_ARRAY_SUPPORT && typeof subject.byteLength === 'number') { + if (Buffer.TYPED_ARRAY_SUPPORT && typeof subject.byteLength === 'number') { // Speed optimization -- use set if we're copying from a typed array buf._set(subject) } else if (isArrayish(subject)) { @@ -107,7 +107,7 @@ function Buffer (subject, encoding, noZero) { } } else if (type === 'string') { buf.write(subject, 0, encoding) - } else if (type === 'number' && !TYPED_ARRAY_SUPPORT && !noZero) { + } else if (type === 'number' && !Buffer.TYPED_ARRAY_SUPPORT && !noZero) { for (i = 0; i < length; i++) { buf[i] = 0 } @@ -414,7 +414,7 @@ Buffer.prototype.copy = function (target, target_start, start, end) { var len = end - start - if (len < 100 || !TYPED_ARRAY_SUPPORT) { + if (len < 100 || !Buffer.TYPED_ARRAY_SUPPORT) { for (var i = 0; i < len; i++) { target[i + target_start] = this[i + start] } @@ -508,7 +508,7 @@ Buffer.prototype.slice = function (start, end) { if (end < start) end = start - if (TYPED_ARRAY_SUPPORT) { + if (Buffer.TYPED_ARRAY_SUPPORT) { return Buffer._augment(this.subarray(start, end)) } else { var sliceLen = end - start @@ -721,164 +721,161 @@ Buffer.prototype.readDoubleBE = function (offset, noAssert) { return readDouble(this, offset, false, noAssert) } -Buffer.prototype.writeUInt8 = function (value, offset, noAssert) { - if (!noAssert) { - assert(value !== undefined && value !== null, 'missing value') - assert(offset !== undefined && offset !== null, 'missing offset') - assert(offset < this.length, 'trying to write beyond buffer length') - verifuint(value, 0xff) - } - - if (offset >= this.length) return +function checkInt (buf, value, offset, ext, max, min) { + assert(Buffer.isBuffer(buf), 'buffer must be a Buffer instance') + assert(value <= max && value >= min, 'value is out of bounds') + assert(offset + ext <= buf.length, 'index out of range') +} +Buffer.prototype.writeUInt8 = function (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) + checkInt(this, value, offset, 1, 0xff, 0) + if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value) this[offset] = value return offset + 1 } -function writeUInt16 (buf, value, offset, littleEndian, noAssert) { - if (!noAssert) { - assert(value !== undefined && value !== null, 'missing value') - assert(typeof littleEndian === 'boolean', 'missing or invalid endian') - assert(offset !== undefined && offset !== null, 'missing offset') - assert(offset + 1 < buf.length, 'trying to write beyond buffer length') - verifuint(value, 0xffff) - } - - var len = buf.length - if (offset >= len) - return - - for (var i = 0, j = Math.min(len - offset, 2); i < j; i++) { - buf[offset + i] = - (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>> - (littleEndian ? i : 1 - i) * 8 +function objectWriteUInt16 (buf, value, offset, littleEndian) { + if (value < 0) value = 0xffff + value + 1 + for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; i++) { + buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>> + (littleEndian ? i : 1 - i) * 8 } - return offset + 2 } Buffer.prototype.writeUInt16LE = function (value, offset, noAssert) { - return writeUInt16(this, value, offset, true, noAssert) + value = +value + offset = offset >>> 0 + if (!noAssert) + checkInt(this, value, offset, 2, 0xffff, 0) + if (Buffer.TYPED_ARRAY_SUPPORT) { + this[offset] = value + this[offset + 1] = (value >>> 8) + } else objectWriteUInt16(this, value, offset, true) + return offset + 2 } Buffer.prototype.writeUInt16BE = function (value, offset, noAssert) { - return writeUInt16(this, value, offset, false, noAssert) + value = +value + offset = offset >>> 0 + if (!noAssert) + checkInt(this, value, offset, 2, 0xffff, 0) + if (Buffer.TYPED_ARRAY_SUPPORT) { + this[offset] = (value >>> 8) + this[offset + 1] = value + } else objectWriteUInt16(this, value, offset, false) + return offset + 2 } -function writeUInt32 (buf, value, offset, littleEndian, noAssert) { - if (!noAssert) { - assert(value !== undefined && value !== null, 'missing value') - assert(typeof littleEndian === 'boolean', 'missing or invalid endian') - assert(offset !== undefined && offset !== null, 'missing offset') - assert(offset + 3 < buf.length, 'trying to write beyond buffer length') - verifuint(value, 0xffffffff) - } - - var len = buf.length - if (offset >= len) - return - - for (var i = 0, j = Math.min(len - offset, 4); i < j; i++) { - buf[offset + i] = - (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff +function objectWriteUInt32 (buf, value, offset, littleEndian) { + if (value < 0) value = 0xffffffff + value + 1 + for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; i++) { + buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff } - return offset + 4 } Buffer.prototype.writeUInt32LE = function (value, offset, noAssert) { - return writeUInt32(this, value, offset, true, noAssert) + value = +value + offset = offset >>> 0 + if (!noAssert) + checkInt(this, value, offset, 4, 0xffffffff, 0) + if (Buffer.TYPED_ARRAY_SUPPORT) { + this[offset + 3] = (value >>> 24) + this[offset + 2] = (value >>> 16) + this[offset + 1] = (value >>> 8) + this[offset] = value + } else objectWriteUInt32(this, value, offset, true) + return offset + 4 } Buffer.prototype.writeUInt32BE = function (value, offset, noAssert) { - return writeUInt32(this, value, offset, false, noAssert) + value = +value + offset = offset >>> 0 + if (!noAssert) + checkInt(this, value, offset, 4, 0xffffffff, 0) + if (Buffer.TYPED_ARRAY_SUPPORT) { + this[offset] = (value >>> 24) + this[offset + 1] = (value >>> 16) + this[offset + 2] = (value >>> 8) + this[offset + 3] = value + } else objectWriteUInt32(this, value, offset, false) + return offset + 4 } Buffer.prototype.writeInt8 = function (value, offset, noAssert) { - if (!noAssert) { - assert(value !== undefined && value !== null, 'missing value') - assert(offset !== undefined && offset !== null, 'missing offset') - assert(offset < this.length, 'Trying to write beyond buffer length') - verifsint(value, 0x7f, -0x80) - } - - if (offset >= this.length) - return - - if (value >= 0) - this.writeUInt8(value, offset, noAssert) - else - this.writeUInt8(0xff + value + 1, offset, noAssert) + value = +value + offset = offset >>> 0 + if (!noAssert) + checkInt(this, value, offset, 1, 0x7f, -0x80) + if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value) + if (value < 0) value = 0xff + value + 1 + this[offset] = value return offset + 1 } -function writeInt16 (buf, value, offset, littleEndian, noAssert) { - if (!noAssert) { - assert(value !== undefined && value !== null, 'missing value') - assert(typeof littleEndian === 'boolean', 'missing or invalid endian') - assert(offset !== undefined && offset !== null, 'missing offset') - assert(offset + 1 < buf.length, 'Trying to write beyond buffer length') - verifsint(value, 0x7fff, -0x8000) - } - - var len = buf.length - if (offset >= len) - return - - if (value >= 0) - writeUInt16(buf, value, offset, littleEndian, noAssert) - else - writeUInt16(buf, 0xffff + value + 1, offset, littleEndian, noAssert) - return offset + 2 -} - Buffer.prototype.writeInt16LE = function (value, offset, noAssert) { - return writeInt16(this, value, offset, true, noAssert) + value = +value + offset = offset >>> 0 + if (!noAssert) + checkInt(this, value, offset, 2, 0x7fff, -0x8000) + if (Buffer.TYPED_ARRAY_SUPPORT) { + this[offset] = value + this[offset + 1] = (value >>> 8) + } else objectWriteUInt16(this, value, offset, true) + return offset + 2 } Buffer.prototype.writeInt16BE = function (value, offset, noAssert) { - return writeInt16(this, value, offset, false, noAssert) + value = +value + offset = offset >>> 0 + if (!noAssert) + checkInt(this, value, offset, 2, 0x7fff, -0x8000) + if (Buffer.TYPED_ARRAY_SUPPORT) { + this[offset] = (value >>> 8) + this[offset + 1] = value + } else objectWriteUInt16(this, value, offset, false) + return offset + 2 } -function writeInt32 (buf, value, offset, littleEndian, noAssert) { - if (!noAssert) { - assert(value !== undefined && value !== null, 'missing value') - assert(typeof littleEndian === 'boolean', 'missing or invalid endian') - assert(offset !== undefined && offset !== null, 'missing offset') - assert(offset + 3 < buf.length, 'Trying to write beyond buffer length') - verifsint(value, 0x7fffffff, -0x80000000) - } - - var len = buf.length - if (offset >= len) - return - - if (value >= 0) - writeUInt32(buf, value, offset, littleEndian, noAssert) - else - writeUInt32(buf, 0xffffffff + value + 1, offset, littleEndian, noAssert) +Buffer.prototype.writeInt32LE = function (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) + checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) + if (Buffer.TYPED_ARRAY_SUPPORT) { + this[offset] = value + this[offset + 1] = (value >>> 8) + this[offset + 2] = (value >>> 16) + this[offset + 3] = (value >>> 24) + } else objectWriteUInt32(this, value, offset, true) return offset + 4 } -Buffer.prototype.writeInt32LE = function (value, offset, noAssert) { - return writeInt32(this, value, offset, true, noAssert) +Buffer.prototype.writeInt32BE = function (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) + checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) + if (value < 0) value = 0xffffffff + value + 1 + if (Buffer.TYPED_ARRAY_SUPPORT) { + this[offset] = (value >>> 24) + this[offset + 1] = (value >>> 16) + this[offset + 2] = (value >>> 8) + this[offset + 3] = value + } else objectWriteUInt32(this, value, offset, false) + return offset + 4 } -Buffer.prototype.writeInt32BE = function (value, offset, noAssert) { - return writeInt32(this, value, offset, false, noAssert) +function checkIEEE754 (buf, value, offset, ext, max, min) { + assert(value <= max && value >= min, 'value is out of bounds') + assert(offset + ext <= buf.length, 'index out of range') } function writeFloat (buf, value, offset, littleEndian, noAssert) { - if (!noAssert) { - assert(value !== undefined && value !== null, 'missing value') - assert(typeof littleEndian === 'boolean', 'missing or invalid endian') - assert(offset !== undefined && offset !== null, 'missing offset') - assert(offset + 3 < buf.length, 'Trying to write beyond buffer length') - verifIEEE754(value, 3.4028234663852886e+38, -3.4028234663852886e+38) - } - - var len = buf.length - if (offset >= len) - return - + if (!noAssert) + checkIEEE754(this, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38) ieee754.write(buf, value, offset, littleEndian, 23, 4) return offset + 4 } @@ -892,19 +889,8 @@ Buffer.prototype.writeFloatBE = function (value, offset, noAssert) { } function writeDouble (buf, value, offset, littleEndian, noAssert) { - if (!noAssert) { - assert(value !== undefined && value !== null, 'missing value') - assert(typeof littleEndian === 'boolean', 'missing or invalid endian') - assert(offset !== undefined && offset !== null, 'missing offset') - assert(offset + 7 < buf.length, - 'Trying to write beyond buffer length') - verifIEEE754(value, 1.7976931348623157E+308, -1.7976931348623157E+308) - } - - var len = buf.length - if (offset >= len) - return - + if (!noAssert) + checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308) ieee754.write(buf, value, offset, littleEndian, 52, 8) return offset + 8 } @@ -967,7 +953,7 @@ Buffer.prototype.inspect = function () { */ Buffer.prototype.toArrayBuffer = function () { if (typeof Uint8Array !== 'undefined') { - if (TYPED_ARRAY_SUPPORT) { + if (Buffer.TYPED_ARRAY_SUPPORT) { return (new Buffer(this)).buffer } else { var buf = new Uint8Array(this.length) @@ -1139,31 +1125,6 @@ function decodeUtf8Char (str) { } } -/* - * We have to make sure that the value is a valid integer. This means that it - * is non-negative. It has no fractional component and that it does not - * exceed the maximum allowed value. - */ -function verifuint (value, max) { - assert(typeof value === 'number', 'cannot write a non-number as a number') - assert(value >= 0, 'specified a negative value for writing an unsigned value') - assert(value <= max, 'value is larger than maximum value for type') - assert(Math.floor(value) === value, 'value has a fractional component') -} - -function verifsint (value, max, min) { - assert(typeof value === 'number', 'cannot write a non-number as a number') - assert(value <= max, 'value larger than maximum allowed value') - assert(value >= min, 'value smaller than minimum allowed value') - assert(Math.floor(value) === value, 'value has a fractional component') -} - -function verifIEEE754 (value, max, min) { - assert(typeof value === 'number', 'cannot write a non-number as a number') - assert(value <= max, 'value larger than maximum allowed value') - assert(value >= min, 'value smaller than minimum allowed value') -} - function assert (test, message) { if (!test) throw new Error(message || 'Failed assertion') } diff --git a/package.json b/package.json index c63f02a..3510d89 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "url": "git://github.com/feross/buffer.git" }, "scripts": { - "test": "tape test/*.js", + "test": "tape test/*.js && OBJECT_IMPL=true tape test/*.js", "prepublish": "./bundle.sh", "perf": "cd perf/solo && browserify --debug readUInt32BE.js > bundle.js && open index.html", "size": "browserify -r ./ | uglifyjs -c -m | gzip | wc -c" diff --git a/test/base64.js b/test/base64.js index e5ecf25..57b2f7b 100644 --- a/test/base64.js +++ b/test/base64.js @@ -1,5 +1,7 @@ var B = require('../').Buffer var test = require('tape') +if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false + test('base64: ignore whitespace', function (t) { var text = '\n YW9ldQ== ' diff --git a/test/basic.js b/test/basic.js index 5ebb306..2424d4b 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,5 +1,7 @@ var B = require('../').Buffer var test = require('tape') +if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false + test('indexes from a string', function(t) { var buf = new B('abc') @@ -27,3 +29,21 @@ test('setting index value should modify buffer contents', function(t) { t.equal(buf.toString(), 'abm') t.end() }) + +test('storing negative number should cast to unsigned', function (t) { + var buf = new B(1) + + if (Buffer.TYPED_ARRAY_SUPPORT) { + // This does not work with the object implementation -- nothing we can do! + buf[0] = -3 + t.equal(buf[0], 253) + } + + buf = new B(1) + buf.writeInt8(-3, 0) + t.equal(buf[0], 253) + + t.end() +}) + +// TODO: test write negative with diff --git a/test/compare.js b/test/compare.js index 5d4d76c..d4ec003 100644 --- a/test/compare.js +++ b/test/compare.js @@ -1,5 +1,7 @@ var B = require('../').Buffer var test = require('tape') +if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false + test('buffer.compare', function (t) { var b = new B(1).fill('a') diff --git a/test/constructor.js b/test/constructor.js index 1508f1f..e109a11 100644 --- a/test/constructor.js +++ b/test/constructor.js @@ -1,5 +1,7 @@ var B = require('../').Buffer var test = require('tape') +if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false + test('new buffer from array', function (t) { t.equal( diff --git a/test/deprecated.js b/test/deprecated.js index f252543..faa6215 100644 --- a/test/deprecated.js +++ b/test/deprecated.js @@ -1,5 +1,7 @@ var B = require('../').Buffer var test = require('tape') +if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false + test('.get (deprecated)', function (t) { var b = new B([7, 42]) @@ -15,4 +17,4 @@ test('.set (deprecated)', function (t) { t.equal(b[0], 7) t.equal(b[1], 42) t.end() -}) \ No newline at end of file +}) diff --git a/test/methods.js b/test/methods.js index 1cbc172..e19f9da 100644 --- a/test/methods.js +++ b/test/methods.js @@ -1,5 +1,7 @@ var B = require('../').Buffer var test = require('tape') +if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false + test('buffer.toJSON', function (t) { var data = [1, 2, 3, 4] @@ -29,87 +31,6 @@ test('buffer.copy', function (t) { t.end() }) -test('hex of write{Uint,Int}{8,16,32}{LE,BE}', function (t) { - t.plan(2 * (2 * 2 * 2 + 2)) - var hex = [ - '03', '0300', '0003', '03000000', '00000003', - 'fd', 'fdff', 'fffd', 'fdffffff', 'fffffffd' - ] - var reads = [ 3, 3, 3, 3, 3, -3, -3, -3, -3, -3 ] - var xs = ['UInt','Int'] - var ys = [8,16,32] - for (var i = 0; i < xs.length; i++) { - var x = xs[i] - for (var j = 0; j < ys.length; j++) { - var y = ys[j] - var endianesses = (y === 8) ? [''] : ['LE','BE'] - for (var k = 0; k < endianesses.length; k++) { - var z = endianesses[k] - - var v1 = new B(y / 8) - var writefn = 'write' + x + y + z - var val = (x === 'Int') ? -3 : 3 - v1[writefn](val, 0) - t.equal( - v1.toString('hex'), - hex.shift() - ) - var readfn = 'read' + x + y + z - t.equal( - v1[readfn](0), - reads.shift() - ) - } - } - } - t.end() -}) - -test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) { - t.plan(3 * (2 * 2 * 2 + 2)) - var hex = [ - '', '03', '00', '030000', '000000', - '', 'fd', 'ff', 'fdffff', 'ffffff' - ] - var reads = [ - undefined, 3, 0, 3, 0, - undefined, 253, -256, 16777213, -256 - ] - var xs = ['UInt','Int'] - var ys = [8,16,32] - for (var i = 0; i < xs.length; i++) { - var x = xs[i] - for (var j = 0; j < ys.length; j++) { - var y = ys[j] - var endianesses = (y === 8) ? [''] : ['LE','BE'] - for (var k = 0; k < endianesses.length; k++) { - var z = endianesses[k] - - var v1 = new B(y / 8 - 1) - var next = new B(4) - next.writeUInt32BE(0, 0) - var writefn = 'write' + x + y + z - var val = (x === 'Int') ? -3 : 3 - v1[writefn](val, 0, true) - t.equal( - v1.toString('hex'), - hex.shift() - ) - // check that nothing leaked to next buffer. - t.equal(next.readUInt32BE(0), 0) - // check that no bytes are read from next buffer. - next.writeInt32BE(~0, 0) - var readfn = 'read' + x + y + z - t.equal( - v1[readfn](0, true), - reads.shift() - ) - } - } - } - t.end() -}) - test('test offset returns are correct', function (t) { var b = new B(16) t.equal(4, b.writeUInt32LE(0, 0)) diff --git a/test/slice.js b/test/slice.js index 4efbe30..285d0ab 100644 --- a/test/slice.js +++ b/test/slice.js @@ -1,5 +1,7 @@ var B = require('../').Buffer var test = require('tape') +if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false + test('modifying buffer created by .slice() modifies original memory', function (t) { if (!B._useTypedArrays) return t.end() diff --git a/test/static.js b/test/static.js index 4dc90f2..f4f6dfc 100644 --- a/test/static.js +++ b/test/static.js @@ -1,5 +1,7 @@ var B = require('../').Buffer var test = require('tape') +if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false + test('Buffer.isEncoding', function (t) { t.equal(B.isEncoding('HEX'), true) diff --git a/test/to-string.js b/test/to-string.js index 714ffd7..6b388aa 100644 --- a/test/to-string.js +++ b/test/to-string.js @@ -1,5 +1,7 @@ var B = require('../').Buffer var test = require('tape') +if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false + test('utf8 buffer to base64', function (t) { t.equal( diff --git a/test/user-agent.js b/test/user-agent.js index cab30c5..b8e78e6 100644 --- a/test/user-agent.js +++ b/test/user-agent.js @@ -1,6 +1,8 @@ // var B = require('../').Buffer // var test = require('tape') // var useragent = require('useragent') +// if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false + // test('expected browsers get Uint8Array implementation', function (t) { // if (typeof navigator === 'undefined') { diff --git a/test/utf16.js b/test/utf16.js index d34f72b..5462d0e 100644 --- a/test/utf16.js +++ b/test/utf16.js @@ -1,5 +1,7 @@ var B = require('../').Buffer var test = require('tape') +if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false + test('detect utf16 surrogate pairs', function(t) { var text = '\uD83D\uDE38' + '\uD83D\uDCAD' + '\uD83D\uDC4D' @@ -30,4 +32,4 @@ test('throw on orphaned utf16 surrogate trail code point', function(t) { } t.equal(err instanceof URIError, true) t.end() -}) \ No newline at end of file +}) diff --git a/test/write.js b/test/write.js new file mode 100644 index 0000000..18ecb8b --- /dev/null +++ b/test/write.js @@ -0,0 +1,112 @@ +var B = require('../').Buffer +var test = require('tape') +if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false + +test('buffer.write string should get parsed as number', function (t) { + var b = new B(64) + b.writeUInt16LE('1003', 0) + t.equal(b.readUInt16LE(0), 1003) + t.end() +}) + +test('buffer.writeUInt8 a fractional number will get Math.floored', function (t) { + // Some extra work is necessary to make this test pass with the Object implementation + + var b = new B(1) + b.writeInt8(5.5, 0) + t.equal(b[0], 5) + t.end() +}) + +test('writeUint8 with a negative number throws', function (t) { + var buf = new B(1) + + t.throws(function () { + buf.writeUInt8(-3, 0) + }) + + t.end() +}) + +test('hex of write{Uint,Int}{8,16,32}{LE,BE}', function (t) { + t.plan(2 * (2 * 2 * 2 + 2)) + var hex = [ + '03', '0300', '0003', '03000000', '00000003', + 'fd', 'fdff', 'fffd', 'fdffffff', 'fffffffd' + ] + var reads = [ 3, 3, 3, 3, 3, -3, -3, -3, -3, -3 ] + var xs = ['UInt','Int'] + var ys = [8,16,32] + for (var i = 0; i < xs.length; i++) { + var x = xs[i] + for (var j = 0; j < ys.length; j++) { + var y = ys[j] + var endianesses = (y === 8) ? [''] : ['LE','BE'] + for (var k = 0; k < endianesses.length; k++) { + var z = endianesses[k] + + var v1 = new B(y / 8) + var writefn = 'write' + x + y + z + var val = (x === 'Int') ? -3 : 3 + console.log(writefn, val) + v1[writefn](val, 0) + t.equal( + v1.toString('hex'), + hex.shift() + ) + var readfn = 'read' + x + y + z + console.log(v1[0], v1[readfn](0)) + t.equal( + v1[readfn](0), + reads.shift() + ) + } + } + } + t.end() +}) + +test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) { + t.plan(3 * (2 * 2 * 2 + 2)) + var hex = [ + '', '03', '00', '030000', '000000', + '', 'fd', 'ff', 'fdffff', 'ffffff' + ] + var reads = [ + undefined, 3, 0, 3, 0, + undefined, 253, -256, 16777213, -256 + ] + var xs = ['UInt','Int'] + var ys = [8,16,32] + for (var i = 0; i < xs.length; i++) { + var x = xs[i] + for (var j = 0; j < ys.length; j++) { + var y = ys[j] + var endianesses = (y === 8) ? [''] : ['LE','BE'] + for (var k = 0; k < endianesses.length; k++) { + var z = endianesses[k] + + var v1 = new B(y / 8 - 1) + var next = new B(4) + next.writeUInt32BE(0, 0) + var writefn = 'write' + x + y + z + var val = (x === 'Int') ? -3 : 3 + v1[writefn](val, 0, true) + t.equal( + v1.toString('hex'), + hex.shift() + ) + // check that nothing leaked to next buffer. + t.equal(next.readUInt32BE(0), 0) + // check that no bytes are read from next buffer. + next.writeInt32BE(~0, 0) + var readfn = 'read' + x + y + z + t.equal( + v1[readfn](0, true), + reads.shift() + ) + } + } + } + t.end() +}) -- 2.34.1