11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(8);
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci['LE', 'BE'].forEach(function(endianness) {
81cb0ef41Sopenharmony_ci  // Should allow simple BigInts to be written and read
91cb0ef41Sopenharmony_ci  let val = 123456789n;
101cb0ef41Sopenharmony_ci  buf[`writeBigInt64${endianness}`](val, 0);
111cb0ef41Sopenharmony_ci  let rtn = buf[`readBigInt64${endianness}`](0);
121cb0ef41Sopenharmony_ci  assert.strictEqual(val, rtn);
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci  // Should allow INT64_MAX to be written and read
151cb0ef41Sopenharmony_ci  val = 0x7fffffffffffffffn;
161cb0ef41Sopenharmony_ci  buf[`writeBigInt64${endianness}`](val, 0);
171cb0ef41Sopenharmony_ci  rtn = buf[`readBigInt64${endianness}`](0);
181cb0ef41Sopenharmony_ci  assert.strictEqual(val, rtn);
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  // Should read and write a negative signed 64-bit integer
211cb0ef41Sopenharmony_ci  val = -123456789n;
221cb0ef41Sopenharmony_ci  buf[`writeBigInt64${endianness}`](val, 0);
231cb0ef41Sopenharmony_ci  assert.strictEqual(val, buf[`readBigInt64${endianness}`](0));
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  // Should read and write an unsigned 64-bit integer
261cb0ef41Sopenharmony_ci  val = 123456789n;
271cb0ef41Sopenharmony_ci  buf[`writeBigUInt64${endianness}`](val, 0);
281cb0ef41Sopenharmony_ci  assert.strictEqual(val, buf[`readBigUInt64${endianness}`](0));
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  // Should throw a RangeError upon INT64_MAX+1 being written
311cb0ef41Sopenharmony_ci  assert.throws(function() {
321cb0ef41Sopenharmony_ci    const val = 0x8000000000000000n;
331cb0ef41Sopenharmony_ci    buf[`writeBigInt64${endianness}`](val, 0);
341cb0ef41Sopenharmony_ci  }, RangeError);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  // Should throw a RangeError upon UINT64_MAX+1 being written
371cb0ef41Sopenharmony_ci  assert.throws(function() {
381cb0ef41Sopenharmony_ci    const val = 0x10000000000000000n;
391cb0ef41Sopenharmony_ci    buf[`writeBigUInt64${endianness}`](val, 0);
401cb0ef41Sopenharmony_ci  }, {
411cb0ef41Sopenharmony_ci    code: 'ERR_OUT_OF_RANGE',
421cb0ef41Sopenharmony_ci    message: 'The value of "value" is out of range. It must be ' +
431cb0ef41Sopenharmony_ci      '>= 0n and < 2n ** 64n. Received 18_446_744_073_709_551_616n'
441cb0ef41Sopenharmony_ci  });
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  // Should throw a TypeError upon invalid input
471cb0ef41Sopenharmony_ci  assert.throws(function() {
481cb0ef41Sopenharmony_ci    buf[`writeBigInt64${endianness}`]('bad', 0);
491cb0ef41Sopenharmony_ci  }, TypeError);
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  // Should throw a TypeError upon invalid input
521cb0ef41Sopenharmony_ci  assert.throws(function() {
531cb0ef41Sopenharmony_ci    buf[`writeBigUInt64${endianness}`]('bad', 0);
541cb0ef41Sopenharmony_ci  }, TypeError);
551cb0ef41Sopenharmony_ci});
56