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 @ fe103357

History | View | Annotate | Download (6.71 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

    
55
// initialize a zero-filled buffer
56
var buffer = new Buffer(16);
57
buffer.fill(0);
58

    
59
// only one of these instantiations should succeed, as the other ones will be
60
// unaligned
61
var errors = 0;
62
var offset;
63
for (var i = 0; i < 8; i++) {
64
  try {
65
    new Float64Array(buffer, i);
66
    offset = i;
67
  } catch (e) {
68
    errors += 1;
69
  }
70
}
71

    
72
assert.equal(errors, 7);
73

    
74
var uint8 = new Uint8Array(buffer, offset);
75
var uint16 = new Uint16Array(buffer, offset);
76
var uint16slice = new Uint16Array(buffer, offset + 2, 2);
77
var uint32 = new Uint32Array(buffer, offset);
78

    
79
assert.equal(uint8.BYTES_PER_ELEMENT, 1);
80
assert.equal(uint16.BYTES_PER_ELEMENT, 2);
81
assert.equal(uint16slice.BYTES_PER_ELEMENT, 2);
82
assert.equal(uint32.BYTES_PER_ELEMENT, 4);
83

    
84
// now change the underlying buffer
85
buffer[offset    ] = 0x08;
86
buffer[offset + 1] = 0x09;
87
buffer[offset + 2] = 0x0a;
88
buffer[offset + 3] = 0x0b;
89
buffer[offset + 4] = 0x0c;
90
buffer[offset + 5] = 0x0d;
91
buffer[offset + 6] = 0x0e;
92
buffer[offset + 7] = 0x0f;
93

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

98
  uint8       | 0x08 | 0x09 | 0x0a | 0x0b | 0x0c | 0x0d | 0x0e | 0x0f |
99
  uint16      |    0x0908   |    0x0b0a   |    0x0d0c   |    0x0f0e   |
100
  uint16slice --------------|    0x0b0a   |    0x0d0c   |--------------
101
  uint32      |         0x0b0a0908        |         0x0f0e0d0c        |
102
*/
103

    
104
assert.equal(uint8[0], 0x08);
105
assert.equal(uint8[1], 0x09);
106
assert.equal(uint8[2], 0x0a);
107
assert.equal(uint8[3], 0x0b);
108
assert.equal(uint8[4], 0x0c);
109
assert.equal(uint8[5], 0x0d);
110
assert.equal(uint8[6], 0x0e);
111
assert.equal(uint8[7], 0x0f);
112

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

    
117
if (IS_LITTLE_ENDIAN) {
118
  assert.equal(uint16[0], 0x0908);
119
  assert.equal(uint16[1], 0x0b0a);
120
  assert.equal(uint16[2], 0x0d0c);
121
  assert.equal(uint16[3], 0x0f0e);
122

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

    
126
  assert.equal(uint32[0], 0x0b0a0908);
127
  assert.equal(uint32[1], 0x0f0e0d0c);
128
} else {
129
  assert.equal(uint16[0], 0x0809);
130
  assert.equal(uint16[1], 0x0a0b);
131
  assert.equal(uint16[2], 0x0c0d);
132
  assert.equal(uint16[3], 0x0e0f);
133

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

    
137
  assert.equal(uint32[0], 0x08090a0b);
138
  assert.equal(uint32[1], 0x0c0d0e0f);
139
}
140

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

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

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

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

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

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

    
164
// test clamped array
165
var uint8c = new Uint8ClampedArray(buffer);
166
uint8c[0] = -1;
167
uint8c[1] = 257;
168

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

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

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

    
178
(function() {
179
  var numbers = [];
180
  for (var i = 128; i <= 255; ++i) numbers.push(i);
181
  var array = new Uint8Array(numbers);
182
  var view = new DataView(array.buffer);
183
  for (var i = 128; i <= 255; ++i) assert.equal(view.getInt8(i - 128), i - 256);
184
})();
185

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

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

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

    
205
(function() {
206
  // Backing store should not be shared.
207
  var a = new Uint8Array(1);
208
  var b = new Uint8Array(a);
209
  a[0] = 0;
210
  b[0] = 1;
211
  assert.equal(a[0], 0);
212
  assert.equal(b[0], 1);
213
  assert.notEqual(a, b.buffer);
214
  assert.notEqual(a.buffer, b.buffer);
215
})();
216

    
217
(function() {
218
  // Backing store should not be shared.
219
  var a = new Uint8Array(2);
220
  var b = new Uint16Array(a);
221
  a[0] = 0;
222
  a[1] = 0;
223
  b[0] = 257;
224
  assert.equal(a[0], 0);
225
  assert.equal(a[1], 0);
226
  assert.equal(b[0], 257);
227
  assert.notEqual(a, b.buffer);
228
  assert.notEqual(a.buffer, b.buffer);
229
})();
230

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

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