summaryrefslogtreecommitdiff
path: root/test/parallel/test-buffer-swap.js
blob: 82b550790e723222a005eb8369edcdd1fbb86549 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict';

require('../common');
const assert = require('assert');

// Test buffers small enough to use the JS implementation
{
  const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
                           0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]);

  assert.strictEqual(buf, buf.swap16());
  assert.deepStrictEqual(buf, Buffer.from([0x02, 0x01, 0x04, 0x03, 0x06, 0x05,
                                           0x08, 0x07, 0x0a, 0x09, 0x0c, 0x0b,
                                           0x0e, 0x0d, 0x10, 0x0f]));
  buf.swap16(); // restore

  assert.strictEqual(buf, buf.swap32());
  assert.deepStrictEqual(buf, Buffer.from([0x04, 0x03, 0x02, 0x01, 0x08, 0x07,
                                           0x06, 0x05, 0x0c, 0x0b, 0x0a, 0x09,
                                           0x10, 0x0f, 0x0e, 0x0d]));
  buf.swap32(); // restore

  assert.strictEqual(buf, buf.swap64());
  assert.deepStrictEqual(buf, Buffer.from([0x08, 0x07, 0x06, 0x05, 0x04, 0x03,
                                           0x02, 0x01, 0x10, 0x0f, 0x0e, 0x0d,
                                           0x0c, 0x0b, 0x0a, 0x09]));
}

// Operates in-place
{
  const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7]);
  buf.slice(1, 5).swap32();
  assert.deepStrictEqual(buf, Buffer.from([0x1, 0x5, 0x4, 0x3, 0x2, 0x6, 0x7]));
  buf.slice(1, 5).swap16();
  assert.deepStrictEqual(buf, Buffer.from([0x1, 0x4, 0x5, 0x2, 0x3, 0x6, 0x7]));

  // Length assertions
  const re16 = /Buffer size must be a multiple of 16-bits/;
  const re32 = /Buffer size must be a multiple of 32-bits/;
  const re64 = /Buffer size must be a multiple of 64-bits/;

  assert.throws(() => Buffer.from(buf).swap16(), re16);
  assert.throws(() => Buffer.alloc(1025).swap16(), re16);
  assert.throws(() => Buffer.from(buf).swap32(), re32);
  assert.throws(() => buf.slice(1, 3).swap32(), re32);
  assert.throws(() => Buffer.alloc(1025).swap32(), re32);
  assert.throws(() => buf.slice(1, 3).swap64(), re64);
  assert.throws(() => Buffer.alloc(1025).swap64(), re64);
}

{
  const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
                           0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
                           0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
                           0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]);

  buf.slice(2, 18).swap64();

  assert.deepStrictEqual(buf, Buffer.from([0x01, 0x02, 0x0a, 0x09, 0x08, 0x07,
                                           0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
                                           0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b,
                                           0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
                                           0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
                                           0x0f, 0x10]));
}

// Force use of native code (Buffer size above threshold limit for js impl)
{
  const bufData = new Uint32Array(256).fill(0x04030201);
  const buf = Buffer.from(bufData.buffer, bufData.byteOffset);
  const otherBufData = new Uint32Array(256).fill(0x03040102);
  const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset);
  buf.swap16();
  assert.deepStrictEqual(buf, otherBuf);
}

{
  const bufData = new Uint32Array(256).fill(0x04030201);
  const buf = Buffer.from(bufData.buffer);
  const otherBufData = new Uint32Array(256).fill(0x01020304);
  const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset);
  buf.swap32();
  assert.deepStrictEqual(buf, otherBuf);
}

{
  const bufData = new Uint8Array(256 * 8);
  const otherBufData = new Uint8Array(256 * 8);
  for (let i = 0; i < bufData.length; i++) {
    bufData[i] = i % 8;
    otherBufData[otherBufData.length - i - 1] = i % 8;
  }
  const buf = Buffer.from(bufData.buffer, bufData.byteOffset);
  const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset);
  buf.swap64();
  assert.deepStrictEqual(buf, otherBuf);
}

// Test native code with buffers that are not memory-aligned
{
  const bufData = new Uint8Array(256 * 8);
  const otherBufData = new Uint8Array(256 * 8 - 2);
  for (let i = 0; i < bufData.length; i++) {
    bufData[i] = i % 2;
  }
  for (let i = 1; i < otherBufData.length; i++) {
    otherBufData[otherBufData.length - i] = (i + 1) % 2;
  }
  const buf = Buffer.from(bufData.buffer, bufData.byteOffset);
  // 0|1 0|1 0|1...
  const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset);
  // 0|0 1|0 1|0...

  buf.slice(1, buf.length - 1).swap16();
  assert.deepStrictEqual(buf.slice(0, otherBuf.length), otherBuf);
}

{
  const bufData = new Uint8Array(256 * 8);
  const otherBufData = new Uint8Array(256 * 8 - 4);
  for (let i = 0; i < bufData.length; i++) {
    bufData[i] = i % 4;
  }
  for (let i = 1; i < otherBufData.length; i++) {
    otherBufData[otherBufData.length - i] = (i + 1) % 4;
  }
  const buf = Buffer.from(bufData.buffer, bufData.byteOffset);
  // 0|1 2 3 0|1 2 3...
  const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset);
  // 0|0 3 2 1|0 3 2...

  buf.slice(1, buf.length - 3).swap32();
  assert.deepStrictEqual(buf.slice(0, otherBuf.length), otherBuf);
}

{
  const bufData = new Uint8Array(256 * 8);
  const otherBufData = new Uint8Array(256 * 8 - 8);
  for (let i = 0; i < bufData.length; i++) {
    bufData[i] = i % 8;
  }
  for (let i = 1; i < otherBufData.length; i++) {
    otherBufData[otherBufData.length - i] = (i + 1) % 8;
  }
  const buf = Buffer.from(bufData.buffer, bufData.byteOffset);
  // 0|1 2 3 4 5 6 7 0|1 2 3 4...
  const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset);
  // 0|0 7 6 5 4 3 2 1|0 7 6 5...

  buf.slice(1, buf.length - 7).swap64();
  assert.deepStrictEqual(buf.slice(0, otherBuf.length), otherBuf);
}