11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_cirequire('../common'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci// Test (64 bit) double 71cb0ef41Sopenharmony_ciconst buffer = Buffer.allocUnsafe(8); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_cibuffer[0] = 0x55; 101cb0ef41Sopenharmony_cibuffer[1] = 0x55; 111cb0ef41Sopenharmony_cibuffer[2] = 0x55; 121cb0ef41Sopenharmony_cibuffer[3] = 0x55; 131cb0ef41Sopenharmony_cibuffer[4] = 0x55; 141cb0ef41Sopenharmony_cibuffer[5] = 0x55; 151cb0ef41Sopenharmony_cibuffer[6] = 0xd5; 161cb0ef41Sopenharmony_cibuffer[7] = 0x3f; 171cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleBE(0), 1.1945305291680097e+103); 181cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleLE(0), 0.3333333333333333); 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_cibuffer[0] = 1; 211cb0ef41Sopenharmony_cibuffer[1] = 0; 221cb0ef41Sopenharmony_cibuffer[2] = 0; 231cb0ef41Sopenharmony_cibuffer[3] = 0; 241cb0ef41Sopenharmony_cibuffer[4] = 0; 251cb0ef41Sopenharmony_cibuffer[5] = 0; 261cb0ef41Sopenharmony_cibuffer[6] = 0xf0; 271cb0ef41Sopenharmony_cibuffer[7] = 0x3f; 281cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleBE(0), 7.291122019655968e-304); 291cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleLE(0), 1.0000000000000002); 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_cibuffer[0] = 2; 321cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleBE(0), 4.778309726801735e-299); 331cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleLE(0), 1.0000000000000004); 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_cibuffer[0] = 1; 361cb0ef41Sopenharmony_cibuffer[6] = 0; 371cb0ef41Sopenharmony_cibuffer[7] = 0; 381cb0ef41Sopenharmony_ci// eslint-disable-next-line no-loss-of-precision 391cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleBE(0), 7.291122019556398e-304); 401cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleLE(0), 5e-324); 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_cibuffer[0] = 0xff; 431cb0ef41Sopenharmony_cibuffer[1] = 0xff; 441cb0ef41Sopenharmony_cibuffer[2] = 0xff; 451cb0ef41Sopenharmony_cibuffer[3] = 0xff; 461cb0ef41Sopenharmony_cibuffer[4] = 0xff; 471cb0ef41Sopenharmony_cibuffer[5] = 0xff; 481cb0ef41Sopenharmony_cibuffer[6] = 0x0f; 491cb0ef41Sopenharmony_cibuffer[7] = 0x00; 501cb0ef41Sopenharmony_ciassert.ok(Number.isNaN(buffer.readDoubleBE(0))); 511cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleLE(0), 2.225073858507201e-308); 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_cibuffer[6] = 0xef; 541cb0ef41Sopenharmony_cibuffer[7] = 0x7f; 551cb0ef41Sopenharmony_ciassert.ok(Number.isNaN(buffer.readDoubleBE(0))); 561cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleLE(0), 1.7976931348623157e+308); 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_cibuffer[0] = 0; 591cb0ef41Sopenharmony_cibuffer[1] = 0; 601cb0ef41Sopenharmony_cibuffer[2] = 0; 611cb0ef41Sopenharmony_cibuffer[3] = 0; 621cb0ef41Sopenharmony_cibuffer[4] = 0; 631cb0ef41Sopenharmony_cibuffer[5] = 0; 641cb0ef41Sopenharmony_cibuffer[6] = 0xf0; 651cb0ef41Sopenharmony_cibuffer[7] = 0x3f; 661cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleBE(0), 3.03865e-319); 671cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleLE(0), 1); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_cibuffer[6] = 0; 701cb0ef41Sopenharmony_cibuffer[7] = 0x40; 711cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleBE(0), 3.16e-322); 721cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleLE(0), 2); 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_cibuffer[7] = 0xc0; 751cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleBE(0), 9.5e-322); 761cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleLE(0), -2); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_cibuffer[6] = 0x10; 791cb0ef41Sopenharmony_cibuffer[7] = 0; 801cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleBE(0), 2.0237e-320); 811cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleLE(0), 2.2250738585072014e-308); 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_cibuffer[6] = 0; 841cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleBE(0), 0); 851cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleLE(0), 0); 861cb0ef41Sopenharmony_ciassert.ok(1 / buffer.readDoubleLE(0) >= 0); 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_cibuffer[7] = 0x80; 891cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleBE(0), 6.3e-322); 901cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleLE(0), -0); 911cb0ef41Sopenharmony_ciassert.ok(1 / buffer.readDoubleLE(0) < 0); 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_cibuffer[6] = 0xf0; 941cb0ef41Sopenharmony_cibuffer[7] = 0x7f; 951cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleBE(0), 3.0418e-319); 961cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleLE(0), Infinity); 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_cibuffer[7] = 0xff; 991cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleBE(0), 3.04814e-319); 1001cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readDoubleLE(0), -Infinity); 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci['readDoubleLE', 'readDoubleBE'].forEach((fn) => { 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_ci // Verify that default offset works fine. 1051cb0ef41Sopenharmony_ci buffer[fn](undefined); 1061cb0ef41Sopenharmony_ci buffer[fn](); 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci ['', '0', null, {}, [], () => {}, true, false].forEach((off) => { 1091cb0ef41Sopenharmony_ci assert.throws( 1101cb0ef41Sopenharmony_ci () => buffer[fn](off), 1111cb0ef41Sopenharmony_ci { code: 'ERR_INVALID_ARG_TYPE' } 1121cb0ef41Sopenharmony_ci ); 1131cb0ef41Sopenharmony_ci }); 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci [Infinity, -1, 1].forEach((offset) => { 1161cb0ef41Sopenharmony_ci assert.throws( 1171cb0ef41Sopenharmony_ci () => buffer[fn](offset), 1181cb0ef41Sopenharmony_ci { 1191cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 1201cb0ef41Sopenharmony_ci name: 'RangeError', 1211cb0ef41Sopenharmony_ci message: 'The value of "offset" is out of range. ' + 1221cb0ef41Sopenharmony_ci `It must be >= 0 and <= 0. Received ${offset}` 1231cb0ef41Sopenharmony_ci }); 1241cb0ef41Sopenharmony_ci }); 1251cb0ef41Sopenharmony_ci 1261cb0ef41Sopenharmony_ci assert.throws( 1271cb0ef41Sopenharmony_ci () => Buffer.alloc(1)[fn](1), 1281cb0ef41Sopenharmony_ci { 1291cb0ef41Sopenharmony_ci code: 'ERR_BUFFER_OUT_OF_BOUNDS', 1301cb0ef41Sopenharmony_ci name: 'RangeError', 1311cb0ef41Sopenharmony_ci message: 'Attempt to access memory outside buffer bounds' 1321cb0ef41Sopenharmony_ci }); 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ci [NaN, 1.01].forEach((offset) => { 1351cb0ef41Sopenharmony_ci assert.throws( 1361cb0ef41Sopenharmony_ci () => buffer[fn](offset), 1371cb0ef41Sopenharmony_ci { 1381cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 1391cb0ef41Sopenharmony_ci name: 'RangeError', 1401cb0ef41Sopenharmony_ci message: 'The value of "offset" is out of range. ' + 1411cb0ef41Sopenharmony_ci `It must be an integer. Received ${offset}` 1421cb0ef41Sopenharmony_ci }); 1431cb0ef41Sopenharmony_ci }); 1441cb0ef41Sopenharmony_ci}); 145