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

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

Issue 1283703004: Add includes method to typed arrays (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 4 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 | « src/harmony-array-includes.js ('k') | no next file » | 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-array-includes
6
7 // Largely ported from
8 // https://github.com/tc39/Array.prototype.includes/tree/master/test/built-ins/T ypedArray/prototype/includes
9 // using https://www.npmjs.org/package/test262-to-mjsunit with further edits
10
11
12 function testTypedArrays(callback) {
13 [
14 Uint8Array,
15 Int8Array,
16 Uint16Array,
17 Int16Array,
18 Uint32Array,
19 Int32Array,
20 Uint8ClampedArray,
21 Float32Array,
22 Float64Array
23 ]
24 .forEach(callback);
25 }
26
27 testTypedArrays.floatOnly = function (callback) {
28 [Float32Array, Float64Array].forEach(callback);
29 };
30
31
32 // %TypedArray%.prototype.includes throws a TypeError when used on non-typed
33 // arrays
34 (function() {
35 var taIncludes = Uint8Array.prototype.includes;
36
37 assertThrows(function() {
38 taIncludes.call({
39 length: 2,
40 0: 1,
41 1: 2
42 }, 2);
43 }, TypeError);
44
45 assertThrows(function() {
46 taIncludes.call([1, 2, 3], 2);
47 }, TypeError);
48
49 assertThrows(function() {
50 taIncludes.call(null, 2);
51 }, TypeError);
52
53 assertThrows(function() {
54 taIncludes.call(undefined, 2);
55 }, TypeError);
56 })();
57
58
59 // %TypedArray%.prototype.includes should terminate if ToNumber ends up being
60 // called on a symbol fromIndex
61 (function() {
62 testTypedArrays(function(TypedArrayConstructor) {
63 var ta = new TypedArrayConstructor([1, 2, 3]);
64
65 assertThrows(function() {
66 ta.includes(2, Symbol());
67 }, TypeError);
68 });
69 })();
70
71
72 // %TypedArray%.prototype.includes should terminate if an exception occurs
73 // converting the fromIndex to a number
74 (function() {
75 function Test262Error() {}
76
77 var fromIndex = {
78 valueOf: function() {
79 throw new Test262Error();
80 }
81 };
82
83 testTypedArrays(function(TypedArrayConstructor) {
84 var ta = new TypedArrayConstructor([1, 2, 3]);
85
86 assertThrows(function() {
87 ta.includes(2, fromIndex);
88 }, Test262Error);
89 });
90 })();
91
92
93 // %TypedArray%.prototype.includes should search the whole array, as the
94 // optional second argument fromIndex defaults to 0
95 (function() {
96 testTypedArrays(function(TypedArrayConstructor) {
97 var ta = new TypedArrayConstructor([1, 2, 3]);
98 assertTrue(ta.includes(1));
99 assertTrue(ta.includes(2));
100 assertTrue(ta.includes(3));
101 });
102 })();
103
104
105 // %TypedArray%.prototype.includes returns false if fromIndex is greater or
106 // equal to the length of the array
107 (function() {
108 testTypedArrays(function(TypedArrayConstructor) {
109 var ta = new TypedArrayConstructor([1, 2]);
110 assertFalse(ta.includes(2, 3));
111 assertFalse(ta.includes(2, 2));
112 });
113 })();
114
115
116 // %TypedArray%.prototype.includes searches the whole array if the computed
117 // index from the given negative fromIndex argument is less than 0
118 (function() {
119 testTypedArrays(function(TypedArrayConstructor) {
120 var ta = new TypedArrayConstructor([1, 3]);
121 assertTrue(ta.includes(1, -4));
122 assertTrue(ta.includes(1, -4));
123 });
124 })();
125
126
127 // %TypedArray%.prototype.includes should use a negative value as the offset
128 // from the end of the array to compute fromIndex
129 (function() {
130 testTypedArrays(function(TypedArrayConstructor) {
131 var ta = new TypedArrayConstructor([12, 13]);
132 assertTrue(ta.includes(13, -1));
133 assertFalse(ta.includes(12, -1));
134 assertTrue(ta.includes(12, -2));
135 });
136 })();
137
138
139 // %TypedArray%.prototype.includes converts its fromIndex parameter to an
140 // integer
141 (function() {
142 testTypedArrays(function(TypedArrayConstructor) {
143 var ta = new TypedArrayConstructor([1, 2, 3]);
144 assertFalse(ta.includes(1, 3.3));
145 assertTrue(ta.includes(1, -Infinity));
146 assertTrue(ta.includes(3, 2.9));
147 assertTrue(ta.includes(3, NaN));
148
149 var numberLike = {
150 valueOf: function() {
151 return 2;
152 }
153 };
154
155 assertFalse(ta.includes(1, numberLike));
156 assertFalse(ta.includes(1, "2"));
157 assertTrue(ta.includes(3, numberLike));
158 assertTrue(ta.includes(3, "2"));
159 });
160 })();
161
162
163 // %TypedArray%.prototype.includes should have length 1
164 (function() {
165 assertEquals(1, Uint8Array.prototype.includes.length);
166 })();
167
168
169 // %TypedArray%.prototype.includes should have name property with value
170 // 'includes'
171 (function() {
172 assertEquals("includes", Uint8Array.prototype.includes.name);
173 })();
174
175
176 // %TypedArray%.prototype.includes should always return false on zero-length
177 // typed arrays
178 (function() {
179 testTypedArrays(function(TypedArrayConstructor) {
180 var ta = new TypedArrayConstructor([]);
181 assertFalse(ta.includes(2));
182 assertFalse(ta.includes());
183 assertFalse(ta.includes(undefined));
184 assertFalse(ta.includes(NaN));
185 });
186 })();
187
188
189 // %TypedArray%.prototype.includes should use the SameValueZero algorithm to
190 // compare
191 (function() {
192 testTypedArrays.floatOnly(function(FloatArrayConstructor) {
193 assertTrue(new FloatArrayConstructor([1, 2, NaN]).includes(NaN));
194 assertTrue(new FloatArrayConstructor([1, 2, -0]).includes(+0));
195 assertTrue(new FloatArrayConstructor([1, 2, -0]).includes(-0));
196 assertTrue(new FloatArrayConstructor([1, 2, +0]).includes(-0));
197 assertTrue(new FloatArrayConstructor([1, 2, +0]).includes(+0));
198 assertFalse(new FloatArrayConstructor([1, 2, -Infinity]).includes(+Infinity) );
199 assertTrue(new FloatArrayConstructor([1, 2, -Infinity]).includes(-Infinity)) ;
200 assertFalse(new FloatArrayConstructor([1, 2, +Infinity]).includes(-Infinity) );
201 assertTrue(new FloatArrayConstructor([1, 2, +Infinity]).includes(+Infinity)) ;
202 });
203 })();
OLDNEW
« no previous file with comments | « src/harmony-array-includes.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698