OLD | NEW |
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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 var o = { value: 42 } | 127 var o = { value: 42 } |
128 a = [0]; | 128 a = [0]; |
129 assertFalse(a.every(function(n) { return this.value == n; }, o)); | 129 assertFalse(a.every(function(n) { return this.value == n; }, o)); |
130 a = [42]; | 130 a = [42]; |
131 assertTrue(a.every(function(n) { return this.value == n; }, o)); | 131 assertTrue(a.every(function(n) { return this.value == n; }, o)); |
132 | 132 |
133 // Modify original array. | 133 // Modify original array. |
134 a = [0,1]; | 134 a = [0,1]; |
135 assertFalse(a.every(function(n, index, array) { array[index] = n + 1; return n
== 1;})); | 135 assertFalse(a.every(function(n, index, array) { array[index] = n + 1; return n
== 1;})); |
136 assertArrayEquals([1,1], a); | 136 assertArrayEquals([1,1], a); |
137 | 137 |
138 // Only loop through initial part of array eventhough elements are | 138 // Only loop through initial part of array eventhough elements are |
139 // added. | 139 // added. |
140 a = [1,1]; | 140 a = [1,1]; |
141 assertTrue(a.every(function(n, index, array) { array.push(n + 1); return n ==
1;})); | 141 assertTrue(a.every(function(n, index, array) { array.push(n + 1); return n ==
1;})); |
142 assertArrayEquals([1,1,2,2], a); | 142 assertArrayEquals([1,1,2,2], a); |
143 | 143 |
144 // Respect holes. | 144 // Respect holes. |
145 a = new Array(20); | 145 a = new Array(20); |
146 var count = 0; | 146 var count = 0; |
147 a[2] = 2; | 147 a[2] = 2; |
148 a[15] = 2; | 148 a[15] = 2; |
149 assertTrue(a.every(function(n) { count++; return n == 2; })); | 149 assertTrue(a.every(function(n) { count++; return n == 2; })); |
150 assertEquals(2, count); | 150 assertEquals(2, count); |
151 | 151 |
152 })(); | 152 })(); |
153 | 153 |
154 // | 154 // |
155 // Array.prototype.map | 155 // Array.prototype.map |
156 // | 156 // |
157 (function() { | 157 (function() { |
158 var a = [0,1,2,3,4]; | 158 var a = [0,1,2,3,4]; |
159 | 159 |
160 // Simple use. | 160 // Simple use. |
161 var result = [1,2,3,4,5]; | 161 var result = [1,2,3,4,5]; |
162 assertArrayEquals(result, a.map(function(n) { return n + 1; })); | 162 assertArrayEquals(result, a.map(function(n) { return n + 1; })); |
163 assertEquals(a, a); | 163 assertEquals(a, a); |
164 | 164 |
165 // Use specified object as this object when calling the function. | 165 // Use specified object as this object when calling the function. |
166 var o = { delta: 42 } | 166 var o = { delta: 42 } |
167 result = [42,43,44,45,46]; | 167 result = [42,43,44,45,46]; |
168 assertArrayEquals(result, a.map(function(n) { return this.delta + n; }, o)); | 168 assertArrayEquals(result, a.map(function(n) { return this.delta + n; }, o)); |
169 | 169 |
170 // Modify original array. | 170 // Modify original array. |
171 a = [0,1,2,3,4]; | 171 a = [0,1,2,3,4]; |
172 result = [1,2,3,4,5]; | 172 result = [1,2,3,4,5]; |
173 assertArrayEquals(result, a.map(function(n, index, array) { array[index] = n +
1; return n + 1;})); | 173 assertArrayEquals(result, a.map(function(n, index, array) { array[index] = n +
1; return n + 1;})); |
174 assertArrayEquals(result, a); | 174 assertArrayEquals(result, a); |
175 | 175 |
176 // Only loop through initial part of array eventhough elements are | 176 // Only loop through initial part of array eventhough elements are |
177 // added. | 177 // added. |
178 a = [0,1,2,3,4]; | 178 a = [0,1,2,3,4]; |
179 result = [1,2,3,4,5]; | 179 result = [1,2,3,4,5]; |
180 assertArrayEquals(result, a.map(function(n, index, array) { array.push(n); ret
urn n + 1;})); | 180 assertArrayEquals(result, a.map(function(n, index, array) { array.push(n); ret
urn n + 1;})); |
181 assertArrayEquals([0,1,2,3,4,0,1,2,3,4], a); | 181 assertArrayEquals([0,1,2,3,4,0,1,2,3,4], a); |
182 | 182 |
183 // Respect holes. | 183 // Respect holes. |
184 a = new Array(20); | 184 a = new Array(20); |
185 a[15] = 2; | 185 a[15] = 2; |
186 a = a.map(function(n) { return 2*n; }); | 186 a = a.map(function(n) { return 2*n; }); |
187 for (var i in a) assertEquals(4, a[i]); | 187 for (var i in a) assertEquals(4, a[i]); |
188 | 188 |
189 })(); | 189 })(); |
190 | 190 |
191 // | 191 // |
192 // Array.prototype.some | 192 // Array.prototype.some |
193 // | 193 // |
194 (function() { | 194 (function() { |
195 var a = [0,1,2,3,4]; | 195 var a = [0,1,2,3,4]; |
196 | 196 |
197 // Simple use. | 197 // Simple use. |
198 assertTrue(a.some(function(n) { return n == 3})); | 198 assertTrue(a.some(function(n) { return n == 3})); |
199 assertFalse(a.some(function(n) { return n == 5})); | 199 assertFalse(a.some(function(n) { return n == 5})); |
200 | 200 |
201 // Use specified object as this object when calling the function. | 201 // Use specified object as this object when calling the function. |
202 var o = { element: 42 }; | 202 var o = { element: 42 }; |
203 a = [1,42,3]; | 203 a = [1,42,3]; |
204 assertTrue(a.some(function(n) { return this.element == n; }, o)); | 204 assertTrue(a.some(function(n) { return this.element == n; }, o)); |
205 a = [1]; | 205 a = [1]; |
206 assertFalse(a.some(function(n) { return this.element == n; }, o)); | 206 assertFalse(a.some(function(n) { return this.element == n; }, o)); |
207 | 207 |
208 // Modify original array. | 208 // Modify original array. |
209 a = [0,1,2,3]; | 209 a = [0,1,2,3]; |
210 assertTrue(a.some(function(n, index, array) { array[index] = n + 1; return n =
= 2; })); | 210 assertTrue(a.some(function(n, index, array) { array[index] = n + 1; return n =
= 2; })); |
211 assertArrayEquals([1,2,3,3], a); | 211 assertArrayEquals([1,2,3,3], a); |
212 | 212 |
213 // Only loop through initial part when elements are added. | 213 // Only loop through initial part when elements are added. |
214 a = [0,1,2]; | 214 a = [0,1,2]; |
215 assertFalse(a.some(function(n, index, array) { array.push(42); return n == 42;
})); | 215 assertFalse(a.some(function(n, index, array) { array.push(42); return n == 42;
})); |
216 assertArrayEquals([0,1,2,42,42,42], a); | 216 assertArrayEquals([0,1,2,42,42,42], a); |
217 | 217 |
218 // Respect holes. | 218 // Respect holes. |
219 a = new Array(20); | 219 a = new Array(20); |
220 var count = 0; | 220 var count = 0; |
221 a[2] = 42; | 221 a[2] = 42; |
222 a[10] = 2; | 222 a[10] = 2; |
223 a[15] = 42; | 223 a[15] = 42; |
224 assertTrue(a.some(function(n) { count++; return n == 2; })); | 224 assertTrue(a.some(function(n) { count++; return n == 2; })); |
225 assertEquals(2, count); | 225 assertEquals(2, count); |
226 | 226 |
227 })(); | 227 })(); |
228 | 228 |
OLD | NEW |