node/test/parallel/test-writeuint.js
Rich Trott d795301025 test: remove unnecessary assignments
common.js needs to be loaded in all tests so that there is checking
for variable leaks and possibly other things. However, it does not
need to be assigned to a variable if nothing in common.js is referred
to elsewhere in the test.

The main tradeoff for this bit of code churn is that it gets the code
base most of the way to being able to enable the no-unused-vars rule in
eslint.

(The non-tooling benefit is that it lessens cognitive load when reading
tests as it is an immediate indication that none of the functions or
properties in common.js will be used by the test.)

PR-URL: https://github.com/nodejs/node/pull/4563
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2016-01-19 11:52:30 -08:00

146 lines
3.3 KiB
JavaScript

'use strict';
/*
* A battery of tests to help us read a series of uints
*/
require('../common');
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
*/
function test8(clazz) {
var data = new clazz(4);
data.writeUInt8(23, 0);
data.writeUInt8(23, 1);
data.writeUInt8(23, 2);
data.writeUInt8(23, 3);
ASSERT.equal(23, data[0]);
ASSERT.equal(23, data[1]);
ASSERT.equal(23, data[2]);
ASSERT.equal(23, data[3]);
data.writeUInt8(23, 0);
data.writeUInt8(23, 1);
data.writeUInt8(23, 2);
data.writeUInt8(23, 3);
ASSERT.equal(23, data[0]);
ASSERT.equal(23, data[1]);
ASSERT.equal(23, data[2]);
ASSERT.equal(23, data[3]);
data.writeUInt8(255, 0);
ASSERT.equal(255, data[0]);
data.writeUInt8(255, 0);
ASSERT.equal(255, data[0]);
}
function test16(clazz) {
var value = 0x2343;
var data = new clazz(4);
data.writeUInt16BE(value, 0);
ASSERT.equal(0x23, data[0]);
ASSERT.equal(0x43, data[1]);
data.writeUInt16BE(value, 1);
ASSERT.equal(0x23, data[1]);
ASSERT.equal(0x43, data[2]);
data.writeUInt16BE(value, 2);
ASSERT.equal(0x23, data[2]);
ASSERT.equal(0x43, data[3]);
data.writeUInt16LE(value, 0);
ASSERT.equal(0x23, data[1]);
ASSERT.equal(0x43, data[0]);
data.writeUInt16LE(value, 1);
ASSERT.equal(0x23, data[2]);
ASSERT.equal(0x43, data[1]);
data.writeUInt16LE(value, 2);
ASSERT.equal(0x23, data[3]);
ASSERT.equal(0x43, data[2]);
value = 0xff80;
data.writeUInt16LE(value, 0);
ASSERT.equal(0xff, data[1]);
ASSERT.equal(0x80, data[0]);
data.writeUInt16BE(value, 0);
ASSERT.equal(0xff, data[0]);
ASSERT.equal(0x80, data[1]);
}
function test32(clazz) {
var data = new clazz(6);
var value = 0xe7f90a6d;
data.writeUInt32BE(value, 0);
ASSERT.equal(0xe7, data[0]);
ASSERT.equal(0xf9, data[1]);
ASSERT.equal(0x0a, data[2]);
ASSERT.equal(0x6d, data[3]);
data.writeUInt32BE(value, 1);
ASSERT.equal(0xe7, data[1]);
ASSERT.equal(0xf9, data[2]);
ASSERT.equal(0x0a, data[3]);
ASSERT.equal(0x6d, data[4]);
data.writeUInt32BE(value, 2);
ASSERT.equal(0xe7, data[2]);
ASSERT.equal(0xf9, data[3]);
ASSERT.equal(0x0a, data[4]);
ASSERT.equal(0x6d, data[5]);
data.writeUInt32LE(value, 0);
ASSERT.equal(0xe7, data[3]);
ASSERT.equal(0xf9, data[2]);
ASSERT.equal(0x0a, data[1]);
ASSERT.equal(0x6d, data[0]);
data.writeUInt32LE(value, 1);
ASSERT.equal(0xe7, data[4]);
ASSERT.equal(0xf9, data[3]);
ASSERT.equal(0x0a, data[2]);
ASSERT.equal(0x6d, data[1]);
data.writeUInt32LE(value, 2);
ASSERT.equal(0xe7, data[5]);
ASSERT.equal(0xf9, data[4]);
ASSERT.equal(0x0a, data[3]);
ASSERT.equal(0x6d, data[2]);
}
function testUint(clazz) {
const data = new clazz(8);
var val = 1;
// Test 0 to 5 bytes.
for (var i = 0; i <= 5; i++) {
const errmsg = `byteLength: ${i}`;
ASSERT.throws(function() {
data.writeUIntBE(val, 0, i);
}, /value is out of bounds/, errmsg);
ASSERT.throws(function() {
data.writeUIntLE(val, 0, i);
}, /value is out of bounds/, errmsg);
val *= 0x100;
}
}
test8(Buffer);
test16(Buffer);
test32(Buffer);
testUint(Buffer);