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

Side by Side Diff: test/mjsunit/harmony/typedarrays-every.js

Issue 737483003: Implement .every() on typed arrays Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 6 years, 1 month 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-typedarray.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
1 // Copyright 2014 the V8 project authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Flags: --harmony-arrays --allow-natives-syntax 5 // Flags: --harmony-arrays --allow-natives-syntax
6 6
7 // The tests here are essentially the same as the ones for
8 // %TypedArray%.prototype.forEach, modified to take into account
9 // that callbacks must return a boolean value, plus an extra test
10 // to check whether returning "false" stops iteration.
11
7 var typedArrayConstructors = [ 12 var typedArrayConstructors = [
8 Uint8Array, 13 Uint8Array,
9 Int8Array, 14 Int8Array,
10 Uint16Array, 15 Uint16Array,
11 Int16Array, 16 Int16Array,
12 Uint32Array, 17 Uint32Array,
13 Int32Array, 18 Int32Array,
14 Uint8ClampedArray, 19 Uint8ClampedArray,
15 Float32Array, 20 Float32Array,
16 Float64Array]; 21 Float64Array];
17 22
18 function CheckTypedArrayIsNeutered(array) { 23 function CheckTypedArrayIsNeutered(array) {
19 assertEquals(0, array.byteLength); 24 assertEquals(0, array.byteLength);
20 assertEquals(0, array.byteOffset); 25 assertEquals(0, array.byteOffset);
21 assertEquals(0, array.length); 26 assertEquals(0, array.length);
22 } 27 }
23 28
24 function TestTypedArrayForEach(constructor) { 29 function TestTypedArrayEvery(constructor) {
25 assertEquals(1, constructor.prototype.forEach.length); 30 assertEquals(1, constructor.prototype.every.length);
26 31
27 var a = new constructor(2); 32 var a = new constructor(2);
28 a[0] = 0; 33 a[0] = 0;
29 a[1] = 1; 34 a[1] = 1;
30 35
31 var count = 0; 36 var count = 0;
32 a.forEach(function (n) { count++; }); 37 a.every(function (n) { count++; return true; });
33 assertEquals(2, count); 38 assertEquals(2, count);
34 39
35 // Use specified object as this object when calling the function. 40 // Use specified object as this object when calling the function.
36 var o = { value: 42 }; 41 var o = { value: 42 };
37 var result = []; 42 var result = [];
38 a.forEach(function (n, index, array) { result.push(this.value); }, o); 43 a.every(function (n, index, array) { result.push(this.value); return true; }, o);
39 assertArrayEquals([42, 42], result); 44 assertArrayEquals([42, 42], result);
40 45
41 // Modify the original array. 46 // Modify the original array.
42 count = 0; 47 count = 0;
43 a.forEach(function (n, index, array) { array[index] = n + 1; count++ }); 48 a.every(function (n, index, array) { array[index] = n + 1; count++; return tru e; });
44 assertEquals(2, count); 49 assertEquals(2, count);
45 assertArrayEquals([1, 2], a); 50 assertArrayEquals([1, 2], a);
46 51
52 // Returning "false" stops the iteration.
53 count = 0;
54 a.every(function (n, index, array) { array[index] = 42; count++; return false; });
55 assertEquals(1, count);
56 assertArrayEquals([42, 2], a);
57
47 // Check that values passed as second argument are wrapped into 58 // Check that values passed as second argument are wrapped into
48 // objects when calling into sloppy mode functions. 59 // objects when calling into sloppy mode functions.
49 function CheckWrapping(value, wrapper) { 60 function CheckWrapping(value, wrapper) {
50 var wrappedValue = new wrapper(value); 61 var wrappedValue = new wrapper(value);
51 62
52 a.forEach(function () { 63 a.every(function () {
53 assertEquals("object", typeof this); 64 assertEquals("object", typeof this);
54 assertEquals(wrappedValue, this); 65 assertEquals(wrappedValue, this);
66 return true;
55 }, value); 67 }, value);
56 68
57 a.forEach(function () { 69 a.every(function () {
58 "use strict"; 70 "use strict";
59 assertEquals(typeof value, typeof this); 71 assertEquals(typeof value, typeof this);
60 assertEquals(value, this); 72 assertEquals(value, this);
73 return true;
61 }, value); 74 }, value);
62 } 75 }
63 CheckWrapping(true, Boolean); 76 CheckWrapping(true, Boolean);
64 CheckWrapping(false, Boolean); 77 CheckWrapping(false, Boolean);
65 CheckWrapping("xxx", String); 78 CheckWrapping("xxx", String);
66 CheckWrapping(42, Number); 79 CheckWrapping(42, Number);
67 CheckWrapping(3.14, Number); 80 CheckWrapping(3.14, Number);
68 CheckWrapping({}, Object); 81 CheckWrapping({}, Object);
69 82
70 // Throw before completing iteration, only the first element 83 // Throw before completing iteration, only the first element
71 // should be modified when thorwing mid-way. 84 // should be modified when thorwing mid-way.
72 count = 0; 85 count = 0;
73 a[0] = 42; 86 a[0] = 42;
74 a[1] = 42; 87 a[1] = 42;
75 try { 88 try {
76 a.forEach(function (n, index, array) { 89 a.every(function (n, index, array) {
77 if (count > 0) throw "meh"; 90 if (count > 0) throw "meh";
78 array[index] = n + 1; 91 array[index] = n + 1;
79 count++; 92 count++;
93 return true;
80 }); 94 });
81 } catch (e) { 95 } catch (e) {
82 } 96 }
83 assertEquals(1, count); 97 assertEquals(1, count);
84 assertEquals(43, a[0]); 98 assertEquals(43, a[0]);
85 assertEquals(42, a[1]); 99 assertEquals(42, a[1]);
86 100
87 // Neutering the buffer backing the typed array mid-way should 101 // Neutering the buffer backing the typed array mid-way should
88 // still make .forEach() finish, and the array should keep being 102 // still make .every() finish, and the array should keep being
89 // empty after neutering it. 103 // empty after neutering it.
90 count = 0; 104 count = 0;
91 a.forEach(function (n, index, array) { 105 a.every(function (n, index, array) {
92 if (count > 0) %ArrayBufferNeuter(array.buffer); 106 if (count > 0) %ArrayBufferNeuter(array.buffer);
93 array[index] = n + 1; 107 array[index] = n + 1;
94 count++; 108 count++;
109 return true;
95 }); 110 });
96 assertEquals(2, count); 111 assertEquals(2, count);
97 CheckTypedArrayIsNeutered(a); 112 CheckTypedArrayIsNeutered(a);
98 assertEquals(undefined, a[0]); 113 assertEquals(undefined, a[0]);
99 114
100 // The method must work for typed arrays created from ArrayBuffer. 115 // The method must work for typed arrays created from ArrayBuffer.
101 // The length of the ArrayBuffer is chosen so it is a multiple of 116 // The length of the ArrayBuffer is chosen so it is a multiple of
102 // all lengths of the typed array items. 117 // all lengths of the typed array items.
103 a = new constructor(new ArrayBuffer(64)); 118 a = new constructor(new ArrayBuffer(64));
104 count = 0; 119 count = 0;
105 a.forEach(function (n) { count++ }); 120 a.every(function (n) { count++; return true; });
106 assertEquals(a.length, count); 121 assertEquals(a.length, count);
107 122
108 // Externalizing the array mid-way accessing the .buffer property 123 // Externalizing the array mid-way accessing the .buffer property
109 // should work. 124 // should work.
110 a = new constructor(2); 125 a = new constructor(2);
111 count = 0; 126 count = 0;
112 var buffer = undefined; 127 var buffer = undefined;
113 a.forEach(function (n, index, array) { 128 a.every(function (n, index, array) {
114 if (count++ > 0) 129 if (count++ > 0)
115 buffer = array.buffer; 130 buffer = array.buffer;
131 return true;
116 }); 132 });
117 assertEquals(2, count); 133 assertEquals(2, count);
118 assertTrue(!!buffer); 134 assertTrue(!!buffer);
119 assertEquals("ArrayBuffer", %_ClassOf(buffer)); 135 assertEquals("ArrayBuffer", %_ClassOf(buffer));
120 assertSame(buffer, a.buffer); 136 assertSame(buffer, a.buffer);
121 137
122 // The %TypedArray%.forEach() method should not work when 138 // The %TypedArray%.every() method should not work when
123 // transplanted to objects that are not typed arrays. 139 // transplanted to objects that are not typed arrays.
124 assertThrows(function () { constructor.prototype.forEach.call([1, 2, 3], funct ion (x) {}) }, TypeError); 140 assertThrows(function () { constructor.prototype.every.call([1, 2, 3], functio n (x) {}) }, TypeError);
125 assertThrows(function () { constructor.prototype.forEach.call("abc", function (x) {}) }, TypeError); 141 assertThrows(function () { constructor.prototype.every.call("abc", function (x ) {}) }, TypeError);
126 assertThrows(function () { constructor.prototype.forEach.call({}, function (x) {}) }, TypeError); 142 assertThrows(function () { constructor.prototype.every.call({}, function (x) { }) }, TypeError);
127 assertThrows(function () { constructor.prototype.forEach.call(0, function (x) {}) }, TypeError); 143 assertThrows(function () { constructor.prototype.every.call(0, function (x) {} ) }, TypeError);
128 144
129 // Method must be useable on instances of other typed arrays. 145 // Method must be useable on instances of other typed arrays.
130 for (var i = 0; i < typedArrayConstructors.length; i++) { 146 for (var otherConstructor of typedArrayConstructors) {
131 count = 0; 147 count = 0;
132 a = new typedArrayConstructors[i](4); 148 a = new otherConstructor(4);
133 constructor.prototype.forEach.call(a, function (x) { count++ }); 149 constructor.prototype.every.call(a, function (x) { count++; return true; });
134 assertEquals(a.length, count); 150 assertEquals(a.length, count);
135 } 151 }
136 } 152 }
137 153
138 for (i = 0; i < typedArrayConstructors.length; i++) { 154 for (var constructor of typedArrayConstructors) {
139 TestTypedArrayForEach(typedArrayConstructors[i]); 155 TestTypedArrayEvery(constructor);
140 } 156 }
OLDNEW
« no previous file with comments | « src/harmony-typedarray.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698