summaryrefslogtreecommitdiff
path: root/tests/node_compat/test/parallel/test-buffer-writeint.js
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2024-02-10 13:22:13 -0700
committerGitHub <noreply@github.com>2024-02-10 20:22:13 +0000
commitf5e46c9bf2f50d66a953fa133161fc829cecff06 (patch)
tree8faf2f5831c1c7b11d842cd9908d141082c869a5 /tests/node_compat/test/parallel/test-buffer-writeint.js
parentd2477f780630a812bfd65e3987b70c0d309385bb (diff)
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests -> tests, and updates of relative paths for files. This is the first step towards aggregate all of the integration test files under tests/, which will lead to a set of integration tests that can run without the CLI binary being built. While we could leave these tests under `cli`, it would require us to keep a more complex directory structure for the various test runners. In addition, we have a lot of complexity to ignore various test files in the `cli` project itself (cargo publish exclusion rules, autotests = false, etc). And finally, the `tests/` folder will eventually house the `test_ffi`, `test_napi` and other testing code, reducing the size of the root repo directory. For easier review, the extremely large and noisy "move" is in the first commit (with no changes -- just a move), while the remainder of the changes to actual files is in the second commit.
Diffstat (limited to 'tests/node_compat/test/parallel/test-buffer-writeint.js')
-rw-r--r--tests/node_compat/test/parallel/test-buffer-writeint.js277
1 files changed, 277 insertions, 0 deletions
diff --git a/tests/node_compat/test/parallel/test-buffer-writeint.js b/tests/node_compat/test/parallel/test-buffer-writeint.js
new file mode 100644
index 000000000..117ba47c0
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-buffer-writeint.js
@@ -0,0 +1,277 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tools/node_compat/setup.ts`. Do not modify this file manually.
+
+'use strict';
+
+// Tests to verify signed integers are correctly written
+
+require('../common');
+const assert = require('assert');
+const errorOutOfBounds = {
+ code: 'ERR_OUT_OF_RANGE',
+ name: 'RangeError',
+ message: new RegExp('^The value of "value" is out of range\\. ' +
+ 'It must be >= -\\d+ and <= \\d+\\. Received .+$')
+};
+
+// Test 8 bit
+{
+ const buffer = Buffer.alloc(2);
+
+ buffer.writeInt8(0x23, 0);
+ buffer.writeInt8(-5, 1);
+ assert.ok(buffer.equals(new Uint8Array([ 0x23, 0xfb ])));
+
+ /* Make sure we handle min/max correctly */
+ buffer.writeInt8(0x7f, 0);
+ buffer.writeInt8(-0x80, 1);
+ assert.ok(buffer.equals(new Uint8Array([ 0x7f, 0x80 ])));
+
+ assert.throws(() => {
+ buffer.writeInt8(0x7f + 1, 0);
+ }, errorOutOfBounds);
+ assert.throws(() => {
+ buffer.writeInt8(-0x80 - 1, 0);
+ }, errorOutOfBounds);
+
+ // Verify that default offset works fine.
+ buffer.writeInt8(23, undefined);
+ buffer.writeInt8(23);
+
+ ['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
+ assert.throws(
+ () => buffer.writeInt8(23, off),
+ { code: 'ERR_INVALID_ARG_TYPE' });
+ });
+
+ [NaN, Infinity, -1, 1.01].forEach((off) => {
+ assert.throws(
+ () => buffer.writeInt8(23, off),
+ { code: 'ERR_OUT_OF_RANGE' });
+ });
+}
+
+// Test 16 bit
+{
+ const buffer = Buffer.alloc(4);
+
+ buffer.writeInt16BE(0x0023, 0);
+ buffer.writeInt16LE(0x0023, 2);
+ assert.ok(buffer.equals(new Uint8Array([ 0x00, 0x23, 0x23, 0x00 ])));
+
+ buffer.writeInt16BE(-5, 0);
+ buffer.writeInt16LE(-5, 2);
+ assert.ok(buffer.equals(new Uint8Array([ 0xff, 0xfb, 0xfb, 0xff ])));
+
+ buffer.writeInt16BE(-1679, 0);
+ buffer.writeInt16LE(-1679, 2);
+ assert.ok(buffer.equals(new Uint8Array([ 0xf9, 0x71, 0x71, 0xf9 ])));
+
+ /* Make sure we handle min/max correctly */
+ buffer.writeInt16BE(0x7fff, 0);
+ buffer.writeInt16BE(-0x8000, 2);
+ assert.ok(buffer.equals(new Uint8Array([ 0x7f, 0xff, 0x80, 0x00 ])));
+
+ buffer.writeInt16LE(0x7fff, 0);
+ buffer.writeInt16LE(-0x8000, 2);
+ assert.ok(buffer.equals(new Uint8Array([ 0xff, 0x7f, 0x00, 0x80 ])));
+
+ ['writeInt16BE', 'writeInt16LE'].forEach((fn) => {
+
+ // Verify that default offset works fine.
+ buffer[fn](23, undefined);
+ buffer[fn](23);
+
+ assert.throws(() => {
+ buffer[fn](0x7fff + 1, 0);
+ }, errorOutOfBounds);
+ assert.throws(() => {
+ buffer[fn](-0x8000 - 1, 0);
+ }, errorOutOfBounds);
+
+ ['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
+ assert.throws(
+ () => buffer[fn](23, off),
+ { code: 'ERR_INVALID_ARG_TYPE' });
+ });
+
+ [NaN, Infinity, -1, 1.01].forEach((off) => {
+ assert.throws(
+ () => buffer[fn](23, off),
+ { code: 'ERR_OUT_OF_RANGE' });
+ });
+ });
+}
+
+// Test 32 bit
+{
+ const buffer = Buffer.alloc(8);
+
+ buffer.writeInt32BE(0x23, 0);
+ buffer.writeInt32LE(0x23, 4);
+ assert.ok(buffer.equals(new Uint8Array([
+ 0x00, 0x00, 0x00, 0x23, 0x23, 0x00, 0x00, 0x00,
+ ])));
+
+ buffer.writeInt32BE(-5, 0);
+ buffer.writeInt32LE(-5, 4);
+ assert.ok(buffer.equals(new Uint8Array([
+ 0xff, 0xff, 0xff, 0xfb, 0xfb, 0xff, 0xff, 0xff,
+ ])));
+
+ buffer.writeInt32BE(-805306713, 0);
+ buffer.writeInt32LE(-805306713, 4);
+ assert.ok(buffer.equals(new Uint8Array([
+ 0xcf, 0xff, 0xfe, 0xa7, 0xa7, 0xfe, 0xff, 0xcf,
+ ])));
+
+ /* Make sure we handle min/max correctly */
+ buffer.writeInt32BE(0x7fffffff, 0);
+ buffer.writeInt32BE(-0x80000000, 4);
+ assert.ok(buffer.equals(new Uint8Array([
+ 0x7f, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,
+ ])));
+
+ buffer.writeInt32LE(0x7fffffff, 0);
+ buffer.writeInt32LE(-0x80000000, 4);
+ assert.ok(buffer.equals(new Uint8Array([
+ 0xff, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x80,
+ ])));
+
+ ['writeInt32BE', 'writeInt32LE'].forEach((fn) => {
+
+ // Verify that default offset works fine.
+ buffer[fn](23, undefined);
+ buffer[fn](23);
+
+ assert.throws(() => {
+ buffer[fn](0x7fffffff + 1, 0);
+ }, errorOutOfBounds);
+ assert.throws(() => {
+ buffer[fn](-0x80000000 - 1, 0);
+ }, errorOutOfBounds);
+
+ ['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
+ assert.throws(
+ () => buffer[fn](23, off),
+ { code: 'ERR_INVALID_ARG_TYPE' });
+ });
+
+ [NaN, Infinity, -1, 1.01].forEach((off) => {
+ assert.throws(
+ () => buffer[fn](23, off),
+ { code: 'ERR_OUT_OF_RANGE' });
+ });
+ });
+}
+
+// Test 48 bit
+{
+ const value = 0x1234567890ab;
+ const buffer = Buffer.allocUnsafe(6);
+ buffer.writeIntBE(value, 0, 6);
+ assert.ok(buffer.equals(new Uint8Array([
+ 0x12, 0x34, 0x56, 0x78, 0x90, 0xab,
+ ])));
+
+ buffer.writeIntLE(value, 0, 6);
+ assert.ok(buffer.equals(new Uint8Array([
+ 0xab, 0x90, 0x78, 0x56, 0x34, 0x12,
+ ])));
+}
+
+// Test Int
+{
+ const data = Buffer.alloc(8);
+
+ // Check byteLength.
+ ['writeIntBE', 'writeIntLE'].forEach((fn) => {
+ ['', '0', null, {}, [], () => {}, true, false, undefined].forEach((bl) => {
+ assert.throws(
+ () => data[fn](23, 0, bl),
+ { code: 'ERR_INVALID_ARG_TYPE' });
+ });
+
+ [Infinity, -1].forEach((byteLength) => {
+ assert.throws(
+ () => data[fn](23, 0, byteLength),
+ {
+ code: 'ERR_OUT_OF_RANGE',
+ message: 'The value of "byteLength" is out of range. ' +
+ `It must be >= 1 and <= 6. Received ${byteLength}`
+ }
+ );
+ });
+
+ [NaN, 1.01].forEach((byteLength) => {
+ assert.throws(
+ () => data[fn](42, 0, byteLength),
+ {
+ code: 'ERR_OUT_OF_RANGE',
+ name: 'RangeError',
+ message: 'The value of "byteLength" is out of range. ' +
+ `It must be an integer. Received ${byteLength}`
+ });
+ });
+ });
+
+ // Test 1 to 6 bytes.
+ for (let i = 1; i <= 6; i++) {
+ ['writeIntBE', 'writeIntLE'].forEach((fn) => {
+ const min = -(2 ** (i * 8 - 1));
+ const max = 2 ** (i * 8 - 1) - 1;
+ let range = `>= ${min} and <= ${max}`;
+ if (i > 4) {
+ range = `>= -(2 ** ${i * 8 - 1}) and < 2 ** ${i * 8 - 1}`;
+ }
+ [min - 1, max + 1].forEach((val) => {
+ const received = i > 4 ?
+ String(val).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1_') :
+ val;
+ assert.throws(() => {
+ data[fn](val, 0, i);
+ }, {
+ code: 'ERR_OUT_OF_RANGE',
+ name: 'RangeError',
+ message: 'The value of "value" is out of range. ' +
+ `It must be ${range}. Received ${received}`
+ });
+ });
+
+ ['', '0', null, {}, [], () => {}, true, false, undefined].forEach((o) => {
+ assert.throws(
+ () => data[fn](min, o, i),
+ {
+ code: 'ERR_INVALID_ARG_TYPE',
+ name: 'TypeError'
+ });
+ });
+
+ [Infinity, -1, -4294967295].forEach((offset) => {
+ assert.throws(
+ () => data[fn](min, offset, i),
+ {
+ code: 'ERR_OUT_OF_RANGE',
+ name: 'RangeError',
+ message: 'The value of "offset" is out of range. ' +
+ `It must be >= 0 and <= ${8 - i}. Received ${offset}`
+ });
+ });
+
+ [NaN, 1.01].forEach((offset) => {
+ assert.throws(
+ () => data[fn](max, offset, i),
+ {
+ code: 'ERR_OUT_OF_RANGE',
+ name: 'RangeError',
+ message: 'The value of "offset" is out of range. ' +
+ `It must be an integer. Received ${offset}`
+ });
+ });
+ });
+ }
+}