buffer: add .read*() and .write*() methods to SlowBuffer prototype

Fixes #2138.
This commit is contained in:
Ben Noordhuis 2011-11-17 14:14:43 +01:00
parent 06d71ad457
commit 59a9a9b5b0
9 changed files with 101 additions and 48 deletions

View File

@ -1119,3 +1119,32 @@ Buffer.prototype.writeDoubleLE = function(value, offset, noAssert) {
Buffer.prototype.writeDoubleBE = function(value, offset, noAssert) { Buffer.prototype.writeDoubleBE = function(value, offset, noAssert) {
writeDouble(this, value, offset, true, noAssert); writeDouble(this, value, offset, true, noAssert);
}; };
SlowBuffer.prototype.readUInt8 = Buffer.prototype.readUInt8;
SlowBuffer.prototype.readUInt16LE = Buffer.prototype.readUInt16LE;
SlowBuffer.prototype.readUInt16BE = Buffer.prototype.readUInt16BE;
SlowBuffer.prototype.readUInt32LE = Buffer.prototype.readUInt32LE;
SlowBuffer.prototype.readUInt32BE = Buffer.prototype.readUInt32BE;
SlowBuffer.prototype.readInt8 = Buffer.prototype.readInt8;
SlowBuffer.prototype.readInt16LE = Buffer.prototype.readInt16LE;
SlowBuffer.prototype.readInt16BE = Buffer.prototype.readInt16BE;
SlowBuffer.prototype.readInt32LE = Buffer.prototype.readInt32LE;
SlowBuffer.prototype.readInt32BE = Buffer.prototype.readInt32BE;
SlowBuffer.prototype.readFloatLE = Buffer.prototype.readFloatLE;
SlowBuffer.prototype.readFloatBE = Buffer.prototype.readFloatBE;
SlowBuffer.prototype.readDoubleLE = Buffer.prototype.readDoubleLE;
SlowBuffer.prototype.readDoubleBE = Buffer.prototype.readDoubleBE;
SlowBuffer.prototype.writeUInt8 = Buffer.prototype.writeUInt8;
SlowBuffer.prototype.writeUInt16LE = Buffer.prototype.writeUInt16LE;
SlowBuffer.prototype.writeUInt16BE = Buffer.prototype.writeUInt16BE;
SlowBuffer.prototype.writeUInt32LE = Buffer.prototype.writeUInt32LE;
SlowBuffer.prototype.writeUInt32BE = Buffer.prototype.writeUInt32BE;
SlowBuffer.prototype.writeInt8 = Buffer.prototype.writeInt8;
SlowBuffer.prototype.writeInt16LE = Buffer.prototype.writeInt16LE;
SlowBuffer.prototype.writeInt16BE = Buffer.prototype.writeInt16BE;
SlowBuffer.prototype.writeInt32LE = Buffer.prototype.writeInt32LE;
SlowBuffer.prototype.writeInt32BE = Buffer.prototype.writeInt32BE;
SlowBuffer.prototype.writeFloatLE = Buffer.prototype.writeFloatLE;
SlowBuffer.prototype.writeFloatBE = Buffer.prototype.writeFloatBE;
SlowBuffer.prototype.writeDoubleLE = Buffer.prototype.writeDoubleLE;
SlowBuffer.prototype.writeDoubleBE = Buffer.prototype.writeDoubleBE;

View File

@ -1,13 +1,14 @@
/* /*
* Tests to verify we're reading in doubles correctly * Tests to verify we're reading in doubles correctly
*/ */
var SlowBuffer = process.binding('buffer').SlowBuffer;
var ASSERT = require('assert'); var ASSERT = require('assert');
/* /*
* Test (64 bit) double * Test (64 bit) double
*/ */
function test() { function test(clazz) {
var buffer = new Buffer(8); var buffer = new clazz(8);
buffer[0] = 0x55; buffer[0] = 0x55;
buffer[1] = 0x55; buffer[1] = 0x55;
@ -104,4 +105,5 @@ function test() {
} }
test(); test(Buffer);
test(SlowBuffer);

View File

@ -1,13 +1,14 @@
/* /*
* Tests to verify we're reading in floats correctly * Tests to verify we're reading in floats correctly
*/ */
var SlowBuffer = process.binding('buffer').SlowBuffer;
var ASSERT = require('assert'); var ASSERT = require('assert');
/* /*
* Test (32 bit) float * Test (32 bit) float
*/ */
function test() { function test(clazz) {
var buffer = new Buffer(4); var buffer = new clazz(4);
buffer[0] = 0; buffer[0] = 0;
buffer[1] = 0; buffer[1] = 0;
@ -66,4 +67,5 @@ function test() {
} }
test(); test(Buffer);
test(SlowBuffer);

View File

@ -1,13 +1,14 @@
/* /*
* Tests to verify we're reading in signed integers correctly * Tests to verify we're reading in signed integers correctly
*/ */
var SlowBuffer = process.binding('buffer').SlowBuffer;
var ASSERT = require('assert'); var ASSERT = require('assert');
/* /*
* Test 8 bit signed integers * Test 8 bit signed integers
*/ */
function test8() { function test8(clazz) {
var data = new Buffer(4); var data = new clazz(4);
data[0] = 0x23; data[0] = 0x23;
ASSERT.equal(0x23, data.readInt8(0)); ASSERT.equal(0x23, data.readInt8(0));
@ -26,8 +27,8 @@ function test8() {
} }
function test16() { function test16(clazz) {
var buffer = new Buffer(6); var buffer = new clazz(6);
buffer[0] = 0x16; buffer[0] = 0x16;
buffer[1] = 0x79; buffer[1] = 0x79;
@ -59,8 +60,8 @@ function test16() {
} }
function test32() { function test32(clazz) {
var buffer = new Buffer(6); var buffer = new clazz(6);
buffer[0] = 0x43; buffer[0] = 0x43;
buffer[1] = 0x53; buffer[1] = 0x53;
@ -91,6 +92,9 @@ function test32() {
} }
test8(); test8(Buffer);
test16(); test8(SlowBuffer);
test32(); test16(Buffer);
test16(SlowBuffer);
test32(Buffer);
test32(SlowBuffer);

View File

@ -2,6 +2,7 @@
* A battery of tests to help us read a series of uints * A battery of tests to help us read a series of uints
*/ */
var SlowBuffer = process.binding('buffer').SlowBuffer;
var ASSERT = require('assert'); var ASSERT = require('assert');
/* /*
@ -11,8 +12,8 @@ var ASSERT = require('assert');
* - Correctly using the offsets * - Correctly using the offsets
* - Correctly interpreting values that are beyond the signed range as unsigned * - Correctly interpreting values that are beyond the signed range as unsigned
*/ */
function test8() { function test8(clazz) {
var data = new Buffer(4); var data = new clazz(4);
data[0] = 23; data[0] = 23;
data[1] = 23; data[1] = 23;
@ -36,8 +37,8 @@ function test8() {
* - Correctly using the offsets * - Correctly using the offsets
* - Correctly interpreting values that are beyond the signed range as unsigned * - Correctly interpreting values that are beyond the signed range as unsigned
*/ */
function test16() { function test16(clazz) {
var data = new Buffer(4); var data = new clazz(4);
data[0] = 0; data[0] = 0;
data[1] = 0x23; data[1] = 0x23;
@ -64,8 +65,8 @@ function test16() {
* - Correctly using the offsets * - Correctly using the offsets
* - Correctly interpreting values that are beyond the signed range as unsigned * - Correctly interpreting values that are beyond the signed range as unsigned
*/ */
function test32() { function test32(clazz) {
var data = new Buffer(8); var data = new clazz(8);
data[0] = 0x32; data[0] = 0x32;
data[1] = 0x65; data[1] = 0x65;
@ -82,6 +83,9 @@ function test32() {
} }
test8(); test8(Buffer);
test16(); test8(SlowBuffer);
test32(); test16(Buffer);
test16(SlowBuffer);
test32(Buffer);
test32(SlowBuffer);

View File

@ -1,10 +1,11 @@
/* /*
* Tests to verify we're writing doubles correctly * Tests to verify we're writing doubles correctly
*/ */
var SlowBuffer = process.binding('buffer').SlowBuffer;
var ASSERT = require('assert'); var ASSERT = require('assert');
function test() { function test(clazz) {
var buffer = new Buffer(16); var buffer = new clazz(16);
buffer.writeDoubleBE(2.225073858507201e-308, 0); buffer.writeDoubleBE(2.225073858507201e-308, 0);
buffer.writeDoubleLE(2.225073858507201e-308, 8); buffer.writeDoubleLE(2.225073858507201e-308, 8);
@ -103,4 +104,5 @@ function test() {
} }
test(); test(Buffer);
test(SlowBuffer);

View File

@ -1,10 +1,11 @@
/* /*
* Tests to verify we're writing floats correctly * Tests to verify we're writing floats correctly
*/ */
var SlowBuffer = process.binding('buffer').SlowBuffer;
var ASSERT = require('assert'); var ASSERT = require('assert');
function test() { function test(clazz) {
var buffer = new Buffer(8); var buffer = new clazz(8);
buffer.writeFloatBE(1, 0); buffer.writeFloatBE(1, 0);
buffer.writeFloatLE(1, 4); buffer.writeFloatLE(1, 4);
@ -63,4 +64,5 @@ function test() {
} }
test(); test(Buffer);
test(SlowBuffer);

View File

@ -1,10 +1,11 @@
/* /*
* Tests to verify we're writing signed integers correctly * Tests to verify we're writing signed integers correctly
*/ */
var SlowBuffer = process.binding('buffer').SlowBuffer;
var ASSERT = require('assert'); var ASSERT = require('assert');
function test8() { function test8(clazz) {
var buffer = new Buffer(2); var buffer = new clazz(2);
buffer.writeInt8(0x23, 0); buffer.writeInt8(0x23, 0);
buffer.writeInt8(-5, 1); buffer.writeInt8(-5, 1);
@ -35,8 +36,8 @@ function test8() {
} }
function test16() { function test16(clazz) {
var buffer = new Buffer(6); var buffer = new clazz(6);
buffer.writeInt16BE(0x0023, 0); buffer.writeInt16BE(0x0023, 0);
buffer.writeInt16LE(0x0023, 2); buffer.writeInt16LE(0x0023, 2);
@ -88,8 +89,8 @@ function test16() {
} }
function test32() { function test32(clazz) {
var buffer = new Buffer(8); var buffer = new clazz(8);
buffer.writeInt32BE(0x23, 0); buffer.writeInt32BE(0x23, 0);
buffer.writeInt32LE(0x23, 4); buffer.writeInt32LE(0x23, 4);
@ -161,6 +162,9 @@ function test32() {
} }
test8(); test8(Buffer);
test16(); test8(SlowBuffer);
test32(); test16(Buffer);
test16(SlowBuffer);
test32(Buffer);
test32(SlowBuffer);

View File

@ -1,6 +1,7 @@
/* /*
* A battery of tests to help us read a series of uints * A battery of tests to help us read a series of uints
*/ */
var SlowBuffer = process.binding('buffer').SlowBuffer;
var ASSERT = require('assert'); var ASSERT = require('assert');
/* /*
@ -10,8 +11,8 @@ var ASSERT = require('assert');
* - Correctly using the offsets * - Correctly using the offsets
* - Correctly interpreting values that are beyond the signed range as unsigned * - Correctly interpreting values that are beyond the signed range as unsigned
*/ */
function test8() { function test8(clazz) {
var data = new Buffer(4); var data = new clazz(4);
data.writeUInt8(23, 0); data.writeUInt8(23, 0);
data.writeUInt8(23, 1); data.writeUInt8(23, 1);
@ -39,9 +40,9 @@ function test8() {
} }
function test16() { function test16(clazz) {
var value = 0x2343; var value = 0x2343;
var data = new Buffer(4); var data = new clazz(4);
data.writeUInt16BE(value, 0); data.writeUInt16BE(value, 0);
ASSERT.equal(0x23, data[0]); ASSERT.equal(0x23, data[0]);
@ -78,8 +79,8 @@ function test16() {
} }
function test32() { function test32(clazz) {
var data = new Buffer(6); var data = new clazz(6);
var value = 0xe7f90a6d; var value = 0xe7f90a6d;
data.writeUInt32BE(value, 0); data.writeUInt32BE(value, 0);
@ -120,6 +121,9 @@ function test32() {
} }
test8(); test8(Buffer);
test16(); test8(SlowBuffer);
test32(); test16(Buffer);
test16(SlowBuffer);
test32(Buffer);
test32(SlowBuffer);