diff options
author | James M Snell <jasnell@gmail.com> | 2016-08-23 14:16:45 -0700 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2016-08-26 07:15:31 -0700 |
commit | 7053922c1a76ac56ef245923ec171b01cd90d2f4 (patch) | |
tree | 0464984232c835fe6e1d3ac2b429bdaaf77a6cbe /test/parallel/test-buffer-copy.js | |
parent | b3e7ac2605c18c041293cd3ab892c963e1145176 (diff) | |
download | android-node-v8-7053922c1a76ac56ef245923ec171b01cd90d2f4.tar.gz android-node-v8-7053922c1a76ac56ef245923ec171b01cd90d2f4.tar.bz2 android-node-v8-7053922c1a76ac56ef245923ec171b01cd90d2f4.zip |
test: clean up / refactor buffer tests, remove duplication
Remove duplication of buffer tests, separate out into separate
files, update and cleanup code, move to using strictEqual where
possible.
PR-URL: https://github.com/nodejs/node/pull/8256
Reviewed-By: Michaƫl Zasso <mic.besace@gmail.com>
Diffstat (limited to 'test/parallel/test-buffer-copy.js')
-rw-r--r-- | test/parallel/test-buffer-copy.js | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/test/parallel/test-buffer-copy.js b/test/parallel/test-buffer-copy.js new file mode 100644 index 0000000000..e5721db0fb --- /dev/null +++ b/test/parallel/test-buffer-copy.js @@ -0,0 +1,120 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); + +var b = Buffer.allocUnsafe(1024); +var c = Buffer.allocUnsafe(512); +var cntr = 0; + +{ + // copy 512 bytes, from 0 to 512. + b.fill(++cntr); + c.fill(++cntr); + const copied = b.copy(c, 0, 0, 512); + assert.strictEqual(512, copied); + for (let i = 0; i < c.length; i++) { + assert.strictEqual(b[i], c[i]); + } +} + +{ + // copy c into b, without specifying sourceEnd + b.fill(++cntr); + c.fill(++cntr); + const copied = c.copy(b, 0, 0); + assert.strictEqual(c.length, copied); + for (let i = 0; i < c.length; i++) { + assert.strictEqual(c[i], b[i]); + } +} + +{ + // copy c into b, without specifying sourceStart + b.fill(++cntr); + c.fill(++cntr); + const copied = c.copy(b, 0); + assert.strictEqual(c.length, copied); + for (let i = 0; i < c.length; i++) { + assert.strictEqual(c[i], b[i]); + } +} + +{ + // copy longer buffer b to shorter c without targetStart + b.fill(++cntr); + c.fill(++cntr); + const copied = b.copy(c); + assert.strictEqual(c.length, copied); + for (let i = 0; i < c.length; i++) { + assert.strictEqual(b[i], c[i]); + } +} + +{ + // copy starting near end of b to c + b.fill(++cntr); + c.fill(++cntr); + const copied = b.copy(c, 0, b.length - Math.floor(c.length / 2)); + assert.strictEqual(Math.floor(c.length / 2), copied); + for (let i = 0; i < Math.floor(c.length / 2); i++) { + assert.strictEqual(b[b.length - Math.floor(c.length / 2) + i], c[i]); + } + for (let i = Math.floor(c.length / 2) + 1; i < c.length; i++) { + assert.strictEqual(c[c.length - 1], c[i]); + } +} + +{ + // try to copy 513 bytes, and check we don't overrun c + b.fill(++cntr); + c.fill(++cntr); + const copied = b.copy(c, 0, 0, 513); + assert.strictEqual(c.length, copied); + for (let i = 0; i < c.length; i++) { + assert.strictEqual(b[i], c[i]); + } +} + +{ + // copy 768 bytes from b into b + b.fill(++cntr); + b.fill(++cntr, 256); + const copied = b.copy(b, 0, 256, 1024); + assert.strictEqual(768, copied); + for (let i = 0; i < b.length; i++) { + assert.strictEqual(cntr, b[i]); + } +} + +// copy string longer than buffer length (failure will segfault) +var bb = Buffer.allocUnsafe(10); +bb.fill('hello crazy world'); + + +// try to copy from before the beginning of b +assert.doesNotThrow(() => { b.copy(c, 0, 100, 10); }); + +// copy throws at negative sourceStart +assert.throws(function() { + Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), 0, -1); +}, RangeError); + +{ + // check sourceEnd resets to targetEnd if former is greater than the latter + b.fill(++cntr); + c.fill(++cntr); + b.copy(c, 0, 0, 1025); + for (let i = 0; i < c.length; i++) { + assert.strictEqual(b[i], c[i]); + } +} + +// throw with negative sourceEnd +assert.throws(() => b.copy(c, 0, 0, -1), RangeError); + +// when sourceStart is greater than sourceEnd, zero copied +assert.strictEqual(b.copy(c, 0, 100, 10), 0); + +// when targetStart > targetLength, zero copied +assert.strictEqual(b.copy(c, 512, 0, 10), 0); |