diff options
Diffstat (limited to 'test/parallel/test-buffer-arraybuffer.js')
-rw-r--r-- | test/parallel/test-buffer-arraybuffer.js | 62 |
1 files changed, 59 insertions, 3 deletions
diff --git a/test/parallel/test-buffer-arraybuffer.js b/test/parallel/test-buffer-arraybuffer.js index 56811a950e..864081e14d 100644 --- a/test/parallel/test-buffer-arraybuffer.js +++ b/test/parallel/test-buffer-arraybuffer.js @@ -9,7 +9,7 @@ const LENGTH = 16; const ab = new ArrayBuffer(LENGTH); const dv = new DataView(ab); const ui = new Uint8Array(ab); -const buf = new Buffer(ab); +const buf = Buffer.from(ab); assert.ok(buf instanceof Buffer); @@ -42,12 +42,68 @@ assert.throws(function() { function AB() { } Object.setPrototypeOf(AB, ArrayBuffer); Object.setPrototypeOf(AB.prototype, ArrayBuffer.prototype); - new Buffer(new AB()); + Buffer.from(new AB()); }, TypeError); // write{Double,Float}{LE,BE} with noAssert should not crash, cf. #3766 -var b = new Buffer(1); +var 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); + ab[0] = 1; + ab[1] = 2; + ab[2] = 3; + ab[3] = 4; + ab[4] = 5; + const buf = Buffer.from(ab.buffer, 1, 3); + assert.equal(buf.length, 3); + assert.equal(buf[0], 2); + assert.equal(buf[1], 3); + assert.equal(buf[2], 4); + buf[0] = 9; + assert.equal(ab[1], 9); + + assert.throws(() => Buffer.from(ab.buffer, 6), (err) => { + assert(err instanceof RangeError); + assert(/'offset' is out of bounds/.test(err.message)); + return true; + }); + assert.throws(() => Buffer.from(ab.buffer, 3, 6), (err) => { + assert(err instanceof RangeError); + assert(/'length' is out of bounds/.test(err.message)); + return true; + }); +} + +// Test the deprecated Buffer() version also +{ + const ab = new Uint8Array(5); + ab[0] = 1; + ab[1] = 2; + ab[2] = 3; + ab[3] = 4; + ab[4] = 5; + const buf = Buffer(ab.buffer, 1, 3); + assert.equal(buf.length, 3); + assert.equal(buf[0], 2); + assert.equal(buf[1], 3); + assert.equal(buf[2], 4); + buf[0] = 9; + assert.equal(ab[1], 9); + + assert.throws(() => Buffer(ab.buffer, 6), (err) => { + assert(err instanceof RangeError); + assert(/'offset' is out of bounds/.test(err.message)); + return true; + }); + assert.throws(() => Buffer(ab.buffer, 3, 6), (err) => { + assert(err instanceof RangeError); + assert(/'length' is out of bounds/.test(err.message)); + return true; + }); +} |