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 // Flags: --harmony-arrays | |
6 | |
7 var typedArrayConstructors = [ | |
8 Uint8Array, | |
9 Int8Array, | |
10 Uint16Array, | |
11 Int16Array, | |
12 Uint32Array, | |
13 Int32Array, | |
14 Uint8ClampedArray, | |
15 Float32Array, | |
16 Float64Array]; | |
17 | |
18 for (var constructor of typedArrayConstructors) { | |
19 | |
20 assertEquals(1, constructor.prototype.findIndex.length); | |
21 | |
22 var a = new constructor([21, 22, 23, 24]); | |
23 assertEquals(-1, a.findIndex(function() { return false; })); | |
24 assertEquals(-1, a.findIndex(function(val) { return 121 === val; })); | |
25 assertEquals(0, a.findIndex(function() { return true; })); | |
26 assertEquals(1, a.findIndex(function(val) { return 22 === val; }), undefined); | |
27 assertEquals(2, a.findIndex(function(val) { return 23 === val; }), null); | |
28 assertEquals(3, a.findIndex(function(val) { return 24 === val; })); | |
29 | |
30 | |
31 // | |
32 // Test predicate is not called when array is empty | |
33 // | |
34 (function() { | |
35 var a = new constructor([]); | |
36 var l = -1; | |
37 var o = -1; | |
38 var v = -1; | |
39 var k = -1; | |
40 | |
41 a.findIndex(function(val, key, obj) { | |
42 o = obj; | |
43 l = obj.length; | |
44 v = val; | |
45 k = key; | |
46 | |
47 return false; | |
48 }); | |
49 | |
50 assertEquals(-1, l); | |
51 assertEquals(-1, o); | |
52 assertEquals(-1, v); | |
53 assertEquals(-1, k); | |
54 })(); | |
55 | |
56 | |
57 // | |
58 // Test predicate is called with correct argumetns | |
59 // | |
60 (function() { | |
61 var a = new constructor([5]); | |
62 var l = -1; | |
63 var o = -1; | |
64 var v = -1; | |
65 var k = -1; | |
66 | |
67 var index = a.findIndex(function(val, key, obj) { | |
68 o = obj; | |
69 l = obj.length; | |
70 v = val; | |
71 k = key; | |
72 | |
73 return false; | |
74 }); | |
75 | |
76 assertArrayEquals(a, o); | |
77 assertEquals(a.length, l); | |
78 assertEquals(5, v); | |
79 assertEquals(0, k); | |
80 assertEquals(-1, index); | |
81 })(); | |
82 | |
83 | |
84 // | |
85 // Test predicate is called array.length times | |
86 // | |
87 (function() { | |
88 var a = new constructor([1, 2, 3, 4, 5]); | |
89 var l = 0; | |
90 | |
91 a.findIndex(function() { | |
92 l++; | |
93 return false; | |
94 }); | |
95 | |
96 assertEquals(a.length, l); | |
97 })(); | |
98 | |
99 | |
100 // | |
101 // Test array modifications | |
102 // | |
103 (function() { | |
104 a = new constructor([1, 2, 3]); | |
105 a.findIndex(function(val, key) { a[key] = ++val; return false; }); | |
106 assertArrayEquals([2, 3, 4], a); | |
107 assertEquals(3, a.length); | |
108 })(); | |
109 | |
110 | |
111 // | |
112 // Test thisArg | |
113 // | |
114 (function() { | |
115 // Test String as a thisArg | |
116 var index = new constructor([1, 2, 3]).findIndex(function(val, key) { | |
117 return this.charAt(Number(key)) === String(val); | |
118 }, "321"); | |
119 assertEquals(1, index); | |
120 | |
121 // Test object as a thisArg | |
122 var thisArg = { | |
123 elementAt: function(key) { | |
124 return this[key]; | |
125 } | |
126 }; | |
127 Array.prototype.push.apply(thisArg, [3, 2, 1]); | |
128 | |
129 index = new constructor([1, 2, 3]).findIndex(function(val, key) { | |
130 return this.elementAt(key) === val; | |
131 }, thisArg); | |
132 assertEquals(1, index); | |
133 | |
134 // Create a new object in each function call when receiver is a | |
135 // primitive value. See ECMA-262, Annex C. | |
136 a = []; | |
137 new constructor([1, 2]).findIndex(function() { a.push(this) }, ""); | |
138 assertTrue(a[0] !== a[1]); | |
139 | |
140 // Do not create a new object otherwise. | |
141 a = []; | |
142 new constructor([1, 2]).findIndex(function() { a.push(this) }, {}); | |
143 assertEquals(a[0], a[1]); | |
144 | |
145 // In strict mode primitive values should not be coerced to an object. | |
146 a = []; | |
147 new constructor([1, 2]).findIndex(function() { 'use strict'; a.push(this); },
""); | |
148 assertEquals("", a[0]); | |
149 assertEquals(a[0], a[1]); | |
150 | |
151 })(); | |
152 | |
153 // Test exceptions | |
154 assertThrows('constructor.prototype.findIndex.call(null, function() { })', | |
155 TypeError); | |
156 assertThrows('constructor.prototype.findIndex.call(undefined, function() { })', | |
157 TypeError); | |
158 assertThrows('constructor.prototype.findIndex.apply(null, function() { }, [])', | |
159 TypeError); | |
160 assertThrows('constructor.prototype.findIndex.apply(undefined, function() { }, [
])', | |
161 TypeError); | |
162 assertThrows('constructor.prototype.findIndex.apply([], function() { }, [])', | |
163 TypeError); | |
164 assertThrows('constructor.prototype.findIndex.apply({}, function() { }, [])', | |
165 TypeError); | |
166 assertThrows('constructor.prototype.findIndex.apply("", function() { }, [])', | |
167 TypeError); | |
168 | |
169 assertThrows('new constructor([]).findIndex(null)', TypeError); | |
170 assertThrows('new constructor([]).findIndex(undefined)', TypeError); | |
171 assertThrows('new constructor([]).findIndex(0)', TypeError); | |
172 assertThrows('new constructor([]).findIndex(true)', TypeError); | |
173 assertThrows('new constructor([]).findIndex(false)', TypeError); | |
174 assertThrows('new constructor([]).findIndex("")', TypeError); | |
175 assertThrows('new constructor([]).findIndex({})', TypeError); | |
176 assertThrows('new constructor([]).findIndex([])', TypeError); | |
177 assertThrows('new constructor([]).findIndex(/\d+/)', TypeError); | |
178 | |
179 // Shadowing length doesn't affect findIndex, unlike Array.prototype.findIndex | |
180 a = new constructor([1, 2]); | |
181 Object.defineProperty(a, 'length', {value: 1}); | |
182 var x = 0; | |
183 assertEquals(a.findIndex(function(elt) { x += elt; return false; }), -1); | |
184 assertEquals(x, 3); | |
185 assertEquals(Array.prototype.findIndex.call(a, | |
186 function(elt) { x += elt; return false; }), -1); | |
187 assertEquals(x, 4); | |
188 | |
189 } | |
OLD | NEW |