/** * single- and double-precision floating point implementation test * * Copyright (C) 2017-2019 Andras Radics * Licensed under the Apache License, Version 2.0 */ 'use strict'; var fp = require('./'); var isBigeCpu; var testValues = [ 0, -0, 1, -1, 1e10, -1e10, 1e-10, -1e-10, 123e10, -123e10, 123e-10, -123e-10, 0.25, Math.pow(2, 40), NaN, Infinity, -Infinity, 1 * Math.pow(2, 129), 1 * Math.pow(2, -129), 1.0171355313990822e-45, // tiny denorm, just 1 bit 1.102423499998344e-38, // large denorm, 1.111 2e-127 6.3382528129361155e+29, // mantissa carry-out rounding error 4.2199627472983003e-11, // FloatLE rounding error 5e-324, 1.206082573339918e-308, 1024, 192648575.99999553, 1e203, Math.pow(2, 10), Math.pow(2, 100), 1.1754942807573643e-38, // 0xFFFFFF / 0x1000000 * Math.pow(2, -126), denorm float that rounds to norm 8.758115402030107e-47, // Math.pow(2, -126-27), undersmall value that rounds to zero 1 + (0xffffffffff / 0x10000000000), // 1.ffffffffff, overflows into hidden 1s bit after rounding ]; var tmpbuf = new Buffer(10); var fpbuf = new Buffer(tmpbuf); // // Check that the value val as read/written by nodejs is bit-for-bit identical to ieee-float. // Type is the operation to tests, one of 'FloatLE', 'FloatBE', 'DoubleLE' or 'DoubleBE'. // function checkValue( t, val, type ) { var read = 'read' + type; var write = 'write' + type; if (typeof val === 'number') { // nodejs stores NaN from the math library as received, which can be // positive/negative/signaling/non-signaling. Normalize them all to // the standard javascript NaN so the bit patterns are identical. if (!val && val !== 0) val = NaN; tmpbuf[write](val, 0); var expect = tmpbuf[read](0); fp[write](fpbuf, val); } else { for (var i=0; i<8; i++) tmpbuf[i] = val[i]; var expect = tmpbuf[read](0); if (!expect && expect !== 0) expect = NaN; fp[write](fpbuf, expect); } var v2 = fp[read](tmpbuf, 0); if (!expect) { if (expect !== 0) t.ok(isNaN(v2), "wanted NaN: " + v2); else if (expect == 0 && 1/expect < 0) { t.equal(v2, 0); t.equal(1/v2, -Infinity, "wanted -0: " + v2 + ", " + 1/v2); } else if (expect == 0 && 1/expect > 0) { t.equal(v2, 0); t.equal(1/v2, +Infinity, "wanted +0: " + v2 + ", " + 1/v2); } } else t.equal(v2, expect); if (expect) { // compare non-NaN values directly // the compareBytes loop is faster than deepEqual, but use deepEqual for the error message if (!compareBytes(fpbuf, tmpbuf, "")) t.deepEqual(fpbuf, tmpbuf, write + " " + expect); } else { // there are many flavors of NaN, check stored (normalized) bit patterns // avoid isNaN, it slows the test greatly tmpbuf[write](expect, 0); if (!compareBytes(fpbuf, tmpbuf, "")) t.deepEqual(fpbuf, tmpbuf, write + " " + expect); } } function compareBytes( a, b ) { for (var i=0; i<8; i++) if (a[i] !== b[i]) return false; return true; } module.exports = { before: function(done) { isBigeCpu = fp._getBigeCpu(); done(); }, beforeEach: function(done) { // clear temps used by checkValue doubles for (var i=0; i<10; i++) { tmpbuf[i] = 0; fpbuf[i] = 0; } done(); }, 'should export expected functions': function(t) { var fp = require('./'); var funcs = ['readWord', 'writeWord', 'writeDoubleWord', 'readFloatLE', 'readFloatBE', 'readDoubleLE', 'readDoubleBE', 'writeFloatLE', 'writeFloatBE', 'writeDoubleLE', 'writeDoubleBE']; for (var i=0; i