Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2ff4704

Browse filesBrowse files
committed
test: move test-readuint to test-buffer-readuint
Backport of a changed test from #20054 PR-URL: #21170 Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 85dc9ac commit 2ff4704
Copy full SHA for 2ff4704

File tree

Expand file treeCollapse file tree

2 files changed

+167
-110
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+167
-110
lines changed
Open diff view settings
Collapse file
+167Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
6+
// Test OOB
7+
{
8+
const buffer = Buffer.alloc(4);
9+
10+
['UInt8', 'UInt16BE', 'UInt16LE', 'UInt32BE', 'UInt32LE'].forEach((fn) => {
11+
12+
// Verify that default offset works fine.
13+
buffer[`read${fn}`](undefined);
14+
buffer[`read${fn}`]();
15+
16+
['', '0', null, {}, [], () => {}, true, false].forEach((o) => {
17+
assert.throws(
18+
() => buffer[`read${fn}`](o),
19+
{
20+
code: 'ERR_INVALID_ARG_TYPE',
21+
name: 'TypeError [ERR_INVALID_ARG_TYPE]'
22+
});
23+
});
24+
25+
[Infinity, -1, -4294967295].forEach((offset) => {
26+
assert.throws(
27+
() => buffer[`read${fn}`](offset),
28+
{
29+
code: 'ERR_OUT_OF_RANGE',
30+
name: 'RangeError [ERR_OUT_OF_RANGE]'
31+
});
32+
});
33+
34+
[NaN, 1.01].forEach((offset) => {
35+
assert.throws(
36+
() => buffer[`read${fn}`](offset),
37+
{
38+
code: 'ERR_OUT_OF_RANGE',
39+
name: 'RangeError [ERR_OUT_OF_RANGE]',
40+
message: 'The value of "offset" is out of range. ' +
41+
`It must be an integer. Received ${offset}`
42+
});
43+
});
44+
});
45+
}
46+
47+
// Test 8 bit unsigned integers
48+
{
49+
const data = Buffer.from([0xff, 0x2a, 0x2a, 0x2a]);
50+
assert.strictEqual(255, data.readUInt8(0));
51+
assert.strictEqual(42, data.readUInt8(1));
52+
assert.strictEqual(42, data.readUInt8(2));
53+
assert.strictEqual(42, data.readUInt8(3));
54+
}
55+
56+
// Test 16 bit unsigned integers
57+
{
58+
const data = Buffer.from([0x00, 0x2a, 0x42, 0x3f]);
59+
assert.strictEqual(0x2a, data.readUInt16BE(0));
60+
assert.strictEqual(0x2a42, data.readUInt16BE(1));
61+
assert.strictEqual(0x423f, data.readUInt16BE(2));
62+
assert.strictEqual(0x2a00, data.readUInt16LE(0));
63+
assert.strictEqual(0x422a, data.readUInt16LE(1));
64+
assert.strictEqual(0x3f42, data.readUInt16LE(2));
65+
66+
data[0] = 0xfe;
67+
data[1] = 0xfe;
68+
assert.strictEqual(0xfefe, data.readUInt16BE(0));
69+
assert.strictEqual(0xfefe, data.readUInt16LE(0));
70+
}
71+
72+
// Test 32 bit unsigned integers
73+
{
74+
const data = Buffer.from([0x32, 0x65, 0x42, 0x56, 0x23, 0xff]);
75+
assert.strictEqual(0x32654256, data.readUInt32BE(0));
76+
assert.strictEqual(0x65425623, data.readUInt32BE(1));
77+
assert.strictEqual(0x425623ff, data.readUInt32BE(2));
78+
assert.strictEqual(0x56426532, data.readUInt32LE(0));
79+
assert.strictEqual(0x23564265, data.readUInt32LE(1));
80+
assert.strictEqual(0xff235642, data.readUInt32LE(2));
81+
}
82+
83+
// Test UInt
84+
{
85+
const buffer = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
86+
87+
assert.strictEqual(buffer.readUIntLE(0), 0x01);
88+
assert.strictEqual(buffer.readUIntLE(0, 1), 0x01);
89+
assert.strictEqual(buffer.readUIntBE(0), 0x01);
90+
assert.strictEqual(buffer.readUIntBE(0, 1), 0x01);
91+
assert.strictEqual(buffer.readUIntLE(0, 3), 0x030201);
92+
assert.strictEqual(buffer.readUIntBE(0, 3), 0x010203);
93+
assert.strictEqual(buffer.readUIntLE(0, 5), 0x0504030201);
94+
assert.strictEqual(buffer.readUIntBE(0, 5), 0x0102030405);
95+
assert.strictEqual(buffer.readUIntLE(0, 6), 0x060504030201);
96+
assert.strictEqual(buffer.readUIntBE(0, 6), 0x010203040506);
97+
assert.strictEqual(buffer.readUIntLE(1, 6), 0x070605040302);
98+
assert.strictEqual(buffer.readUIntBE(1, 6), 0x020304050607);
99+
assert.strictEqual(buffer.readUIntLE(2, 6), 0x080706050403);
100+
assert.strictEqual(buffer.readUIntBE(2, 6), 0x030405060708);
101+
102+
// Check byteLength.
103+
['readUIntBE', 'readUIntLE'].forEach((fn) => {
104+
['', '0', null, {}, [], () => {}, true, false].forEach((len) => {
105+
assert.throws(
106+
() => buffer[fn](0, len),
107+
{ code: 'ERR_INVALID_ARG_TYPE' });
108+
});
109+
110+
[Infinity, -1].forEach((byteLength) => {
111+
assert.throws(
112+
() => buffer[fn](0, byteLength),
113+
{
114+
code: 'ERR_OUT_OF_RANGE',
115+
message: 'The value of "byteLength" is out of range. ' +
116+
`It must be >= 1 and <= 6. Received ${byteLength}`
117+
});
118+
});
119+
120+
[NaN, 1.01].forEach((byteLength) => {
121+
assert.throws(
122+
() => buffer[fn](0, byteLength),
123+
{
124+
code: 'ERR_OUT_OF_RANGE',
125+
name: 'RangeError [ERR_OUT_OF_RANGE]',
126+
message: 'The value of "byteLength" is out of range. ' +
127+
`It must be an integer. Received ${byteLength}`
128+
});
129+
});
130+
});
131+
132+
// Test 1 to 6 bytes.
133+
for (let i = 1; i < 6; i++) {
134+
['readUIntBE', 'readUIntLE'].forEach((fn) => {
135+
['', '0', null, {}, [], () => {}, true, false].forEach((o) => {
136+
assert.throws(
137+
() => buffer[fn](o, i),
138+
{
139+
code: 'ERR_INVALID_ARG_TYPE',
140+
name: 'TypeError [ERR_INVALID_ARG_TYPE]'
141+
});
142+
});
143+
144+
[Infinity, -1, -4294967295].forEach((offset) => {
145+
assert.throws(
146+
() => buffer[fn](offset, i),
147+
{
148+
code: 'ERR_OUT_OF_RANGE',
149+
name: 'RangeError [ERR_OUT_OF_RANGE]',
150+
message: 'The value of "offset" is out of range. ' +
151+
`It must be >= 0 and <= ${8 - i}. Received ${offset}`
152+
});
153+
});
154+
155+
[NaN, 1.01].forEach((offset) => {
156+
assert.throws(
157+
() => buffer[fn](offset, i),
158+
{
159+
code: 'ERR_OUT_OF_RANGE',
160+
name: 'RangeError [ERR_OUT_OF_RANGE]',
161+
message: 'The value of "offset" is out of range. ' +
162+
`It must be an integer. Received ${offset}`
163+
});
164+
});
165+
});
166+
}
167+
}
Collapse file

‎test/parallel/test-readuint.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-readuint.js
-110Lines changed: 0 additions & 110 deletions
This file was deleted.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.