11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_cirequire('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// Test 32 bit float
71cb0ef41Sopenharmony_ciconst buffer = Buffer.alloc(4);
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_cibuffer[0] = 0;
101cb0ef41Sopenharmony_cibuffer[1] = 0;
111cb0ef41Sopenharmony_cibuffer[2] = 0x80;
121cb0ef41Sopenharmony_cibuffer[3] = 0x3f;
131cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readFloatBE(0), 4.600602988224807e-41);
141cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readFloatLE(0), 1);
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cibuffer[0] = 0;
171cb0ef41Sopenharmony_cibuffer[1] = 0;
181cb0ef41Sopenharmony_cibuffer[2] = 0;
191cb0ef41Sopenharmony_cibuffer[3] = 0xc0;
201cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readFloatBE(0), 2.6904930515036488e-43);
211cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readFloatLE(0), -2);
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_cibuffer[0] = 0xff;
241cb0ef41Sopenharmony_cibuffer[1] = 0xff;
251cb0ef41Sopenharmony_cibuffer[2] = 0x7f;
261cb0ef41Sopenharmony_cibuffer[3] = 0x7f;
271cb0ef41Sopenharmony_ciassert.ok(Number.isNaN(buffer.readFloatBE(0)));
281cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readFloatLE(0), 3.4028234663852886e+38);
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cibuffer[0] = 0xab;
311cb0ef41Sopenharmony_cibuffer[1] = 0xaa;
321cb0ef41Sopenharmony_cibuffer[2] = 0xaa;
331cb0ef41Sopenharmony_cibuffer[3] = 0x3e;
341cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readFloatBE(0), -1.2126478207002966e-12);
351cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readFloatLE(0), 0.3333333432674408);
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_cibuffer[0] = 0;
381cb0ef41Sopenharmony_cibuffer[1] = 0;
391cb0ef41Sopenharmony_cibuffer[2] = 0;
401cb0ef41Sopenharmony_cibuffer[3] = 0;
411cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readFloatBE(0), 0);
421cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readFloatLE(0), 0);
431cb0ef41Sopenharmony_ciassert.ok(1 / buffer.readFloatLE(0) >= 0);
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_cibuffer[3] = 0x80;
461cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readFloatBE(0), 1.793662034335766e-43);
471cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readFloatLE(0), -0);
481cb0ef41Sopenharmony_ciassert.ok(1 / buffer.readFloatLE(0) < 0);
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_cibuffer[0] = 0;
511cb0ef41Sopenharmony_cibuffer[1] = 0;
521cb0ef41Sopenharmony_cibuffer[2] = 0x80;
531cb0ef41Sopenharmony_cibuffer[3] = 0x7f;
541cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readFloatBE(0), 4.609571298396486e-41);
551cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readFloatLE(0), Infinity);
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_cibuffer[0] = 0;
581cb0ef41Sopenharmony_cibuffer[1] = 0;
591cb0ef41Sopenharmony_cibuffer[2] = 0x80;
601cb0ef41Sopenharmony_cibuffer[3] = 0xff;
611cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readFloatBE(0), 4.627507918739843e-41);
621cb0ef41Sopenharmony_ciassert.strictEqual(buffer.readFloatLE(0), -Infinity);
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci['readFloatLE', 'readFloatBE'].forEach((fn) => {
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  // Verify that default offset works fine.
671cb0ef41Sopenharmony_ci  buffer[fn](undefined);
681cb0ef41Sopenharmony_ci  buffer[fn]();
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  ['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
711cb0ef41Sopenharmony_ci    assert.throws(
721cb0ef41Sopenharmony_ci      () => buffer[fn](off),
731cb0ef41Sopenharmony_ci      { code: 'ERR_INVALID_ARG_TYPE' }
741cb0ef41Sopenharmony_ci    );
751cb0ef41Sopenharmony_ci  });
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  [Infinity, -1, 1].forEach((offset) => {
781cb0ef41Sopenharmony_ci    assert.throws(
791cb0ef41Sopenharmony_ci      () => buffer[fn](offset),
801cb0ef41Sopenharmony_ci      {
811cb0ef41Sopenharmony_ci        code: 'ERR_OUT_OF_RANGE',
821cb0ef41Sopenharmony_ci        name: 'RangeError',
831cb0ef41Sopenharmony_ci        message: 'The value of "offset" is out of range. ' +
841cb0ef41Sopenharmony_ci                 `It must be >= 0 and <= 0. Received ${offset}`
851cb0ef41Sopenharmony_ci      });
861cb0ef41Sopenharmony_ci  });
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci  assert.throws(
891cb0ef41Sopenharmony_ci    () => Buffer.alloc(1)[fn](1),
901cb0ef41Sopenharmony_ci    {
911cb0ef41Sopenharmony_ci      code: 'ERR_BUFFER_OUT_OF_BOUNDS',
921cb0ef41Sopenharmony_ci      name: 'RangeError',
931cb0ef41Sopenharmony_ci      message: 'Attempt to access memory outside buffer bounds'
941cb0ef41Sopenharmony_ci    });
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci  [NaN, 1.01].forEach((offset) => {
971cb0ef41Sopenharmony_ci    assert.throws(
981cb0ef41Sopenharmony_ci      () => buffer[fn](offset),
991cb0ef41Sopenharmony_ci      {
1001cb0ef41Sopenharmony_ci        code: 'ERR_OUT_OF_RANGE',
1011cb0ef41Sopenharmony_ci        name: 'RangeError',
1021cb0ef41Sopenharmony_ci        message: 'The value of "offset" is out of range. ' +
1031cb0ef41Sopenharmony_ci                 `It must be an integer. Received ${offset}`
1041cb0ef41Sopenharmony_ci      });
1051cb0ef41Sopenharmony_ci  });
1061cb0ef41Sopenharmony_ci});
107