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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 // in the array. | 146 // in the array. |
147 function TestArraySortingWithUnsoundComparisonFunction() { | 147 function TestArraySortingWithUnsoundComparisonFunction() { |
148 var a = [ 3, void 0, 2 ]; | 148 var a = [ 3, void 0, 2 ]; |
149 a.sort(function(x, y) { return 1; }); | 149 a.sort(function(x, y) { return 1; }); |
150 a.sort(); | 150 a.sort(); |
151 assertArrayEquals([ 2, 3, void 0 ], a); | 151 assertArrayEquals([ 2, 3, void 0 ], a); |
152 } | 152 } |
153 | 153 |
154 TestArraySortingWithUnsoundComparisonFunction(); | 154 TestArraySortingWithUnsoundComparisonFunction(); |
155 | 155 |
156 | |
156 function TestSparseNonArraySorting(length) { | 157 function TestSparseNonArraySorting(length) { |
157 assertTrue(length > 101); | 158 assertTrue(length > 101); |
158 var obj = {length: length}; | 159 var obj = {length: length}; |
159 obj[0] = 42; | 160 obj[0] = 42; |
160 obj[10] = 37; | 161 obj[10] = 37; |
161 obj[100] = undefined; | 162 obj[100] = undefined; |
162 obj[length - 1] = null; | 163 obj[length - 1] = null; |
163 Array.prototype.sort.call(obj); | 164 Array.prototype.sort.call(obj); |
164 assertEquals(length, obj.length, "objsort length unaffected"); | 165 assertEquals(length, obj.length, "objsort length unaffected"); |
165 assertEquals(37, obj[0], "objsort smallest number"); | 166 assertEquals(37, obj[0], "objsort smallest number"); |
166 assertEquals(42, obj[1], "objsort largest number"); | 167 assertEquals(42, obj[1], "objsort largest number"); |
167 assertEquals(null, obj[2], "objsort null"); | 168 assertEquals(null, obj[2], "objsort null"); |
168 assertEquals(undefined, obj[3], "objsort undefined"); | 169 assertEquals(undefined, obj[3], "objsort undefined"); |
169 assertTrue(3 in obj, "objsort undefined retained"); | 170 assertTrue(3 in obj, "objsort undefined retained"); |
170 assertFalse(4 in obj, "objsort non-existing retained"); | 171 assertFalse(4 in obj, "objsort non-existing retained"); |
171 } | 172 } |
172 | 173 |
173 TestSparseNonArraySorting(5000); | 174 TestSparseNonArraySorting(5000); |
174 TestSparseNonArraySorting(500000); | 175 TestSparseNonArraySorting(500000); |
175 TestSparseNonArraySorting(Math.pow(2, 31) + 1); | 176 TestSparseNonArraySorting(Math.pow(2, 31) + 1); |
176 | 177 |
178 | |
177 function TestArrayLongerLength(length) { | 179 function TestArrayLongerLength(length) { |
178 var x = new Array(4); | 180 var x = new Array(4); |
179 x[0] = 42; | 181 x[0] = 42; |
180 x[2] = 37; | 182 x[2] = 37; |
181 x.length = length; | 183 x.length = length; |
182 Array.prototype.sort.call(x); | 184 Array.prototype.sort.call(x); |
183 assertEquals(length, x.length, "longlength length"); | 185 assertEquals(length, x.length, "longlength length"); |
184 assertEquals(37, x[0], "longlength first"); | 186 assertEquals(37, x[0], "longlength first"); |
185 assertEquals(42, x[1], "longlength second"); | 187 assertEquals(42, x[1], "longlength second"); |
186 assertFalse(2 in x,"longlength third"); | 188 assertFalse(2 in x,"longlength third"); |
187 } | 189 } |
188 | 190 |
189 TestArrayLongerLength(4); | 191 TestArrayLongerLength(4); |
190 TestArrayLongerLength(10); | 192 TestArrayLongerLength(10); |
191 TestArrayLongerLength(1000); | 193 TestArrayLongerLength(1000); |
192 TestArrayLongerLength(500000); | 194 TestArrayLongerLength(500000); |
193 TestArrayLongerLength(Math.pow(2,32) - 1); | 195 TestArrayLongerLength(Math.pow(2,32) - 1); |
194 | 196 |
197 | |
195 function TestNonArrayLongerLength(length) { | 198 function TestNonArrayLongerLength(length) { |
196 var x = {}; | 199 var x = {}; |
197 x[0] = 42; | 200 x[0] = 42; |
198 x[2] = 37; | 201 x[2] = 37; |
199 x.length = length; | 202 x.length = length; |
200 Array.prototype.sort.call(x); | 203 Array.prototype.sort.call(x); |
201 assertEquals(length, x.length, "longlength length"); | 204 assertEquals(length, x.length, "longlength length"); |
202 assertEquals(37, x[0], "longlength first"); | 205 assertEquals(37, x[0], "longlength first"); |
203 assertEquals(42, x[1], "longlength second"); | 206 assertEquals(42, x[1], "longlength second"); |
204 assertFalse(2 in x,"longlength third"); | 207 assertFalse(2 in x,"longlength third"); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
251 | 254 |
252 for (var i = 0; i < 5; i++) { | 255 for (var i = 0; i < 5; i++) { |
253 x[i * 2 * scale] = 2 * (4 - i); | 256 x[i * 2 * scale] = 2 * (4 - i); |
254 y[(i * 2 + 1) * scale] = 2 * (4 - i) + 1; | 257 y[(i * 2 + 1) * scale] = 2 * (4 - i) + 1; |
255 } | 258 } |
256 | 259 |
257 var name = "SparseSortInherit(" + scale + ")-"; | 260 var name = "SparseSortInherit(" + scale + ")-"; |
258 | 261 |
259 Array.prototype.sort.call(y); | 262 Array.prototype.sort.call(y); |
260 | 263 |
261 assertEquals(length, y.length, name+"length"); | 264 assertEquals(length, y.length, name +"length"); |
William Hesse
2009/05/04 08:55:21
name + "length" ? Spaces on both sides of operato
| |
262 | 265 |
263 for (var i = 0; i < 10; i++) { | 266 for (var i = 0; i < 10; i++) { |
264 assertTrue(y.hasOwnProperty(i), name + "hasvalue" + i); | 267 assertTrue(y.hasOwnProperty(i), name + "hasvalue" + i); |
265 assertEquals(i, y[i], name + "value" + i); | 268 assertEquals(i, y[i], name + "value" + i); |
266 } | 269 } |
267 for (var i = 10; i < length; i++) { | 270 for (var i = 10; i < length; i++) { |
268 assertEquals(x.hasOwnProperty(i), y.hasOwnProperty(i), name+"hasundef"+i); | 271 assertEquals(x.hasOwnProperty(i), y.hasOwnProperty(i), |
William Hesse
2009/05/04 08:55:21
Remove space at end of line.
| |
272 name + "hasundef" + i); | |
269 assertEquals(undefined, y[i], name+"undefined"+i); | 273 assertEquals(undefined, y[i], name+"undefined"+i); |
270 if (x.hasOwnProperty(i)) { | 274 if (x.hasOwnProperty(i)) { |
271 assertTrue(0 == i % (2 * scale), name+"new_x"+i); | 275 assertTrue(0 == i % (2 * scale), name + "new_x" + i); |
272 } | 276 } |
273 } | 277 } |
274 } | 278 } |
275 | 279 |
276 TestSparseInheritedElementSort(10); | 280 TestSparseInheritedElementSort(10); |
277 TestSparseInheritedElementSort(100); | 281 TestSparseInheritedElementSort(100); |
278 TestSparseInheritedElementSort(1000); | 282 TestSparseInheritedElementSort(1000); |
279 TestSparseInheritedElementSort(10000); | |
280 | 283 |
281 function TestSpecialCasesInheritedElementSort() { | 284 function TestSpecialCasesInheritedElementSort() { |
282 | 285 |
283 var x = { | 286 var x = { |
284 1:"d1", | 287 1:"d1", |
285 2:"c1", | 288 2:"c1", |
286 3:"b1", | 289 3:"b1", |
287 4: undefined, | 290 4: undefined, |
288 __proto__: { | 291 __proto__: { |
289 length: 10000, | 292 length: 10000, |
(...skipping 16 matching lines...) Expand all Loading... | |
306 } | 309 } |
307 }; | 310 }; |
308 Array.prototype.sort.call(x); | 311 Array.prototype.sort.call(x); |
309 | 312 |
310 var name = "SpecialInherit-"; | 313 var name = "SpecialInherit-"; |
311 | 314 |
312 assertEquals(10000, x.length, name + "length"); | 315 assertEquals(10000, x.length, name + "length"); |
313 var sorted = ["a2", "a3", "b1", "b2", "c1", "c2", "d1", "d2", "e3", | 316 var sorted = ["a2", "a3", "b1", "b2", "c1", "c2", "d1", "d2", "e3", |
314 undefined, undefined, undefined]; | 317 undefined, undefined, undefined]; |
315 for (var i = 0; i < sorted.length; i++) { | 318 for (var i = 0; i < sorted.length; i++) { |
316 assertTrue(x[0], x.hasOwnProperty(i) + "has" + i) | 319 assertTrue(x.hasOwnProperty(i), name + "has" + i) |
317 assertEquals(sorted[i], x[i], name + i); | 320 assertEquals(sorted[i], x[i], name + i); |
318 } | 321 } |
319 assertFalse(x.hasOwnProperty(sorted.length), name + "haspost"); | 322 assertFalse(x.hasOwnProperty(sorted.length), name + "haspost"); |
320 assertFalse(sorted.length in x, name + "haspost2"); | 323 assertFalse(sorted.length in x, name + "haspost2"); |
321 assertEquals(undefined, x[12000], name + "XX12000"); | |
322 | 324 |
323 assertTrue(x.hasOwnProperty(10), name + "hasundefined10"); | 325 assertTrue(x.hasOwnProperty(10), name + "hasundefined10"); |
324 assertEquals(undefined, x[10], name + "undefined10"); | 326 assertEquals(undefined, x[10], name + "undefined10"); |
325 assertTrue(x.hasOwnProperty(100), name + "hasundefined100"); | 327 assertTrue(x.hasOwnProperty(100), name + "hasundefined100"); |
326 assertEquals(undefined, x[100], name + "undefined100"); | 328 assertEquals(undefined, x[100], name + "undefined100"); |
327 assertTrue(x.hasOwnProperty(1000), name + "hasundefined1000"); | 329 assertTrue(x.hasOwnProperty(1000), name + "hasundefined1000"); |
328 assertEquals(undefined, x[1000], name + "undefined1000"); | 330 assertEquals(undefined, x[1000], name + "undefined1000"); |
329 assertTrue(x.hasOwnProperty(2000), name + "hasundefined2000"); | 331 assertTrue(x.hasOwnProperty(2000), name + "hasundefined2000"); |
330 assertEquals(undefined, x[2000], name + "undefined2000"); | 332 assertEquals(undefined, x[2000], name + "undefined2000"); |
331 assertTrue(x.hasOwnProperty(8000), name + "hasundefined8000"); | 333 assertTrue(x.hasOwnProperty(8000), name + "hasundefined8000"); |
332 assertEquals(undefined, x[8000], name + "undefined8000"); | 334 assertEquals(undefined, x[8000], name + "undefined8000"); |
333 | 335 |
334 assertFalse(x.hasOwnProperty(11), name + "hasundefined11"); | |
William Hesse
2009/05/04 08:55:21
Is what this test is meant to test still tested?
Lasse Reichstein
2009/05/04 09:07:07
Yes. It's the sorted.length test above (11 should
| |
335 assertEquals(undefined, x[11], name + "undefined11"); | |
336 | |
337 assertFalse(x.hasOwnProperty(12000), name + "has12000"); | 336 assertFalse(x.hasOwnProperty(12000), name + "has12000"); |
338 assertEquals("XX", x[12000], name + "XX12000"); | 337 assertEquals("XX", x[12000], name + "XX12000"); |
339 | 338 |
340 } | 339 } |
340 | |
341 TestSpecialCasesInheritedElementSort(); | |
342 | |
OLD | NEW |