File: test-typed-arrays.js

package info (click to toggle)
nodejs 0.10.29~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,760 kB
  • ctags: 58,506
  • sloc: cpp: 330,356; ansic: 47,440; python: 12,756; sh: 1,365; makefile: 665; lisp: 222; ruby: 76; xml: 16; awk: 10
file content (249 lines) | stat: -rw-r--r-- 6,967 bytes parent folder | download | duplicates (2)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright Joyent, Inc. and other Node contributors.

// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:

// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

/*
 * Test to verify we are using Typed Arrays
 * (http://www.khronos.org/registry/typedarray/specs/latest/) correctly Test to
 * verify Buffer can used in Typed Arrays
 */

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

[
  'ArrayBuffer',
  'Int8Array',
  'Uint8Array',
  'Int16Array',
  'Uint16Array',
  'Int32Array',
  'Uint32Array',
  'Float32Array',
  'Float64Array',
  'Uint8ClampedArray'
].forEach(function(name) {
  var expected = '[object ' + name + ']';
  var clazz = global[name];
  var obj = new clazz(1);

  assert.equal(obj.toString(), expected);
  assert.equal(Object.prototype.toString.call(obj), expected);

  obj = new DataView(obj.buffer || obj);
  assert.equal(obj.toString(), '[object DataView]');
  assert.equal(Object.prototype.toString.call(obj), '[object DataView]');

  // Calling constructor as function should work.
  clazz(32);
});

// Calling constructor as function should work.
DataView(ArrayBuffer(32));

var buffer = new ArrayBuffer(16);
var uint8 = new Uint8Array(buffer);
var uint16 = new Uint16Array(buffer);
var uint16slice = new Uint16Array(buffer, 2, 2);
var uint32 = new Uint32Array(buffer);

assert.equal(uint8.BYTES_PER_ELEMENT, 1);
assert.equal(uint16.BYTES_PER_ELEMENT, 2);
assert.equal(uint16slice.BYTES_PER_ELEMENT, 2);
assert.equal(uint32.BYTES_PER_ELEMENT, 4);

// now change the underlying buffer
buffer[0] = 0x08;
buffer[1] = 0x09;
buffer[2] = 0x0a;
buffer[3] = 0x0b;
buffer[4] = 0x0c;
buffer[5] = 0x0d;
buffer[6] = 0x0e;
buffer[7] = 0x0f;

/*
  This is what we expect the variables to look like at this point (on
  little-endian machines):

  uint8       | 0x08 | 0x09 | 0x0a | 0x0b | 0x0c | 0x0d | 0x0e | 0x0f |
  uint16      |    0x0908   |    0x0b0a   |    0x0d0c   |    0x0f0e   |
  uint16slice --------------|    0x0b0a   |    0x0d0c   |--------------
  uint32      |         0x0b0a0908        |         0x0f0e0d0c        |
*/

assert.equal(uint8[0], 0x08);
assert.equal(uint8[1], 0x09);
assert.equal(uint8[2], 0x0a);
assert.equal(uint8[3], 0x0b);
assert.equal(uint8[4], 0x0c);
assert.equal(uint8[5], 0x0d);
assert.equal(uint8[6], 0x0e);
assert.equal(uint8[7], 0x0f);

// determine whether or not typed array values are stored little-endian first
// internally
var IS_LITTLE_ENDIAN = (new Uint16Array([0x1234])).buffer[0] === 0x34;

if (IS_LITTLE_ENDIAN) {
  assert.equal(uint16[0], 0x0908);
  assert.equal(uint16[1], 0x0b0a);
  assert.equal(uint16[2], 0x0d0c);
  assert.equal(uint16[3], 0x0f0e);

  assert.equal(uint16slice[0], 0x0b0a);
  assert.equal(uint16slice[1], 0x0d0c);

  assert.equal(uint32[0], 0x0b0a0908);
  assert.equal(uint32[1], 0x0f0e0d0c);
} else {
  assert.equal(uint16[0], 0x0809);
  assert.equal(uint16[1], 0x0a0b);
  assert.equal(uint16[2], 0x0c0d);
  assert.equal(uint16[3], 0x0e0f);

  assert.equal(uint16slice[0], 0x0a0b);
  assert.equal(uint16slice[1], 0x0c0d);

  assert.equal(uint32[0], 0x08090a0b);
  assert.equal(uint32[1], 0x0c0d0e0f);
}

// test .subarray(begin, end)
var sub = uint8.subarray(2, 4);

assert.ok(sub instanceof Uint8Array);
assert.equal(sub[0], 0x0a);
assert.equal(sub[1], 0x0b);

// modifications of a value in the subarray of `uint8` should propagate to
// the other views
sub[0] = 0x12;
sub[1] = 0x34;

assert.equal(uint8[2], 0x12);
assert.equal(uint8[3], 0x34);

// test .set(index, value), .set(arr, offset) and .get(index)
uint8.set(1, 0x09);
uint8.set([0x0a, 0x0b], 2);

assert.equal(uint8.get(1), 0x09);
assert.equal(uint8.get(2), 0x0a);
assert.equal(uint8.get(3), 0x0b);

// test clamped array
var uint8c = new Uint8ClampedArray(buffer);
uint8c[0] = -1;
uint8c[1] = 257;

assert.equal(uint8c[0], 0);
assert.equal(uint8c[1], 255);

uint8c.set(0, -10);
uint8c.set(1, 260);

assert.equal(uint8c[0], 0);
assert.equal(uint8c[1], 255);

(function() {
  var numbers = [];
  for (var i = 128; i <= 255; ++i) numbers.push(i);
  var array = new Uint8Array(numbers);
  var view = new DataView(array.buffer);
  for (var i = 128; i <= 255; ++i) assert.equal(view.getInt8(i - 128), i - 256);
})();

assert.throws(function() {
  var buf = new DataView(new ArrayBuffer(8));
  buf.getFloat64(0xffffffff, true);
}, /Index out of range/);

assert.throws(function() {
  var buf = new DataView(new ArrayBuffer(8));
  buf.setFloat64(0xffffffff, 0.0, true);
}, /Index out of range/);

// DataView::setGeneric() default endianness regression test,
// see https://github.com/joyent/node/issues/4626
(function() {
  var buf = new Uint8Array(2);
  var view = new DataView(buf.buffer);
  view.setUint16(0, 1);
  assert.equal(view.getUint16(0), 1);
})();

(function() {
  // Typed array should make a copy of the buffer object, i.e. it's not shared.
  var b = new Buffer([0]);
  var a = new Uint8Array(b);
  assert.notEqual(a.buffer, b);
  assert.equal(a[0], 0);
  assert.equal(b[0], 0);
  a[0] = 1;
  assert.equal(a[0], 1);
  assert.equal(b[0], 0);
  a[0] = 0;
  b[0] = 1;
  assert.equal(a[0], 0);
  assert.equal(b[0], 1);
})();

(function() {
  // Backing store should not be shared.
  var a = new Uint8Array(1);
  var b = new Uint8Array(a);
  a[0] = 0;
  b[0] = 1;
  assert.equal(a[0], 0);
  assert.equal(b[0], 1);
  assert.notEqual(a, b.buffer);
  assert.notEqual(a.buffer, b.buffer);
})();

(function() {
  // Backing store should not be shared.
  var a = new Uint8Array(2);
  var b = new Uint16Array(a);
  a[0] = 0;
  a[1] = 0;
  b[0] = 257;
  assert.equal(a[0], 0);
  assert.equal(a[1], 0);
  assert.equal(b[0], 257);
  assert.notEqual(a, b.buffer);
  assert.notEqual(a.buffer, b.buffer);
})();

(function() {
  // Backing store should be shared.
  var abuf = new ArrayBuffer(32);
  var a = new Uint8Array(abuf);
  var b = new Uint8Array(abuf);
  a[0] = 0;
  b[0] = 1;
  assert.equal(a[0], 1);
  assert.equal(b[0], 1);
  assert.equal(a.buffer, b.buffer);
})();

assert.throws(function() {
  new DataView(new Int8Array(1));
});