blob: 09d36dfaf5cbd70c32983840cac534bd660f0f7e [file] [log] [blame]
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001// Copyright 2012 the V8 project authors. All rights reserved.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
karlklose@chromium.org83a47282011-05-11 11:54:09 +000028// Flags: --allow-natives-syntax --expose-gc
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000029
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +000030// Helper
31function assertInstance(o, f) {
32 assertSame(o.constructor, f);
33 assertInstanceof(o, f);
34}
35
vegorov@chromium.org74f333b2011-04-06 11:17:46 +000036// This is a regression test for overlapping key and value registers.
37function f(a) {
38 a[0] = 0;
39 a[1] = 0;
40}
41
42var a = new Int32Array(2);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000043for (var i = 0; i < 5; i++) {
vegorov@chromium.org74f333b2011-04-06 11:17:46 +000044 f(a);
45}
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000046%OptimizeFunctionOnNextCall(f);
47f(a);
vegorov@chromium.org74f333b2011-04-06 11:17:46 +000048
49assertEquals(0, a[0]);
50assertEquals(0, a[1]);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +000051
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000052// No-parameter constructor should fail right now.
53function abfunc1() {
54 return new ArrayBuffer();
55}
56assertThrows(abfunc1);
57
58// Test derivation from an ArrayBuffer
59var ab = new ArrayBuffer(12);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +000060assertInstance(ab, ArrayBuffer);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000061var derived_uint8 = new Uint8Array(ab);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +000062assertInstance(derived_uint8, Uint8Array);
rossberg@chromium.org400388e2012-06-06 09:29:22 +000063assertSame(ab, derived_uint8.buffer);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000064assertEquals(12, derived_uint8.length);
rossberg@chromium.org400388e2012-06-06 09:29:22 +000065assertEquals(12, derived_uint8.byteLength);
66assertEquals(0, derived_uint8.byteOffset);
67assertEquals(1, derived_uint8.BYTES_PER_ELEMENT);
68var derived_uint8_2 = new Uint8Array(ab,7);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +000069assertInstance(derived_uint8_2, Uint8Array);
rossberg@chromium.org400388e2012-06-06 09:29:22 +000070assertSame(ab, derived_uint8_2.buffer);
71assertEquals(5, derived_uint8_2.length);
72assertEquals(5, derived_uint8_2.byteLength);
73assertEquals(7, derived_uint8_2.byteOffset);
74assertEquals(1, derived_uint8_2.BYTES_PER_ELEMENT);
75var derived_int16 = new Int16Array(ab);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +000076assertInstance(derived_int16, Int16Array);
rossberg@chromium.org400388e2012-06-06 09:29:22 +000077assertSame(ab, derived_int16.buffer);
78assertEquals(6, derived_int16.length);
79assertEquals(12, derived_int16.byteLength);
80assertEquals(0, derived_int16.byteOffset);
81assertEquals(2, derived_int16.BYTES_PER_ELEMENT);
82var derived_int16_2 = new Int16Array(ab,6);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +000083assertInstance(derived_int16_2, Int16Array);
rossberg@chromium.org400388e2012-06-06 09:29:22 +000084assertSame(ab, derived_int16_2.buffer);
85assertEquals(3, derived_int16_2.length);
86assertEquals(6, derived_int16_2.byteLength);
87assertEquals(6, derived_int16_2.byteOffset);
88assertEquals(2, derived_int16_2.BYTES_PER_ELEMENT);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000089var derived_uint32 = new Uint32Array(ab);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +000090assertInstance(derived_uint32, Uint32Array);
rossberg@chromium.org400388e2012-06-06 09:29:22 +000091assertSame(ab, derived_uint32.buffer);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000092assertEquals(3, derived_uint32.length);
rossberg@chromium.org400388e2012-06-06 09:29:22 +000093assertEquals(12, derived_uint32.byteLength);
94assertEquals(0, derived_uint32.byteOffset);
95assertEquals(4, derived_uint32.BYTES_PER_ELEMENT);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000096var derived_uint32_2 = new Uint32Array(ab,4);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +000097assertInstance(derived_uint32_2, Uint32Array);
rossberg@chromium.org400388e2012-06-06 09:29:22 +000098assertSame(ab, derived_uint32_2.buffer);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000099assertEquals(2, derived_uint32_2.length);
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000100assertEquals(8, derived_uint32_2.byteLength);
101assertEquals(4, derived_uint32_2.byteOffset);
102assertEquals(4, derived_uint32_2.BYTES_PER_ELEMENT);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000103var derived_uint32_3 = new Uint32Array(ab,4,1);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000104assertInstance(derived_uint32_3, Uint32Array);
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000105assertSame(ab, derived_uint32_3.buffer);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000106assertEquals(1, derived_uint32_3.length);
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000107assertEquals(4, derived_uint32_3.byteLength);
108assertEquals(4, derived_uint32_3.byteOffset);
109assertEquals(4, derived_uint32_3.BYTES_PER_ELEMENT);
110var derived_float64 = new Float64Array(ab,0,1);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000111assertInstance(derived_float64, Float64Array);
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000112assertSame(ab, derived_float64.buffer);
113assertEquals(1, derived_float64.length);
114assertEquals(8, derived_float64.byteLength);
115assertEquals(0, derived_float64.byteOffset);
116assertEquals(8, derived_float64.BYTES_PER_ELEMENT);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000117
118// If a given byteOffset and length references an area beyond the end of the
119// ArrayBuffer an exception is raised.
120function abfunc3() {
121 new Uint32Array(ab,4,3);
122}
123assertThrows(abfunc3);
124function abfunc4() {
125 new Uint32Array(ab,16);
126}
127assertThrows(abfunc4);
128
129// The given byteOffset must be a multiple of the element size of the specific
130// type, otherwise an exception is raised.
131function abfunc5() {
132 new Uint32Array(ab,5);
133}
134assertThrows(abfunc5);
135
136// If length is not explicitly specified, the length of the ArrayBuffer minus
137// the byteOffset must be a multiple of the element size of the specific type,
138// or an exception is raised.
139var ab2 = new ArrayBuffer(13);
140function abfunc6() {
141 new Uint32Array(ab2,4);
142}
143assertThrows(abfunc6);
144
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000145// Test that an array constructed without an array buffer creates one properly.
146a = new Uint8Array(31);
147assertEquals(a.byteLength, a.buffer.byteLength);
148assertEquals(a.length, a.buffer.byteLength);
149assertEquals(a.length * a.BYTES_PER_ELEMENT, a.buffer.byteLength);
150a = new Int16Array(5);
151assertEquals(a.byteLength, a.buffer.byteLength);
152assertEquals(a.length * a.BYTES_PER_ELEMENT, a.buffer.byteLength);
153a = new Float64Array(7);
154assertEquals(a.byteLength, a.buffer.byteLength);
155assertEquals(a.length * a.BYTES_PER_ELEMENT, a.buffer.byteLength);
156
157// Test that an implicitly created buffer is a valid buffer.
158a = new Float64Array(7);
159assertSame(a.buffer, (new Uint16Array(a.buffer)).buffer);
160assertSame(a.buffer, (new Float32Array(a.buffer,4)).buffer);
161assertSame(a.buffer, (new Int8Array(a.buffer,3,51)).buffer);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000162assertInstance(a.buffer, ArrayBuffer);
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000163
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000164// Test the correct behavior of the |BYTES_PER_ELEMENT| property (which is
165// "constant", but not read-only).
166a = new Int32Array(2);
167assertEquals(4, a.BYTES_PER_ELEMENT);
168a.BYTES_PER_ELEMENT = 42;
169assertEquals(42, a.BYTES_PER_ELEMENT);
170a = new Uint8Array(2);
171assertEquals(1, a.BYTES_PER_ELEMENT);
172a = new Int16Array(2);
173assertEquals(2, a.BYTES_PER_ELEMENT);
174
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000175// Test Float64Arrays.
176function get(a, index) {
177 return a[index];
178}
179function set(a, index, value) {
180 a[index] = value;
181}
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000182function temp() {
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000183var array = new Float64Array(2);
184for (var i = 0; i < 5; i++) {
185 set(array, 0, 2.5);
186 assertEquals(2.5, array[0]);
187}
188%OptimizeFunctionOnNextCall(set);
189set(array, 0, 2.5);
190assertEquals(2.5, array[0]);
191set(array, 1, 3.5);
192assertEquals(3.5, array[1]);
193for (var i = 0; i < 5; i++) {
194 assertEquals(2.5, get(array, 0));
195 assertEquals(3.5, array[1]);
196}
197%OptimizeFunctionOnNextCall(get);
198assertEquals(2.5, get(array, 0));
199assertEquals(3.5, get(array, 1));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000200}
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000201
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000202// Test non-number parameters.
203var array_with_length_from_non_number = new Int32Array("2");
204assertEquals(2, array_with_length_from_non_number.length);
205array_with_length_from_non_number = new Int32Array(undefined);
206assertEquals(0, array_with_length_from_non_number.length);
207var foo = { valueOf: function() { return 3; } };
208array_with_length_from_non_number = new Int32Array(foo);
209assertEquals(3, array_with_length_from_non_number.length);
210foo = { toString: function() { return "4"; } };
211array_with_length_from_non_number = new Int32Array(foo);
212assertEquals(4, array_with_length_from_non_number.length);
213
214
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000215// Test loads and stores.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000216types = [Array, Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array,
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000217 Uint32Array, Uint8ClampedArray, Float32Array, Float64Array];
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000218
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000219test_result_nan = [NaN, 0, 0, 0, 0, 0, 0, 0, NaN, NaN];
220test_result_low_int = [-1, -1, 255, -1, 65535, -1, 0xFFFFFFFF, 0, -1, -1];
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000221test_result_low_double = [-1.25, -1, 255, -1, 65535, -1, 0xFFFFFFFF, 0, -1.25, -1.25];
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000222test_result_middle = [253.75, -3, 253, 253, 253, 253, 253, 254, 253.75, 253.75];
223test_result_high_int = [256, 0, 0, 256, 256, 256, 256, 255, 256, 256];
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000224test_result_high_double = [256.25, 0, 0, 256, 256, 256, 256, 255, 256.25, 256.25];
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000225
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000226const kElementCount = 40;
227
228function test_load(array, sum) {
229 for (var i = 0; i < kElementCount; i++) {
230 sum += array[i];
231 }
232 return sum;
233}
234
235function test_load_const_key(array, sum) {
236 sum += array[0];
237 sum += array[1];
238 sum += array[2];
239 return sum;
240}
241
242function test_store(array, sum) {
243 for (var i = 0; i < kElementCount; i++) {
244 sum += array[i] = i+1;
245 }
246 return sum;
247}
248
249function test_store_const_key(array, sum) {
250 sum += array[0] = 1;
251 sum += array[1] = 2;
252 sum += array[2] = 3;
253 return sum;
254}
255
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000256function zero() {
257 return 0.0;
258}
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000259
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000260function test_store_middle_tagged(array, sum) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000261 array[0] = 253.75;
262 return array[0];
263}
264
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000265function test_store_high_tagged(array, sum) {
266 array[0] = 256.25;
267 return array[0];
268}
269
270function test_store_middle_double(array, sum) {
271 array[0] = 253.75 + zero(); // + forces double type feedback
272 return array[0];
273}
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000274
275function test_store_high_double(array, sum) {
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000276 array[0] = 256.25 + zero(); // + forces double type feedback
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000277 return array[0];
278}
279
280function test_store_high_double(array, sum) {
281 array[0] = 256.25;
282 return array[0];
283}
284
285function test_store_low_int(array, sum) {
286 array[0] = -1;
287 return array[0];
288}
289
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000290function test_store_low_tagged(array, sum) {
291 array[0] = -1.25;
292 return array[0];
293}
294
295function test_store_low_double(array, sum) {
296 array[0] = -1.25 + zero(); // + forces double type feedback
297 return array[0];
298}
299
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000300function test_store_high_int(array, sum) {
301 array[0] = 256;
302 return array[0];
303}
304
305function test_store_nan(array, sum) {
306 array[0] = NaN;
307 return array[0];
308}
309
310const kRuns = 10;
311
312function run_test(test_func, array, expected_result) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000313 for (var i = 0; i < 5; i++) test_func(array, 0);
314 %OptimizeFunctionOnNextCall(test_func);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000315 var sum = 0;
316 for (var i = 0; i < kRuns; i++) {
317 sum = test_func(array, sum);
318 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000319 assertEquals(expected_result, sum);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000320 %DeoptimizeFunction(test_func);
321 gc(); // Makes V8 forget about type information for test_func.
322}
323
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000324function run_bounds_test(test_func, array, expected_result) {
325 assertEquals(undefined, a[kElementCount]);
326 a[kElementCount] = 456;
327 assertEquals(undefined, a[kElementCount]);
328 assertEquals(undefined, a[kElementCount+1]);
329 a[kElementCount+1] = 456;
330 assertEquals(undefined, a[kElementCount+1]);
331}
332
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000333for (var t = 0; t < types.length; t++) {
334 var type = types[t];
335 var a = new type(kElementCount);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000336
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000337 for (var i = 0; i < kElementCount; i++) {
338 a[i] = i;
339 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000340
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000341 // Run test functions defined above.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000342 run_test(test_load, a, 780 * kRuns);
343 run_test(test_load_const_key, a, 3 * kRuns);
344 run_test(test_store, a, 820 * kRuns);
345 run_test(test_store_const_key, a, 6 * kRuns);
346 run_test(test_store_low_int, a, test_result_low_int[t]);
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000347 run_test(test_store_low_double, a, test_result_low_double[t]);
348 run_test(test_store_low_tagged, a, test_result_low_double[t]);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000349 run_test(test_store_high_int, a, test_result_high_int[t]);
350 run_test(test_store_nan, a, test_result_nan[t]);
351 run_test(test_store_middle_double, a, test_result_middle[t]);
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000352 run_test(test_store_middle_tagged, a, test_result_middle[t]);
353 run_test(test_store_high_double, a, test_result_high_double[t]);
354 run_test(test_store_high_tagged, a, test_result_high_double[t]);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000355
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000356 // Test the correct behavior of the |length| property (which is read-only).
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000357 if (t != 0) {
358 assertEquals(kElementCount, a.length);
359 a.length = 2;
360 assertEquals(kElementCount, a.length);
361 assertTrue(delete a.length);
362 a.length = 2;
363 assertEquals(2, a.length);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000364
365 // Make sure bounds checks are handled correctly for external arrays.
366 run_bounds_test(a);
367 run_bounds_test(a);
368 run_bounds_test(a);
369 %OptimizeFunctionOnNextCall(run_bounds_test);
370 run_bounds_test(a);
371 %DeoptimizeFunction(run_bounds_test);
372 gc(); // Makes V8 forget about type information for test_func.
373
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000374 }
ager@chromium.org560b07b2011-05-23 16:33:44 +0000375
376 function array_load_set_smi_check(a) {
377 return a[0] = a[0] = 1;
378 }
379
380 array_load_set_smi_check(a);
381 array_load_set_smi_check(0);
382
383 function array_load_set_smi_check2(a) {
384 return a[0] = a[0] = 1;
385 }
386
387 array_load_set_smi_check2(a);
388 %OptimizeFunctionOnNextCall(array_load_set_smi_check2);
389 array_load_set_smi_check2(a);
390 array_load_set_smi_check2(0);
391 %DeoptimizeFunction(array_load_set_smi_check2);
392 gc(); // Makes V8 forget about type information for array_load_set_smi_check.
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000393}
yangguo@chromium.org56454712012-02-16 15:33:53 +0000394
395// Check handling of undefined in 32- and 64-bit external float arrays.
396
397function store_float32_undefined(ext_array) {
398 ext_array[0] = undefined;
399}
400
401var float32_array = new Float32Array(1);
402// Make sure runtime does it right
403store_float32_undefined(float32_array);
404assertTrue(isNaN(float32_array[0]));
405// Make sure the ICs do it right
406store_float32_undefined(float32_array);
407assertTrue(isNaN(float32_array[0]));
408// Make sure that Cranskshft does it right.
409%OptimizeFunctionOnNextCall(store_float32_undefined);
410store_float32_undefined(float32_array);
411assertTrue(isNaN(float32_array[0]));
412
413function store_float64_undefined(ext_array) {
414 ext_array[0] = undefined;
415}
416
417var float64_array = new Float64Array(1);
418// Make sure runtime does it right
419store_float64_undefined(float64_array);
420assertTrue(isNaN(float64_array[0]));
421// Make sure the ICs do it right
422store_float64_undefined(float64_array);
423assertTrue(isNaN(float64_array[0]));
424// Make sure that Cranskshft does it right.
425%OptimizeFunctionOnNextCall(store_float64_undefined);
426store_float64_undefined(float64_array);
427assertTrue(isNaN(float64_array[0]));
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000428
429
430// Check handling of 0-sized buffers and arrays.
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000431ab = new ArrayBuffer(0);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000432assertInstance(ab, ArrayBuffer);
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000433assertEquals(0, ab.byteLength);
434a = new Int8Array(ab);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000435assertInstance(a, Int8Array);
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000436assertEquals(0, a.byteLength);
437assertEquals(0, a.length);
438a[0] = 1;
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000439assertEquals(undefined, a[0]);
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000440ab = new ArrayBuffer(16);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000441assertInstance(ab, ArrayBuffer);
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000442a = new Float32Array(ab,4,0);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000443assertInstance(a, Float32Array);
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000444assertEquals(0, a.byteLength);
445assertEquals(0, a.length);
446a[0] = 1;
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000447assertEquals(undefined, a[0]);
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000448a = new Uint16Array(0);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000449assertInstance(a, Uint16Array);
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000450assertEquals(0, a.byteLength);
451assertEquals(0, a.length);
452a[0] = 1;
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000453assertEquals(undefined, a[0]);
454
455
456// Check construction from arrays.
457a = new Uint32Array([]);
458assertInstance(a, Uint32Array);
459assertEquals(0, a.length);
460assertEquals(0, a.byteLength);
461assertEquals(0, a.buffer.byteLength);
462assertEquals(4, a.BYTES_PER_ELEMENT);
463assertInstance(a.buffer, ArrayBuffer);
464a = new Uint16Array([1,2,3]);
465assertInstance(a, Uint16Array);
466assertEquals(3, a.length);
467assertEquals(6, a.byteLength);
468assertEquals(6, a.buffer.byteLength);
469assertEquals(2, a.BYTES_PER_ELEMENT);
470assertEquals(1, a[0]);
471assertEquals(3, a[2]);
472assertInstance(a.buffer, ArrayBuffer);
473a = new Uint32Array(a);
474assertInstance(a, Uint32Array);
475assertEquals(3, a.length);
476assertEquals(12, a.byteLength);
477assertEquals(12, a.buffer.byteLength);
478assertEquals(4, a.BYTES_PER_ELEMENT);
479assertEquals(1, a[0]);
480assertEquals(3, a[2]);
481assertInstance(a.buffer, ArrayBuffer);
482
483// Check subarrays.
484a = new Uint16Array([1,2,3,4,5,6]);
485aa = a.subarray(3);
486assertInstance(aa, Uint16Array);
487assertEquals(3, aa.length);
488assertEquals(6, aa.byteLength);
489assertEquals(2, aa.BYTES_PER_ELEMENT);
490assertSame(a.buffer, aa.buffer);
491aa = a.subarray(3,5);
492assertInstance(aa, Uint16Array);
493assertEquals(2, aa.length);
494assertEquals(4, aa.byteLength);
495assertEquals(2, aa.BYTES_PER_ELEMENT);
496assertSame(a.buffer, aa.buffer);
497aa = a.subarray(4,8);
498assertInstance(aa, Uint16Array);
499assertEquals(2, aa.length);
500assertEquals(4, aa.byteLength);
501assertEquals(2, aa.BYTES_PER_ELEMENT);
502assertSame(a.buffer, aa.buffer);
503aa = a.subarray(9);
504assertInstance(aa, Uint16Array);
505assertEquals(0, aa.length);
506assertEquals(0, aa.byteLength);
507assertEquals(2, aa.BYTES_PER_ELEMENT);
508assertSame(a.buffer, aa.buffer);
509aa = a.subarray(-4);
510assertInstance(aa, Uint16Array);
511assertEquals(4, aa.length);
512assertEquals(8, aa.byteLength);
513assertEquals(2, aa.BYTES_PER_ELEMENT);
514assertSame(a.buffer, aa.buffer);
515aa = a.subarray(-3,-1);
516assertInstance(aa, Uint16Array);
517assertEquals(2, aa.length);
518assertEquals(4, aa.byteLength);
519assertEquals(2, aa.BYTES_PER_ELEMENT);
520assertSame(a.buffer, aa.buffer);
521aa = a.subarray(3,2);
522assertInstance(aa, Uint16Array);
523assertEquals(0, aa.length);
524assertEquals(0, aa.byteLength);
525assertEquals(2, aa.BYTES_PER_ELEMENT);
526assertSame(a.buffer, aa.buffer);
527aa = a.subarray(-3,-4);
528assertInstance(aa, Uint16Array);
529assertEquals(0, aa.length);
530assertEquals(0, aa.byteLength);
531assertEquals(2, aa.BYTES_PER_ELEMENT);
532assertSame(a.buffer, aa.buffer);
533aa = a.subarray(0,-8);
534assertInstance(aa, Uint16Array);
535assertEquals(0, aa.length);
536assertEquals(0, aa.byteLength);
537assertEquals(2, aa.BYTES_PER_ELEMENT);
538assertSame(a.buffer, aa.buffer);
539
540assertThrows(function(){ a.subarray.call({}, 0) });
541assertThrows(function(){ a.subarray.call([], 0) });
542assertThrows(function(){ a.subarray.call(a) });
mstarzinger@chromium.orgc6d9cee2012-07-03 10:03:19 +0000543
544
545// Call constructors directly as functions, and through .call and .apply
546
547b = ArrayBuffer(100)
548a = Int8Array(b, 5, 77)
549assertInstance(b, ArrayBuffer)
550assertInstance(a, Int8Array)
551assertSame(b, a.buffer)
552assertEquals(5, a.byteOffset)
553assertEquals(77, a.byteLength)
554b = ArrayBuffer.call(null, 10)
555a = Uint16Array.call(null, b, 2, 4)
556assertInstance(b, ArrayBuffer)
557assertInstance(a, Uint16Array)
558assertSame(b, a.buffer)
559assertEquals(2, a.byteOffset)
560assertEquals(8, a.byteLength)
561b = ArrayBuffer.apply(null, [1000])
562a = Float32Array.apply(null, [b, 128, 1])
563assertInstance(b, ArrayBuffer)
564assertInstance(a, Float32Array)
565assertSame(b, a.buffer)
566assertEquals(128, a.byteOffset)
567assertEquals(4, a.byteLength)
svenpanne@chromium.org619781a2012-07-05 08:22:44 +0000568
569
570// Test array.set in different combinations.
571
572function assertArrayPrefix(expected, array) {
573 for (var i = 0; i < expected.length; ++i) {
574 assertEquals(expected[i], array[i]);
575 }
576}
577
578var a11 = new Int16Array([1, 2, 3, 4, 0, -1])
579var a12 = new Uint16Array(15)
580a12.set(a11, 3)
581assertArrayPrefix([0, 0, 0, 1, 2, 3, 4, 0, 0xffff, 0, 0], a12)
582assertThrows(function(){ a11.set(a12) })
583
584var a21 = [1, undefined, 10, NaN, 0, -1, {valueOf: function() {return 3}}]
585var a22 = new Int32Array(12)
586a22.set(a21, 2)
587assertArrayPrefix([0, 0, 1, 0, 10, 0, 0, -1, 3, 0], a22)
588
589var a31 = new Float32Array([2, 4, 6, 8, 11, NaN, 1/0, -3])
590var a32 = a31.subarray(2, 6)
591a31.set(a32, 4)
592assertArrayPrefix([2, 4, 6, 8, 6, 8, 11, NaN], a31)
593assertArrayPrefix([6, 8, 6, 8], a32)
594
595var a4 = new Uint8ClampedArray([3,2,5,6])
596a4.set(a4)
597assertArrayPrefix([3, 2, 5, 6], a4)
598
599// Cases with overlapping backing store but different element sizes.
600var b = new ArrayBuffer(4)
601var a5 = new Int16Array(b)
602var a50 = new Int8Array(b)
603var a51 = new Int8Array(b, 0, 2)
604var a52 = new Int8Array(b, 1, 2)
605var a53 = new Int8Array(b, 2, 2)
606
607a5.set([0x5050, 0x0a0a])
608assertArrayPrefix([0x50, 0x50, 0x0a, 0x0a], a50)
609assertArrayPrefix([0x50, 0x50], a51)
610assertArrayPrefix([0x50, 0x0a], a52)
611assertArrayPrefix([0x0a, 0x0a], a53)
612
613a50.set([0x50, 0x50, 0x0a, 0x0a])
614a51.set(a5)
615assertArrayPrefix([0x50, 0x0a, 0x0a, 0x0a], a50)
616
617a50.set([0x50, 0x50, 0x0a, 0x0a])
618a52.set(a5)
619assertArrayPrefix([0x50, 0x50, 0x0a, 0x0a], a50)
620
621a50.set([0x50, 0x50, 0x0a, 0x0a])
622a53.set(a5)
623assertArrayPrefix([0x50, 0x50, 0x50, 0x0a], a50)
624
625a50.set([0x50, 0x51, 0x0a, 0x0b])
626a5.set(a51)
627assertArrayPrefix([0x0050, 0x0051], a5)
628
629a50.set([0x50, 0x51, 0x0a, 0x0b])
630a5.set(a52)
631assertArrayPrefix([0x0051, 0x000a], a5)
632
633a50.set([0x50, 0x51, 0x0a, 0x0b])
634a5.set(a53)
635assertArrayPrefix([0x000a, 0x000b], a5)
636
637// Mixed types of same size.
638var a61 = new Float32Array([1.2, 12.3])
639var a62 = new Int32Array(2)
640a62.set(a61)
641assertArrayPrefix([1, 12], a62)
642a61.set(a62)
643assertArrayPrefix([1, 12], a61)
644
645// Invalid source
646assertThrows(function() { a.set(0) })
647assertThrows(function() { a.set({}) })
648
649
650// Test arraybuffer.slice
651
652var a0 = new Int8Array([1, 2, 3, 4, 5, 6])
653var b0 = a0.buffer
654
655var b1 = b0.slice(0)
656assertEquals(b0.byteLength, b1.byteLength)
657assertArrayPrefix([1, 2, 3, 4, 5, 6], Int8Array(b1))
658
659var b2 = b0.slice(3)
660assertEquals(b0.byteLength - 3, b2.byteLength)
661assertArrayPrefix([4, 5, 6], Int8Array(b2))
662
663var b3 = b0.slice(2, 4)
664assertEquals(2, b3.byteLength)
665assertArrayPrefix([3, 4], Int8Array(b3))