11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_cirequire('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// Test OOB
71cb0ef41Sopenharmony_ci{
81cb0ef41Sopenharmony_ci  const buffer = Buffer.alloc(4);
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci  ['Int8', 'Int16BE', 'Int16LE', 'Int32BE', 'Int32LE'].forEach((fn) => {
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci    // Verify that default offset works fine.
131cb0ef41Sopenharmony_ci    buffer[`read${fn}`](undefined);
141cb0ef41Sopenharmony_ci    buffer[`read${fn}`]();
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci    ['', '0', null, {}, [], () => {}, true, false].forEach((o) => {
171cb0ef41Sopenharmony_ci      assert.throws(
181cb0ef41Sopenharmony_ci        () => buffer[`read${fn}`](o),
191cb0ef41Sopenharmony_ci        {
201cb0ef41Sopenharmony_ci          code: 'ERR_INVALID_ARG_TYPE',
211cb0ef41Sopenharmony_ci          name: 'TypeError'
221cb0ef41Sopenharmony_ci        });
231cb0ef41Sopenharmony_ci    });
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci    [Infinity, -1, -4294967295].forEach((offset) => {
261cb0ef41Sopenharmony_ci      assert.throws(
271cb0ef41Sopenharmony_ci        () => buffer[`read${fn}`](offset),
281cb0ef41Sopenharmony_ci        {
291cb0ef41Sopenharmony_ci          code: 'ERR_OUT_OF_RANGE',
301cb0ef41Sopenharmony_ci          name: 'RangeError'
311cb0ef41Sopenharmony_ci        });
321cb0ef41Sopenharmony_ci    });
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci    [NaN, 1.01].forEach((offset) => {
351cb0ef41Sopenharmony_ci      assert.throws(
361cb0ef41Sopenharmony_ci        () => buffer[`read${fn}`](offset),
371cb0ef41Sopenharmony_ci        {
381cb0ef41Sopenharmony_ci          code: 'ERR_OUT_OF_RANGE',
391cb0ef41Sopenharmony_ci          name: 'RangeError',
401cb0ef41Sopenharmony_ci          message: 'The value of "offset" is out of range. ' +
411cb0ef41Sopenharmony_ci                   `It must be an integer. Received ${offset}`
421cb0ef41Sopenharmony_ci        });
431cb0ef41Sopenharmony_ci    });
441cb0ef41Sopenharmony_ci  });
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci// Test 8 bit signed integers
481cb0ef41Sopenharmony_ci{
491cb0ef41Sopenharmony_ci  const data = Buffer.from([0x23, 0xab, 0x7c, 0xef]);
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  assert.strictEqual(data.readInt8(0), 0x23);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  data[0] = 0xff;
541cb0ef41Sopenharmony_ci  assert.strictEqual(data.readInt8(0), -1);
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  data[0] = 0x87;
571cb0ef41Sopenharmony_ci  assert.strictEqual(data.readInt8(0), -121);
581cb0ef41Sopenharmony_ci  assert.strictEqual(data.readInt8(1), -85);
591cb0ef41Sopenharmony_ci  assert.strictEqual(data.readInt8(2), 124);
601cb0ef41Sopenharmony_ci  assert.strictEqual(data.readInt8(3), -17);
611cb0ef41Sopenharmony_ci}
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci// Test 16 bit integers
641cb0ef41Sopenharmony_ci{
651cb0ef41Sopenharmony_ci  const buffer = Buffer.from([0x16, 0x79, 0x65, 0x6e, 0x69, 0x78]);
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt16BE(0), 0x1679);
681cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt16LE(0), 0x7916);
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  buffer[0] = 0xff;
711cb0ef41Sopenharmony_ci  buffer[1] = 0x80;
721cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt16BE(0), -128);
731cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt16LE(0), -32513);
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  buffer[0] = 0x77;
761cb0ef41Sopenharmony_ci  buffer[1] = 0x65;
771cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt16BE(0), 0x7765);
781cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt16BE(1), 0x6565);
791cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt16BE(2), 0x656e);
801cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt16BE(3), 0x6e69);
811cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt16BE(4), 0x6978);
821cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt16LE(0), 0x6577);
831cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt16LE(1), 0x6565);
841cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt16LE(2), 0x6e65);
851cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt16LE(3), 0x696e);
861cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt16LE(4), 0x7869);
871cb0ef41Sopenharmony_ci}
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci// Test 32 bit integers
901cb0ef41Sopenharmony_ci{
911cb0ef41Sopenharmony_ci  const buffer = Buffer.from([0x43, 0x53, 0x16, 0x79, 0x36, 0x17]);
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt32BE(0), 0x43531679);
941cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt32LE(0), 0x79165343);
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci  buffer[0] = 0xff;
971cb0ef41Sopenharmony_ci  buffer[1] = 0xfe;
981cb0ef41Sopenharmony_ci  buffer[2] = 0xef;
991cb0ef41Sopenharmony_ci  buffer[3] = 0xfa;
1001cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt32BE(0), -69638);
1011cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt32LE(0), -84934913);
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci  buffer[0] = 0x42;
1041cb0ef41Sopenharmony_ci  buffer[1] = 0xc3;
1051cb0ef41Sopenharmony_ci  buffer[2] = 0x95;
1061cb0ef41Sopenharmony_ci  buffer[3] = 0xa9;
1071cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt32BE(0), 0x42c395a9);
1081cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt32BE(1), -1013601994);
1091cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt32BE(2), -1784072681);
1101cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt32LE(0), -1449802942);
1111cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt32LE(1), 917083587);
1121cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readInt32LE(2), 389458325);
1131cb0ef41Sopenharmony_ci}
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci// Test Int
1161cb0ef41Sopenharmony_ci{
1171cb0ef41Sopenharmony_ci  const buffer = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readIntLE(0, 1), 0x01);
1201cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readIntBE(0, 1), 0x01);
1211cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readIntLE(0, 3), 0x030201);
1221cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readIntBE(0, 3), 0x010203);
1231cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readIntLE(0, 5), 0x0504030201);
1241cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readIntBE(0, 5), 0x0102030405);
1251cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readIntLE(0, 6), 0x060504030201);
1261cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readIntBE(0, 6), 0x010203040506);
1271cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readIntLE(1, 6), 0x070605040302);
1281cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readIntBE(1, 6), 0x020304050607);
1291cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readIntLE(2, 6), 0x080706050403);
1301cb0ef41Sopenharmony_ci  assert.strictEqual(buffer.readIntBE(2, 6), 0x030405060708);
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  // Check byteLength.
1331cb0ef41Sopenharmony_ci  ['readIntBE', 'readIntLE'].forEach((fn) => {
1341cb0ef41Sopenharmony_ci    ['', '0', null, {}, [], () => {}, true, false, undefined].forEach((len) => {
1351cb0ef41Sopenharmony_ci      assert.throws(
1361cb0ef41Sopenharmony_ci        () => buffer[fn](0, len),
1371cb0ef41Sopenharmony_ci        { code: 'ERR_INVALID_ARG_TYPE' });
1381cb0ef41Sopenharmony_ci    });
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci    [Infinity, -1].forEach((byteLength) => {
1411cb0ef41Sopenharmony_ci      assert.throws(
1421cb0ef41Sopenharmony_ci        () => buffer[fn](0, byteLength),
1431cb0ef41Sopenharmony_ci        {
1441cb0ef41Sopenharmony_ci          code: 'ERR_OUT_OF_RANGE',
1451cb0ef41Sopenharmony_ci          message: 'The value of "byteLength" is out of range. ' +
1461cb0ef41Sopenharmony_ci                   `It must be >= 1 and <= 6. Received ${byteLength}`
1471cb0ef41Sopenharmony_ci        });
1481cb0ef41Sopenharmony_ci    });
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci    [NaN, 1.01].forEach((byteLength) => {
1511cb0ef41Sopenharmony_ci      assert.throws(
1521cb0ef41Sopenharmony_ci        () => buffer[fn](0, byteLength),
1531cb0ef41Sopenharmony_ci        {
1541cb0ef41Sopenharmony_ci          code: 'ERR_OUT_OF_RANGE',
1551cb0ef41Sopenharmony_ci          name: 'RangeError',
1561cb0ef41Sopenharmony_ci          message: 'The value of "byteLength" is out of range. ' +
1571cb0ef41Sopenharmony_ci                   `It must be an integer. Received ${byteLength}`
1581cb0ef41Sopenharmony_ci        });
1591cb0ef41Sopenharmony_ci    });
1601cb0ef41Sopenharmony_ci  });
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ci  // Test 1 to 6 bytes.
1631cb0ef41Sopenharmony_ci  for (let i = 1; i <= 6; i++) {
1641cb0ef41Sopenharmony_ci    ['readIntBE', 'readIntLE'].forEach((fn) => {
1651cb0ef41Sopenharmony_ci      ['', '0', null, {}, [], () => {}, true, false, undefined].forEach((o) => {
1661cb0ef41Sopenharmony_ci        assert.throws(
1671cb0ef41Sopenharmony_ci          () => buffer[fn](o, i),
1681cb0ef41Sopenharmony_ci          {
1691cb0ef41Sopenharmony_ci            code: 'ERR_INVALID_ARG_TYPE',
1701cb0ef41Sopenharmony_ci            name: 'TypeError'
1711cb0ef41Sopenharmony_ci          });
1721cb0ef41Sopenharmony_ci      });
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci      [Infinity, -1, -4294967295].forEach((offset) => {
1751cb0ef41Sopenharmony_ci        assert.throws(
1761cb0ef41Sopenharmony_ci          () => buffer[fn](offset, i),
1771cb0ef41Sopenharmony_ci          {
1781cb0ef41Sopenharmony_ci            code: 'ERR_OUT_OF_RANGE',
1791cb0ef41Sopenharmony_ci            name: 'RangeError',
1801cb0ef41Sopenharmony_ci            message: 'The value of "offset" is out of range. ' +
1811cb0ef41Sopenharmony_ci                     `It must be >= 0 and <= ${8 - i}. Received ${offset}`
1821cb0ef41Sopenharmony_ci          });
1831cb0ef41Sopenharmony_ci      });
1841cb0ef41Sopenharmony_ci
1851cb0ef41Sopenharmony_ci      [NaN, 1.01].forEach((offset) => {
1861cb0ef41Sopenharmony_ci        assert.throws(
1871cb0ef41Sopenharmony_ci          () => buffer[fn](offset, i),
1881cb0ef41Sopenharmony_ci          {
1891cb0ef41Sopenharmony_ci            code: 'ERR_OUT_OF_RANGE',
1901cb0ef41Sopenharmony_ci            name: 'RangeError',
1911cb0ef41Sopenharmony_ci            message: 'The value of "offset" is out of range. ' +
1921cb0ef41Sopenharmony_ci                     `It must be an integer. Received ${offset}`
1931cb0ef41Sopenharmony_ci          });
1941cb0ef41Sopenharmony_ci      });
1951cb0ef41Sopenharmony_ci    });
1961cb0ef41Sopenharmony_ci  }
1971cb0ef41Sopenharmony_ci}
198