From 3e9f636b648e334d7d25fa848f89df024ed563b6 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Tue, 7 Sep 2010 22:00:16 -0700 Subject: [PATCH] Improve long buffer test --- lib/buffer.js | 18 ++++++++---------- test/simple/test-buffer.js | 16 +++++++++------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 6eabc6f0b5..571eb5ba86 100644 --- a/lib/buffer.js +++ b/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 { diff --git a/test/simple/test-buffer.js b/test/simple/test-buffer.js index abe4e15e97..99fb8bc43e 100644 --- a/test/simple/test-buffer.js +++ b/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);