1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00
denoland-deno/tests/node_compat/test/parallel/test-buffer-readint.js
Matt Mastracci f5e46c9bf2
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.
2024-02-10 20:22:13 +00:00

204 lines
6.1 KiB
JavaScript

// 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';
require('../common');
const assert = require('assert');
// Test OOB
{
const buffer = Buffer.alloc(4);
['Int8', 'Int16BE', 'Int16LE', 'Int32BE', 'Int32LE'].forEach((fn) => {
// Verify that default offset works fine.
buffer[`read${fn}`](undefined);
buffer[`read${fn}`]();
['', '0', null, {}, [], () => {}, true, false].forEach((o) => {
assert.throws(
() => buffer[`read${fn}`](o),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError'
});
});
[Infinity, -1, -4294967295].forEach((offset) => {
assert.throws(
() => buffer[`read${fn}`](offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError'
});
});
[NaN, 1.01].forEach((offset) => {
assert.throws(
() => buffer[`read${fn}`](offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "offset" is out of range. ' +
`It must be an integer. Received ${offset}`
});
});
});
}
// Test 8 bit signed integers
{
const data = Buffer.from([0x23, 0xab, 0x7c, 0xef]);
assert.strictEqual(data.readInt8(0), 0x23);
data[0] = 0xff;
assert.strictEqual(data.readInt8(0), -1);
data[0] = 0x87;
assert.strictEqual(data.readInt8(0), -121);
assert.strictEqual(data.readInt8(1), -85);
assert.strictEqual(data.readInt8(2), 124);
assert.strictEqual(data.readInt8(3), -17);
}
// Test 16 bit integers
{
const buffer = Buffer.from([0x16, 0x79, 0x65, 0x6e, 0x69, 0x78]);
assert.strictEqual(buffer.readInt16BE(0), 0x1679);
assert.strictEqual(buffer.readInt16LE(0), 0x7916);
buffer[0] = 0xff;
buffer[1] = 0x80;
assert.strictEqual(buffer.readInt16BE(0), -128);
assert.strictEqual(buffer.readInt16LE(0), -32513);
buffer[0] = 0x77;
buffer[1] = 0x65;
assert.strictEqual(buffer.readInt16BE(0), 0x7765);
assert.strictEqual(buffer.readInt16BE(1), 0x6565);
assert.strictEqual(buffer.readInt16BE(2), 0x656e);
assert.strictEqual(buffer.readInt16BE(3), 0x6e69);
assert.strictEqual(buffer.readInt16BE(4), 0x6978);
assert.strictEqual(buffer.readInt16LE(0), 0x6577);
assert.strictEqual(buffer.readInt16LE(1), 0x6565);
assert.strictEqual(buffer.readInt16LE(2), 0x6e65);
assert.strictEqual(buffer.readInt16LE(3), 0x696e);
assert.strictEqual(buffer.readInt16LE(4), 0x7869);
}
// Test 32 bit integers
{
const buffer = Buffer.from([0x43, 0x53, 0x16, 0x79, 0x36, 0x17]);
assert.strictEqual(buffer.readInt32BE(0), 0x43531679);
assert.strictEqual(buffer.readInt32LE(0), 0x79165343);
buffer[0] = 0xff;
buffer[1] = 0xfe;
buffer[2] = 0xef;
buffer[3] = 0xfa;
assert.strictEqual(buffer.readInt32BE(0), -69638);
assert.strictEqual(buffer.readInt32LE(0), -84934913);
buffer[0] = 0x42;
buffer[1] = 0xc3;
buffer[2] = 0x95;
buffer[3] = 0xa9;
assert.strictEqual(buffer.readInt32BE(0), 0x42c395a9);
assert.strictEqual(buffer.readInt32BE(1), -1013601994);
assert.strictEqual(buffer.readInt32BE(2), -1784072681);
assert.strictEqual(buffer.readInt32LE(0), -1449802942);
assert.strictEqual(buffer.readInt32LE(1), 917083587);
assert.strictEqual(buffer.readInt32LE(2), 389458325);
}
// Test Int
{
const buffer = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
assert.strictEqual(buffer.readIntLE(0, 1), 0x01);
assert.strictEqual(buffer.readIntBE(0, 1), 0x01);
assert.strictEqual(buffer.readIntLE(0, 3), 0x030201);
assert.strictEqual(buffer.readIntBE(0, 3), 0x010203);
assert.strictEqual(buffer.readIntLE(0, 5), 0x0504030201);
assert.strictEqual(buffer.readIntBE(0, 5), 0x0102030405);
assert.strictEqual(buffer.readIntLE(0, 6), 0x060504030201);
assert.strictEqual(buffer.readIntBE(0, 6), 0x010203040506);
assert.strictEqual(buffer.readIntLE(1, 6), 0x070605040302);
assert.strictEqual(buffer.readIntBE(1, 6), 0x020304050607);
assert.strictEqual(buffer.readIntLE(2, 6), 0x080706050403);
assert.strictEqual(buffer.readIntBE(2, 6), 0x030405060708);
// Check byteLength.
['readIntBE', 'readIntLE'].forEach((fn) => {
['', '0', null, {}, [], () => {}, true, false, undefined].forEach((len) => {
assert.throws(
() => buffer[fn](0, len),
{ code: 'ERR_INVALID_ARG_TYPE' });
});
[Infinity, -1].forEach((byteLength) => {
assert.throws(
() => buffer[fn](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(
() => buffer[fn](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++) {
['readIntBE', 'readIntLE'].forEach((fn) => {
['', '0', null, {}, [], () => {}, true, false, undefined].forEach((o) => {
assert.throws(
() => buffer[fn](o, i),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError'
});
});
[Infinity, -1, -4294967295].forEach((offset) => {
assert.throws(
() => buffer[fn](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(
() => buffer[fn](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}`
});
});
});
}
}