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

Side by Side Diff: test/mjsunit/array-iteration.js

Issue 8888006: Make more JS files beter match the coding standard. Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Address review comments Created 9 years 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 | Annotate | Revision Log
« no previous file with comments | « test/mjsunit/array-indexing.js ('k') | test/mjsunit/array-reduce.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2008 the V8 project authors. All rights reserved. 1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 25 matching lines...) Expand all
36 // 36 //
37 // Array.prototype.filter 37 // Array.prototype.filter
38 // 38 //
39 (function() { 39 (function() {
40 // Simple use. 40 // Simple use.
41 var a = [0,1]; 41 var a = [0,1];
42 assertArrayEquals([0], a.filter(function(n) { return n == 0; })); 42 assertArrayEquals([0], a.filter(function(n) { return n == 0; }));
43 assertArrayEquals(a, a); 43 assertArrayEquals(a, a);
44 44
45 // Use specified object as this object when calling the function. 45 // Use specified object as this object when calling the function.
46 var o = { value: 42 } 46 var o = { value: 42 };
47 a = [1,42,3,42,4]; 47 a = [1,42,3,42,4];
48 assertArrayEquals([42,42], a.filter(function(n) { return this.value == n }, o) ) 48 assertArrayEquals([42,42],
49 a.filter(function(n) { return this.value == n; }, o));
49 50
50 // Modify original array. 51 // Modify original array.
51 a = [1,42,3,42,4]; 52 a = [1,42,3,42,4];
52 assertArrayEquals([42,42], a.filter(function(n, index, array) { array[index] = 43; return 42 == n; })); 53 assertArrayEquals([42,42], a.filter(function(n, index, array) {
54 array[index] = 43;
55 return 42 == n;
56 }));
53 assertArrayEquals([43,43,43,43,43], a); 57 assertArrayEquals([43,43,43,43,43], a);
54 58
55 // Only loop through initial part of array eventhough elements are 59 // Only loop through initial part of array eventhough elements are
56 // added. 60 // added.
57 a = [1,1]; 61 a = [1,1];
58 assertArrayEquals([], a.filter(function(n, index, array) { array.push(n+1); re turn n == 2; })); 62 assertArrayEquals([], a.filter(function(n, index, array) {
63 array.push(n+1);
64 return n == 2;
65 }));
59 assertArrayEquals([1,1,2,2], a); 66 assertArrayEquals([1,1,2,2], a);
60 67
61 // Respect holes. 68 // Respect holes.
62 a = new Array(20); 69 a = new Array(20);
63 var count = 0; 70 var count = 0;
64 a[2] = 2; 71 a[2] = 2;
65 a[15] = 2; 72 a[15] = 2;
66 a[17] = 4; 73 a[17] = 4;
67 var a = a.filter(function(n) { count++; return n == 2; }); 74 var a = a.filter(function(n) { count++; return n == 2; });
68 assertEquals(3, count); 75 assertEquals(3, count);
69 for (var i in a) assertEquals(2, a[i]); 76 for (var i in a) assertEquals(2, a[i]);
70 77
71 })(); 78 })();
72 79
73 80
74 // 81 //
75 // Array.prototype.forEach 82 // Array.prototype.forEach
76 // 83 //
77 (function() { 84 (function() {
78 // Simple use. 85 // Simple use.
79 var a = [0,1]; 86 var a = [0,1];
80 var count = 0; 87 var count = 0;
81 a.forEach(function(n) { count++; }); 88 a.forEach(function(n) { count++; });
82 assertEquals(2, count); 89 assertEquals(2, count);
83 90
84 // Use specified object as this object when calling the function. 91 // Use specified object as this object when calling the function.
85 var o = { value: 42 } 92 var o = { value: 42 };
86 var result = []; 93 var result = [];
87 a.forEach(function(n) { result.push(this.value); }, o); 94 a.forEach(function(n) { result.push(this.value); }, o);
88 assertArrayEquals([42,42], result); 95 assertArrayEquals([42,42], result);
89 96
90 // Modify original array. 97 // Modify original array.
91 a = [0,1]; 98 a = [0,1];
92 count = 0; 99 count = 0;
93 a.forEach(function(n, index, array) { array[index] = n + 1; count++; }); 100 a.forEach(function(n, index, array) { array[index] = n + 1; count++; });
94 assertEquals(2, count); 101 assertEquals(2, count);
95 assertArrayEquals([1,2], a); 102 assertArrayEquals([1,2], a);
(...skipping 15 matching lines...) Expand all
111 118
112 })(); 119 })();
113 120
114 121
115 // 122 //
116 // Array.prototype.every 123 // Array.prototype.every
117 // 124 //
118 (function() { 125 (function() {
119 // Simple use. 126 // Simple use.
120 var a = [0,1]; 127 var a = [0,1];
121 assertFalse(a.every(function(n) { return n == 0 })); 128 assertFalse(a.every(function(n) { return n == 0; }));
122 a = [0,0]; 129 a = [0,0];
123 assertTrue(a.every(function(n) { return n == 0 })); 130 assertTrue(a.every(function(n) { return n == 0; }));
124 assertTrue([].every(function(n) { return n == 0})); 131 assertTrue([].every(function(n) { return n == 0; }));
125 132
126 // Use specified object as this object when calling the function. 133 // Use specified object as this object when calling the function.
127 var o = { value: 42 } 134 var o = { value: 42 };
128 a = [0]; 135 a = [0];
129 assertFalse(a.every(function(n) { return this.value == n; }, o)); 136 assertFalse(a.every(function(n) { return this.value == n; }, o));
130 a = [42]; 137 a = [42];
131 assertTrue(a.every(function(n) { return this.value == n; }, o)); 138 assertTrue(a.every(function(n) { return this.value == n; }, o));
132 139
133 // Modify original array. 140 // Modify original array.
134 a = [0,1]; 141 a = [0,1];
135 assertFalse(a.every(function(n, index, array) { array[index] = n + 1; return n == 1;})); 142 assertFalse(a.every(function(n, index, array) { array[index] = n + 1; return n == 1;}));
136 assertArrayEquals([1,1], a); 143 assertArrayEquals([1,1], a);
137 144
(...skipping 18 matching lines...) Expand all
156 // 163 //
157 (function() { 164 (function() {
158 var a = [0,1,2,3,4]; 165 var a = [0,1,2,3,4];
159 166
160 // Simple use. 167 // Simple use.
161 var result = [1,2,3,4,5]; 168 var result = [1,2,3,4,5];
162 assertArrayEquals(result, a.map(function(n) { return n + 1; })); 169 assertArrayEquals(result, a.map(function(n) { return n + 1; }));
163 assertEquals(a, a); 170 assertEquals(a, a);
164 171
165 // Use specified object as this object when calling the function. 172 // Use specified object as this object when calling the function.
166 var o = { delta: 42 } 173 var o = { delta: 42 };
167 result = [42,43,44,45,46]; 174 result = [42,43,44,45,46];
168 assertArrayEquals(result, a.map(function(n) { return this.delta + n; }, o)); 175 assertArrayEquals(result, a.map(function(n) { return this.delta + n; }, o));
169 176
170 // Modify original array. 177 // Modify original array.
171 a = [0,1,2,3,4]; 178 a = [0,1,2,3,4];
172 result = [1,2,3,4,5]; 179 result = [1,2,3,4,5];
173 assertArrayEquals(result, a.map(function(n, index, array) { array[index] = n + 1; return n + 1;})); 180 assertArrayEquals(result, a.map(
181 function(n, index, array) { array[index] = n + 1; return n + 1;}));
174 assertArrayEquals(result, a); 182 assertArrayEquals(result, a);
175 183
176 // Only loop through initial part of array eventhough elements are 184 // Only loop through initial part of array eventhough elements are
177 // added. 185 // added.
178 a = [0,1,2,3,4]; 186 a = [0,1,2,3,4];
179 result = [1,2,3,4,5]; 187 result = [1,2,3,4,5];
180 assertArrayEquals(result, a.map(function(n, index, array) { array.push(n); ret urn n + 1;})); 188 assertArrayEquals(result, a.map(function(n, index, array) {
189 array.push(n);
190 return n + 1;
191 }));
181 assertArrayEquals([0,1,2,3,4,0,1,2,3,4], a); 192 assertArrayEquals([0,1,2,3,4,0,1,2,3,4], a);
182 193
183 // Respect holes. 194 // Respect holes.
184 a = new Array(20); 195 a = new Array(20);
185 a[15] = 2; 196 a[15] = 2;
186 a = a.map(function(n) { return 2*n; }); 197 a = a.map(function(n) { return 2*n; });
187 for (var i in a) assertEquals(4, a[i]); 198 for (var i in a) assertEquals(4, a[i]);
188 199
189 })(); 200 })();
190 201
191 // 202 //
192 // Array.prototype.some 203 // Array.prototype.some
193 // 204 //
194 (function() { 205 (function() {
195 var a = [0,1,2,3,4]; 206 var a = [0,1,2,3,4];
196 207
197 // Simple use. 208 // Simple use.
198 assertTrue(a.some(function(n) { return n == 3})); 209 assertTrue(a.some(function(n) { return n == 3; }));
199 assertFalse(a.some(function(n) { return n == 5})); 210 assertFalse(a.some(function(n) { return n == 5; }));
200 211
201 // Use specified object as this object when calling the function. 212 // Use specified object as this object when calling the function.
202 var o = { element: 42 }; 213 var o = { element: 42 };
203 a = [1,42,3]; 214 a = [1,42,3];
204 assertTrue(a.some(function(n) { return this.element == n; }, o)); 215 assertTrue(a.some(function(n) { return this.element == n; }, o));
205 a = [1]; 216 a = [1];
206 assertFalse(a.some(function(n) { return this.element == n; }, o)); 217 assertFalse(a.some(function(n) { return this.element == n; }, o));
207 218
208 // Modify original array. 219 // Modify original array.
209 a = [0,1,2,3]; 220 a = [0,1,2,3];
210 assertTrue(a.some(function(n, index, array) { array[index] = n + 1; return n = = 2; })); 221 assertTrue(a.some(function(n, index, array) {
222 array[index] = n + 1;
223 return n == 2;
224 }));
211 assertArrayEquals([1,2,3,3], a); 225 assertArrayEquals([1,2,3,3], a);
212 226
213 // Only loop through initial part when elements are added. 227 // Only loop through initial part when elements are added.
214 a = [0,1,2]; 228 a = [0,1,2];
215 assertFalse(a.some(function(n, index, array) { array.push(42); return n == 42; })); 229 assertFalse(a.some(function(n, index, array) {
230 array.push(42);
231 return n == 42;
232 }));
216 assertArrayEquals([0,1,2,42,42,42], a); 233 assertArrayEquals([0,1,2,42,42,42], a);
217 234
218 // Respect holes. 235 // Respect holes.
219 a = new Array(20); 236 a = new Array(20);
220 var count = 0; 237 var count = 0;
221 a[2] = 42; 238 a[2] = 42;
222 a[10] = 2; 239 a[10] = 2;
223 a[15] = 42; 240 a[15] = 42;
224 assertTrue(a.some(function(n) { count++; return n == 2; })); 241 assertTrue(a.some(function(n) { count++; return n == 2; }));
225 assertEquals(2, count); 242 assertEquals(2, count);
226 243
227 })(); 244 })();
228
OLDNEW
« no previous file with comments | « test/mjsunit/array-indexing.js ('k') | test/mjsunit/array-reduce.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698