11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci// Tests to verify doubles are correctly written 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_cirequire('../common'); 61cb0ef41Sopenharmony_ciconst assert = require('assert'); 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciconst buffer = Buffer.allocUnsafe(16); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_cibuffer.writeDoubleBE(2.225073858507201e-308, 0); 111cb0ef41Sopenharmony_cibuffer.writeDoubleLE(2.225073858507201e-308, 8); 121cb0ef41Sopenharmony_ciassert.ok(buffer.equals(new Uint8Array([ 131cb0ef41Sopenharmony_ci 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 141cb0ef41Sopenharmony_ci 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 151cb0ef41Sopenharmony_ci]))); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_cibuffer.writeDoubleBE(1.0000000000000004, 0); 181cb0ef41Sopenharmony_cibuffer.writeDoubleLE(1.0000000000000004, 8); 191cb0ef41Sopenharmony_ciassert.ok(buffer.equals(new Uint8Array([ 201cb0ef41Sopenharmony_ci 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 211cb0ef41Sopenharmony_ci 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 221cb0ef41Sopenharmony_ci]))); 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_cibuffer.writeDoubleBE(-2, 0); 251cb0ef41Sopenharmony_cibuffer.writeDoubleLE(-2, 8); 261cb0ef41Sopenharmony_ciassert.ok(buffer.equals(new Uint8Array([ 271cb0ef41Sopenharmony_ci 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 281cb0ef41Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 291cb0ef41Sopenharmony_ci]))); 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_cibuffer.writeDoubleBE(1.7976931348623157e+308, 0); 321cb0ef41Sopenharmony_cibuffer.writeDoubleLE(1.7976931348623157e+308, 8); 331cb0ef41Sopenharmony_ciassert.ok(buffer.equals(new Uint8Array([ 341cb0ef41Sopenharmony_ci 0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 351cb0ef41Sopenharmony_ci 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0x7f, 361cb0ef41Sopenharmony_ci]))); 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_cibuffer.writeDoubleBE(0 * -1, 0); 391cb0ef41Sopenharmony_cibuffer.writeDoubleLE(0 * -1, 8); 401cb0ef41Sopenharmony_ciassert.ok(buffer.equals(new Uint8Array([ 411cb0ef41Sopenharmony_ci 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 421cb0ef41Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 431cb0ef41Sopenharmony_ci]))); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_cibuffer.writeDoubleBE(Infinity, 0); 461cb0ef41Sopenharmony_cibuffer.writeDoubleLE(Infinity, 8); 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ciassert.ok(buffer.equals(new Uint8Array([ 491cb0ef41Sopenharmony_ci 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 501cb0ef41Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x7F, 511cb0ef41Sopenharmony_ci]))); 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleBE(0), Infinity); 541cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleLE(8), Infinity); 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_cibuffer.writeDoubleBE(-Infinity, 0); 571cb0ef41Sopenharmony_cibuffer.writeDoubleLE(-Infinity, 8); 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ciassert.ok(buffer.equals(new Uint8Array([ 601cb0ef41Sopenharmony_ci 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 611cb0ef41Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xFF, 621cb0ef41Sopenharmony_ci]))); 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleBE(0), -Infinity); 651cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleLE(8), -Infinity); 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_cibuffer.writeDoubleBE(NaN, 0); 681cb0ef41Sopenharmony_cibuffer.writeDoubleLE(NaN, 8); 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci// JS only knows a single NaN but there exist two platform specific 711cb0ef41Sopenharmony_ci// implementations. Therefore, allow both quiet and signalling NaNs. 721cb0ef41Sopenharmony_ciif (buffer[1] === 0xF7) { 731cb0ef41Sopenharmony_ci assert.ok(buffer.equals(new Uint8Array([ 741cb0ef41Sopenharmony_ci 0x7F, 0xF7, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 751cb0ef41Sopenharmony_ci 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF7, 0x7F, 761cb0ef41Sopenharmony_ci ]))); 771cb0ef41Sopenharmony_ci} else { 781cb0ef41Sopenharmony_ci assert.ok(buffer.equals(new Uint8Array([ 791cb0ef41Sopenharmony_ci 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 801cb0ef41Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x7F, 811cb0ef41Sopenharmony_ci ]))); 821cb0ef41Sopenharmony_ci} 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ciassert.ok(Number.isNaN(buffer.readDoubleBE(0))); 851cb0ef41Sopenharmony_ciassert.ok(Number.isNaN(buffer.readDoubleLE(8))); 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci// OOB in writeDouble{LE,BE} should throw. 881cb0ef41Sopenharmony_ci{ 891cb0ef41Sopenharmony_ci const small = Buffer.allocUnsafe(1); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci ['writeDoubleLE', 'writeDoubleBE'].forEach((fn) => { 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_ci // Verify that default offset works fine. 941cb0ef41Sopenharmony_ci buffer[fn](23, undefined); 951cb0ef41Sopenharmony_ci buffer[fn](23); 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ci assert.throws( 981cb0ef41Sopenharmony_ci () => small[fn](11.11, 0), 991cb0ef41Sopenharmony_ci { 1001cb0ef41Sopenharmony_ci code: 'ERR_BUFFER_OUT_OF_BOUNDS', 1011cb0ef41Sopenharmony_ci name: 'RangeError', 1021cb0ef41Sopenharmony_ci message: 'Attempt to access memory outside buffer bounds' 1031cb0ef41Sopenharmony_ci }); 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci ['', '0', null, {}, [], () => {}, true, false].forEach((off) => { 1061cb0ef41Sopenharmony_ci assert.throws( 1071cb0ef41Sopenharmony_ci () => small[fn](23, off), 1081cb0ef41Sopenharmony_ci { code: 'ERR_INVALID_ARG_TYPE' }); 1091cb0ef41Sopenharmony_ci }); 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ci [Infinity, -1, 9].forEach((offset) => { 1121cb0ef41Sopenharmony_ci assert.throws( 1131cb0ef41Sopenharmony_ci () => buffer[fn](23, offset), 1141cb0ef41Sopenharmony_ci { 1151cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 1161cb0ef41Sopenharmony_ci name: 'RangeError', 1171cb0ef41Sopenharmony_ci message: 'The value of "offset" is out of range. ' + 1181cb0ef41Sopenharmony_ci `It must be >= 0 and <= 8. Received ${offset}` 1191cb0ef41Sopenharmony_ci }); 1201cb0ef41Sopenharmony_ci }); 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci [NaN, 1.01].forEach((offset) => { 1231cb0ef41Sopenharmony_ci assert.throws( 1241cb0ef41Sopenharmony_ci () => buffer[fn](42, offset), 1251cb0ef41Sopenharmony_ci { 1261cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 1271cb0ef41Sopenharmony_ci name: 'RangeError', 1281cb0ef41Sopenharmony_ci message: 'The value of "offset" is out of range. ' + 1291cb0ef41Sopenharmony_ci `It must be an integer. Received ${offset}` 1301cb0ef41Sopenharmony_ci }); 1311cb0ef41Sopenharmony_ci }); 1321cb0ef41Sopenharmony_ci }); 1331cb0ef41Sopenharmony_ci} 134