Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: separate out buffer copy tests
  • Loading branch information
jasnell committed Aug 24, 2016
commit 253e77c660c20674f57db30fb901ba09b46809fb
126 changes: 0 additions & 126 deletions test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ var assert = require('assert');

var Buffer = require('buffer').Buffer;

// counter to ensure unique value is always copied
var cntr = 0;

var b = Buffer.allocUnsafe(1024);

console.log('b.length == %d', b.length);
Expand Down Expand Up @@ -36,129 +33,6 @@ for (const [index, value] of e.entries()) {
assert.strictEqual(value, ui32[index]);
}

{
// copy 512 bytes, from 0 to 512.
b.fill(++cntr);
c.fill(++cntr);
const copied = b.copy(c, 0, 0, 512);
console.log('copied %d bytes from b into c', copied);
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);
console.log('copied %d bytes from c into b w/o sourceEnd', copied);
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);
console.log('copied %d bytes from c into b w/o sourceStart', copied);
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);
console.log('copied %d bytes from b into c w/o targetStart', copied);
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));
console.log('copied %d bytes from end of b into beginning of c', copied);
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);
console.log('copied %d bytes from b trying to overrun c', copied);
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);
console.log('copied %d bytes from b into b', copied);
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);
const copied = b.copy(c, 0, 0, 1025);
console.log('copied %d bytes from b into c', copied);
for (let i = 0; i < c.length; i++) {
assert.strictEqual(b[i], c[i]);
}
}

// throw with negative sourceEnd
console.log('test copy at negative sourceEnd');
assert.throws(function() {
b.copy(c, 0, 0, -1);
}, RangeError);

// when sourceStart is greater than sourceEnd, zero copied
assert.equal(b.copy(c, 0, 100, 10), 0);

// when targetStart > targetLength, zero copied
assert.equal(b.copy(c, 512, 0, 10), 0);

var caught_error;

// invalid encoding for Buffer.toString
Expand Down
120 changes: 120 additions & 0 deletions test/parallel/test-buffer-copy.js
Original file line number Diff line number Diff line change
@@ -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);
Loading