11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Tests to verify signed integers are correctly written
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_cirequire('../common');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst errorOutOfBounds = {
81cb0ef41Sopenharmony_ci  code: 'ERR_OUT_OF_RANGE',
91cb0ef41Sopenharmony_ci  name: 'RangeError',
101cb0ef41Sopenharmony_ci  message: new RegExp('^The value of "value" is out of range\\. ' +
111cb0ef41Sopenharmony_ci                      'It must be >= -\\d+ and <= \\d+\\. Received .+$')
121cb0ef41Sopenharmony_ci};
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci// Test 8 bit
151cb0ef41Sopenharmony_ci{
161cb0ef41Sopenharmony_ci  const buffer = Buffer.alloc(2);
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  buffer.writeInt8(0x23, 0);
191cb0ef41Sopenharmony_ci  buffer.writeInt8(-5, 1);
201cb0ef41Sopenharmony_ci  assert.ok(buffer.equals(new Uint8Array([ 0x23, 0xfb ])));
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  /* Make sure we handle min/max correctly */
231cb0ef41Sopenharmony_ci  buffer.writeInt8(0x7f, 0);
241cb0ef41Sopenharmony_ci  buffer.writeInt8(-0x80, 1);
251cb0ef41Sopenharmony_ci  assert.ok(buffer.equals(new Uint8Array([ 0x7f, 0x80 ])));
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  assert.throws(() => {
281cb0ef41Sopenharmony_ci    buffer.writeInt8(0x7f + 1, 0);
291cb0ef41Sopenharmony_ci  }, errorOutOfBounds);
301cb0ef41Sopenharmony_ci  assert.throws(() => {
311cb0ef41Sopenharmony_ci    buffer.writeInt8(-0x80 - 1, 0);
321cb0ef41Sopenharmony_ci  }, errorOutOfBounds);
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  // Verify that default offset works fine.
351cb0ef41Sopenharmony_ci  buffer.writeInt8(23, undefined);
361cb0ef41Sopenharmony_ci  buffer.writeInt8(23);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  ['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
391cb0ef41Sopenharmony_ci    assert.throws(
401cb0ef41Sopenharmony_ci      () => buffer.writeInt8(23, off),
411cb0ef41Sopenharmony_ci      { code: 'ERR_INVALID_ARG_TYPE' });
421cb0ef41Sopenharmony_ci  });
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  [NaN, Infinity, -1, 1.01].forEach((off) => {
451cb0ef41Sopenharmony_ci    assert.throws(
461cb0ef41Sopenharmony_ci      () => buffer.writeInt8(23, off),
471cb0ef41Sopenharmony_ci      { code: 'ERR_OUT_OF_RANGE' });
481cb0ef41Sopenharmony_ci  });
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci// Test 16 bit
521cb0ef41Sopenharmony_ci{
531cb0ef41Sopenharmony_ci  const buffer = Buffer.alloc(4);
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  buffer.writeInt16BE(0x0023, 0);
561cb0ef41Sopenharmony_ci  buffer.writeInt16LE(0x0023, 2);
571cb0ef41Sopenharmony_ci  assert.ok(buffer.equals(new Uint8Array([ 0x00, 0x23, 0x23, 0x00 ])));
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  buffer.writeInt16BE(-5, 0);
601cb0ef41Sopenharmony_ci  buffer.writeInt16LE(-5, 2);
611cb0ef41Sopenharmony_ci  assert.ok(buffer.equals(new Uint8Array([ 0xff, 0xfb, 0xfb, 0xff ])));
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  buffer.writeInt16BE(-1679, 0);
641cb0ef41Sopenharmony_ci  buffer.writeInt16LE(-1679, 2);
651cb0ef41Sopenharmony_ci  assert.ok(buffer.equals(new Uint8Array([ 0xf9, 0x71, 0x71, 0xf9 ])));
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  /* Make sure we handle min/max correctly */
681cb0ef41Sopenharmony_ci  buffer.writeInt16BE(0x7fff, 0);
691cb0ef41Sopenharmony_ci  buffer.writeInt16BE(-0x8000, 2);
701cb0ef41Sopenharmony_ci  assert.ok(buffer.equals(new Uint8Array([ 0x7f, 0xff, 0x80, 0x00 ])));
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  buffer.writeInt16LE(0x7fff, 0);
731cb0ef41Sopenharmony_ci  buffer.writeInt16LE(-0x8000, 2);
741cb0ef41Sopenharmony_ci  assert.ok(buffer.equals(new Uint8Array([ 0xff, 0x7f, 0x00, 0x80 ])));
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  ['writeInt16BE', 'writeInt16LE'].forEach((fn) => {
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci    // Verify that default offset works fine.
791cb0ef41Sopenharmony_ci    buffer[fn](23, undefined);
801cb0ef41Sopenharmony_ci    buffer[fn](23);
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci    assert.throws(() => {
831cb0ef41Sopenharmony_ci      buffer[fn](0x7fff + 1, 0);
841cb0ef41Sopenharmony_ci    }, errorOutOfBounds);
851cb0ef41Sopenharmony_ci    assert.throws(() => {
861cb0ef41Sopenharmony_ci      buffer[fn](-0x8000 - 1, 0);
871cb0ef41Sopenharmony_ci    }, errorOutOfBounds);
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci    ['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
901cb0ef41Sopenharmony_ci      assert.throws(
911cb0ef41Sopenharmony_ci        () => buffer[fn](23, off),
921cb0ef41Sopenharmony_ci        { code: 'ERR_INVALID_ARG_TYPE' });
931cb0ef41Sopenharmony_ci    });
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci    [NaN, Infinity, -1, 1.01].forEach((off) => {
961cb0ef41Sopenharmony_ci      assert.throws(
971cb0ef41Sopenharmony_ci        () => buffer[fn](23, off),
981cb0ef41Sopenharmony_ci        { code: 'ERR_OUT_OF_RANGE' });
991cb0ef41Sopenharmony_ci    });
1001cb0ef41Sopenharmony_ci  });
1011cb0ef41Sopenharmony_ci}
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci// Test 32 bit
1041cb0ef41Sopenharmony_ci{
1051cb0ef41Sopenharmony_ci  const buffer = Buffer.alloc(8);
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci  buffer.writeInt32BE(0x23, 0);
1081cb0ef41Sopenharmony_ci  buffer.writeInt32LE(0x23, 4);
1091cb0ef41Sopenharmony_ci  assert.ok(buffer.equals(new Uint8Array([
1101cb0ef41Sopenharmony_ci    0x00, 0x00, 0x00, 0x23, 0x23, 0x00, 0x00, 0x00,
1111cb0ef41Sopenharmony_ci  ])));
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci  buffer.writeInt32BE(-5, 0);
1141cb0ef41Sopenharmony_ci  buffer.writeInt32LE(-5, 4);
1151cb0ef41Sopenharmony_ci  assert.ok(buffer.equals(new Uint8Array([
1161cb0ef41Sopenharmony_ci    0xff, 0xff, 0xff, 0xfb, 0xfb, 0xff, 0xff, 0xff,
1171cb0ef41Sopenharmony_ci  ])));
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci  buffer.writeInt32BE(-805306713, 0);
1201cb0ef41Sopenharmony_ci  buffer.writeInt32LE(-805306713, 4);
1211cb0ef41Sopenharmony_ci  assert.ok(buffer.equals(new Uint8Array([
1221cb0ef41Sopenharmony_ci    0xcf, 0xff, 0xfe, 0xa7, 0xa7, 0xfe, 0xff, 0xcf,
1231cb0ef41Sopenharmony_ci  ])));
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  /* Make sure we handle min/max correctly */
1261cb0ef41Sopenharmony_ci  buffer.writeInt32BE(0x7fffffff, 0);
1271cb0ef41Sopenharmony_ci  buffer.writeInt32BE(-0x80000000, 4);
1281cb0ef41Sopenharmony_ci  assert.ok(buffer.equals(new Uint8Array([
1291cb0ef41Sopenharmony_ci    0x7f, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,
1301cb0ef41Sopenharmony_ci  ])));
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  buffer.writeInt32LE(0x7fffffff, 0);
1331cb0ef41Sopenharmony_ci  buffer.writeInt32LE(-0x80000000, 4);
1341cb0ef41Sopenharmony_ci  assert.ok(buffer.equals(new Uint8Array([
1351cb0ef41Sopenharmony_ci    0xff, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x80,
1361cb0ef41Sopenharmony_ci  ])));
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci  ['writeInt32BE', 'writeInt32LE'].forEach((fn) => {
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci    // Verify that default offset works fine.
1411cb0ef41Sopenharmony_ci    buffer[fn](23, undefined);
1421cb0ef41Sopenharmony_ci    buffer[fn](23);
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci    assert.throws(() => {
1451cb0ef41Sopenharmony_ci      buffer[fn](0x7fffffff + 1, 0);
1461cb0ef41Sopenharmony_ci    }, errorOutOfBounds);
1471cb0ef41Sopenharmony_ci    assert.throws(() => {
1481cb0ef41Sopenharmony_ci      buffer[fn](-0x80000000 - 1, 0);
1491cb0ef41Sopenharmony_ci    }, errorOutOfBounds);
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci    ['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
1521cb0ef41Sopenharmony_ci      assert.throws(
1531cb0ef41Sopenharmony_ci        () => buffer[fn](23, off),
1541cb0ef41Sopenharmony_ci        { code: 'ERR_INVALID_ARG_TYPE' });
1551cb0ef41Sopenharmony_ci    });
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci    [NaN, Infinity, -1, 1.01].forEach((off) => {
1581cb0ef41Sopenharmony_ci      assert.throws(
1591cb0ef41Sopenharmony_ci        () => buffer[fn](23, off),
1601cb0ef41Sopenharmony_ci        { code: 'ERR_OUT_OF_RANGE' });
1611cb0ef41Sopenharmony_ci    });
1621cb0ef41Sopenharmony_ci  });
1631cb0ef41Sopenharmony_ci}
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci// Test 48 bit
1661cb0ef41Sopenharmony_ci{
1671cb0ef41Sopenharmony_ci  const value = 0x1234567890ab;
1681cb0ef41Sopenharmony_ci  const buffer = Buffer.allocUnsafe(6);
1691cb0ef41Sopenharmony_ci  buffer.writeIntBE(value, 0, 6);
1701cb0ef41Sopenharmony_ci  assert.ok(buffer.equals(new Uint8Array([
1711cb0ef41Sopenharmony_ci    0x12, 0x34, 0x56, 0x78, 0x90, 0xab,
1721cb0ef41Sopenharmony_ci  ])));
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci  buffer.writeIntLE(value, 0, 6);
1751cb0ef41Sopenharmony_ci  assert.ok(buffer.equals(new Uint8Array([
1761cb0ef41Sopenharmony_ci    0xab, 0x90, 0x78, 0x56, 0x34, 0x12,
1771cb0ef41Sopenharmony_ci  ])));
1781cb0ef41Sopenharmony_ci}
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci// Test Int
1811cb0ef41Sopenharmony_ci{
1821cb0ef41Sopenharmony_ci  const data = Buffer.alloc(8);
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci  // Check byteLength.
1851cb0ef41Sopenharmony_ci  ['writeIntBE', 'writeIntLE'].forEach((fn) => {
1861cb0ef41Sopenharmony_ci    ['', '0', null, {}, [], () => {}, true, false, undefined].forEach((bl) => {
1871cb0ef41Sopenharmony_ci      assert.throws(
1881cb0ef41Sopenharmony_ci        () => data[fn](23, 0, bl),
1891cb0ef41Sopenharmony_ci        { code: 'ERR_INVALID_ARG_TYPE' });
1901cb0ef41Sopenharmony_ci    });
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci    [Infinity, -1].forEach((byteLength) => {
1931cb0ef41Sopenharmony_ci      assert.throws(
1941cb0ef41Sopenharmony_ci        () => data[fn](23, 0, byteLength),
1951cb0ef41Sopenharmony_ci        {
1961cb0ef41Sopenharmony_ci          code: 'ERR_OUT_OF_RANGE',
1971cb0ef41Sopenharmony_ci          message: 'The value of "byteLength" is out of range. ' +
1981cb0ef41Sopenharmony_ci                   `It must be >= 1 and <= 6. Received ${byteLength}`
1991cb0ef41Sopenharmony_ci        }
2001cb0ef41Sopenharmony_ci      );
2011cb0ef41Sopenharmony_ci    });
2021cb0ef41Sopenharmony_ci
2031cb0ef41Sopenharmony_ci    [NaN, 1.01].forEach((byteLength) => {
2041cb0ef41Sopenharmony_ci      assert.throws(
2051cb0ef41Sopenharmony_ci        () => data[fn](42, 0, byteLength),
2061cb0ef41Sopenharmony_ci        {
2071cb0ef41Sopenharmony_ci          code: 'ERR_OUT_OF_RANGE',
2081cb0ef41Sopenharmony_ci          name: 'RangeError',
2091cb0ef41Sopenharmony_ci          message: 'The value of "byteLength" is out of range. ' +
2101cb0ef41Sopenharmony_ci                   `It must be an integer. Received ${byteLength}`
2111cb0ef41Sopenharmony_ci        });
2121cb0ef41Sopenharmony_ci    });
2131cb0ef41Sopenharmony_ci  });
2141cb0ef41Sopenharmony_ci
2151cb0ef41Sopenharmony_ci  // Test 1 to 6 bytes.
2161cb0ef41Sopenharmony_ci  for (let i = 1; i <= 6; i++) {
2171cb0ef41Sopenharmony_ci    ['writeIntBE', 'writeIntLE'].forEach((fn) => {
2181cb0ef41Sopenharmony_ci      const min = -(2 ** (i * 8 - 1));
2191cb0ef41Sopenharmony_ci      const max = 2 ** (i * 8 - 1) - 1;
2201cb0ef41Sopenharmony_ci      let range = `>= ${min} and <= ${max}`;
2211cb0ef41Sopenharmony_ci      if (i > 4) {
2221cb0ef41Sopenharmony_ci        range = `>= -(2 ** ${i * 8 - 1}) and < 2 ** ${i * 8 - 1}`;
2231cb0ef41Sopenharmony_ci      }
2241cb0ef41Sopenharmony_ci      [min - 1, max + 1].forEach((val) => {
2251cb0ef41Sopenharmony_ci        const received = i > 4 ?
2261cb0ef41Sopenharmony_ci          String(val).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1_') :
2271cb0ef41Sopenharmony_ci          val;
2281cb0ef41Sopenharmony_ci        assert.throws(() => {
2291cb0ef41Sopenharmony_ci          data[fn](val, 0, i);
2301cb0ef41Sopenharmony_ci        }, {
2311cb0ef41Sopenharmony_ci          code: 'ERR_OUT_OF_RANGE',
2321cb0ef41Sopenharmony_ci          name: 'RangeError',
2331cb0ef41Sopenharmony_ci          message: 'The value of "value" is out of range. ' +
2341cb0ef41Sopenharmony_ci                   `It must be ${range}. Received ${received}`
2351cb0ef41Sopenharmony_ci        });
2361cb0ef41Sopenharmony_ci      });
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_ci      ['', '0', null, {}, [], () => {}, true, false, undefined].forEach((o) => {
2391cb0ef41Sopenharmony_ci        assert.throws(
2401cb0ef41Sopenharmony_ci          () => data[fn](min, o, i),
2411cb0ef41Sopenharmony_ci          {
2421cb0ef41Sopenharmony_ci            code: 'ERR_INVALID_ARG_TYPE',
2431cb0ef41Sopenharmony_ci            name: 'TypeError'
2441cb0ef41Sopenharmony_ci          });
2451cb0ef41Sopenharmony_ci      });
2461cb0ef41Sopenharmony_ci
2471cb0ef41Sopenharmony_ci      [Infinity, -1, -4294967295].forEach((offset) => {
2481cb0ef41Sopenharmony_ci        assert.throws(
2491cb0ef41Sopenharmony_ci          () => data[fn](min, offset, i),
2501cb0ef41Sopenharmony_ci          {
2511cb0ef41Sopenharmony_ci            code: 'ERR_OUT_OF_RANGE',
2521cb0ef41Sopenharmony_ci            name: 'RangeError',
2531cb0ef41Sopenharmony_ci            message: 'The value of "offset" is out of range. ' +
2541cb0ef41Sopenharmony_ci                     `It must be >= 0 and <= ${8 - i}. Received ${offset}`
2551cb0ef41Sopenharmony_ci          });
2561cb0ef41Sopenharmony_ci      });
2571cb0ef41Sopenharmony_ci
2581cb0ef41Sopenharmony_ci      [NaN, 1.01].forEach((offset) => {
2591cb0ef41Sopenharmony_ci        assert.throws(
2601cb0ef41Sopenharmony_ci          () => data[fn](max, offset, i),
2611cb0ef41Sopenharmony_ci          {
2621cb0ef41Sopenharmony_ci            code: 'ERR_OUT_OF_RANGE',
2631cb0ef41Sopenharmony_ci            name: 'RangeError',
2641cb0ef41Sopenharmony_ci            message: 'The value of "offset" is out of range. ' +
2651cb0ef41Sopenharmony_ci                     `It must be an integer. Received ${offset}`
2661cb0ef41Sopenharmony_ci          });
2671cb0ef41Sopenharmony_ci      });
2681cb0ef41Sopenharmony_ci    });
2691cb0ef41Sopenharmony_ci  }
2701cb0ef41Sopenharmony_ci}
271