Browse Source

Endian argument should be a boolean. Signed integers shouldn't run through checks for unsigned integers. Clean up jslint. Provide unchecked uint entry points.

v0.7.4-release
Robert Mustacchi 14 years ago
committed by Ryan Dahl
parent
commit
0df08c6a0c
  1. 154
      doc/api/buffers.markdown
  2. 363
      lib/buffer.js
  3. 4
      lib/buffer_ieee754.js
  4. 60
      test/simple/test-readdouble.js
  5. 36
      test/simple/test-readfloat.js
  6. 72
      test/simple/test-readint.js
  7. 48
      test/simple/test-readuint.js
  8. 20
      test/simple/test-writedouble.js
  9. 20
      test/simple/test-writefloat.js
  10. 36
      test/simple/test-writeint.js
  11. 48
      test/simple/test-writeuint.js

154
doc/api/buffers.markdown

@ -172,11 +172,11 @@ from the original Buffer.
// abc
// !bc
### buffer.readUInt8(offset, endian)
### buffer.readUInt8(offset, bigEndian)
Reads an unsigned 8 bit integer from the buffer at the specified offset. Endian
must be either 'big' or 'little' and specifies what endian ordering to read the
bytes from the buffer in.
Reads an unsigned 8 bit integer from the buffer at the specified offset. If
`bigEndian` is true, reads bytes in a big endian format, otherwise reads them as
little endian.
Example:
@ -188,8 +188,8 @@ Example:
buf[3] = 0x42;
for (ii = 0; ii < buf.length; ii++) {
console.log(buf.readUInt8(ii, 'big'));
console.log(buf.readUInt8(ii, 'little'));
console.log(buf.readUInt8(ii, true));
console.log(buf.readUInt8(ii, false));
}
// 0x3
@ -201,11 +201,11 @@ Example:
// 0x42
// 0x42
### buffer.readUInt16(offset, endian)
### buffer.readUInt16(offset, bigEndian)
Reads an unsigned 16 bit integer from the buffer at the specified offset. Endian
must be either 'big' or 'little' and specifies what endian ordering to read the
bytes from the buffer in.
Reads an unsigned 16 bit integer from the buffer at the specified offset. If
`bigEndian` is true, reads bytes in a big endian format, otherwise reads them as
little endian.
Example:
@ -216,12 +216,12 @@ Example:
buf[2] = 0x23;
buf[3] = 0x42;
console.log(buf.readUInt16(0, 'big'));
console.log(buf.readUInt16(0, 'little'));
console.log(buf.readUInt16(1, 'big'));
console.log(buf.readUInt16(1, 'little'));
console.log(buf.readUInt16(2, 'big'));
console.log(buf.readUInt16(2, 'little'));
console.log(buf.readUInt16(0, true));
console.log(buf.readUInt16(0, false));
console.log(buf.readUInt16(1, true));
console.log(buf.readUInt16(1, false));
console.log(buf.readUInt16(2, true));
console.log(buf.readUInt16(2, false));
// 0x0304
// 0x0403
@ -230,11 +230,12 @@ Example:
// 0x2342
// 0x4223
### buffer.readUInt32(offset, endian)
### buffer.readUInt32(offset, bigEndian)
Reads an unsigned 32 bit integer from the buffer at the specified offset. If
`bigEndian` is true, reads bytes in a big endian format, otherwise reads them as
little endian.
Reads an unsigned 32 bit integer from the buffer at the specified offset. Endian
must be either 'big' or 'little' and specifies what endian ordering to read the
bytes from the buffer in.
Example:
@ -245,44 +246,43 @@ Example:
buf[2] = 0x23;
buf[3] = 0x42;
console.log(buf.readUInt32(0, 'big'));
console.log(buf.readUInt32(0, 'little'));
console.log(buf.readUInt32(0, true));
console.log(buf.readUInt32(0, false));
// 0x03042342
// 0x42230403
### buffer.readInt8(offset, endian)
### buffer.readInt8(offset, bigEndian)
Reads a signed 8 bit integer from the buffer at the specified offset. Endian
must be either 'big' or 'little' and specifies what endian ordering to read the
bytes from the buffer in.
Reads a signed 8 bit integer from the buffer at the specified offset. If
`bigEndian` is true, reads bytes in a big endian format, otherwise reads them as
little endian.
Works as `buffer.readUInt8`, except buffer contents are treated as twos
complement signed values.
### buffer.readInt16(offset, endian)
### buffer.readInt16(offset, bigEndian)
Reads a signed 16 bit integer from the buffer at the specified offset. Endian
must be either 'big' or 'little' and specifies what endian ordering to read the
bytes from the buffer in.
Reads a signed 16 bit integer from the buffer at the specified offset. If
`bigEndian` is true, reads bytes in a big endian format, otherwise reads them as
little endian.
Works as `buffer.readUInt16`, except buffer contents are treated as twos
complement signed values.
### buffer.readInt32(offset, endian)
### buffer.readInt32(offset, bigEndian)
Reads a signed 32 bit integer from the buffer at the specified offset. Endian
must be either 'big' or 'little' and specifies what endian ordering to read the
bytes from the buffer in.
Reads a signed 32 bit integer from the buffer at the specified offset. If
`bigEndian` is true, reads bytes in a big endian format, otherwise reads them as
little endian.
Works as `buffer.readUInt32`, except buffer contents are treated as twos
complement signed values.
### buffer.readFloat(offset, endian)
### buffer.readFloat(offset, bigEndian)
Reads a 32 bit float from the buffer at the specified offset.
Reads a 32 bit float from the buffer at the specified offset. Endian must be
either 'big' or 'little' and specifies what endian ordering to read the bytes
from the buffer in.
Example:
@ -293,14 +293,14 @@ Example:
buf[2] = 0x80;
buf[3] = 0x3f;
console.log(buf.readFloat(0, 'little'));
console.log(buf.readFloat(0, false));
// 0x01
### buffer.readDouble(offset, endian)
### buffer.readDouble(offset, bigEndian)
Reads a 64 bit double from the buffer at the specified offset. Endian must be
either 'big' or 'little' and specifies what endian ordering to read the bytes
either true or false and specifies what endian ordering to read the bytes
from the buffer in.
Example:
@ -316,11 +316,11 @@ Example:
buf[6] = 0xd5;
buf[7] = 0x3f;
console.log(buf.readDouble(0, 'little'));
console.log(buf.readDouble(0, false));
// 0.3333333333333333
### buffer.writeUInt8(value, offset, endian)
### buffer.writeUInt8(value, offset, bigEndian)
Writes `value` to the buffer at the specified offset with specified endian
format. Note, `value` must be a valid 8 bit unsigned integer.
@ -328,24 +328,24 @@ format. Note, `value` must be a valid 8 bit unsigned integer.
Example:
var buf = new Buffer(4);
buf.writeUInt8(0x3, 0, 'big');
buf.writeUInt8(0x4, 1, 'big');
buf.writeUInt8(0x23, 2, 'big');
buf.writeUInt8(0x42, 3, 'big');
buf.writeUInt8(0x3, 0, true);
buf.writeUInt8(0x4, 1, true);
buf.writeUInt8(0x23, 2, true);
buf.writeUInt8(0x42, 3, true);
console.log(buf);
buf.writeUInt8(0x3, 0, 'little');
buf.writeUInt8(0x4, 1, 'little');
buf.writeUInt8(0x23, 2, 'little');
buf.writeUInt8(0x42, 3, 'little');
buf.writeUInt8(0x3, 0, false);
buf.writeUInt8(0x4, 1, false);
buf.writeUInt8(0x23, 2, false);
buf.writeUInt8(0x42, 3, false);
console.log(buf);
// <Buffer 03 04 23 42>
// <Buffer 03 04 23 42>
### buffer.writeUInt16(value, offset, endian)
### buffer.writeUInt16(value, offset, bigEndian)
Writes `value` to the buffer at the specified offset with specified endian
format. Note, `value` must be a valid 16 bit unsigned integer.
@ -353,20 +353,20 @@ format. Note, `value` must be a valid 16 bit unsigned integer.
Example:
var buf = new Buffer(4);
buf.writeUInt16(0xdead, 0, 'big');
buf.writeUInt16(0xbeef, 2, 'big');
buf.writeUInt16(0xdead, 0, true);
buf.writeUInt16(0xbeef, 2, true);
console.log(buf);
buf.writeUInt16(0xdead, 0, 'little');
buf.writeUInt16(0xbeef, 2, 'little');
buf.writeUInt16(0xdead, 0, false);
buf.writeUInt16(0xbeef, 2, false);
console.log(buf);
// <Buffer de ad be ef>
// <Buffer ad de ef be>
### buffer.writeUInt32(value, offset, endian)
### buffer.writeUInt32(value, offset, bigEndian)
Writes `value` to the buffer at the specified offset with specified endian
format. Note, `value` must be a valid 32 bit unsigned integer.
@ -374,18 +374,18 @@ format. Note, `value` must be a valid 32 bit unsigned integer.
Example:
var buf = new Buffer(4);
buf.writeUInt32(0xfeedface, 0, 'big');
buf.writeUInt32(0xfeedface, 0, true);
console.log(buf);
buf.writeUInt32(0xfeedface, 0, 'little');
buf.writeUInt32(0xfeedface, 0, false);
console.log(buf);
// <Buffer fe ed fa ce>
// <Buffer ce fa ed fe>
### buffer.writeInt8(value, offset, endian)
### buffer.writeInt8(value, offset, bigEndian)
Writes `value` to the buffer at the specified offset with specified endian
format. Note, `value` must be a valid 16 bit signed integer.
@ -393,7 +393,7 @@ format. Note, `value` must be a valid 16 bit signed integer.
Works as `buffer.writeUInt8`, except value is written out as a two's complement
signed integer into `buffer`.
### buffer.writeInt16(value, offset, endian)
### buffer.writeInt16(value, offset, bigEndian)
Writes `value` to the buffer at the specified offset with specified endian
format. Note, `value` must be a valid 16 bit unsigned integer.
@ -401,15 +401,15 @@ format. Note, `value` must be a valid 16 bit unsigned integer.
Works as `buffer.writeUInt16`, except value is written out as a two's complement
signed integer into `buffer`.
### buffer.writeInt32(value, offset, endian)
### buffer.writeInt32(value, offset, bigEndian)
Writes `value` to the buffer at the specified offset with specified endian
format. Note, `value` must be a valid 16 bit signed integer.
format. Note, `value` must be a valid 32 bit signed integer.
Works as `buffer.writeUInt832, except value is written out as a two's complement
Works as `buffer.writeUInt32`, except value is written out as a two's complement
signed integer into `buffer`.
### buffer.writeFloat(value, offset, endian)
### buffer.writeFloat(value, offset, bigEndian)
Writes `value` to the buffer at the specified offset with specified endian
format. Note, `value` must be a valid 32 bit float.
@ -417,18 +417,18 @@ format. Note, `value` must be a valid 32 bit float.
Example:
var buf = new Buffer(4);
buf.writeFloat(0xcafebabe, 0, 'big');
buf.writeFloat(0xcafebabe, 0, true);
console.log(buf);
buf.writeFloat(0xcafebabe, 0, 'little');
buf.writeFloat(0xcafebabe, 0, false);
console.log(buf);
// <Buffer 4f 4a fe bb>
// <Buffer bb fe 4a 4f>
### buffer.writeDouble(value, offset, endian)
### buffer.writeDouble(value, offset, bigEndian)
Writes `value` to the buffer at the specified offset with specified endian
format. Note, `value` must be a valid 64 bit double.
@ -436,17 +436,31 @@ format. Note, `value` must be a valid 64 bit double.
Example:
var buf = new Buffer(8);
buf.writeFloat(0xdeadbeefcafebabe, 0, 'big');
buf.writeFloat(0xdeadbeefcafebabe, 0, true);
console.log(buf);
buf.writeFloat(0xdeadbeefcafebabe, 0, 'little');
buf.writeFloat(0xdeadbeefcafebabe, 0, false);
console.log(buf);
// <Buffer 43 eb d5 b7 dd f9 5f d7>
// <Buffer d7 5f f9 dd b7 d5 eb 43>
### buffer.readUInt8NoChk(value, offset, bigEndian)
### buffer.readUInt16NoChk(value, offset, bigEndian)
### buffer.readUInt32NoChk(value, offset, bigEndian)
### buffer.writeUInt8NoChk(value, offset, bigEndian)
### buffer.writeUInt16NoChk(value, offset, bigEndian)
### buffer.writeUInt32NoChk(value, offset, bigEndian)
These functions all work as per the versions without the NoChk suffix. These
functions allow you to do use the raw functionality without any kind of
validation for correctness. This means that value may be too large for the
specific function and offset may be beyond the end of the buffer leading to the
values being silently dropped. These should not be used unless you are certain
of correctness.
### buffer.fill(value, offset=0, length=-1)

363
lib/buffer.js

@ -415,30 +415,30 @@ Buffer.byteLength = SlowBuffer.byteLength;
// fill(value, start=0, end=buffer.length)
Buffer.prototype.fill = function fill (value, start, end) {
Buffer.prototype.fill = function fill(value, start, end) {
value || (value = 0);
start || (start = 0);
end || (end = this.length);
end || (end = this.length);
if (typeof value === "string") {
if (typeof value === 'string') {
value = value.charCodeAt(0);
}
if (!(typeof value === "number") || isNaN(value)) {
throw new Error("value is not a number");
if (!(typeof value === 'number') || isNaN(value)) {
throw new Error('value is not a number');
}
if (end < start) throw new Error("end < start");
if (end < start) throw new Error('end < start');
// Fill 0 bytes; we're done
if (end === start) return 0;
if (this.length == 0) return 0;
if (start < 0 || start >= this.length) {
throw new Error("start out of bounds");
throw new Error('start out of bounds');
}
if (end < 0 || end > this.length) {
throw new Error("end out of bounds");
throw new Error('end out of bounds');
}
return this.parent.fill(value,
@ -524,42 +524,28 @@ Buffer.prototype.asciiWrite = function(string, offset) {
return this.write(string, offset, 'ascii');
};
Buffer.prototype.readUInt8 = function(offset, endian) {
Buffer.prototype.readUInt8NoChk = function(offset, bigEndian) {
var buffer = this;
return buffer[offset];
};
assert.ok(endian !== undefined && endian !== null,
'missing endian');
assert.ok(endian == 'big' || endian == 'little',
'bad endian value');
Buffer.prototype.readUInt8 = function(offset, bigEndian) {
var buffer = this;
assert.ok(offset !== undefined && offset !== null,
'missing offset');
'missing offset');
assert.ok(offset < buffer.length,
'Trying to read beyond buffer length');
'Trying to read beyond buffer length');
return buffer[offset];
return buffer.readUInt8NoChk(offset, bigEndian);
};
Buffer.prototype.readUInt16 = function(offset, endian) {
Buffer.prototype.readUInt16NoChk = function(offset, bigEndian) {
var val = 0;
var buffer = this;
assert.ok(endian !== undefined && endian !== null,
'missing endian');
assert.ok(endian == 'big' || endian == 'little',
'bad endian value');
assert.ok(offset !== undefined && offset !== null,
'missing offset');
assert.ok(offset + 1 < buffer.length,
'Trying to read beyond buffer length');
if (endian == 'big') {
if (bigEndian) {
val = buffer[offset] << 8;
val |= buffer[offset + 1];
} else {
@ -570,24 +556,26 @@ Buffer.prototype.readUInt16 = function(offset, endian) {
return val;
};
Buffer.prototype.readUInt32 = function(offset, endian) {
var val = 0;
Buffer.prototype.readUInt16 = function(offset, bigEndian) {
var buffer = this;
assert.ok(endian !== undefined && endian !== null,
'missing endian');
assert.ok(endian == 'big' || endian == 'little',
'bad endian value');
assert.ok(typeof (bigEndian) === 'boolean',
'missing or invalid endian');
assert.ok(offset !== undefined && offset !== null,
'missing offset');
'missing offset');
assert.ok(offset + 3 < buffer.length,
'Trying to read beyond buffer length');
assert.ok(offset + 1 < buffer.length,
'Trying to read beyond buffer length');
return buffer.readUInt16NoChk(offset, bigEndian);
};
Buffer.prototype.readUInt32NoChk = function(offset, bigEndian) {
var val = 0;
var buffer = this;
if (endian == 'big') {
if (bigEndian) {
val = buffer[offset + 1] << 16;
val |= buffer[offset + 2] << 8;
val |= buffer[offset + 3];
@ -602,6 +590,22 @@ Buffer.prototype.readUInt32 = function(offset, endian) {
return val;
};
Buffer.prototype.readUInt32 = function(offset, bigEndian) {
var val = 0;
var buffer = this;
assert.ok(typeof (bigEndian) === 'boolean',
'missing or invalid endian');
assert.ok(offset !== undefined && offset !== null,
'missing offset');
assert.ok(offset + 3 < buffer.length,
'Trying to read beyond buffer length');
return buffer.readUInt32NoChk(offset, bigEndian);
};
/*
* Signed integer types, yay team! A reminder on how two's complement actually
@ -648,21 +652,15 @@ Buffer.prototype.readUInt32 = function(offset, endian) {
* (0x007f + 1) * -1
* (0x0080) * -1
*/
Buffer.prototype.readInt8 = function(offset, endian) {
Buffer.prototype.readInt8 = function(offset, bigEndian) {
var buffer = this;
var neg;
assert.ok(endian !== undefined && endian !== null,
'missing endian');
assert.ok(endian == 'big' || endian == 'little',
'bad endian value');
assert.ok(offset !== undefined && offset !== null,
'missing offset');
'missing offset');
assert.ok(offset < buffer.length,
'Trying to read beyond buffer length');
'Trying to read beyond buffer length');
neg = buffer[offset] & 0x80;
if (!neg) {
@ -673,23 +671,20 @@ Buffer.prototype.readInt8 = function(offset, endian) {
};
Buffer.prototype.readInt16 = function(offset, endian) {
Buffer.prototype.readInt16 = function(offset, bigEndian) {
var buffer = this;
var neg;
assert.ok(endian !== undefined && endian !== null,
'missing endian');
assert.ok(endian == 'big' || endian == 'little',
'bad endian value');
assert.ok(typeof (bigEndian) === 'boolean',
'missing or invalid endian');
assert.ok(offset !== undefined && offset !== null,
'missing offset');
'missing offset');
assert.ok(offset + 1 < buffer.length,
'Trying to read beyond buffer length');
'Trying to read beyond buffer length');
val = buffer.readUInt16(offset, endian);
val = buffer.readUInt16NoChk(offset, bigEndian);
neg = val & 0x8000;
if (!neg) {
return val;
@ -699,23 +694,20 @@ Buffer.prototype.readInt16 = function(offset, endian) {
};
Buffer.prototype.readInt32 = function(offset, endian) {
Buffer.prototype.readInt32 = function(offset, bigEndian) {
var buffer = this;
var neg;
assert.ok(endian !== undefined && endian !== null,
'missing endian');
assert.ok(endian == 'big' || endian == 'little',
'bad endian value');
assert.ok(typeof (bigEndian) === 'boolean',
'missing or invalid endian');
assert.ok(offset !== undefined && offset !== null,
'missing offset');
'missing offset');
assert.ok(offset + 3 < buffer.length,
'Trying to read beyond buffer length');
'Trying to read beyond buffer length');
val = buffer.readUInt32(offset, endian);
val = buffer.readUInt32NoChk(offset, bigEndian);
neg = val & 0x80000000;
if (!neg) {
return (val);
@ -725,40 +717,30 @@ Buffer.prototype.readInt32 = function(offset, endian) {
};
Buffer.prototype.readFloat = function(offset, endian) {
Buffer.prototype.readFloat = function(offset, bigEndian) {
var buffer = this;
assert.ok(endian !== undefined && endian !== null,
'missing endian');
assert.ok(endian == 'big' || endian == 'little',
'bad endian value');
assert.ok(offset !== undefined && offset !== null,
'missing offset');
assert.ok(typeof (bigEndian) === 'boolean',
'missing or invalid endian');
assert.ok(offset + 3 < buffer.length,
'Trying to read beyond buffer length');
'Trying to read beyond buffer length');
return require('buffer_ieee754').readIEEE754(buffer, offset, endian, 23, 4);
return require('buffer_ieee754').readIEEE754(buffer, offset, bigEndian,
23, 4);
};
Buffer.prototype.readDouble = function(offset, endian) {
Buffer.prototype.readDouble = function(offset, bigEndian) {
var buffer = this;
assert.ok(endian !== undefined && endian !== null,
'missing endian');
assert.ok(endian == 'big' || endian == 'little',
'bad endian value');
assert.ok(offset !== undefined && offset !== null,
'missing offset');
assert.ok(typeof (bigEndian) === 'boolean',
'missing or invalid endian');
assert.ok(offset + 7 < buffer.length,
'Trying to read beyond buffer length');
'Trying to read beyond buffer length');
return require('buffer_ieee754').readIEEE754(buffer, offset, endian, 52, 8);
return require('buffer_ieee754').readIEEE754(buffer, offset, bigEndian,
52, 8);
};
@ -773,90 +755,78 @@ Buffer.prototype.readDouble = function(offset, endian) {
*/
function verifuint(value, max) {
assert.ok(typeof (value) == 'number',
'cannot write a non-number as a number');
'cannot write a non-number as a number');
assert.ok(value >= 0,
'specified a negative value for writing an unsigned value');
'specified a negative value for writing an unsigned value');
assert.ok(value <= max, 'value is larger than maximum value for type');
assert.ok(Math.floor(value) === value, 'value has a fractional component');
}
Buffer.prototype.writeUInt8NoChk = function(value, offset, bigEndian) {
var buffer = this;
buffer[offset] = value;
};
Buffer.prototype.writeUInt8 = function(value, offset, endian) {
Buffer.prototype.writeUInt8 = function(value, offset, bigEndian) {
var buffer = this;
assert.ok(value !== undefined && value !== null,
'missing value');
assert.ok(endian !== undefined && endian !== null,
'missing endian');
'missing value');
assert.ok(endian == 'big' || endian == 'little',
'bad endian value');
assert.ok(typeof (bigEndian) === 'boolean',
'missing or invalid endian');
assert.ok(offset !== undefined && offset !== null,
'missing offset');
'missing offset');
assert.ok(offset < buffer.length,
'trying to write beyond buffer length');
'trying to write beyond buffer length');
verifuint(value, 0xff);
buffer[offset] = value;
};
buffer.writeUInt8NoChk(value, offset, bigEndian);
};
Buffer.prototype.writeUInt16 = function(value, offset, endian) {
Buffer.prototype.writeUInt16NoChk = function(value, offset, bigEndian) {
var buffer = this;
assert.ok(value !== undefined && value !== null,
'missing value');
assert.ok(endian !== undefined && endian !== null,
'missing endian');
assert.ok(endian == 'big' || endian == 'little',
'bad endian value');
assert.ok(offset !== undefined && offset !== null,
'missing offset');
assert.ok(offset + 1 < buffer.length,
'trying to write beyond buffer length');
verifuint(value, 0xffff);
if (endian == 'big') {
if (bigEndian) {
buffer[offset] = (value & 0xff00) >>> 8;
buffer[offset + 1] = value & 0x00ff;
} else {
buffer[offset + 1] = (value & 0xff00) >>> 8;
buffer[offset] = value & 0x00ff;
}
};
};
Buffer.prototype.writeUInt32 = function(value, offset, endian) {
Buffer.prototype.writeUInt16 = function(value, offset, bigEndian) {
var buffer = this;
assert.ok(value !== undefined && value !== null,
'missing value');
assert.ok(endian !== undefined && endian !== null,
'missing endian');
'missing value');
assert.ok(endian == 'big' || endian == 'little',
'bad endian value');
assert.ok(typeof (bigEndian) === 'boolean',
'missing or invalid endian');
assert.ok(offset !== undefined && offset !== null,
'missing offset');
'missing offset');
assert.ok(offset + 3 < buffer.length,
'trying to write beyond buffer length');
assert.ok(offset + 1 < buffer.length,
'trying to write beyond buffer length');
verifuint(value, 0xffffffff);
if (endian == 'big') {
verifuint(value, 0xffff);
buffer.writeUInt16NoChk(value, offset, bigEndian);
};
Buffer.prototype.writeUInt32NoChk = function(value, offset, bigEndian) {
var buffer = this;
if (bigEndian) {
buffer[offset] = (value >>> 24) & 0xff;
buffer[offset + 1] = (value >>> 16) & 0xff;
buffer[offset + 2] = (value >>> 8) & 0xff;
@ -869,6 +839,26 @@ Buffer.prototype.writeUInt32 = function(value, offset, endian) {
}
};
Buffer.prototype.writeUInt32 = function(value, offset, bigEndian) {
var buffer = this;
assert.ok(value !== undefined && value !== null,
'missing value');
assert.ok(typeof (bigEndian) === 'boolean',
'missing or invalid endian');
assert.ok(offset !== undefined && offset !== null,
'missing offset');
assert.ok(offset + 3 < buffer.length,
'trying to write beyond buffer length');
verifuint(value, 0xffffffff);
buffer.writeUInt32NoChk(value, offset, bigEndian);
};
/*
* We now move onto our friends in the signed number category. Unlike unsigned
@ -912,7 +902,7 @@ Buffer.prototype.writeUInt32 = function(value, offset, endian) {
*/
function verifsint(value, max, min) {
assert.ok(typeof (value) == 'number',
'cannot write a non-number as a number');
'cannot write a non-number as a number');
assert.ok(value <= max, 'value larger than maximum allowed value');
@ -924,7 +914,7 @@ function verifsint(value, max, min) {
function verifIEEE754(value, max, min) {
assert.ok(typeof (value) == 'number',
'cannot write a non-number as a number');
'cannot write a non-number as a number');
assert.ok(value <= max, 'value larger than maximum allowed value');
@ -932,131 +922,118 @@ function verifIEEE754(value, max, min) {
}
Buffer.prototype.writeInt8 = function(value, offset, endian) {
Buffer.prototype.writeInt8 = function(value, offset, bigEndian) {
var buffer = this;
assert.ok(value !== undefined && value !== null,
'missing value');
'missing value');
assert.ok(endian !== undefined && endian !== null,
'missing endian');
assert.ok(endian == 'big' || endian == 'little',
'bad endian value');
assert.ok(typeof (bigEndian) === 'boolean',
'missing or invalid endian');
assert.ok(offset !== undefined && offset !== null,
'missing offset');
'missing offset');
assert.ok(offset < buffer.length,
'Trying to write beyond buffer length');
'Trying to write beyond buffer length');
verifsint(value, 0x7f, -0xf0);
if (value >= 0) {
buffer.writeUInt8(value, offset, endian);
buffer.writeUInt8NoChk(value, offset, bigEndian);
} else {
buffer.writeUInt8(0xff + value + 1, offset, endian);
buffer.writeUInt8NoChk(0xff + value + 1, offset, bigEndian);
}
};
Buffer.prototype.writeInt16 = function(value, offset, endian) {
Buffer.prototype.writeInt16 = function(value, offset, bigEndian) {
var buffer = this;
assert.ok(value !== undefined && value !== null,
'missing value');
assert.ok(endian !== undefined && endian !== null,
'missing endian');
'missing value');
assert.ok(endian == 'big' || endian == 'little',
'bad endian value');
assert.ok(typeof (bigEndian) === 'boolean',
'missing or invalid endian');
assert.ok(offset !== undefined && offset !== null,
'missing offset');
'missing offset');
assert.ok(offset + 1 < buffer.length,
'Trying to write beyond buffer length');
'Trying to write beyond buffer length');
verifsint(value, 0x7fff, -0xf000);
if (value >= 0) {
buffer.writeUInt16(value, offset, endian);
buffer.writeUInt16NoChk(value, offset, bigEndian);
} else {
buffer.writeUInt16(0xffff + value + 1, offset, endian);
buffer.writeUInt16NoChk(0xffff + value + 1, offset, bigEndian);
}
};
Buffer.prototype.writeInt32 = function(value, offset, endian) {
Buffer.prototype.writeInt32 = function(value, offset, bigEndian) {
var buffer = this;
assert.ok(value !== undefined && value !== null,
'missing value');
assert.ok(endian !== undefined && endian !== null,
'missing endian');
'missing value');
assert.ok(endian == 'big' || endian == 'little',
'bad endian value');
assert.ok(typeof (bigEndian) === 'boolean',
'missing or invalid endian');
assert.ok(offset !== undefined && offset !== null,
'missing offset');
'missing offset');
assert.ok(offset + 3 < buffer.length,
'Trying to write beyond buffer length');
'Trying to write beyond buffer length');
verifsint(value, 0x7fffffff, -0xf0000000);
if (value >= 0) {
buffer.writeUInt32(value, offset, endian);
buffer.writeUInt32NoChk(value, offset, bigEndian);
} else {
buffer.writeUInt32(0xffffffff + value + 1, offset, endian);
buffer.writeUInt32NoChk(0xffffffff + value + 1, offset, bigEndian);
}
};
Buffer.prototype.writeFloat = function(value, offset, endian) {
Buffer.prototype.writeFloat = function(value, offset, bigEndian) {
var buffer = this;
assert.ok(value !== undefined && value !== null,
'missing value');
'missing value');
assert.ok(endian !== undefined && endian !== null,
'missing endian');
assert.ok(endian == 'big' || endian == 'little',
'bad endian value');
assert.ok(typeof (bigEndian) === 'boolean',
'missing or invalid endian');
assert.ok(offset !== undefined && offset !== null,
'missing offset');
'missing offset');
assert.ok(offset + 3 < buffer.length,
'Trying to write beyond buffer length');
'Trying to write beyond buffer length');
verifIEEE754(value, 3.4028234663852886e+38, -3.4028234663852886e+38);
require('buffer_ieee754').writeIEEE754(buffer, value, offset, endian, 23, 4);
require('buffer_ieee754').writeIEEE754(buffer, value, offset, bigEndian,
23, 4);
};
Buffer.prototype.writeDouble = function(value, offset, endian) {
Buffer.prototype.writeDouble = function(value, offset, bigEndian) {
var buffer = this;
assert.ok(value !== undefined && value !== null,
'missing value');
assert.ok(endian !== undefined && endian !== null,
'missing endian');
'missing value');
assert.ok(endian == 'big' || endian == 'little',
'bad endian value');
assert.ok(typeof (bigEndian) === 'boolean',
'missing or invalid endian');
assert.ok(offset !== undefined && offset !== null,
'missing offset');
'missing offset');
assert.ok(offset + 7 < buffer.length,
'Trying to write beyond buffer length');
'Trying to write beyond buffer length');
verifIEEE754(value, 1.7976931348623157E+308, -1.7976931348623157E+308);
require('buffer_ieee754').writeIEEE754(buffer, value, offset, endian, 52, 8);
require('buffer_ieee754').writeIEEE754(buffer, value, offset, bigEndian,
52, 8);
};

4
lib/buffer_ieee754.js

@ -32,7 +32,7 @@
exports.readIEEE754 = function(buffer, offset, endian, mLen, nBytes) {
var e, m,
bBE = (endian === 'big'),
bBE = endian,
eLen = nBytes * 8 - mLen - 1,
eMax = (1 << eLen) - 1,
eBias = eMax >> 1,
@ -66,7 +66,7 @@ exports.readIEEE754 = function(buffer, offset, endian, mLen, nBytes) {
exports.writeIEEE754 = function(buffer, value, offset, endian, mLen, nBytes) {
var e, m, c,
bBE = (endian === 'big'),
bBE = endian,
eLen = nBytes * 8 - mLen - 1,
eMax = (1 << eLen) - 1,
eBias = eMax >> 1,

60
test/simple/test-readdouble.js

@ -16,8 +16,8 @@ function test() {
buffer[5] = 0x55;
buffer[6] = 0xd5;
buffer[7] = 0x3f;
ASSERT.equal(1.1945305291680097e+103, buffer.readDouble(0, 'big'));
ASSERT.equal(0.3333333333333333, buffer.readDouble(0, 'little'));
ASSERT.equal(1.1945305291680097e+103, buffer.readDouble(0, true));
ASSERT.equal(0.3333333333333333, buffer.readDouble(0, false));
buffer[0] = 1;
buffer[1] = 0;
@ -27,18 +27,18 @@ function test() {
buffer[5] = 0;
buffer[6] = 0xf0;
buffer[7] = 0x3f;
ASSERT.equal(7.291122019655968e-304, buffer.readDouble(0, 'big'));
ASSERT.equal(1.0000000000000002, buffer.readDouble(0, 'little'));
ASSERT.equal(7.291122019655968e-304, buffer.readDouble(0, true));
ASSERT.equal(1.0000000000000002, buffer.readDouble(0, false));
buffer[0] = 2;
ASSERT.equal(4.778309726801735e-299, buffer.readDouble(0, 'big'));
ASSERT.equal(1.0000000000000004, buffer.readDouble(0, 'little'));
ASSERT.equal(4.778309726801735e-299, buffer.readDouble(0, true));
ASSERT.equal(1.0000000000000004, buffer.readDouble(0, false));
buffer[0] = 1;
buffer[6] = 0;
buffer[7] = 0;
ASSERT.equal(7.291122019556398e-304, buffer.readDouble(0, 'big'));
ASSERT.equal(5e-324, buffer.readDouble(0, 'little'));
ASSERT.equal(7.291122019556398e-304, buffer.readDouble(0, true));
ASSERT.equal(5e-324, buffer.readDouble(0, false));
buffer[0] = 0xff;
buffer[1] = 0xff;
@ -48,13 +48,13 @@ function test() {
buffer[5] = 0xff;
buffer[6] = 0x0f;
buffer[7] = 0x00;
ASSERT.ok(isNaN(buffer.readDouble(0, 'big')));
ASSERT.equal(2.225073858507201e-308, buffer.readDouble(0, 'little'));
ASSERT.ok(isNaN(buffer.readDouble(0, true)));
ASSERT.equal(2.225073858507201e-308, buffer.readDouble(0, false));
buffer[6] = 0xef;
buffer[7] = 0x7f;
ASSERT.ok(isNaN(buffer.readDouble(0, 'big')));
ASSERT.equal(1.7976931348623157e+308, buffer.readDouble(0, 'little'));
ASSERT.ok(isNaN(buffer.readDouble(0, true)));
ASSERT.equal(1.7976931348623157e+308, buffer.readDouble(0, false));
buffer[0] = 0;
buffer[1] = 0;
@ -64,42 +64,42 @@ function test() {
buffer[5] = 0;
buffer[6] = 0xf0;
buffer[7] = 0x3f;
ASSERT.equal(3.03865e-319, buffer.readDouble(0, 'big'));
ASSERT.equal(1, buffer.readDouble(0, 'little'));
ASSERT.equal(3.03865e-319, buffer.readDouble(0, true));
ASSERT.equal(1, buffer.readDouble(0, false));
buffer[6] = 0;
buffer[7] = 0x40;
ASSERT.equal(3.16e-322, buffer.readDouble(0, 'big'));
ASSERT.equal(2, buffer.readDouble(0, 'little'));
ASSERT.equal(3.16e-322, buffer.readDouble(0, true));
ASSERT.equal(2, buffer.readDouble(0, false));
buffer[7] = 0xc0;
ASSERT.equal(9.5e-322, buffer.readDouble(0, 'big'));
ASSERT.equal(-2, buffer.readDouble(0, 'little'));
ASSERT.equal(9.5e-322, buffer.readDouble(0, true));
ASSERT.equal(-2, buffer.readDouble(0, false));
buffer[6] = 0x10;
buffer[7] = 0;
ASSERT.equal(2.0237e-320, buffer.readDouble(0, 'big'));
ASSERT.equal(2.2250738585072014e-308, buffer.readDouble(0, 'little'));
ASSERT.equal(2.0237e-320, buffer.readDouble(0, true));
ASSERT.equal(2.2250738585072014e-308, buffer.readDouble(0, false));
buffer[6] = 0;
ASSERT.equal(0, buffer.readDouble(0, 'big'));
ASSERT.equal(0, buffer.readDouble(0, 'little'));
ASSERT.equal(false, 1/buffer.readDouble(0, 'little')<0);
ASSERT.equal(0, buffer.readDouble(0, true));
ASSERT.equal(0, buffer.readDouble(0, false));
ASSERT.equal(false, 1/buffer.readDouble(0, false)<0);
buffer[7] = 0x80;
ASSERT.equal(6.3e-322, buffer.readDouble(0, 'big'));
ASSERT.equal(0, buffer.readDouble(0, 'little'));
ASSERT.equal(true, 1/buffer.readDouble(0, 'little')<0);
ASSERT.equal(6.3e-322, buffer.readDouble(0, true));
ASSERT.equal(0, buffer.readDouble(0, false));
ASSERT.equal(true, 1/buffer.readDouble(0, false)<0);
buffer[6] = 0xf0;
buffer[7] = 0x7f;
ASSERT.equal(3.0418e-319, buffer.readDouble(0, 'big'));
ASSERT.equal(Infinity, buffer.readDouble(0, 'little'));
ASSERT.equal(3.0418e-319, buffer.readDouble(0, true));
ASSERT.equal(Infinity, buffer.readDouble(0, false));
buffer[6] = 0xf0;
buffer[7] = 0xff;
ASSERT.equal(3.04814e-319, buffer.readDouble(0, 'big'));
ASSERT.equal(-Infinity, buffer.readDouble(0, 'little'));
ASSERT.equal(3.04814e-319, buffer.readDouble(0, true));
ASSERT.equal(-Infinity, buffer.readDouble(0, false));
}

36
test/simple/test-readfloat.js

@ -12,56 +12,56 @@ function test() {
buffer[1] = 0;
buffer[2] = 0x80;
buffer[3] = 0x3f;
ASSERT.equal(4.600602988224807e-41, buffer.readFloat(0, 'big'));
ASSERT.equal(1, buffer.readFloat(0, 'little'));
ASSERT.equal(4.600602988224807e-41, buffer.readFloat(0, true));
ASSERT.equal(1, buffer.readFloat(0, false));
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0xc0;
ASSERT.equal(2.6904930515036488e-43, buffer.readFloat(0, 'big'));
ASSERT.equal(-2, buffer.readFloat(0, 'little'));
ASSERT.equal(2.6904930515036488e-43, buffer.readFloat(0, true));
ASSERT.equal(-2, buffer.readFloat(0, false));
buffer[0] = 0xff;
buffer[1] = 0xff;
buffer[2] = 0x7f;
buffer[3] = 0x7f;
ASSERT.ok(isNaN(buffer.readFloat(0, 'big')));
ASSERT.equal(3.4028234663852886e+38, buffer.readFloat(0, 'little'));
ASSERT.ok(isNaN(buffer.readFloat(0, true)));
ASSERT.equal(3.4028234663852886e+38, buffer.readFloat(0, false));
buffer[0] = 0xab;
buffer[1] = 0xaa;
buffer[2] = 0xaa;
buffer[3] = 0x3e;
ASSERT.equal(-1.2126478207002966e-12, buffer.readFloat(0, 'big'));
ASSERT.equal(0.3333333432674408, buffer.readFloat(0, 'little'));
ASSERT.equal(-1.2126478207002966e-12, buffer.readFloat(0, true));
ASSERT.equal(0.3333333432674408, buffer.readFloat(0, false));
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0;
ASSERT.equal(0, buffer.readFloat(0, 'big'));
ASSERT.equal(0, buffer.readFloat(0, 'little'));
ASSERT.equal(false, 1/buffer.readFloat(0, 'little')<0);
ASSERT.equal(0, buffer.readFloat(0, true));
ASSERT.equal(0, buffer.readFloat(0, false));
ASSERT.equal(false, 1/buffer.readFloat(0, false)<0);
buffer[3] = 0x80;
ASSERT.equal(1.793662034335766e-43, buffer.readFloat(0, 'big'));
ASSERT.equal(0, buffer.readFloat(0, 'little'));
ASSERT.equal(true, 1/buffer.readFloat(0, 'little')<0);
ASSERT.equal(1.793662034335766e-43, buffer.readFloat(0, true));
ASSERT.equal(0, buffer.readFloat(0, false));
ASSERT.equal(true, 1/buffer.readFloat(0, false)<0);
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0x80;
buffer[3] = 0x7f;
ASSERT.equal(4.609571298396486e-41, buffer.readFloat(0, 'big'));
ASSERT.equal(Infinity, buffer.readFloat(0, 'little'));
ASSERT.equal(4.609571298396486e-41, buffer.readFloat(0, true));
ASSERT.equal(Infinity, buffer.readFloat(0, false));
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0x80;
buffer[3] = 0xff;
ASSERT.equal(4.627507918739843e-41, buffer.readFloat(0, 'big'));
ASSERT.equal(-Infinity, buffer.readFloat(0, 'little'));
ASSERT.equal(4.627507918739843e-41, buffer.readFloat(0, true));
ASSERT.equal(-Infinity, buffer.readFloat(0, false));
}

72
test/simple/test-readint.js

@ -10,25 +10,25 @@ function test8() {
var data = new Buffer(4);
data[0] = 0x23;
ASSERT.equal(0x23, data.readInt8(0, 'big'));
ASSERT.equal(0x23, data.readInt8(0, 'little'));
ASSERT.equal(0x23, data.readInt8(0, true));
ASSERT.equal(0x23, data.readInt8(0, false));
data[0] = 0xff;
ASSERT.equal(-1, data.readInt8(0, 'big'));
ASSERT.equal(-1, data.readInt8(0, 'little'));
ASSERT.equal(-1, data.readInt8(0, true));
ASSERT.equal(-1, data.readInt8(0, false));
data[0] = 0x87;
data[1] = 0xab;
data[2] = 0x7c;
data[3] = 0xef;
ASSERT.equal(-121, data.readInt8(0, 'big'));
ASSERT.equal(-85, data.readInt8(1, 'big'));
ASSERT.equal(124, data.readInt8(2, 'big'));
ASSERT.equal(-17, data.readInt8(3, 'big'));
ASSERT.equal(-121, data.readInt8(0, 'little'));
ASSERT.equal(-85, data.readInt8(1, 'little'));
ASSERT.equal(124, data.readInt8(2, 'little'));
ASSERT.equal(-17, data.readInt8(3, 'little'));
ASSERT.equal(-121, data.readInt8(0, true));
ASSERT.equal(-85, data.readInt8(1, true));
ASSERT.equal(124, data.readInt8(2, true));
ASSERT.equal(-17, data.readInt8(3, true));
ASSERT.equal(-121, data.readInt8(0, false));
ASSERT.equal(-85, data.readInt8(1, false));
ASSERT.equal(124, data.readInt8(2, false));
ASSERT.equal(-17, data.readInt8(3, false));
}
@ -36,13 +36,13 @@ function test16() {
var buffer = new Buffer(6);
buffer[0] = 0x16;
buffer[1] = 0x79;
ASSERT.equal(0x1679, buffer.readInt16(0, 'big'));
ASSERT.equal(0x7916, buffer.readInt16(0, 'little'));
ASSERT.equal(0x1679, buffer.readInt16(0, true));
ASSERT.equal(0x7916, buffer.readInt16(0, false));
buffer[0] = 0xff;
buffer[1] = 0x80;
ASSERT.equal(-128, buffer.readInt16(0, 'big'));
ASSERT.equal(-32513, buffer.readInt16(0, 'little'));
ASSERT.equal(-128, buffer.readInt16(0, true));
ASSERT.equal(-32513, buffer.readInt16(0, false));
/* test offset with weenix */
buffer[0] = 0x77;
@ -51,16 +51,16 @@ function test16() {
buffer[3] = 0x6e;
buffer[4] = 0x69;
buffer[5] = 0x78;
ASSERT.equal(0x7765, buffer.readInt16(0, 'big'));
ASSERT.equal(0x6565, buffer.readInt16(1, 'big'));
ASSERT.equal(0x656e, buffer.readInt16(2, 'big'));
ASSERT.equal(0x6e69, buffer.readInt16(3, 'big'));
ASSERT.equal(0x6978, buffer.readInt16(4, 'big'));
ASSERT.equal(0x6577, buffer.readInt16(0, 'little'));
ASSERT.equal(0x6565, buffer.readInt16(1, 'little'));
ASSERT.equal(0x6e65, buffer.readInt16(2, 'little'));
ASSERT.equal(0x696e, buffer.readInt16(3, 'little'));
ASSERT.equal(0x7869, buffer.readInt16(4, 'little'));
ASSERT.equal(0x7765, buffer.readInt16(0, true));
ASSERT.equal(0x6565, buffer.readInt16(1, true));
ASSERT.equal(0x656e, buffer.readInt16(2, true));
ASSERT.equal(0x6e69, buffer.readInt16(3, true));
ASSERT.equal(0x6978, buffer.readInt16(4, true));
ASSERT.equal(0x6577, buffer.readInt16(0, false));
ASSERT.equal(0x6565, buffer.readInt16(1, false));
ASSERT.equal(0x6e65, buffer.readInt16(2, false));
ASSERT.equal(0x696e, buffer.readInt16(3, false));
ASSERT.equal(0x7869, buffer.readInt16(4, false));
}
@ -70,15 +70,15 @@ function test32() {
buffer[1] = 0x53;
buffer[2] = 0x16;
buffer[3] = 0x79;
ASSERT.equal(0x43531679, buffer.readInt32(0, 'big'));
ASSERT.equal(0x79165343, buffer.readInt32(0, 'little'));
ASSERT.equal(0x43531679, buffer.readInt32(0, true));
ASSERT.equal(0x79165343, buffer.readInt32(0, false));
buffer[0] = 0xff;
buffer[1] = 0xfe;
buffer[2] = 0xef;
buffer[3] = 0xfa;
ASSERT.equal(-69638, buffer.readInt32(0, 'big'));
ASSERT.equal(-84934913, buffer.readInt32(0, 'little'));
ASSERT.equal(-69638, buffer.readInt32(0, true));
ASSERT.equal(-84934913, buffer.readInt32(0, false));
buffer[0] = 0x42;
buffer[1] = 0xc3;
@ -86,12 +86,12 @@ function test32() {
buffer[3] = 0xa9;
buffer[4] = 0x36;
buffer[5] = 0x17;
ASSERT.equal(0x42c395a9, buffer.readInt32(0, 'big'));
ASSERT.equal(-1013601994, buffer.readInt32(1, 'big'));
ASSERT.equal(-1784072681, buffer.readInt32(2, 'big'));
ASSERT.equal(-1449802942, buffer.readInt32(0, 'little'));
ASSERT.equal(917083587, buffer.readInt32(1, 'little'));
ASSERT.equal(389458325, buffer.readInt32(2, 'little'));
ASSERT.equal(0x42c395a9, buffer.readInt32(0, true));
ASSERT.equal(-1013601994, buffer.readInt32(1, true));
ASSERT.equal(-1784072681, buffer.readInt32(2, true));
ASSERT.equal(-1449802942, buffer.readInt32(0, false));
ASSERT.equal(917083587, buffer.readInt32(1, false));
ASSERT.equal(389458325, buffer.readInt32(2, false));
}

48
test/simple/test-readuint.js

@ -17,17 +17,17 @@ function test8() {
data[1] = 23;
data[2] = 23;
data[3] = 23;
ASSERT.equal(23, data.readUInt8(0, 'big'));
ASSERT.equal(23, data.readUInt8(0, 'little'));
ASSERT.equal(23, data.readUInt8(1, 'big'));
ASSERT.equal(23, data.readUInt8(1, 'little'));
ASSERT.equal(23, data.readUInt8(2, 'big'));
ASSERT.equal(23, data.readUInt8(2, 'little'));
ASSERT.equal(23, data.readUInt8(3, 'big'));
ASSERT.equal(23, data.readUInt8(3, 'little'));
ASSERT.equal(23, data.readUInt8(0, true));
ASSERT.equal(23, data.readUInt8(0, false));
ASSERT.equal(23, data.readUInt8(1, true));
ASSERT.equal(23, data.readUInt8(1, false));
ASSERT.equal(23, data.readUInt8(2, true));
ASSERT.equal(23, data.readUInt8(2, false));
ASSERT.equal(23, data.readUInt8(3, true));
ASSERT.equal(23, data.readUInt8(3, false));
data[0] = 255; /* If it became a signed int, would be -1 */
ASSERT.equal(255, data.readUInt8(0, 'big'));
ASSERT.equal(255, data.readUInt8(0, 'little'));
ASSERT.equal(255, data.readUInt8(0, true));
ASSERT.equal(255, data.readUInt8(0, false));
}
@ -47,19 +47,19 @@ function test16() {
data[2] = 0x42;
data[3] = 0x3f;
ASSERT.equal(0x23, data.readUInt16(0, 'big'));
ASSERT.equal(0x2342, data.readUInt16(1, 'big'));
ASSERT.equal(0x423f, data.readUInt16(2, 'big'));
ASSERT.equal(0x23, data.readUInt16(0, true));
ASSERT.equal(0x2342, data.readUInt16(1, true));
ASSERT.equal(0x423f, data.readUInt16(2, true));
ASSERT.equal(0x2300, data.readUInt16(0, 'little'));
ASSERT.equal(0x4223, data.readUInt16(1, 'little'));
ASSERT.equal(0x3f42, data.readUInt16(2, 'little'));
ASSERT.equal(0x2300, data.readUInt16(0, false));
ASSERT.equal(0x4223, data.readUInt16(1, false));
ASSERT.equal(0x3f42, data.readUInt16(2, false));
data[0] = 0xfe;
data[1] = 0xfe;
ASSERT.equal(0xfefe, data.readUInt16(0, 'big'));
ASSERT.equal(0xfefe, data.readUInt16(0, 'little'));
ASSERT.equal(0xfefe, data.readUInt16(0, true));
ASSERT.equal(0xfefe, data.readUInt16(0, false));
}
@ -79,13 +79,13 @@ function test32() {
data[4] = 0x23;
data[5] = 0xff;
ASSERT.equal(0x32654256, data.readUInt32(0, 'big'));
ASSERT.equal(0x65425623, data.readUInt32(1, 'big'));
ASSERT.equal(0x425623ff, data.readUInt32(2, 'big'));
ASSERT.equal(0x32654256, data.readUInt32(0, true));
ASSERT.equal(0x65425623, data.readUInt32(1, true));
ASSERT.equal(0x425623ff, data.readUInt32(2, true));
ASSERT.equal(0x56426532, data.readUInt32(0, 'little'));
ASSERT.equal(0x23564265, data.readUInt32(1, 'little'));
ASSERT.equal(0xff235642, data.readUInt32(2, 'little'));
ASSERT.equal(0x56426532, data.readUInt32(0, false));
ASSERT.equal(0x23564265, data.readUInt32(1, false));
ASSERT.equal(0xff235642, data.readUInt32(2, false));
}

20
test/simple/test-writedouble.js

@ -5,8 +5,8 @@ var ASSERT = require('assert');
function test() {
var buffer = new Buffer(16);
buffer.writeDouble(2.225073858507201e-308, 0, 'big');
buffer.writeDouble(2.225073858507201e-308, 8, 'little');
buffer.writeDouble(2.225073858507201e-308, 0, true);
buffer.writeDouble(2.225073858507201e-308, 8, false);
ASSERT.equal(0x00, buffer[0]);
ASSERT.equal(0x0f, buffer[1]);
ASSERT.equal(0xff, buffer[2]);
@ -24,8 +24,8 @@ function test() {
ASSERT.equal(0x0f, buffer[14]);
ASSERT.equal(0x00, buffer[15]);
buffer.writeDouble(1.0000000000000004, 0, 'big');
buffer.writeDouble(1.0000000000000004, 8, 'little');
buffer.writeDouble(1.0000000000000004, 0, true);
buffer.writeDouble(1.0000000000000004, 8, false);
ASSERT.equal(0x3f, buffer[0]);
ASSERT.equal(0xf0, buffer[1]);
ASSERT.equal(0x00, buffer[2]);
@ -43,8 +43,8 @@ function test() {
ASSERT.equal(0xf0, buffer[14]);
ASSERT.equal(0x3f, buffer[15]);
buffer.writeDouble(-2, 0, 'big');
buffer.writeDouble(-2, 8, 'little');
buffer.writeDouble(-2, 0, true);
buffer.writeDouble(-2, 8, false);
ASSERT.equal(0xc0, buffer[0]);
ASSERT.equal(0x00, buffer[1]);
ASSERT.equal(0x00, buffer[2]);
@ -62,8 +62,8 @@ function test() {
ASSERT.equal(0x00, buffer[14]);
ASSERT.equal(0xc0, buffer[15]);
buffer.writeDouble(1.7976931348623157e+308, 0, 'big');
buffer.writeDouble(1.7976931348623157e+308, 8, 'little');
buffer.writeDouble(1.7976931348623157e+308, 0, true);
buffer.writeDouble(1.7976931348623157e+308, 8, false);
ASSERT.equal(0x7f, buffer[0]);
ASSERT.equal(0xef, buffer[1]);
ASSERT.equal(0xff, buffer[2]);
@ -81,8 +81,8 @@ function test() {
ASSERT.equal(0xef, buffer[14]);
ASSERT.equal(0x7f, buffer[15]);
buffer.writeDouble(0*-1, 0, 'big');
buffer.writeDouble(0*-1, 8, 'little');
buffer.writeDouble(0*-1, 0, true);
buffer.writeDouble(0*-1, 8, false);
ASSERT.equal(0x80, buffer[0]);
ASSERT.equal(0x00, buffer[1]);
ASSERT.equal(0x00, buffer[2]);

20
test/simple/test-writefloat.js

@ -5,8 +5,8 @@ var ASSERT = require('assert');
function test() {
var buffer = new Buffer(8);
buffer.writeFloat(1, 0, 'big');
buffer.writeFloat(1, 4, 'little');
buffer.writeFloat(1, 0, true);
buffer.writeFloat(1, 4, false);
ASSERT.equal(0x3f, buffer[0]);
ASSERT.equal(0x80, buffer[1]);
ASSERT.equal(0x00, buffer[2]);
@ -16,8 +16,8 @@ function test() {
ASSERT.equal(0x80, buffer[6]);
ASSERT.equal(0x3f, buffer[7]);
buffer.writeFloat(1.793662034335766e-43, 0, 'big');
buffer.writeFloat(1.793662034335766e-43, 4, 'little');
buffer.writeFloat(1.793662034335766e-43, 0, true);
buffer.writeFloat(1.793662034335766e-43, 4, false);
ASSERT.equal(0x00, buffer[0]);
ASSERT.equal(0x00, buffer[1]);
ASSERT.equal(0x00, buffer[2]);
@ -27,8 +27,8 @@ function test() {
ASSERT.equal(0x00, buffer[6]);
ASSERT.equal(0x00, buffer[7]);
buffer.writeFloat(1/3, 0, 'big');
buffer.writeFloat(1/3, 4, 'little');
buffer.writeFloat(1/3, 0, true);
buffer.writeFloat(1/3, 4, false);
ASSERT.equal(0x3e, buffer[0]);
ASSERT.equal(0xaa, buffer[1]);
ASSERT.equal(0xaa, buffer[2]);
@ -38,8 +38,8 @@ function test() {
ASSERT.equal(0xaa, buffer[6]);
ASSERT.equal(0x3e, buffer[7]);
buffer.writeFloat(3.4028234663852886e+38, 0, 'big');
buffer.writeFloat(3.4028234663852886e+38, 4, 'little');
buffer.writeFloat(3.4028234663852886e+38, 0, true);
buffer.writeFloat(3.4028234663852886e+38, 4, false);
ASSERT.equal(0x7f, buffer[0]);
ASSERT.equal(0x7f, buffer[1]);
ASSERT.equal(0xff, buffer[2]);
@ -49,8 +49,8 @@ function test() {
ASSERT.equal(0x7f, buffer[6]);
ASSERT.equal(0x7f, buffer[7]);
buffer.writeFloat(0*-1, 0, 'big');
buffer.writeFloat(0*-1, 4, 'little');
buffer.writeFloat(0*-1, 0, true);
buffer.writeFloat(0*-1, 4, false);
ASSERT.equal(0x80, buffer[0]);
ASSERT.equal(0x00, buffer[1]);
ASSERT.equal(0x00, buffer[2]);

36
test/simple/test-writeint.js

@ -5,10 +5,10 @@ var ASSERT = require('assert');
function test8() {
var buffer = new Buffer(4);
buffer.writeInt8(0x23, 0, 'big');
buffer.writeInt8(0x23, 1, 'little');
buffer.writeInt8(-5, 2, 'big');
buffer.writeInt8(-5, 3, 'little');
buffer.writeInt8(0x23, 0, true);
buffer.writeInt8(0x23, 1, false);
buffer.writeInt8(-5, 2, true);
buffer.writeInt8(-5, 3, false);
ASSERT.equal(0x23, buffer[0]);
ASSERT.equal(0x23, buffer[1]);
@ -17,31 +17,31 @@ function test8() {
/* Make sure we handle truncation correctly */
ASSERT.throws(function() {
buffer.writeInt8(0xabc, 0, 'big');
buffer.writeInt8(0xabc, 0, true);
});
ASSERT.throws(function() {
buffer.writeInt8(0xabc, 0, 'little');
buffer.writeInt8(0xabc, 0, false);
});
}
function test16() {
var buffer = new Buffer(6);
buffer.writeInt16(0x0023, 0, 'big');
buffer.writeInt16(0x0023, 2, 'little');
buffer.writeInt16(0x0023, 0, true);
buffer.writeInt16(0x0023, 2, false);
ASSERT.equal(0x00, buffer[0]);
ASSERT.equal(0x23, buffer[1]);
ASSERT.equal(0x23, buffer[2]);
ASSERT.equal(0x00, buffer[3]);
buffer.writeInt16(-5, 0, 'big');
buffer.writeInt16(-5, 2, 'little');
buffer.writeInt16(-5, 0, true);
buffer.writeInt16(-5, 2, false);
ASSERT.equal(0xff, buffer[0]);
ASSERT.equal(0xfb, buffer[1]);
ASSERT.equal(0xfb, buffer[2]);
ASSERT.equal(0xff, buffer[3]);
buffer.writeInt16(-1679, 1, 'big');
buffer.writeInt16(-1679, 3, 'little');
buffer.writeInt16(-1679, 1, true);
buffer.writeInt16(-1679, 3, false);
ASSERT.equal(0xf9, buffer[1]);
ASSERT.equal(0x71, buffer[2]);
ASSERT.equal(0x71, buffer[3]);
@ -51,8 +51,8 @@ function test16() {
function test32() {
var buffer = new Buffer(8);
buffer.writeInt32(0x23, 0, 'big');
buffer.writeInt32(0x23, 4, 'little');
buffer.writeInt32(0x23, 0, true);
buffer.writeInt32(0x23, 4, false);
ASSERT.equal(0x00, buffer[0]);
ASSERT.equal(0x00, buffer[1]);
ASSERT.equal(0x00, buffer[2]);
@ -62,8 +62,8 @@ function test32() {
ASSERT.equal(0x00, buffer[6]);
ASSERT.equal(0x00, buffer[7]);
buffer.writeInt32(-5, 0, 'big');
buffer.writeInt32(-5, 4, 'little');
buffer.writeInt32(-5, 0, true);
buffer.writeInt32(-5, 4, false);
ASSERT.equal(0xff, buffer[0]);
ASSERT.equal(0xff, buffer[1]);
ASSERT.equal(0xff, buffer[2]);
@ -73,8 +73,8 @@ function test32() {
ASSERT.equal(0xff, buffer[6]);
ASSERT.equal(0xff, buffer[7]);
buffer.writeInt32(-805306713, 0, 'big');
buffer.writeInt32(-805306713, 4, 'little');
buffer.writeInt32(-805306713, 0, true);
buffer.writeInt32(-805306713, 4, false);
ASSERT.equal(0xcf, buffer[0]);
ASSERT.equal(0xff, buffer[1]);
ASSERT.equal(0xfe, buffer[2]);

48
test/simple/test-writeuint.js

@ -12,25 +12,25 @@ var ASSERT = require('assert');
*/
function test8() {
var data = new Buffer(4);
data.writeUInt8(23, 0, 'big');
data.writeUInt8(23, 1, 'big');
data.writeUInt8(23, 2, 'big');
data.writeUInt8(23, 3, 'big');
data.writeUInt8(23, 0, true);
data.writeUInt8(23, 1, true);
data.writeUInt8(23, 2, true);
data.writeUInt8(23, 3, true);
ASSERT.equal(23, data[0]);
ASSERT.equal(23, data[1]);
ASSERT.equal(23, data[2]);
ASSERT.equal(23, data[3]);
data.writeUInt8(23, 0, 'little');
data.writeUInt8(23, 1, 'little');
data.writeUInt8(23, 2, 'little');
data.writeUInt8(23, 3, 'little');
data.writeUInt8(23, 0, false);
data.writeUInt8(23, 1, false);
data.writeUInt8(23, 2, false);
data.writeUInt8(23, 3, false);
ASSERT.equal(23, data[0]);
ASSERT.equal(23, data[1]);
ASSERT.equal(23, data[2]);
ASSERT.equal(23, data[3]);
data.writeUInt8(255, 0, 'big');
data.writeUInt8(255, 0, true);
ASSERT.equal(255, data[0]);
data.writeUInt8(255, 0, 'little');
data.writeUInt8(255, 0, false);
ASSERT.equal(255, data[0]);
}
@ -38,34 +38,34 @@ function test8() {
function test16() {
var value = 0x2343;
var data = new Buffer(4);
data.writeUInt16(value, 0, 'big');
data.writeUInt16(value, 0, true);
ASSERT.equal(0x23, data[0]);
ASSERT.equal(0x43, data[1]);
data.writeUInt16(value, 1, 'big');
data.writeUInt16(value, 1, true);
ASSERT.equal(0x23, data[1]);
ASSERT.equal(0x43, data[2]);
data.writeUInt16(value, 2, 'big');
data.writeUInt16(value, 2, true);
ASSERT.equal(0x23, data[2]);
ASSERT.equal(0x43, data[3]);
data.writeUInt16(value, 0, 'little');
data.writeUInt16(value, 0, false);
ASSERT.equal(0x23, data[1]);
ASSERT.equal(0x43, data[0]);
data.writeUInt16(value, 1, 'little');
data.writeUInt16(value, 1, false);
ASSERT.equal(0x23, data[2]);
ASSERT.equal(0x43, data[1]);
data.writeUInt16(value, 2, 'little');
data.writeUInt16(value, 2, false);
ASSERT.equal(0x23, data[3]);
ASSERT.equal(0x43, data[2]);
value = 0xff80;
data.writeUInt16(value, 0, 'little');
data.writeUInt16(value, 0, false);
ASSERT.equal(0xff, data[1]);
ASSERT.equal(0x80, data[0]);
data.writeUInt16(value, 0, 'big');
data.writeUInt16(value, 0, true);
ASSERT.equal(0xff, data[0]);
ASSERT.equal(0x80, data[1]);
}
@ -75,37 +75,37 @@ function test32() {
var data = new Buffer(6);
var value = 0xe7f90a6d;
data.writeUInt32(value, 0, 'big');
data.writeUInt32(value, 0, true);
ASSERT.equal(0xe7, data[0]);
ASSERT.equal(0xf9, data[1]);
ASSERT.equal(0x0a, data[2]);
ASSERT.equal(0x6d, data[3]);
data.writeUInt32(value, 1, 'big');
data.writeUInt32(value, 1, true);
ASSERT.equal(0xe7, data[1]);
ASSERT.equal(0xf9, data[2]);
ASSERT.equal(0x0a, data[3]);
ASSERT.equal(0x6d, data[4]);
data.writeUInt32(value, 2, 'big');
data.writeUInt32(value, 2, true);
ASSERT.equal(0xe7, data[2]);
ASSERT.equal(0xf9, data[3]);
ASSERT.equal(0x0a, data[4]);
ASSERT.equal(0x6d, data[5]);
data.writeUInt32(value, 0, 'little');
data.writeUInt32(value, 0, false);
ASSERT.equal(0xe7, data[3]);
ASSERT.equal(0xf9, data[2]);
ASSERT.equal(0x0a, data[1]);
ASSERT.equal(0x6d, data[0]);
data.writeUInt32(value, 1, 'little');
data.writeUInt32(value, 1, false);
ASSERT.equal(0xe7, data[4]);
ASSERT.equal(0xf9, data[3]);
ASSERT.equal(0x0a, data[2]);
ASSERT.equal(0x6d, data[1]);
data.writeUInt32(value, 2, 'little');
data.writeUInt32(value, 2, false);
ASSERT.equal(0xe7, data[5]);
ASSERT.equal(0xf9, data[4]);
ASSERT.equal(0x0a, data[3]);

Loading…
Cancel
Save