Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1 | // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Based on Mozilla Array.of() tests at http://dxr.mozilla.org/mozilla-central/source/js/src/jit-test/tests/collections |
| 6 | |
| 7 | // Flags: --harmony-arrays |
| 8 | |
| 9 | var typedArrayConstructors = [ |
| 10 | Uint8Array, |
| 11 | Int8Array, |
| 12 | Uint16Array, |
| 13 | Int16Array, |
| 14 | Uint32Array, |
| 15 | Int32Array, |
| 16 | Uint8ClampedArray, |
| 17 | Float32Array, |
| 18 | Float64Array]; |
| 19 | |
| 20 | |
| 21 | function TestTypedArrayOf(constructor) { |
| 22 | // %TypedArray%.of basics. |
| 23 | var a = constructor.of(); |
| 24 | assertEquals(0, a.length); |
| 25 | assertEquals(constructor.prototype, Object.getPrototypeOf(a)); |
| 26 | assertEquals(false, Array.isArray(a)); |
| 27 | |
| 28 | // Items are coerced to numerical values. |
| 29 | a = constructor.of(undefined, null, [], true, false, 3.14); |
| 30 | |
| 31 | // For typed arrays of floating point values, values are not rounded. |
| 32 | if (constructor === Float32Array || constructor === Float64Array) { |
| 33 | assertEquals(NaN, a[0]); |
| 34 | assertEquals(0, a[1]); |
| 35 | assertEquals(0, a[2]); |
| 36 | assertEquals(1, a[3]); |
| 37 | assertEquals(0, a[4]); |
| 38 | assertEquals(true, Math.abs(a[5] - 3.14) < 1e-6); |
| 39 | } else { |
| 40 | assertEquals(0, a[0]); |
| 41 | assertEquals(0, a[1]); |
| 42 | assertEquals(0, a[2]); |
| 43 | assertEquals(1, a[3]); |
| 44 | assertEquals(0, a[4]); |
| 45 | assertEquals(3, a[5]); |
| 46 | } |
| 47 | |
| 48 | var aux = []; |
| 49 | for (var i = 0; i < 100; i++) |
| 50 | aux[i] = i; |
| 51 | |
| 52 | a = constructor.of.apply(constructor, aux); |
| 53 | assertEquals(aux.length, a.length); |
| 54 | assertArrayEquals(aux, a); |
| 55 | |
| 56 | // %TypedArray%.of can be transplanted to other constructors. |
| 57 | var hits = 0; |
| 58 | function Bag(length) { |
| 59 | assertEquals(arguments.length, 1); |
| 60 | assertEquals(length, 2); |
| 61 | this.length = length; |
| 62 | hits++; |
| 63 | } |
| 64 | Bag.of = constructor.of; |
| 65 | |
| 66 | hits = 0; |
| 67 | a = Bag.of("zero", "one"); |
| 68 | assertEquals(1, hits); |
| 69 | assertEquals(2, a.length); |
| 70 | assertArrayEquals(["zero", "one"], a); |
| 71 | assertEquals(Bag.prototype, a.__proto__); |
| 72 | |
| 73 | hits = 0; |
| 74 | actual = constructor.of.call(Bag, "zero", "one"); |
| 75 | assertEquals(1, hits); |
| 76 | assertEquals(2, a.length); |
| 77 | assertArrayEquals(["zero", "one"], a); |
| 78 | assertEquals(Bag.prototype, a.__proto__); |
| 79 | |
| 80 | // %TypedArray%.of does not trigger prototype setters. |
| 81 | // (It defines elements rather than assigning to them.) |
| 82 | var status = "pass"; |
| 83 | Object.defineProperty(constructor.prototype, "0", { |
| 84 | set: function(v) { status = "fail"; } |
| 85 | }); |
| 86 | assertEquals(1, constructor.of(1)[0], 1); |
| 87 | assertEquals("pass", status); |
| 88 | |
| 89 | // Note that %TypedArray%.of does not trigger "length" setter itself, as |
| 90 | // it relies on the constructor to set "length" to the value passed to it. |
| 91 | // If the constructor does not assign "length", the setter should not be |
| 92 | // invoked. |
| 93 | |
| 94 | // Setter on the newly created object. |
| 95 | function Pack() { |
| 96 | Object.defineProperty(this, "length", { |
| 97 | set: function (v) { status = "fail"; } |
| 98 | }); |
| 99 | } |
| 100 | Pack.of = constructor.of; |
| 101 | var pack = Pack.of("wolves", "cards", "cigarettes", "lies"); |
| 102 | assertEquals("pass", status); |
| 103 | |
| 104 | // when the setter is on the new object's prototype |
| 105 | function Bevy() {} |
| 106 | Object.defineProperty(Bevy.prototype, "length", { |
| 107 | set: function (v) { status = "fail"; } |
| 108 | }); |
| 109 | Bevy.of = constructor.of; |
| 110 | var bevy = Bevy.of("quail"); |
| 111 | assertEquals("pass", status); |
| 112 | |
| 113 | // Check superficial features of %TypedArray%.of. |
| 114 | var desc = Object.getOwnPropertyDescriptor(constructor, "of"); |
| 115 | |
| 116 | assertEquals(desc.configurable, false); |
| 117 | assertEquals(desc.enumerable, false); |
| 118 | assertEquals(desc.writable, false); |
| 119 | assertEquals(constructor.of.length, 0); |
| 120 | |
| 121 | // %TypedArray%.of is not a constructor. |
| 122 | assertThrows(function() { new constructor.of(); }, TypeError); |
| 123 | |
| 124 | // For receivers which are not constructors %TypedArray%.of does not |
| 125 | // allocate a typed array using a default constructor, but throws an |
| 126 | // exception. Note that this is different from Array.of, which uses |
| 127 | // Array as default constructor. |
| 128 | for (var x of [undefined, null, false, true, "cow", 42, 3.14]) { |
| 129 | assertThrows(function () { constructor.of.call(x); }, TypeError); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | for (var constructor of typedArrayConstructors) { |
| 134 | TestTypedArrayOf(constructor); |
| 135 | } |