Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1056)

Side by Side Diff: test/mjsunit/harmony/array-findindex.js

Issue 1403633007: Remove stale references to --harmony-arrays flag in mjsunit tests (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/mjsunit/harmony/array-find.js ('k') | test/mjsunit/harmony/array-from.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // 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
28 // Flags: --harmony-arrays
29
30 assertEquals(1, Array.prototype.findIndex.length);
31
32 var a = [21, 22, 23, 24];
33 assertEquals(-1, a.findIndex(function() { return false; }));
34 assertEquals(-1, a.findIndex(function(val) { return 121 === val; }));
35 assertEquals(0, a.findIndex(function() { return true; }));
36 assertEquals(1, a.findIndex(function(val) { return 22 === val; }), undefined);
37 assertEquals(2, a.findIndex(function(val) { return 23 === val; }), null);
38 assertEquals(3, a.findIndex(function(val) { return 24 === val; }));
39
40
41 //
42 // Test predicate is not called when array is empty
43 //
44 (function() {
45 var a = [];
46 var l = -1;
47 var o = -1;
48 var v = -1;
49 var k = -1;
50
51 a.findIndex(function(val, key, obj) {
52 o = obj;
53 l = obj.length;
54 v = val;
55 k = key;
56
57 return false;
58 });
59
60 assertEquals(-1, l);
61 assertEquals(-1, o);
62 assertEquals(-1, v);
63 assertEquals(-1, k);
64 })();
65
66
67 //
68 // Test predicate is called with correct argumetns
69 //
70 (function() {
71 var a = ["b"];
72 var l = -1;
73 var o = -1;
74 var v = -1;
75 var k = -1;
76
77 var index = a.findIndex(function(val, key, obj) {
78 o = obj;
79 l = obj.length;
80 v = val;
81 k = key;
82
83 return false;
84 });
85
86 assertArrayEquals(a, o);
87 assertEquals(a.length, l);
88 assertEquals("b", v);
89 assertEquals(0, k);
90 assertEquals(-1, index);
91 })();
92
93
94 //
95 // Test predicate is called array.length times
96 //
97 (function() {
98 var a = [1, 2, 3, 4, 5];
99 var l = 0;
100
101 a.findIndex(function() {
102 l++;
103 return false;
104 });
105
106 assertEquals(a.length, l);
107 })();
108
109
110 //
111 // Test Array.prototype.findIndex works with String
112 //
113 (function() {
114 var a = "abcd";
115 var l = -1;
116 var o = -1;
117 var v = -1;
118 var k = -1;
119
120 var index = Array.prototype.findIndex.call(a, function(val, key, obj) {
121 o = obj.toString();
122 l = obj.length;
123 v = val;
124 k = key;
125
126 return false;
127 });
128
129 assertEquals(a, o);
130 assertEquals(a.length, l);
131 assertEquals("d", v);
132 assertEquals(3, k);
133 assertEquals(-1, index);
134
135 index = Array.prototype.findIndex.apply(a, [function(val, key, obj) {
136 o = obj.toString();
137 l = obj.length;
138 v = val;
139 k = key;
140
141 return true;
142 }]);
143
144 assertEquals(a, o);
145 assertEquals(a.length, l);
146 assertEquals("a", v);
147 assertEquals(0, k);
148 assertEquals(0, index);
149 })();
150
151
152 //
153 // Test Array.prototype.findIndex works with exotic object
154 //
155 (function() {
156 var l = -1;
157 var o = -1;
158 var v = -1;
159 var k = -1;
160 var a = {
161 prop1: "val1",
162 prop2: "val2",
163 isValid: function() {
164 return this.prop1 === "val1" && this.prop2 === "val2";
165 }
166 };
167
168 Array.prototype.push.apply(a, [30, 31, 32]);
169
170 var index = Array.prototype.findIndex.call(a, function(val, key, obj) {
171 o = obj;
172 l = obj.length;
173 v = val;
174 k = key;
175
176 return !obj.isValid();
177 });
178
179 assertArrayEquals(a, o);
180 assertEquals(3, l);
181 assertEquals(32, v);
182 assertEquals(2, k);
183 assertEquals(-1, index);
184 })();
185
186
187 //
188 // Test array modifications
189 //
190 (function() {
191 var a = [1, 2, 3];
192 a.findIndex(function(val) { a.push(val); return false; });
193 assertArrayEquals([1, 2, 3, 1, 2, 3], a);
194 assertEquals(6, a.length);
195
196 a = [1, 2, 3];
197 a.findIndex(function(val, key) { a[key] = ++val; return false; });
198 assertArrayEquals([2, 3, 4], a);
199 assertEquals(3, a.length);
200 })();
201
202
203 //
204 // Test predicate is called for holes
205 //
206 (function() {
207 var a = new Array(30);
208 a[11] = 21;
209 a[7] = 10;
210 a[29] = 31;
211
212 var count = 0;
213 a.findIndex(function() { count++; return false; });
214 assertEquals(30, count);
215 })();
216
217
218 (function() {
219 var a = [0, 1, , 3];
220 var count = 0;
221 var index = a.findIndex(function(val) { return val === undefined; });
222 assertEquals(2, index);
223 })();
224
225
226 (function() {
227 var a = [0, 1, , 3];
228 a.__proto__ = {
229 __proto__: Array.prototype,
230 2: 42,
231 };
232 var count = 0;
233 var index = a.findIndex(function(val) { return val === 42; });
234 assertEquals(2, index);
235 })();
236
237
238 //
239 // Test thisArg
240 //
241 (function() {
242 // Test String as a thisArg
243 var index = [1, 2, 3].findIndex(function(val, key) {
244 return this.charAt(Number(key)) === String(val);
245 }, "321");
246 assertEquals(1, index);
247
248 // Test object as a thisArg
249 var thisArg = {
250 elementAt: function(key) {
251 return this[key];
252 }
253 };
254 Array.prototype.push.apply(thisArg, ["c", "b", "a"]);
255
256 index = ["a", "b", "c"].findIndex(function(val, key) {
257 return this.elementAt(key) === val;
258 }, thisArg);
259 assertEquals(1, index);
260
261 // Create a new object in each function call when receiver is a
262 // primitive value. See ECMA-262, Annex C.
263 a = [];
264 [1, 2].findIndex(function() { a.push(this) }, "");
265 assertTrue(a[0] !== a[1]);
266
267 // Do not create a new object otherwise.
268 a = [];
269 [1, 2].findIndex(function() { a.push(this) }, {});
270 assertEquals(a[0], a[1]);
271
272 // In strict mode primitive values should not be coerced to an object.
273 a = [];
274 [1, 2].findIndex(function() { 'use strict'; a.push(this); }, "");
275 assertEquals("", a[0]);
276 assertEquals(a[0], a[1]);
277
278 })();
279
280 // Test exceptions
281 assertThrows('Array.prototype.findIndex.call(null, function() { })',
282 TypeError);
283 assertThrows('Array.prototype.findIndex.call(undefined, function() { })',
284 TypeError);
285 assertThrows('Array.prototype.findIndex.apply(null, function() { }, [])',
286 TypeError);
287 assertThrows('Array.prototype.findIndex.apply(undefined, function() { }, [])',
288 TypeError);
289
290 assertThrows('[].findIndex(null)', TypeError);
291 assertThrows('[].findIndex(undefined)', TypeError);
292 assertThrows('[].findIndex(0)', TypeError);
293 assertThrows('[].findIndex(true)', TypeError);
294 assertThrows('[].findIndex(false)', TypeError);
295 assertThrows('[].findIndex("")', TypeError);
296 assertThrows('[].findIndex({})', TypeError);
297 assertThrows('[].findIndex([])', TypeError);
298 assertThrows('[].findIndex(/\d+/)', TypeError);
299
300 assertThrows('Array.prototype.findIndex.call({}, null)', TypeError);
301 assertThrows('Array.prototype.findIndex.call({}, undefined)', TypeError);
302 assertThrows('Array.prototype.findIndex.call({}, 0)', TypeError);
303 assertThrows('Array.prototype.findIndex.call({}, true)', TypeError);
304 assertThrows('Array.prototype.findIndex.call({}, false)', TypeError);
305 assertThrows('Array.prototype.findIndex.call({}, "")', TypeError);
306 assertThrows('Array.prototype.findIndex.call({}, {})', TypeError);
307 assertThrows('Array.prototype.findIndex.call({}, [])', TypeError);
308 assertThrows('Array.prototype.findIndex.call({}, /\d+/)', TypeError);
309
310 assertThrows('Array.prototype.findIndex.apply({}, null, [])', TypeError);
311 assertThrows('Array.prototype.findIndex.apply({}, undefined, [])', TypeError);
312 assertThrows('Array.prototype.findIndex.apply({}, 0, [])', TypeError);
313 assertThrows('Array.prototype.findIndex.apply({}, true, [])', TypeError);
314 assertThrows('Array.prototype.findIndex.apply({}, false, [])', TypeError);
315 assertThrows('Array.prototype.findIndex.apply({}, "", [])', TypeError);
316 assertThrows('Array.prototype.findIndex.apply({}, {}, [])', TypeError);
317 assertThrows('Array.prototype.findIndex.apply({}, [], [])', TypeError);
318 assertThrows('Array.prototype.findIndex.apply({}, /\d+/, [])', TypeError);
OLDNEW
« no previous file with comments | « test/mjsunit/harmony/array-find.js ('k') | test/mjsunit/harmony/array-from.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698