From fd856c9e8f16213a13470188b08f85c3080781e6 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 9 Jan 2016 16:09:24 +0100 Subject: [PATCH] Include `typedarray-to-buffer` test Fixes #93 --- package.json | 1 + test/typedarray-to-buffer.js | 52 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 test/typedarray-to-buffer.js diff --git a/package.json b/package.json index 4a616bb..6b93e1c 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "standard": "^5.0.0", "tape": "^4.0.0", "through2": "^2.0.0", + "typedarray-to-buffer": "^3.0.4", "zuul": "^3.0.0" }, "homepage": "https://github.com/feross/buffer", diff --git a/test/typedarray-to-buffer.js b/test/typedarray-to-buffer.js new file mode 100644 index 0000000..e683b1a --- /dev/null +++ b/test/typedarray-to-buffer.js @@ -0,0 +1,52 @@ +if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false +var B = require('../').Buffer +var toBuffer = require('typedarray-to-buffer') +var test = require('tape') + +test('convert to buffer from Uint8Array', function (t) { + if (typeof Uint8Array !== 'undefined') { + var arr = new Uint8Array([1, 2, 3]) + arr = toBuffer(arr) + + t.deepEqual(arr, new B([1, 2, 3]), 'contents equal') + t.ok(Buffer.isBuffer(arr), 'is buffer') + t.equal(arr.readUInt8(0), 1) + t.equal(arr.readUInt8(1), 2) + t.equal(arr.readUInt8(2), 3) + } else { + t.pass('browser lacks Uint8Array support, skip test') + } + t.end() +}) + +test('convert to buffer from another arrayview type (Uint32Array)', function (t) { + if (typeof Uint32Array !== 'undefined' && B.TYPED_ARRAY_SUPPORT) { + var arr = new Uint32Array([1, 2, 3]) + arr = toBuffer(arr) + + t.deepEqual(arr, new B([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]), 'contents equal') + t.ok(Buffer.isBuffer(arr), 'is buffer') + t.equal(arr.readUInt32LE(0), 1) + t.equal(arr.readUInt32LE(4), 2) + t.equal(arr.readUInt32LE(8), 3) + t.equal(arr instanceof Uint8Array, true) + } else { + t.pass('browser lacks Uint32Array support, skip test') + } + t.end() +}) + +test('convert to buffer from ArrayBuffer', function (t) { + if (typeof Uint32Array !== 'undefined' && B.TYPED_ARRAY_SUPPORT) { + var arr = new Uint32Array([1, 2, 3]).subarray(1, 2) + arr = toBuffer(arr) + + t.deepEqual(arr, new B([2, 0, 0, 0]), 'contents equal') + t.ok(Buffer.isBuffer(arr), 'is buffer') + t.equal(arr.readUInt32LE(0), 2) + t.equal(arr instanceof Uint8Array, true) + } else { + t.pass('browser lacks ArrayBuffer support, skip test') + } + t.end() +}) -- 2.34.1