mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
082cc8d6d8
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. PR-URL: https://github.com/nodejs/node/pull/4408 Reviewed-By: James M Snell <jasnell@gmail.com>
115 lines
3.6 KiB
JavaScript
115 lines
3.6 KiB
JavaScript
'use strict';
|
|
/*
|
|
* Tests to verify we're writing floats correctly
|
|
*/
|
|
require('../common');
|
|
var ASSERT = require('assert');
|
|
|
|
function test(clazz) {
|
|
var buffer = new clazz(8);
|
|
|
|
buffer.writeFloatBE(1, 0);
|
|
buffer.writeFloatLE(1, 4);
|
|
ASSERT.equal(0x3f, buffer[0]);
|
|
ASSERT.equal(0x80, buffer[1]);
|
|
ASSERT.equal(0x00, buffer[2]);
|
|
ASSERT.equal(0x00, buffer[3]);
|
|
ASSERT.equal(0x00, buffer[4]);
|
|
ASSERT.equal(0x00, buffer[5]);
|
|
ASSERT.equal(0x80, buffer[6]);
|
|
ASSERT.equal(0x3f, buffer[7]);
|
|
|
|
buffer.writeFloatBE(1 / 3, 0);
|
|
buffer.writeFloatLE(1 / 3, 4);
|
|
ASSERT.equal(0x3e, buffer[0]);
|
|
ASSERT.equal(0xaa, buffer[1]);
|
|
ASSERT.equal(0xaa, buffer[2]);
|
|
ASSERT.equal(0xab, buffer[3]);
|
|
ASSERT.equal(0xab, buffer[4]);
|
|
ASSERT.equal(0xaa, buffer[5]);
|
|
ASSERT.equal(0xaa, buffer[6]);
|
|
ASSERT.equal(0x3e, buffer[7]);
|
|
|
|
buffer.writeFloatBE(3.4028234663852886e+38, 0);
|
|
buffer.writeFloatLE(3.4028234663852886e+38, 4);
|
|
ASSERT.equal(0x7f, buffer[0]);
|
|
ASSERT.equal(0x7f, buffer[1]);
|
|
ASSERT.equal(0xff, buffer[2]);
|
|
ASSERT.equal(0xff, buffer[3]);
|
|
ASSERT.equal(0xff, buffer[4]);
|
|
ASSERT.equal(0xff, buffer[5]);
|
|
ASSERT.equal(0x7f, buffer[6]);
|
|
ASSERT.equal(0x7f, buffer[7]);
|
|
|
|
buffer.writeFloatLE(1.1754943508222875e-38, 0);
|
|
buffer.writeFloatBE(1.1754943508222875e-38, 4);
|
|
ASSERT.equal(0x00, buffer[0]);
|
|
ASSERT.equal(0x00, buffer[1]);
|
|
ASSERT.equal(0x80, buffer[2]);
|
|
ASSERT.equal(0x00, buffer[3]);
|
|
ASSERT.equal(0x00, buffer[4]);
|
|
ASSERT.equal(0x80, buffer[5]);
|
|
ASSERT.equal(0x00, buffer[6]);
|
|
ASSERT.equal(0x00, buffer[7]);
|
|
|
|
buffer.writeFloatBE(0 * -1, 0);
|
|
buffer.writeFloatLE(0 * -1, 4);
|
|
ASSERT.equal(0x80, buffer[0]);
|
|
ASSERT.equal(0x00, buffer[1]);
|
|
ASSERT.equal(0x00, buffer[2]);
|
|
ASSERT.equal(0x00, buffer[3]);
|
|
ASSERT.equal(0x00, buffer[4]);
|
|
ASSERT.equal(0x00, buffer[5]);
|
|
ASSERT.equal(0x00, buffer[6]);
|
|
ASSERT.equal(0x80, buffer[7]);
|
|
|
|
buffer.writeFloatBE(Infinity, 0);
|
|
buffer.writeFloatLE(Infinity, 4);
|
|
ASSERT.equal(0x7F, buffer[0]);
|
|
ASSERT.equal(0x80, buffer[1]);
|
|
ASSERT.equal(0x00, buffer[2]);
|
|
ASSERT.equal(0x00, buffer[3]);
|
|
ASSERT.equal(0x00, buffer[4]);
|
|
ASSERT.equal(0x00, buffer[5]);
|
|
ASSERT.equal(0x80, buffer[6]);
|
|
ASSERT.equal(0x7F, buffer[7]);
|
|
ASSERT.equal(Infinity, buffer.readFloatBE(0));
|
|
ASSERT.equal(Infinity, buffer.readFloatLE(4));
|
|
|
|
buffer.writeFloatBE(-Infinity, 0);
|
|
buffer.writeFloatLE(-Infinity, 4);
|
|
// Darwin ia32 does the other kind of NaN.
|
|
// Compiler bug. No one really cares.
|
|
ASSERT(0xFF === buffer[0] || 0x7F === buffer[0]);
|
|
ASSERT.equal(0x80, buffer[1]);
|
|
ASSERT.equal(0x00, buffer[2]);
|
|
ASSERT.equal(0x00, buffer[3]);
|
|
ASSERT.equal(0x00, buffer[4]);
|
|
ASSERT.equal(0x00, buffer[5]);
|
|
ASSERT.equal(0x80, buffer[6]);
|
|
ASSERT.equal(0xFF, buffer[7]);
|
|
ASSERT.equal(-Infinity, buffer.readFloatBE(0));
|
|
ASSERT.equal(-Infinity, buffer.readFloatLE(4));
|
|
|
|
buffer.writeFloatBE(NaN, 0);
|
|
buffer.writeFloatLE(NaN, 4);
|
|
// Darwin ia32 does the other kind of NaN.
|
|
// Compiler bug. No one really cares.
|
|
ASSERT(0x7F === buffer[0] || 0xFF === buffer[0]);
|
|
// mips processors use a slightly different NaN
|
|
ASSERT(0xC0 === buffer[1] || 0xBF === buffer[1]);
|
|
ASSERT(0x00 === buffer[2] || 0xFF === buffer[2]);
|
|
ASSERT(0x00 === buffer[3] || 0xFF === buffer[3]);
|
|
ASSERT(0x00 === buffer[4] || 0xFF === buffer[4]);
|
|
ASSERT(0x00 === buffer[5] || 0xFF === buffer[5]);
|
|
ASSERT(0xC0 === buffer[6] || 0xBF === buffer[6]);
|
|
// Darwin ia32 does the other kind of NaN.
|
|
// Compiler bug. No one really cares.
|
|
ASSERT(0x7F === buffer[7] || 0xFF === buffer[7]);
|
|
ASSERT.ok(isNaN(buffer.readFloatBE(0)));
|
|
ASSERT.ok(isNaN(buffer.readFloatLE(4)));
|
|
}
|
|
|
|
|
|
test(Buffer);
|