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

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: 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
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) { array[index] = 43; return 42 == n; }));
Rico 2011/12/08 10:24:37 long line
53 assertArrayEquals([43,43,43,43,43], a); 54 assertArrayEquals([43,43,43,43,43], a);
54 55
55 // Only loop through initial part of array eventhough elements are 56 // Only loop through initial part of array eventhough elements are
56 // added. 57 // added.
57 a = [1,1]; 58 a = [1,1];
58 assertArrayEquals([], a.filter(function(n, index, array) { array.push(n+1); re turn n == 2; })); 59 assertArrayEquals([], a.filter(function(n, index, array) { array.push(n+1); re turn n == 2; }));
Rico 2011/12/08 10:24:37 long line
59 assertArrayEquals([1,1,2,2], a); 60 assertArrayEquals([1,1,2,2], a);
60 61
61 // Respect holes. 62 // Respect holes.
62 a = new Array(20); 63 a = new Array(20);
63 var count = 0; 64 var count = 0;
64 a[2] = 2; 65 a[2] = 2;
65 a[15] = 2; 66 a[15] = 2;
66 a[17] = 4; 67 a[17] = 4;
67 var a = a.filter(function(n) { count++; return n == 2; }); 68 var a = a.filter(function(n) { count++; return n == 2; });
68 assertEquals(3, count); 69 assertEquals(3, count);
69 for (var i in a) assertEquals(2, a[i]); 70 for (var i in a) assertEquals(2, a[i]);
70 71
71 })(); 72 })();
72 73
73 74
74 // 75 //
75 // Array.prototype.forEach 76 // Array.prototype.forEach
76 // 77 //
77 (function() { 78 (function() {
78 // Simple use. 79 // Simple use.
79 var a = [0,1]; 80 var a = [0,1];
80 var count = 0; 81 var count = 0;
81 a.forEach(function(n) { count++; }); 82 a.forEach(function(n) { count++; });
82 assertEquals(2, count); 83 assertEquals(2, count);
83 84
84 // Use specified object as this object when calling the function. 85 // Use specified object as this object when calling the function.
85 var o = { value: 42 } 86 var o = { value: 42 };
86 var result = []; 87 var result = [];
87 a.forEach(function(n) { result.push(this.value); }, o); 88 a.forEach(function(n) { result.push(this.value); }, o);
88 assertArrayEquals([42,42], result); 89 assertArrayEquals([42,42], result);
89 90
90 // Modify original array. 91 // Modify original array.
91 a = [0,1]; 92 a = [0,1];
92 count = 0; 93 count = 0;
93 a.forEach(function(n, index, array) { array[index] = n + 1; count++; }); 94 a.forEach(function(n, index, array) { array[index] = n + 1; count++; });
94 assertEquals(2, count); 95 assertEquals(2, count);
95 assertArrayEquals([1,2], a); 96 assertArrayEquals([1,2], a);
(...skipping 15 matching lines...) Expand all
111 112
112 })(); 113 })();
113 114
114 115
115 // 116 //
116 // Array.prototype.every 117 // Array.prototype.every
117 // 118 //
118 (function() { 119 (function() {
119 // Simple use. 120 // Simple use.
120 var a = [0,1]; 121 var a = [0,1];
121 assertFalse(a.every(function(n) { return n == 0 })); 122 assertFalse(a.every(function(n) { return n == 0; }));
122 a = [0,0]; 123 a = [0,0];
123 assertTrue(a.every(function(n) { return n == 0 })); 124 assertTrue(a.every(function(n) { return n == 0; }));
124 assertTrue([].every(function(n) { return n == 0})); 125 assertTrue([].every(function(n) { return n == 0; }));
125 126
126 // Use specified object as this object when calling the function. 127 // Use specified object as this object when calling the function.
127 var o = { value: 42 } 128 var o = { value: 42 };
128 a = [0]; 129 a = [0];
129 assertFalse(a.every(function(n) { return this.value == n; }, o)); 130 assertFalse(a.every(function(n) { return this.value == n; }, o));
130 a = [42]; 131 a = [42];
131 assertTrue(a.every(function(n) { return this.value == n; }, o)); 132 assertTrue(a.every(function(n) { return this.value == n; }, o));
132 133
133 // Modify original array. 134 // Modify original array.
134 a = [0,1]; 135 a = [0,1];
135 assertFalse(a.every(function(n, index, array) { array[index] = n + 1; return n == 1;})); 136 assertFalse(a.every(function(n, index, array) { array[index] = n + 1; return n == 1;}));
136 assertArrayEquals([1,1], a); 137 assertArrayEquals([1,1], a);
137 138
(...skipping 18 matching lines...) Expand all
156 // 157 //
157 (function() { 158 (function() {
158 var a = [0,1,2,3,4]; 159 var a = [0,1,2,3,4];
159 160
160 // Simple use. 161 // Simple use.
161 var result = [1,2,3,4,5]; 162 var result = [1,2,3,4,5];
162 assertArrayEquals(result, a.map(function(n) { return n + 1; })); 163 assertArrayEquals(result, a.map(function(n) { return n + 1; }));
163 assertEquals(a, a); 164 assertEquals(a, a);
164 165
165 // Use specified object as this object when calling the function. 166 // Use specified object as this object when calling the function.
166 var o = { delta: 42 } 167 var o = { delta: 42 };
167 result = [42,43,44,45,46]; 168 result = [42,43,44,45,46];
168 assertArrayEquals(result, a.map(function(n) { return this.delta + n; }, o)); 169 assertArrayEquals(result, a.map(function(n) { return this.delta + n; }, o));
169 170
170 // Modify original array. 171 // Modify original array.
171 a = [0,1,2,3,4]; 172 a = [0,1,2,3,4];
172 result = [1,2,3,4,5]; 173 result = [1,2,3,4,5];
173 assertArrayEquals(result, a.map(function(n, index, array) { array[index] = n + 1; return n + 1;})); 174 assertArrayEquals(result, a.map(
175 function(n, index, array) { array[index] = n + 1; return n + 1;}));
174 assertArrayEquals(result, a); 176 assertArrayEquals(result, a);
175 177
176 // Only loop through initial part of array eventhough elements are 178 // Only loop through initial part of array eventhough elements are
177 // added. 179 // added.
178 a = [0,1,2,3,4]; 180 a = [0,1,2,3,4];
179 result = [1,2,3,4,5]; 181 result = [1,2,3,4,5];
180 assertArrayEquals(result, a.map(function(n, index, array) { array.push(n); ret urn n + 1;})); 182 assertArrayEquals(result, a.map(function(n, index, array) { array.push(n); ret urn n + 1;}));
Rico 2011/12/08 10:24:37 long line
181 assertArrayEquals([0,1,2,3,4,0,1,2,3,4], a); 183 assertArrayEquals([0,1,2,3,4,0,1,2,3,4], a);
182 184
183 // Respect holes. 185 // Respect holes.
184 a = new Array(20); 186 a = new Array(20);
185 a[15] = 2; 187 a[15] = 2;
186 a = a.map(function(n) { return 2*n; }); 188 a = a.map(function(n) { return 2*n; });
187 for (var i in a) assertEquals(4, a[i]); 189 for (var i in a) assertEquals(4, a[i]);
188 190
189 })(); 191 })();
190 192
191 // 193 //
192 // Array.prototype.some 194 // Array.prototype.some
193 // 195 //
194 (function() { 196 (function() {
195 var a = [0,1,2,3,4]; 197 var a = [0,1,2,3,4];
196 198
197 // Simple use. 199 // Simple use.
198 assertTrue(a.some(function(n) { return n == 3})); 200 assertTrue(a.some(function(n) { return n == 3; }));
199 assertFalse(a.some(function(n) { return n == 5})); 201 assertFalse(a.some(function(n) { return n == 5; }));
200 202
201 // Use specified object as this object when calling the function. 203 // Use specified object as this object when calling the function.
202 var o = { element: 42 }; 204 var o = { element: 42 };
203 a = [1,42,3]; 205 a = [1,42,3];
204 assertTrue(a.some(function(n) { return this.element == n; }, o)); 206 assertTrue(a.some(function(n) { return this.element == n; }, o));
205 a = [1]; 207 a = [1];
206 assertFalse(a.some(function(n) { return this.element == n; }, o)); 208 assertFalse(a.some(function(n) { return this.element == n; }, o));
207 209
208 // Modify original array. 210 // Modify original array.
209 a = [0,1,2,3]; 211 a = [0,1,2,3];
210 assertTrue(a.some(function(n, index, array) { array[index] = n + 1; return n = = 2; })); 212 assertTrue(a.some(function(n, index, array) { array[index] = n + 1; return n = = 2; }));
Rico 2011/12/08 10:24:37 long line
211 assertArrayEquals([1,2,3,3], a); 213 assertArrayEquals([1,2,3,3], a);
212 214
213 // Only loop through initial part when elements are added. 215 // Only loop through initial part when elements are added.
214 a = [0,1,2]; 216 a = [0,1,2];
215 assertFalse(a.some(function(n, index, array) { array.push(42); return n == 42; })); 217 assertFalse(a.some(function(n, index, array) { array.push(42); return n == 42; }));
Rico 2011/12/08 10:24:37 long line
216 assertArrayEquals([0,1,2,42,42,42], a); 218 assertArrayEquals([0,1,2,42,42,42], a);
217 219
218 // Respect holes. 220 // Respect holes.
219 a = new Array(20); 221 a = new Array(20);
220 var count = 0; 222 var count = 0;
221 a[2] = 42; 223 a[2] = 42;
222 a[10] = 2; 224 a[10] = 2;
223 a[15] = 42; 225 a[15] = 42;
224 assertTrue(a.some(function(n) { count++; return n == 2; })); 226 assertTrue(a.some(function(n) { count++; return n == 2; }));
225 assertEquals(2, count); 227 assertEquals(2, count);
226 228
227 })(); 229 })();
228 230
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698