Browse Source

Improve long buffer test

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
3e9f636b64
  1. 18
      lib/buffer.js
  2. 16
      test/simple/test-buffer.js

18
lib/buffer.js

@ -86,7 +86,7 @@ function Buffer (subject, encoding, offset) {
return new Buffer(subject, encoding, offset);
}
var length, type;
var type;
// Are we slicing?
if (typeof offset === 'number') {
@ -97,40 +97,38 @@ function Buffer (subject, encoding, offset) {
// Find the length
switch (type = typeof subject) {
case 'number':
length = subject;
this.length = subject;
break;
case 'string':
length = Buffer.byteLength(subject, encoding);
this.length = Buffer.byteLength(subject, encoding);
break;
case 'object': // Assume object is an array
length = subject.length;
this.length = subject.length;
break;
default:
throw new Error("First argument need to be an number, array or string.");
}
this.length = length;
if (length > Buffer.poolSize) {
if (this.length > Buffer.poolSize) {
// Big buffer, just alloc one.
this.parent = new SlowBuffer(subject, encoding);
this.offset = 0;
} else {
// Small buffer.
if (!pool || pool.length - pool.used < length) allocPool();
if (!pool || pool.length - pool.used < this.length) allocPool();
this.parent = pool;
this.offset = pool.used;
pool.used += length;
pool.used += this.length;
// Do we need to write stuff?
if (type !== 'number') {
// Assume object is an array
if (type === 'object') {
for (var i = 0; i < length; i++) {
for (var i = 0; i < this.length; i++) {
this.parent[i + this.offset] = subject[i];
}
} else {

16
test/simple/test-buffer.js

@ -297,16 +297,18 @@ assert.equal(dot.toString('base64'), '//4uAA==');
// Creating buffers larger than pool size.
l = Buffer.poolSize + 5;
s = ""
for (i = 0; i < Buffer.poolSize + 5; i++) {
for (i = 0; i < l; i++) {
s += "h";
}
b = Buffer(s);
sb = b.toString();
assert.equal(sb, s);
b = Buffer(s);
for (i = 0; l; i++) {
assert.equal("h".charCodeAt(i), b[i], "index " + i + " is not 'h' it was " + b[i]);
}
sb = b.toString();
assert.equal(sb.length, s.length);
assert.equal(sb, s);

Loading…
Cancel
Save