1
// Copyright Joyent, Inc. and other Node contributors.
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
23
* Test to verify we are using Typed Arrays
24
* (http://www.khronos.org/registry/typedarray/specs/latest/) correctly Test to
25
* verify Buffer can used in Typed Arrays
28
var common = require('../common');
29
var assert = require('assert');
42
].forEach(function(name) {
43
var expected = '[object ' + name + ']';
44
var clazz = global[name];
45
var obj = new clazz(1);
47
assert.equal(obj.toString(), expected);
48
assert.equal(Object.prototype.toString.call(obj), expected);
50
obj = new DataView(obj.buffer || obj);
51
assert.equal(obj.toString(), '[object DataView]');
52
assert.equal(Object.prototype.toString.call(obj), '[object DataView]');
54
// Calling constructor as function should work.
58
// Calling constructor as function should work.
59
DataView(ArrayBuffer(32));
61
var buffer = new ArrayBuffer(16);
62
var uint8 = new Uint8Array(buffer);
63
var uint16 = new Uint16Array(buffer);
64
var uint16slice = new Uint16Array(buffer, 2, 2);
65
var uint32 = new Uint32Array(buffer);
67
assert.equal(uint8.BYTES_PER_ELEMENT, 1);
68
assert.equal(uint16.BYTES_PER_ELEMENT, 2);
69
assert.equal(uint16slice.BYTES_PER_ELEMENT, 2);
70
assert.equal(uint32.BYTES_PER_ELEMENT, 4);
72
// now change the underlying buffer
83
This is what we expect the variables to look like at this point (on
84
little-endian machines):
86
uint8 | 0x08 | 0x09 | 0x0a | 0x0b | 0x0c | 0x0d | 0x0e | 0x0f |
87
uint16 | 0x0908 | 0x0b0a | 0x0d0c | 0x0f0e |
88
uint16slice --------------| 0x0b0a | 0x0d0c |--------------
89
uint32 | 0x0b0a0908 | 0x0f0e0d0c |
92
assert.equal(uint8[0], 0x08);
93
assert.equal(uint8[1], 0x09);
94
assert.equal(uint8[2], 0x0a);
95
assert.equal(uint8[3], 0x0b);
96
assert.equal(uint8[4], 0x0c);
97
assert.equal(uint8[5], 0x0d);
98
assert.equal(uint8[6], 0x0e);
99
assert.equal(uint8[7], 0x0f);
101
// determine whether or not typed array values are stored little-endian first
103
var IS_LITTLE_ENDIAN = (new Uint16Array([0x1234])).buffer[0] === 0x34;
105
if (IS_LITTLE_ENDIAN) {
106
assert.equal(uint16[0], 0x0908);
107
assert.equal(uint16[1], 0x0b0a);
108
assert.equal(uint16[2], 0x0d0c);
109
assert.equal(uint16[3], 0x0f0e);
111
assert.equal(uint16slice[0], 0x0b0a);
112
assert.equal(uint16slice[1], 0x0d0c);
114
assert.equal(uint32[0], 0x0b0a0908);
115
assert.equal(uint32[1], 0x0f0e0d0c);
117
assert.equal(uint16[0], 0x0809);
118
assert.equal(uint16[1], 0x0a0b);
119
assert.equal(uint16[2], 0x0c0d);
120
assert.equal(uint16[3], 0x0e0f);
122
assert.equal(uint16slice[0], 0x0a0b);
123
assert.equal(uint16slice[1], 0x0c0d);
125
assert.equal(uint32[0], 0x08090a0b);
126
assert.equal(uint32[1], 0x0c0d0e0f);
129
// test .subarray(begin, end)
130
var sub = uint8.subarray(2, 4);
132
assert.ok(sub instanceof Uint8Array);
133
assert.equal(sub[0], 0x0a);
134
assert.equal(sub[1], 0x0b);
136
// modifications of a value in the subarray of `uint8` should propagate to
141
assert.equal(uint8[2], 0x12);
142
assert.equal(uint8[3], 0x34);
144
// test .set(index, value), .set(arr, offset) and .get(index)
146
uint8.set([0x0a, 0x0b], 2);
148
assert.equal(uint8.get(1), 0x09);
149
assert.equal(uint8.get(2), 0x0a);
150
assert.equal(uint8.get(3), 0x0b);
152
// test clamped array
153
var uint8c = new Uint8ClampedArray(buffer);
157
assert.equal(uint8c[0], 0);
158
assert.equal(uint8c[1], 255);
163
assert.equal(uint8c[0], 0);
164
assert.equal(uint8c[1], 255);
168
for (var i = 128; i <= 255; ++i) numbers.push(i);
169
var array = new Uint8Array(numbers);
170
var view = new DataView(array.buffer);
171
for (var i = 128; i <= 255; ++i) assert.equal(view.getInt8(i - 128), i - 256);
174
assert.throws(function() {
175
var buf = new DataView(new ArrayBuffer(8));
176
buf.getFloat64(0xffffffff, true);
177
}, /Index out of range/);
179
assert.throws(function() {
180
var buf = new DataView(new ArrayBuffer(8));
181
buf.setFloat64(0xffffffff, 0.0, true);
182
}, /Index out of range/);
184
// DataView::setGeneric() default endianness regression test,
185
// see https://github.com/joyent/node/issues/4626
187
var buf = new Uint8Array(2);
188
var view = new DataView(buf.buffer);
189
view.setUint16(0, 1);
190
assert.equal(view.getUint16(0), 1);
194
// Typed array should make a copy of the buffer object, i.e. it's not shared.
195
var b = new Buffer([0]);
196
var a = new Uint8Array(b);
197
assert.notEqual(a.buffer, b);
198
assert.equal(a[0], 0);
199
assert.equal(b[0], 0);
201
assert.equal(a[0], 1);
202
assert.equal(b[0], 0);
205
assert.equal(a[0], 0);
206
assert.equal(b[0], 1);
210
// Backing store should not be shared.
211
var a = new Uint8Array(1);
212
var b = new Uint8Array(a);
215
assert.equal(a[0], 0);
216
assert.equal(b[0], 1);
217
assert.notEqual(a, b.buffer);
218
assert.notEqual(a.buffer, b.buffer);
222
// Backing store should not be shared.
223
var a = new Uint8Array(2);
224
var b = new Uint16Array(a);
228
assert.equal(a[0], 0);
229
assert.equal(a[1], 0);
230
assert.equal(b[0], 257);
231
assert.notEqual(a, b.buffer);
232
assert.notEqual(a.buffer, b.buffer);
236
// Backing store should be shared.
237
var abuf = new ArrayBuffer(32);
238
var a = new Uint8Array(abuf);
239
var b = new Uint8Array(abuf);
242
assert.equal(a[0], 1);
243
assert.equal(b[0], 1);
244
assert.equal(a.buffer, b.buffer);
247
assert.throws(function() {
248
new DataView(new Int8Array(1));