The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / test / simple / test-typed-arrays.js @ ed3d553d

History | View | Annotate | Download (6.8 KB)

1
// Copyright Joyent, Inc. and other Node contributors.
2

    
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:
10

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

    
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.
21

    
22
/*
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
26
 */
27

    
28
var common = require('../common');
29
var assert = require('assert');
30

    
31
[
32
  'ArrayBuffer',
33
  'Int8Array',
34
  'Uint8Array',
35
  'Int16Array',
36
  'Uint16Array',
37
  'Int32Array',
38
  'Uint32Array',
39
  'Float32Array',
40
  'Float64Array',
41
  'Uint8ClampedArray'
42
].forEach(function(name) {
43
  var expected = '[object ' + name + ']';
44
  var clazz = global[name];
45
  var obj = new clazz(1);
46

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

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

    
54
  // Calling constructor as function should work.
55
  clazz(32);
56
});
57

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

    
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);
66

    
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);
71

    
72
// now change the underlying buffer
73
buffer[0] = 0x08;
74
buffer[1] = 0x09;
75
buffer[2] = 0x0a;
76
buffer[3] = 0x0b;
77
buffer[4] = 0x0c;
78
buffer[5] = 0x0d;
79
buffer[6] = 0x0e;
80
buffer[7] = 0x0f;
81

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

86
  uint8       | 0x08 | 0x09 | 0x0a | 0x0b | 0x0c | 0x0d | 0x0e | 0x0f |
87
  uint16      |    0x0908   |    0x0b0a   |    0x0d0c   |    0x0f0e   |
88
  uint16slice --------------|    0x0b0a   |    0x0d0c   |--------------
89
  uint32      |         0x0b0a0908        |         0x0f0e0d0c        |
90
*/
91

    
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);
100

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

    
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);
110

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

    
114
  assert.equal(uint32[0], 0x0b0a0908);
115
  assert.equal(uint32[1], 0x0f0e0d0c);
116
} else {
117
  assert.equal(uint16[0], 0x0809);
118
  assert.equal(uint16[1], 0x0a0b);
119
  assert.equal(uint16[2], 0x0c0d);
120
  assert.equal(uint16[3], 0x0e0f);
121

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

    
125
  assert.equal(uint32[0], 0x08090a0b);
126
  assert.equal(uint32[1], 0x0c0d0e0f);
127
}
128

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

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

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

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

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

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

    
152
// test clamped array
153
var uint8c = new Uint8ClampedArray(buffer);
154
uint8c[0] = -1;
155
uint8c[1] = 257;
156

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

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

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

    
166
(function() {
167
  var numbers = [];
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);
172
})();
173

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

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

    
184
// DataView::setGeneric() default endianness regression test,
185
// see https://github.com/joyent/node/issues/4626
186
(function() {
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);
191
})();
192

    
193
(function() {
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);
200
  a[0] = 1;
201
  assert.equal(a[0], 1);
202
  assert.equal(b[0], 0);
203
  a[0] = 0;
204
  b[0] = 1;
205
  assert.equal(a[0], 0);
206
  assert.equal(b[0], 1);
207
})();
208

    
209
(function() {
210
  // Backing store should not be shared.
211
  var a = new Uint8Array(1);
212
  var b = new Uint8Array(a);
213
  a[0] = 0;
214
  b[0] = 1;
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);
219
})();
220

    
221
(function() {
222
  // Backing store should not be shared.
223
  var a = new Uint8Array(2);
224
  var b = new Uint16Array(a);
225
  a[0] = 0;
226
  a[1] = 0;
227
  b[0] = 257;
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);
233
})();
234

    
235
(function() {
236
  // Backing store should be shared.
237
  var abuf = new ArrayBuffer(32);
238
  var a = new Uint8Array(abuf);
239
  var b = new Uint8Array(abuf);
240
  a[0] = 0;
241
  b[0] = 1;
242
  assert.equal(a[0], 1);
243
  assert.equal(b[0], 1);
244
  assert.equal(a.buffer, b.buffer);
245
})();
246

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