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