Skip to content

Commit 989d344

Browse files
neeharvevanlucas
authored andcommitted
test: use block scoped variable names
PR-URL: #12544 Reviewed-By: James M Snell <[email protected]>
1 parent b82e076 commit 989d344

File tree

1 file changed

+132
-118
lines changed

1 file changed

+132
-118
lines changed

‎test/parallel/test-buffer-swap.js‎

Lines changed: 132 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -4,135 +4,149 @@ require('../common');
44
constassert=require('assert');
55

66
// Test buffers small enough to use the JS implementation
7-
constbuf=Buffer.from([0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
8-
0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10]);
9-
10-
assert.strictEqual(buf,buf.swap16());
11-
assert.deepStrictEqual(buf,Buffer.from([0x02,0x01,0x04,0x03,0x06,0x05,
12-
0x08,0x07,0x0a,0x09,0x0c,0x0b,
13-
0x0e,0x0d,0x10,0x0f]));
14-
buf.swap16();// restore
15-
16-
assert.strictEqual(buf,buf.swap32());
17-
assert.deepStrictEqual(buf,Buffer.from([0x04,0x03,0x02,0x01,0x08,0x07,
18-
0x06,0x05,0x0c,0x0b,0x0a,0x09,
19-
0x10,0x0f,0x0e,0x0d]));
20-
buf.swap32();// restore
21-
22-
assert.strictEqual(buf,buf.swap64());
23-
assert.deepStrictEqual(buf,Buffer.from([0x08,0x07,0x06,0x05,0x04,0x03,
24-
0x02,0x01,0x10,0x0f,0x0e,0x0d,
25-
0x0c,0x0b,0x0a,0x09]));
7+
{
8+
constbuf=Buffer.from([0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
9+
0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10]);
10+
11+
assert.strictEqual(buf,buf.swap16());
12+
assert.deepStrictEqual(buf,Buffer.from([0x02,0x01,0x04,0x03,0x06,0x05,
13+
0x08,0x07,0x0a,0x09,0x0c,0x0b,
14+
0x0e,0x0d,0x10,0x0f]));
15+
buf.swap16();// restore
16+
17+
assert.strictEqual(buf,buf.swap32());
18+
assert.deepStrictEqual(buf,Buffer.from([0x04,0x03,0x02,0x01,0x08,0x07,
19+
0x06,0x05,0x0c,0x0b,0x0a,0x09,
20+
0x10,0x0f,0x0e,0x0d]));
21+
buf.swap32();// restore
22+
23+
assert.strictEqual(buf,buf.swap64());
24+
assert.deepStrictEqual(buf,Buffer.from([0x08,0x07,0x06,0x05,0x04,0x03,
25+
0x02,0x01,0x10,0x0f,0x0e,0x0d,
26+
0x0c,0x0b,0x0a,0x09]));
27+
}
2628

2729
// Operates in-place
28-
constbuf3=Buffer.from([0x1,0x2,0x3,0x4,0x5,0x6,0x7]);
29-
buf3.slice(1,5).swap32();
30-
assert.deepStrictEqual(buf3,Buffer.from([0x1,0x5,0x4,0x3,0x2,0x6,0x7]));
31-
32-
buf3.slice(1,5).swap16();
33-
assert.deepStrictEqual(buf3,Buffer.from([0x1,0x4,0x5,0x2,0x3,0x6,0x7]));
34-
35-
constbuf3_64=Buffer.from([0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
36-
0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,
37-
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
38-
0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10]);
39-
buf3_64.slice(2,18).swap64();
40-
assert.deepStrictEqual(buf3_64,Buffer.from([0x01,0x02,0x0a,0x09,0x08,0x07,
41-
0x06,0x05,0x04,0x03,0x02,0x01,
42-
0x10,0x0f,0x0e,0x0d,0x0c,0x0b,
43-
0x03,0x04,0x05,0x06,0x07,0x08,
44-
0x09,0x0a,0x0b,0x0c,0x0d,0x0e,
45-
0x0f,0x10]));
46-
47-
// Force use of native code (Buffer size above threshold limit for js impl)
48-
constbuf4A=newUint32Array(256).fill(0x04030201);
49-
constbuf4=Buffer.from(buf4A.buffer,buf4A.byteOffset);
50-
constbuf5A=newUint32Array(256).fill(0x03040102);
51-
constbuf5=Buffer.from(buf5A.buffer,buf5A.byteOffset);
52-
53-
buf4.swap16();
54-
assert.deepStrictEqual(buf4,buf5);
55-
56-
constbuf6A=newUint32Array(256).fill(0x04030201);
57-
constbuf6=Buffer.from(buf6A.buffer);
58-
constbu7A=newUint32Array(256).fill(0x01020304);
59-
constbuf7=Buffer.from(bu7A.buffer,bu7A.byteOffset);
60-
61-
buf6.swap32();
62-
assert.deepStrictEqual(buf6,buf7);
63-
64-
constbuf8A=newUint8Array(256*8);
65-
constbuf9A=newUint8Array(256*8);
66-
for(leti=0;i<buf8A.length;i++){
67-
buf8A[i]=i%8;
68-
buf9A[buf9A.length-i-1]=i%8;
30+
{
31+
constbuf=Buffer.from([0x1,0x2,0x3,0x4,0x5,0x6,0x7]);
32+
buf.slice(1,5).swap32();
33+
assert.deepStrictEqual(buf,Buffer.from([0x1,0x5,0x4,0x3,0x2,0x6,0x7]));
34+
buf.slice(1,5).swap16();
35+
assert.deepStrictEqual(buf,Buffer.from([0x1,0x4,0x5,0x2,0x3,0x6,0x7]));
36+
37+
// Length assertions
38+
constre16=/Buffersizemustbeamultipleof16-bits/;
39+
constre32=/Buffersizemustbeamultipleof32-bits/;
40+
constre64=/Buffersizemustbeamultipleof64-bits/;
41+
42+
assert.throws(()=>Buffer.from(buf).swap16(),re16);
43+
assert.throws(()=>Buffer.alloc(1025).swap16(),re16);
44+
assert.throws(()=>Buffer.from(buf).swap32(),re32);
45+
assert.throws(()=>buf.slice(1,3).swap32(),re32);
46+
assert.throws(()=>Buffer.alloc(1025).swap32(),re32);
47+
assert.throws(()=>buf.slice(1,3).swap64(),re64);
48+
assert.throws(()=>Buffer.alloc(1025).swap64(),re64);
6949
}
70-
constbuf8=Buffer.from(buf8A.buffer,buf8A.byteOffset);
71-
constbuf9=Buffer.from(buf9A.buffer,buf9A.byteOffset);
72-
73-
buf8.swap64();
74-
assert.deepStrictEqual(buf8,buf9);
7550

76-
// Test native code with buffers that are not memory-aligned
77-
constbuf10A=newUint8Array(256*8);
78-
constbuf11A=newUint8Array(256*8-2);
79-
for(leti=0;i<buf10A.length;i++){
80-
buf10A[i]=i%2;
81-
}
82-
for(leti=1;i<buf11A.length;i++){
83-
buf11A[buf11A.length-i]=(i+1)%2;
84-
}
85-
constbuf10=Buffer.from(buf10A.buffer,buf10A.byteOffset);
86-
// 0|1 0|1 0|1...
87-
constbuf11=Buffer.from(buf11A.buffer,buf11A.byteOffset);
88-
// 0|0 1|0 1|0...
51+
{
52+
constbuf=Buffer.from([0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
53+
0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,
54+
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
55+
0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10]);
8956

90-
buf10.slice(1,buf10.length-1).swap16();
91-
assert.deepStrictEqual(buf10.slice(0,buf11.length),buf11);
57+
buf.slice(2,18).swap64();
9258

59+
assert.deepStrictEqual(buf,Buffer.from([0x01,0x02,0x0a,0x09,0x08,0x07,
60+
0x06,0x05,0x04,0x03,0x02,0x01,
61+
0x10,0x0f,0x0e,0x0d,0x0c,0x0b,
62+
0x03,0x04,0x05,0x06,0x07,0x08,
63+
0x09,0x0a,0x0b,0x0c,0x0d,0x0e,
64+
0x0f,0x10]));
65+
}
9366

94-
constbuf12A=newUint8Array(256*8);
95-
constbuf13A=newUint8Array(256*8-4);
96-
for(leti=0;i<buf12A.length;i++){
97-
buf12A[i]=i%4;
67+
// Force use of native code (Buffer size above threshold limit for js impl)
68+
{
69+
constbufData=newUint32Array(256).fill(0x04030201);
70+
constbuf=Buffer.from(bufData.buffer,bufData.byteOffset);
71+
constotherBufData=newUint32Array(256).fill(0x03040102);
72+
constotherBuf=Buffer.from(otherBufData.buffer,otherBufData.byteOffset);
73+
buf.swap16();
74+
assert.deepStrictEqual(buf,otherBuf);
9875
}
99-
for(leti=1;i<buf13A.length;i++){
100-
buf13A[buf13A.length-i]=(i+1)%4;
76+
77+
{
78+
constbufData=newUint32Array(256).fill(0x04030201);
79+
constbuf=Buffer.from(bufData.buffer);
80+
constotherBufData=newUint32Array(256).fill(0x01020304);
81+
constotherBuf=Buffer.from(otherBufData.buffer,otherBufData.byteOffset);
82+
buf.swap32();
83+
assert.deepStrictEqual(buf,otherBuf);
10184
}
102-
constbuf12=Buffer.from(buf12A.buffer,buf12A.byteOffset);
103-
// 0|1 2 3 0|1 2 3...
104-
constbuf13=Buffer.from(buf13A.buffer,buf13A.byteOffset);
105-
// 0|0 3 2 1|0 3 2...
10685

107-
buf12.slice(1,buf12.length-3).swap32();
108-
assert.deepStrictEqual(buf12.slice(0,buf13.length),buf13);
86+
{
87+
constbufData=newUint8Array(256*8);
88+
constotherBufData=newUint8Array(256*8);
89+
for(leti=0;i<bufData.length;i++){
90+
bufData[i]=i%8;
91+
otherBufData[otherBufData.length-i-1]=i%8;
92+
}
93+
constbuf=Buffer.from(bufData.buffer,bufData.byteOffset);
94+
constotherBuf=Buffer.from(otherBufData.buffer,otherBufData.byteOffset);
95+
buf.swap64();
96+
assert.deepStrictEqual(buf,otherBuf);
97+
}
10998

99+
// Test native code with buffers that are not memory-aligned
100+
{
101+
constbufData=newUint8Array(256*8);
102+
constotherBufData=newUint8Array(256*8-2);
103+
for(leti=0;i<bufData.length;i++){
104+
bufData[i]=i%2;
105+
}
106+
for(leti=1;i<otherBufData.length;i++){
107+
otherBufData[otherBufData.length-i]=(i+1)%2;
108+
}
109+
constbuf=Buffer.from(bufData.buffer,bufData.byteOffset);
110+
// 0|1 0|1 0|1...
111+
constotherBuf=Buffer.from(otherBufData.buffer,otherBufData.byteOffset);
112+
// 0|0 1|0 1|0...
113+
114+
buf.slice(1,buf.length-1).swap16();
115+
assert.deepStrictEqual(buf.slice(0,otherBuf.length),otherBuf);
116+
}
110117

111-
constbuf14A=newUint8Array(256*8);
112-
constbuf15A=newUint8Array(256*8-8);
113-
for(leti=0;i<buf14A.length;i++){
114-
buf14A[i]=i%8;
118+
{
119+
constbufData=newUint8Array(256*8);
120+
constotherBufData=newUint8Array(256*8-4);
121+
for(leti=0;i<bufData.length;i++){
122+
bufData[i]=i%4;
123+
}
124+
for(leti=1;i<otherBufData.length;i++){
125+
otherBufData[otherBufData.length-i]=(i+1)%4;
126+
}
127+
constbuf=Buffer.from(bufData.buffer,bufData.byteOffset);
128+
// 0|1 2 3 0|1 2 3...
129+
constotherBuf=Buffer.from(otherBufData.buffer,otherBufData.byteOffset);
130+
// 0|0 3 2 1|0 3 2...
131+
132+
buf.slice(1,buf.length-3).swap32();
133+
assert.deepStrictEqual(buf.slice(0,otherBuf.length),otherBuf);
115134
}
116-
for(leti=1;i<buf15A.length;i++){
117-
buf15A[buf15A.length-i]=(i+1)%8;
135+
136+
{
137+
constbufData=newUint8Array(256*8);
138+
constotherBufData=newUint8Array(256*8-8);
139+
for(leti=0;i<bufData.length;i++){
140+
bufData[i]=i%8;
141+
}
142+
for(leti=1;i<otherBufData.length;i++){
143+
otherBufData[otherBufData.length-i]=(i+1)%8;
144+
}
145+
constbuf=Buffer.from(bufData.buffer,bufData.byteOffset);
146+
// 0|1 2 3 4 5 6 7 0|1 2 3 4...
147+
constotherBuf=Buffer.from(otherBufData.buffer,otherBufData.byteOffset);
148+
// 0|0 7 6 5 4 3 2 1|0 7 6 5...
149+
150+
buf.slice(1,buf.length-7).swap64();
151+
assert.deepStrictEqual(buf.slice(0,otherBuf.length),otherBuf);
118152
}
119-
constbuf14=Buffer.from(buf14A.buffer,buf14A.byteOffset);
120-
// 0|1 2 3 4 5 6 7 0|1 2 3 4...
121-
constbuf15=Buffer.from(buf15A.buffer,buf15A.byteOffset);
122-
// 0|0 7 6 5 4 3 2 1|0 7 6 5...
123-
124-
buf14.slice(1,buf14.length-7).swap64();
125-
assert.deepStrictEqual(buf14.slice(0,buf15.length),buf15);
126-
127-
// Length assertions
128-
constre16=/Buffersizemustbeamultipleof16-bits/;
129-
constre32=/Buffersizemustbeamultipleof32-bits/;
130-
constre64=/Buffersizemustbeamultipleof64-bits/;
131-
132-
assert.throws(()=>Buffer.from(buf3).swap16(),re16);
133-
assert.throws(()=>Buffer.alloc(1025).swap16(),re16);
134-
assert.throws(()=>Buffer.from(buf3).swap32(),re32);
135-
assert.throws(()=>buf3.slice(1,3).swap32(),re32);
136-
assert.throws(()=>Buffer.alloc(1025).swap32(),re32);
137-
assert.throws(()=>buf3.slice(1,3).swap64(),re64);
138-
assert.throws(()=>Buffer.alloc(1025).swap64(),re64);

0 commit comments

Comments
(0)