@@ -4,135 +4,149 @@ require('../common');
44const assert = require ( 'assert' ) ;
55
66// Test buffers small enough to use the JS implementation
7- const buf = 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+ const buf = 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- const buf3 = 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- const buf3_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- const buf4A = new Uint32Array ( 256 ) . fill ( 0x04030201 ) ;
49- const buf4 = Buffer . from ( buf4A . buffer , buf4A . byteOffset ) ;
50- const buf5A = new Uint32Array ( 256 ) . fill ( 0x03040102 ) ;
51- const buf5 = Buffer . from ( buf5A . buffer , buf5A . byteOffset ) ;
52-
53- buf4 . swap16 ( ) ;
54- assert . deepStrictEqual ( buf4 , buf5 ) ;
55-
56- const buf6A = new Uint32Array ( 256 ) . fill ( 0x04030201 ) ;
57- const buf6 = Buffer . from ( buf6A . buffer ) ;
58- const bu7A = new Uint32Array ( 256 ) . fill ( 0x01020304 ) ;
59- const buf7 = Buffer . from ( bu7A . buffer , bu7A . byteOffset ) ;
60-
61- buf6 . swap32 ( ) ;
62- assert . deepStrictEqual ( buf6 , buf7 ) ;
63-
64- const buf8A = new Uint8Array ( 256 * 8 ) ;
65- const buf9A = new Uint8Array ( 256 * 8 ) ;
66- for ( let i = 0 ; i < buf8A . length ; i ++ ) {
67- buf8A [ i ] = i % 8 ;
68- buf9A [ buf9A . length - i - 1 ] = i % 8 ;
30+ {
31+ const buf = 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+ const re16 = / B u f f e r s i z e m u s t b e a m u l t i p l e o f 1 6 - b i t s / ;
39+ const re32 = / B u f f e r s i z e m u s t b e a m u l t i p l e o f 3 2 - b i t s / ;
40+ const re64 = / B u f f e r s i z e m u s t b e a m u l t i p l e o f 6 4 - b i t s / ;
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- const buf8 = Buffer . from ( buf8A . buffer , buf8A . byteOffset ) ;
71- const buf9 = 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- const buf10A = new Uint8Array ( 256 * 8 ) ;
78- const buf11A = new Uint8Array ( 256 * 8 - 2 ) ;
79- for ( let i = 0 ; i < buf10A . length ; i ++ ) {
80- buf10A [ i ] = i % 2 ;
81- }
82- for ( let i = 1 ; i < buf11A . length ; i ++ ) {
83- buf11A [ buf11A . length - i ] = ( i + 1 ) % 2 ;
84- }
85- const buf10 = Buffer . from ( buf10A . buffer , buf10A . byteOffset ) ;
86- // 0|1 0|1 0|1...
87- const buf11 = Buffer . from ( buf11A . buffer , buf11A . byteOffset ) ;
88- // 0|0 1|0 1|0...
51+ {
52+ const buf = 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- const buf12A = new Uint8Array ( 256 * 8 ) ;
95- const buf13A = new Uint8Array ( 256 * 8 - 4 ) ;
96- for ( let i = 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+ const bufData = new Uint32Array ( 256 ) . fill ( 0x04030201 ) ;
70+ const buf = Buffer . from ( bufData . buffer , bufData . byteOffset ) ;
71+ const otherBufData = new Uint32Array ( 256 ) . fill ( 0x03040102 ) ;
72+ const otherBuf = Buffer . from ( otherBufData . buffer , otherBufData . byteOffset ) ;
73+ buf . swap16 ( ) ;
74+ assert . deepStrictEqual ( buf , otherBuf ) ;
9875}
99- for ( let i = 1 ; i < buf13A . length ; i ++ ) {
100- buf13A [ buf13A . length - i ] = ( i + 1 ) % 4 ;
76+
77+ {
78+ const bufData = new Uint32Array ( 256 ) . fill ( 0x04030201 ) ;
79+ const buf = Buffer . from ( bufData . buffer ) ;
80+ const otherBufData = new Uint32Array ( 256 ) . fill ( 0x01020304 ) ;
81+ const otherBuf = Buffer . from ( otherBufData . buffer , otherBufData . byteOffset ) ;
82+ buf . swap32 ( ) ;
83+ assert . deepStrictEqual ( buf , otherBuf ) ;
10184}
102- const buf12 = Buffer . from ( buf12A . buffer , buf12A . byteOffset ) ;
103- // 0|1 2 3 0|1 2 3...
104- const buf13 = 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+ const bufData = new Uint8Array ( 256 * 8 ) ;
88+ const otherBufData = new Uint8Array ( 256 * 8 ) ;
89+ for ( let i = 0 ; i < bufData . length ; i ++ ) {
90+ bufData [ i ] = i % 8 ;
91+ otherBufData [ otherBufData . length - i - 1 ] = i % 8 ;
92+ }
93+ const buf = Buffer . from ( bufData . buffer , bufData . byteOffset ) ;
94+ const otherBuf = 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+ const bufData = new Uint8Array ( 256 * 8 ) ;
102+ const otherBufData = new Uint8Array ( 256 * 8 - 2 ) ;
103+ for ( let i = 0 ; i < bufData . length ; i ++ ) {
104+ bufData [ i ] = i % 2 ;
105+ }
106+ for ( let i = 1 ; i < otherBufData . length ; i ++ ) {
107+ otherBufData [ otherBufData . length - i ] = ( i + 1 ) % 2 ;
108+ }
109+ const buf = Buffer . from ( bufData . buffer , bufData . byteOffset ) ;
110+ // 0|1 0|1 0|1...
111+ const otherBuf = 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- const buf14A = new Uint8Array ( 256 * 8 ) ;
112- const buf15A = new Uint8Array ( 256 * 8 - 8 ) ;
113- for ( let i = 0 ; i < buf14A . length ; i ++ ) {
114- buf14A [ i ] = i % 8 ;
118+ {
119+ const bufData = new Uint8Array ( 256 * 8 ) ;
120+ const otherBufData = new Uint8Array ( 256 * 8 - 4 ) ;
121+ for ( let i = 0 ; i < bufData . length ; i ++ ) {
122+ bufData [ i ] = i % 4 ;
123+ }
124+ for ( let i = 1 ; i < otherBufData . length ; i ++ ) {
125+ otherBufData [ otherBufData . length - i ] = ( i + 1 ) % 4 ;
126+ }
127+ const buf = Buffer . from ( bufData . buffer , bufData . byteOffset ) ;
128+ // 0|1 2 3 0|1 2 3...
129+ const otherBuf = 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 ( let i = 1 ; i < buf15A . length ; i ++ ) {
117- buf15A [ buf15A . length - i ] = ( i + 1 ) % 8 ;
135+
136+ {
137+ const bufData = new Uint8Array ( 256 * 8 ) ;
138+ const otherBufData = new Uint8Array ( 256 * 8 - 8 ) ;
139+ for ( let i = 0 ; i < bufData . length ; i ++ ) {
140+ bufData [ i ] = i % 8 ;
141+ }
142+ for ( let i = 1 ; i < otherBufData . length ; i ++ ) {
143+ otherBufData [ otherBufData . length - i ] = ( i + 1 ) % 8 ;
144+ }
145+ const buf = Buffer . from ( bufData . buffer , bufData . byteOffset ) ;
146+ // 0|1 2 3 4 5 6 7 0|1 2 3 4...
147+ const otherBuf = 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- const buf14 = Buffer . from ( buf14A . buffer , buf14A . byteOffset ) ;
120- // 0|1 2 3 4 5 6 7 0|1 2 3 4...
121- const buf15 = 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- const re16 = / B u f f e r s i z e m u s t b e a m u l t i p l e o f 1 6 - b i t s / ;
129- const re32 = / B u f f e r s i z e m u s t b e a m u l t i p l e o f 3 2 - b i t s / ;
130- const re64 = / B u f f e r s i z e m u s t b e a m u l t i p l e o f 6 4 - b i t s / ;
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