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

Side by Side Diff: test/mjsunit/harmony/typedarray-find.js

Issue 1215863003: Include Harmony Array/TypedArray methods unconditionally (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: remove the flag and move the tests Created 5 years, 5 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/typedarray-fill.js ('k') | test/mjsunit/harmony/typedarray-findindex.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 2015 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.find.length);
21
22 var a = new constructor([21, 22, 23, 24]);
23 assertEquals(undefined, a.find(function() { return false; }));
24 assertEquals(21, a.find(function() { return true; }));
25 assertEquals(undefined, a.find(function(val) { return 121 === val; }));
26 assertEquals(24, a.find(function(val) { return 24 === val; }));
27 assertEquals(23, a.find(function(val) { return 23 === val; }), null);
28 assertEquals(22, a.find(function(val) { return 22 === val; }), undefined);
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.find(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 arguments
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 found = a.find(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(undefined, found);
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 var found = a.find(function() {
91 l++;
92 return false;
93 });
94
95 assertEquals(a.length, l);
96 assertEquals(undefined, found);
97 })();
98
99
100 //
101 // Test array modifications
102 //
103 (function() {
104 a = new constructor([1, 2, 3]);
105 found = a.find(function(val, key) { a[key] = ++val; return false; });
106 assertArrayEquals([2, 3, 4], a);
107 assertEquals(3, a.length);
108 assertEquals(undefined, found);
109 })();
110
111 //
112 // Test thisArg
113 //
114 (function() {
115 // Test String as a thisArg
116 var found = new constructor([1, 2, 3]).find(function(val, key) {
117 return this.charAt(Number(key)) === String(val);
118 }, "321");
119 assertEquals(2, found);
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 found = new constructor([1, 2, 3]).find(function(val, key) {
130 return this.elementAt(key) === val;
131 }, thisArg);
132 assertEquals(2, found);
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]).find(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]).find(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]).find(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.find.call(null, function() { })',
155 TypeError);
156 assertThrows('constructor.prototype.find.call(undefined, function() { })',
157 TypeError);
158 assertThrows('constructor.prototype.find.apply(null, function() { }, [])',
159 TypeError);
160 assertThrows('constructor.prototype.find.apply(undefined, function() { }, [])',
161 TypeError);
162 assertThrows('constructor.prototype.find.apply([], function() { }, [])',
163 TypeError);
164 assertThrows('constructor.prototype.find.apply({}, function() { }, [])',
165 TypeError);
166 assertThrows('constructor.prototype.find.apply("", function() { }, [])',
167 TypeError);
168
169 assertThrows('new constructor([]).find(null)', TypeError);
170 assertThrows('new constructor([]).find(undefined)', TypeError);
171 assertThrows('new constructor([]).find(0)', TypeError);
172 assertThrows('new constructor([]).find(true)', TypeError);
173 assertThrows('new constructor([]).find(false)', TypeError);
174 assertThrows('new constructor([]).find("")', TypeError);
175 assertThrows('new constructor([]).find({})', TypeError);
176 assertThrows('new constructor([]).find([])', TypeError);
177 assertThrows('new constructor([]).find(/\d+/)', TypeError);
178
179 // Shadowing length doesn't affect find, unlike Array.prototype.find
180 a = new constructor([1, 2]);
181 Object.defineProperty(a, 'length', {value: 1});
182 var x = 0;
183 assertEquals(a.find(function(elt) { x += elt; return false; }), undefined);
184 assertEquals(x, 3);
185 assertEquals(Array.prototype.find.call(a,
186 function(elt) { x += elt; return false; }), undefined);
187 assertEquals(x, 4);
188
189 }
OLDNEW
« no previous file with comments | « test/mjsunit/harmony/typedarray-fill.js ('k') | test/mjsunit/harmony/typedarray-findindex.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698