• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3require('../common');
4const assert = require('assert');
5
6// Test buffers small enough to use the JS implementation
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}
28
29// Operates in-place
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 = /Buffer size must be a multiple of 16-bits/;
39  const re32 = /Buffer size must be a multiple of 32-bits/;
40  const re64 = /Buffer size must be a multiple of 64-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);
49}
50
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]);
56
57  buf.slice(2, 18).swap64();
58
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}
66
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);
75}
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);
84}
85
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}
98
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}
117
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);
134}
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);
152}
153