2011-05-01 20:38:10 +02:00
|
|
|
/*
|
|
|
|
* A battery of tests to help us read a series of uints
|
|
|
|
*/
|
2012-02-20 13:22:12 +01:00
|
|
|
var common = require('../common');
|
2011-05-01 20:38:10 +02:00
|
|
|
var ASSERT = require('assert');
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to check the following things:
|
|
|
|
* - We are correctly resolving big endian (doesn't mean anything for 8 bit)
|
|
|
|
* - Correctly resolving little endian (doesn't mean anything for 8 bit)
|
|
|
|
* - Correctly using the offsets
|
|
|
|
* - Correctly interpreting values that are beyond the signed range as unsigned
|
|
|
|
*/
|
2011-11-17 14:14:43 +01:00
|
|
|
function test8(clazz) {
|
|
|
|
var data = new clazz(4);
|
2011-08-09 06:40:10 +02:00
|
|
|
|
|
|
|
data.writeUInt8(23, 0);
|
|
|
|
data.writeUInt8(23, 1);
|
|
|
|
data.writeUInt8(23, 2);
|
|
|
|
data.writeUInt8(23, 3);
|
2011-05-01 20:38:10 +02:00
|
|
|
ASSERT.equal(23, data[0]);
|
|
|
|
ASSERT.equal(23, data[1]);
|
|
|
|
ASSERT.equal(23, data[2]);
|
|
|
|
ASSERT.equal(23, data[3]);
|
2011-08-09 06:40:10 +02:00
|
|
|
|
|
|
|
data.writeUInt8(23, 0);
|
|
|
|
data.writeUInt8(23, 1);
|
|
|
|
data.writeUInt8(23, 2);
|
|
|
|
data.writeUInt8(23, 3);
|
2011-05-01 20:38:10 +02:00
|
|
|
ASSERT.equal(23, data[0]);
|
|
|
|
ASSERT.equal(23, data[1]);
|
|
|
|
ASSERT.equal(23, data[2]);
|
|
|
|
ASSERT.equal(23, data[3]);
|
2011-08-09 06:40:10 +02:00
|
|
|
|
|
|
|
data.writeUInt8(255, 0);
|
2011-05-01 20:38:10 +02:00
|
|
|
ASSERT.equal(255, data[0]);
|
2011-08-09 06:40:10 +02:00
|
|
|
|
|
|
|
data.writeUInt8(255, 0);
|
2011-05-01 20:38:10 +02:00
|
|
|
ASSERT.equal(255, data[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-17 14:14:43 +01:00
|
|
|
function test16(clazz) {
|
2011-05-01 20:38:10 +02:00
|
|
|
var value = 0x2343;
|
2011-11-17 14:14:43 +01:00
|
|
|
var data = new clazz(4);
|
2011-08-09 06:40:10 +02:00
|
|
|
|
|
|
|
data.writeUInt16BE(value, 0);
|
2011-05-01 20:38:10 +02:00
|
|
|
ASSERT.equal(0x23, data[0]);
|
|
|
|
ASSERT.equal(0x43, data[1]);
|
2011-08-09 06:40:10 +02:00
|
|
|
|
|
|
|
data.writeUInt16BE(value, 1);
|
2011-05-01 20:38:10 +02:00
|
|
|
ASSERT.equal(0x23, data[1]);
|
|
|
|
ASSERT.equal(0x43, data[2]);
|
2011-08-09 06:40:10 +02:00
|
|
|
|
|
|
|
data.writeUInt16BE(value, 2);
|
2011-05-01 20:38:10 +02:00
|
|
|
ASSERT.equal(0x23, data[2]);
|
|
|
|
ASSERT.equal(0x43, data[3]);
|
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
data.writeUInt16LE(value, 0);
|
2011-05-01 20:38:10 +02:00
|
|
|
ASSERT.equal(0x23, data[1]);
|
|
|
|
ASSERT.equal(0x43, data[0]);
|
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
data.writeUInt16LE(value, 1);
|
2011-05-01 20:38:10 +02:00
|
|
|
ASSERT.equal(0x23, data[2]);
|
|
|
|
ASSERT.equal(0x43, data[1]);
|
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
data.writeUInt16LE(value, 2);
|
2011-05-01 20:38:10 +02:00
|
|
|
ASSERT.equal(0x23, data[3]);
|
|
|
|
ASSERT.equal(0x43, data[2]);
|
|
|
|
|
|
|
|
value = 0xff80;
|
2011-08-09 06:40:10 +02:00
|
|
|
data.writeUInt16LE(value, 0);
|
2011-05-01 20:38:10 +02:00
|
|
|
ASSERT.equal(0xff, data[1]);
|
|
|
|
ASSERT.equal(0x80, data[0]);
|
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
data.writeUInt16BE(value, 0);
|
2011-05-01 20:38:10 +02:00
|
|
|
ASSERT.equal(0xff, data[0]);
|
|
|
|
ASSERT.equal(0x80, data[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-17 14:14:43 +01:00
|
|
|
function test32(clazz) {
|
|
|
|
var data = new clazz(6);
|
2011-05-01 20:38:10 +02:00
|
|
|
var value = 0xe7f90a6d;
|
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
data.writeUInt32BE(value, 0);
|
2011-05-01 20:38:10 +02:00
|
|
|
ASSERT.equal(0xe7, data[0]);
|
|
|
|
ASSERT.equal(0xf9, data[1]);
|
|
|
|
ASSERT.equal(0x0a, data[2]);
|
|
|
|
ASSERT.equal(0x6d, data[3]);
|
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
data.writeUInt32BE(value, 1);
|
2011-05-01 20:38:10 +02:00
|
|
|
ASSERT.equal(0xe7, data[1]);
|
|
|
|
ASSERT.equal(0xf9, data[2]);
|
|
|
|
ASSERT.equal(0x0a, data[3]);
|
|
|
|
ASSERT.equal(0x6d, data[4]);
|
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
data.writeUInt32BE(value, 2);
|
2011-05-01 20:38:10 +02:00
|
|
|
ASSERT.equal(0xe7, data[2]);
|
|
|
|
ASSERT.equal(0xf9, data[3]);
|
|
|
|
ASSERT.equal(0x0a, data[4]);
|
|
|
|
ASSERT.equal(0x6d, data[5]);
|
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
data.writeUInt32LE(value, 0);
|
2011-05-01 20:38:10 +02:00
|
|
|
ASSERT.equal(0xe7, data[3]);
|
|
|
|
ASSERT.equal(0xf9, data[2]);
|
|
|
|
ASSERT.equal(0x0a, data[1]);
|
|
|
|
ASSERT.equal(0x6d, data[0]);
|
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
data.writeUInt32LE(value, 1);
|
2011-05-01 20:38:10 +02:00
|
|
|
ASSERT.equal(0xe7, data[4]);
|
|
|
|
ASSERT.equal(0xf9, data[3]);
|
|
|
|
ASSERT.equal(0x0a, data[2]);
|
|
|
|
ASSERT.equal(0x6d, data[1]);
|
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
data.writeUInt32LE(value, 2);
|
2011-05-01 20:38:10 +02:00
|
|
|
ASSERT.equal(0xe7, data[5]);
|
|
|
|
ASSERT.equal(0xf9, data[4]);
|
|
|
|
ASSERT.equal(0x0a, data[3]);
|
|
|
|
ASSERT.equal(0x6d, data[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-17 14:14:43 +01:00
|
|
|
test8(Buffer);
|
|
|
|
test16(Buffer);
|
|
|
|
test32(Buffer);
|