Skip to content

Instantly share code, notes, and snippets.

@bnoordhuis
Created July 22, 2011 00:39
Show Gist options
  • Save bnoordhuis/766d99dced407ec214eb to your computer and use it in GitHub Desktop.
Save bnoordhuis/766d99dced407ec214eb to your computer and use it in GitHub Desktop.
diff --git a/test/simple/test-buffer.js b/test/simple/test-buffer.js
index 9cd4959..1fecbf5 100644
--- a/test/simple/test-buffer.js
+++ b/test/simple/test-buffer.js
@@ -24,9 +24,11 @@ var assert = require('assert');
var Buffer = require('buffer').Buffer;
+for (var i__ = 0; i__ < 5000; ++i__) {
+
var b = Buffer(1024); // safe constructor
-console.log('b.length == ' + b.length);
+//console.log('b.length == ' + b.length);
assert.strictEqual(1024, b.length);
b[0] = -1;
@@ -41,22 +43,22 @@ for (var i = 0; i < 1024; i++) {
}
var c = new Buffer(512);
-console.log('c.length == %d', c.length);
+//console.log('c.length == %d', c.length);
assert.strictEqual(512, c.length);
// copy 512 bytes, from 0 to 512.
var copied = b.copy(c, 0, 0, 512);
-console.log('copied ' + copied + ' bytes from b into c');
+//console.log('copied ' + copied + ' bytes from b into c');
assert.equal(512, copied);
for (var i = 0; i < c.length; i++) {
- common.print('.');
+// common.print('.');
assert.equal(i % 256, c[i]);
}
-console.log('');
+//console.log('');
// try to copy 513 bytes, and hope we don't overrun c, which is only 512 long
var copied = b.copy(c, 0, 0, 513);
-console.log('copied ' + copied + ' bytes from b into c');
+//console.log('copied ' + copied + ' bytes from b into c');
assert.strictEqual(512, copied);
for (var i = 0; i < c.length; i++) {
assert.equal(i % 256, c[i]);
@@ -64,7 +66,7 @@ for (var i = 0; i < c.length; i++) {
// copy all of c back into b, without specifying sourceEnd
var copied = c.copy(b, 0, 0);
-console.log('copied ' + copied + ' bytes from c back into b');
+//console.log('copied ' + copied + ' bytes from c back into b');
assert.strictEqual(512, copied);
for (var i = 0; i < b.length; i++) {
assert.equal(i % 256, b[i]);
@@ -72,7 +74,7 @@ for (var i = 0; i < b.length; i++) {
// copy 768 bytes from b into b
var copied = b.copy(b, 0, 256, 1024);
-console.log('copied ' + copied + ' bytes from b into c');
+//console.log('copied ' + copied + ' bytes from b into c');
assert.strictEqual(768, copied);
for (var i = 0; i < c.length; i++) {
assert.equal(i % 256, c[i]);
@@ -214,7 +216,7 @@ for (var j = 0; j < 100; j++) {
var testValue = '\u00F6\u65E5\u672C\u8A9E'; // ö日本語
var buffer = new Buffer(32);
var size = buffer.write(testValue, 0, 'utf8');
-console.log('bytes written to buffer: ' + size);
+//console.log('bytes written to buffer: ' + size);
var slice = buffer.toString('utf8', 0, size);
assert.equal(slice, testValue);
@@ -240,27 +242,27 @@ assert.equal(d[2], 255);
assert.deepEqual(d, new Buffer(d));
var e = new Buffer('über');
-console.error('uber: \'%s\'', e.toString());
+//console.error('uber: \'%s\'', e.toString());
assert.deepEqual(e, new Buffer([195, 188, 98, 101, 114]));
var f = new Buffer('über', 'ascii');
-console.error('f.length: %d (should be 4)', f.length);
+//console.error('f.length: %d (should be 4)', f.length);
assert.deepEqual(f, new Buffer([252, 98, 101, 114]));
var f = new Buffer('über', 'ucs2');
-console.error('f.length: %d (should be 8)', f.length);
+//console.error('f.length: %d (should be 8)', f.length);
assert.deepEqual(f, new Buffer([252, 0, 98, 0, 101, 0, 114, 0]));
var f = new Buffer('привет', 'ucs2');
-console.error('f.length: %d (should be 12)', f.length);
+//console.error('f.length: %d (should be 12)', f.length);
assert.deepEqual(f, new Buffer([63, 4, 64, 4, 56, 4, 50, 4, 53, 4, 66, 4]));
assert.equal(f.toString('ucs2'), 'привет');
var f = new Buffer([0, 0, 0, 0, 0]);
assert.equal(f.length, 5);
var size = f.write('あいうえお', 'ucs2');
-console.error('bytes written to buffer: %d (should be 4)', size);
-console.error('chars written to buffer: %d (should be 2)', Buffer._charsWritten);
+//console.error('bytes written to buffer: %d (should be 4)', size);
+//console.error('chars written to buffer: %d (should be 2)', Buffer._charsWritten);
assert.equal(size, 4);
assert.equal(Buffer._charsWritten, 2);
assert.deepEqual(f, new Buffer([0x42, 0x30, 0x44, 0x30, 0x00]));
@@ -453,7 +455,7 @@ assert.equal(12, Buffer.byteLength('Il était tué', 'binary'));
assert.equal(0, Buffer('hello').slice(0, 0).length);
// test hex toString
-console.log('Create hex string from buffer');
+//console.log('Create hex string from buffer');
var hexb = new Buffer(256);
for (var i = 0; i < 256; i ++) {
hexb[i] = i;
@@ -477,14 +479,14 @@ assert.equal(hexStr,
'e0e1e2e3e4e5e6e7e8e9eaebecedeeef'+
'f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff');
-console.log('Create buffer from hex string');
+//console.log('Create buffer from hex string');
var hexb2 = new Buffer(hexStr, 'hex');
for (var i = 0; i < 256; i ++) {
assert.equal(hexb2[i], hexb[i]);
}
// test an invalid slice end.
-console.log('Try to slice off the end of the buffer');
+//console.log('Try to slice off the end of the buffer');
var b = new Buffer([1,2,3,4,5]);
var b2 = b.toString('hex', 1, 10000);
var b3 = b.toString('hex', 1, 5);
@@ -509,12 +511,12 @@ function buildSlowBuffer (data) {
var x = buildSlowBuffer([0x81,0xa3,0x66,0x6f,0x6f,0xa3,0x62,0x61,0x72]);
-console.log(x.inspect())
+//console.log(x.inspect())
assert.equal('<SlowBuffer 81 a3 66 6f 6f a3 62 61 72>', x.inspect());
var z = x.slice(4);
-console.log(z.inspect())
-console.log(z.length)
+//console.log(z.inspect())
+//console.log(z.length)
assert.equal(5, z.length);
assert.equal(0x6f, z[0]);
assert.equal(0xa3, z[1]);
@@ -523,31 +525,31 @@ assert.equal(0x61, z[3]);
assert.equal(0x72, z[4]);
var z = x.slice(0);
-console.log(z.inspect())
-console.log(z.length)
+//console.log(z.inspect())
+//console.log(z.length)
assert.equal(z.length, x.length);
var z = x.slice(0, 4);
-console.log(z.inspect())
-console.log(z.length)
+//console.log(z.inspect())
+//console.log(z.length)
assert.equal(4, z.length);
assert.equal(0x81, z[0]);
assert.equal(0xa3, z[1]);
var z = x.slice(0, 9);
-console.log(z.inspect())
-console.log(z.length)
+//console.log(z.inspect())
+//console.log(z.length)
assert.equal(9, z.length);
var z = x.slice(1, 4);
-console.log(z.inspect())
-console.log(z.length)
+//console.log(z.inspect())
+//console.log(z.length)
assert.equal(3, z.length);
assert.equal(0xa3, z[0]);
var z = x.slice(2, 4);
-console.log(z.inspect())
-console.log(z.length)
+//console.log(z.inspect())
+//console.log(z.length)
assert.equal(2, z.length);
assert.equal(0x66, z[0]);
assert.equal(0x6f, z[1]);
@@ -621,3 +623,4 @@ assert.equal(written, 9);
written = buf.write('あいう\0'); // 3bytes * 3 + 1byte
assert.equal(written, 10);
+} // __i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment