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

16
test/simple/test-buffer.js

@ -297,16 +297,18 @@ assert.equal(dot.toString('base64'), '//4uAA==');
// Creating buffers larger than pool size. // Creating buffers larger than pool size.
l = Buffer.poolSize + 5;
s = "" s = ""
for (i = 0; i < Buffer.poolSize + 5; i++) { for (i = 0; i < l; i++) {
s += "h"; 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