diff options
author | Anna Henningsen <anna@addaleax.net> | 2016-12-10 22:28:37 +0100 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2016-12-21 07:48:14 +0100 |
commit | beca3244e2792bc4257f480b138c1b603de3781c (patch) | |
tree | f298c9f0deef44b2003f4bc4da1098fef0347d76 /test | |
parent | 6cb33c0764f307e33d29655cd1bac2916656352a (diff) | |
download | node-new-beca3244e2792bc4257f480b138c1b603de3781c.tar.gz |
buffer: allow Uint8Array input to methods
Allow all methods on `buffer` and `Buffer` to take `Uint8Array`
arguments where it makes sense. On the native side, there is
effectively no difference, and as a bonus the `isUint8Array`
check is faster than `instanceof Buffer`.
PR-URL: https://github.com/nodejs/node/pull/10236
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/parallel/test-buffer-compare.js | 5 | ||||
-rw-r--r-- | test/parallel/test-buffer-concat.js | 7 | ||||
-rw-r--r-- | test/parallel/test-buffer-equals.js | 1 | ||||
-rw-r--r-- | test/parallel/test-buffer-indexof.js | 8 | ||||
-rw-r--r-- | test/parallel/test-icu-transcode.js | 10 |
5 files changed, 29 insertions, 2 deletions
diff --git a/test/parallel/test-buffer-compare.js b/test/parallel/test-buffer-compare.js index c0db39a6e3..c9e50081ed 100644 --- a/test/parallel/test-buffer-compare.js +++ b/test/parallel/test-buffer-compare.js @@ -6,10 +6,12 @@ const assert = require('assert'); const b = Buffer.alloc(1, 'a'); const c = Buffer.alloc(1, 'c'); const d = Buffer.alloc(2, 'aa'); +const e = new Uint8Array([ 0x61, 0x61 ]); // ASCII 'aa', same as d assert.strictEqual(b.compare(c), -1); assert.strictEqual(c.compare(d), 1); assert.strictEqual(d.compare(b), 1); +assert.strictEqual(d.compare(e), 0); assert.strictEqual(b.compare(d), -1); assert.strictEqual(b.compare(b), 0); @@ -18,6 +20,9 @@ assert.strictEqual(Buffer.compare(c, d), 1); assert.strictEqual(Buffer.compare(d, b), 1); assert.strictEqual(Buffer.compare(b, d), -1); assert.strictEqual(Buffer.compare(c, c), 0); +assert.strictEqual(Buffer.compare(e, e), 0); +assert.strictEqual(Buffer.compare(d, e), 0); +assert.strictEqual(Buffer.compare(d, b), 1); assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(0)), 0); assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(1)), -1); diff --git a/test/parallel/test-buffer-concat.js b/test/parallel/test-buffer-concat.js index 800f1055aa..1239c9e8e1 100644 --- a/test/parallel/test-buffer-concat.js +++ b/test/parallel/test-buffer-concat.js @@ -35,7 +35,8 @@ function assertWrongList(value) { Buffer.concat(value); }, function(err) { return err instanceof TypeError && - err.message === '"list" argument must be an Array of Buffers'; + err.message === '"list" argument must be an Array of Buffer ' + + 'or Uint8Array instances'; }); } @@ -60,3 +61,7 @@ assert.deepStrictEqual(Buffer.concat([empty], 4096), Buffer.alloc(4096)); assert.deepStrictEqual( Buffer.concat([random10], 40), Buffer.concat([random10, Buffer.alloc(30)])); + +assert.deepStrictEqual(Buffer.concat([new Uint8Array([0x41, 0x42]), + new Uint8Array([0x43, 0x44])]), + Buffer.from('ABCD')); diff --git a/test/parallel/test-buffer-equals.js b/test/parallel/test-buffer-equals.js index 2b460c5c6a..cc7c71ed48 100644 --- a/test/parallel/test-buffer-equals.js +++ b/test/parallel/test-buffer-equals.js @@ -12,5 +12,6 @@ assert.ok(b.equals(c)); assert.ok(!c.equals(d)); assert.ok(!d.equals(e)); assert.ok(d.equals(d)); +assert.ok(d.equals(new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65]))); assert.throws(() => Buffer.alloc(1).equals('abc')); diff --git a/test/parallel/test-buffer-indexof.js b/test/parallel/test-buffer-indexof.js index 746a272316..6aab628fe9 100644 --- a/test/parallel/test-buffer-indexof.js +++ b/test/parallel/test-buffer-indexof.js @@ -524,3 +524,11 @@ assert.equal(0, reallyLong.lastIndexOf(pattern)); assert.strictEqual(buf.indexOf(0xff), -1); assert.strictEqual(buf.indexOf(0xffff), -1); } + +// Test that Uint8Array arguments are okay. +{ + const needle = new Uint8Array([ 0x66, 0x6f, 0x6f ]); + const haystack = Buffer.from('a foo b foo'); + assert.strictEqual(haystack.indexOf(needle), 2); + assert.strictEqual(haystack.lastIndexOf(needle), haystack.length - 3); +} diff --git a/test/parallel/test-icu-transcode.js b/test/parallel/test-icu-transcode.js index c099e754ca..fc588b220f 100644 --- a/test/parallel/test-icu-transcode.js +++ b/test/parallel/test-icu-transcode.js @@ -40,7 +40,7 @@ for (const test in tests) { assert.throws( () => buffer.transcode(null, 'utf8', 'ascii'), - /^TypeError: "source" argument must be a Buffer$/ + /^TypeError: "source" argument must be a Buffer or Uint8Array$/ ); assert.throws( @@ -62,3 +62,11 @@ assert.deepStrictEqual( assert.deepStrictEqual( buffer.transcode(Buffer.from('hä', 'latin1'), 'latin1', 'utf16le'), Buffer.from('hä', 'utf16le')); + +// Test that Uint8Array arguments are okay. +{ + const uint8array = new Uint8Array([...Buffer.from('hä', 'latin1')]); + assert.deepStrictEqual( + buffer.transcode(uint8array, 'latin1', 'utf16le'), + Buffer.from('hä', 'utf16le')); +} |