OLD | NEW |
---|---|
(Empty) | |
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/s ource/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 | |
23 // %TypedArray%.of basics. | |
24 assertEquals(constructor.of.length, 0); | |
wingo
2014/10/21 15:23:13
remove this check, it's done below
| |
25 | |
26 var a = constructor.of(); | |
27 assertEquals(0, a.length); | |
28 assertEquals(constructor.prototype, Object.getPrototypeOf(a)); | |
29 assertEquals(false, Array.isArray(a)); | |
30 | |
31 // Items are coerced to numerical values. | |
32 a = constructor.of(undefined, null, [], true, false, 3.14); | |
33 assertEquals(true, !!a[3]); | |
wingo
2014/10/21 15:23:13
go ahead and inline these into the branches of the
| |
34 assertEquals(false, !!a[4]); | |
35 | |
36 // For typed arrays of floating point values, values are not rounded. | |
37 if (constructor === Float32Array || constructor === Float64Array) { | |
38 assertEquals(NaN, a[0]); | |
39 assertEquals(0, a[1]); | |
40 assertEquals(0, a[2]); | |
41 assertEquals(true, (a[5] - 3.14) < 1e-6); | |
42 } else { | |
43 assertEquals(0, a[0]); | |
44 assertEquals(0, a[1]); | |
45 assertEquals(0, a[2]); | |
46 assertEquals(3, a[5]); | |
47 } | |
48 | |
49 var aux = []; | |
50 for (var i = 0; i < 100; i++) | |
51 aux[i] = i; | |
52 | |
53 a = constructor.of.apply(constructor, aux); | |
54 assertEquals(aux.length, a.length); | |
55 assertArrayEquals(aux, a); | |
56 | |
57 // %TypedArray%.of can be transplanted to other constructors. | |
58 | |
59 var hits = 0; | |
60 function Bag(length) { | |
61 assertEquals(arguments.length, 1); | |
62 assertEquals(length, 2); | |
63 this.length = length; | |
64 hits++; | |
65 } | |
66 Bag.of = constructor.of; | |
67 | |
68 hits = 0; | |
69 a = Bag.of("zero", "one"); | |
70 assertEquals(1, hits); | |
71 assertEquals(2, a.length); | |
72 assertArrayEquals(["zero", "one"], a); | |
73 assertEquals(Bag.prototype, a.__proto__); | |
74 | |
75 hits = 0; | |
76 actual = constructor.of.call(Bag, "zero", "one"); | |
77 assertEquals(1, hits); | |
78 assertEquals(2, a.length); | |
79 assertArrayEquals(["zero", "one"], a); | |
80 assertEquals(Bag.prototype, a.__proto__); | |
81 | |
82 // %TypedArray%.of does not trigger prototype setters. | |
83 // (It defines elements rather than assigning to them.) | |
84 | |
85 var status = "pass"; | |
86 Object.defineProperty(constructor.prototype, "0", {set: function(v) {status = "FAIL 1"}}); | |
wingo
2014/10/21 15:23:13
80 columns
| |
87 assertEquals(1, constructor.of(1)[0], 1); | |
wingo
2014/10/21 15:23:13
does this test what you mean to test? not sure, a
| |
88 assertEquals("pass", status); | |
89 | |
90 // Array.of calls a "length" setter if one is present. | |
wingo
2014/10/21 15:23:13
This doesn't appear in the spec AFAICS.
| |
91 | |
92 var hits = 0; | |
93 var lastObj = null, lastVal = undefined; | |
94 function setter(v) { | |
95 hits++; | |
96 lastObj = this; | |
97 lastVal = v; | |
98 } | |
99 | |
100 // when the setter is on the new object | |
101 function Pack() { | |
102 Object.defineProperty(this, "length", {set: setter}); | |
103 } | |
104 Pack.of = constructor.of; | |
105 var pack = Pack.of("wolves", "cards", "cigarettes", "lies"); | |
106 assertEquals(lastObj, pack); | |
107 assertEquals(lastVal, 4); | |
108 | |
109 // when the setter is on the new object's prototype | |
110 function Bevy() {} | |
111 Object.defineProperty(Bevy.prototype, "length", {set: setter}); | |
112 Bevy.of = constructor.of; | |
113 var bevy = Bevy.of("quail"); | |
114 assertEquals(lastObj, bevy); | |
115 assertEquals(lastVal, 1); | |
116 | |
117 | |
118 // Array.of does a strict assignment to the new object's .length. | |
119 // The assignment is strict even if the code we're calling from is not strict. | |
120 | |
121 function Empty() {} | |
122 Empty.of = constructor.of; | |
123 Object.defineProperty(Empty.prototype, "length", {get: function() { return 0; }}); | |
wingo
2014/10/21 15:23:13
80
| |
124 | |
125 var nothing = new Empty; | |
126 nothing.length = 2; // no exception; this is not a strict mode assignment | |
127 | |
128 assertThrows(function() { Empty.of(); }, TypeError); | |
129 | |
130 | |
131 // Check superficial features of Array.of. | |
132 | |
133 var desc = Object.getOwnPropertyDescriptor(constructor, "of"); | |
134 | |
135 assertEquals(desc.configurable, true); | |
wingo
2014/10/21 15:23:13
should be false i think
| |
136 assertEquals(desc.enumerable, false); | |
137 assertEquals(desc.writable, true); | |
wingo
2014/10/21 15:23:13
should be false
| |
138 assertEquals(constructor.of.length, 0); | |
139 assertThrows(function() { new constructor.of() }, TypeError); // not a constr uctor | |
wingo
2014/10/21 15:23:13
80
| |
140 | |
141 // When the this-value passed in is not a constructor, the result is an array. | |
142 [undefined, null, false, "cow"].forEach(function(val) { | |
wingo
2014/10/21 15:23:13
cleaner to do
for (var x of [undefined, null, fal
| |
143 assertEquals(true, constructor.of(val) instanceof constructor); | |
wingo
2014/10/21 15:23:13
this doesn't appear to test what you intend to tes
| |
144 }); | |
145 } | |
146 | |
147 for (var i = 0; i < typedArrayConstructors.length; i++) { | |
wingo
2014/10/21 15:23:13
for (var constructor of typedArrayConstructors) {
| |
148 TestTypedArrayOf(typedArrayConstructors[i]); | |
149 } | |
OLD | NEW |