Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / js / script-tests / array-sort-modifying-tostring.js
blob270465b50ced10c93e6c1879b911938f934bce4d
1 description(
2 "Test of array with toString() override that truncates array."
3 );
5 var maxSize = 2000;
6 var keepSize = 100;
7 var digits = 4;
8 var countToDelete = maxSize - keepSize;
10 var a = new Array(maxSize);
12 function do_gc() {
13 if (window.GCController)
14 return GCController.collect();
16 for (var i = 0; i < 1000; i++)
17 new String(i);
20 function Item(val) {
21 this.value = val;
24 function toString_Mutate() {
25 a.splice(keepSize, countToDelete);
26 do_gc();
27 for (var i = keepSize; i < countToDelete; i++) {
28 delete a[i];
31 if ((this != undefined) && (this.value != undefined)) {
32 var s = this.value.toString();
33 if (s.length < digits)
34 s = ('0000' + s).slice(-digits);
36 return s;
37 } else
38 return "Undef";
41 function test() {
42 for (var i = 0; i < a.length; i++) {
43 a[i] = new Item(a.length - i - 1);
44 a[i].toString = toString_Mutate;
46 try {
47 a.sort();
48 if (a.length == maxSize)
49 testPassed("Array length is unchanged.");
50 else
51 testFailed("Array length should be " + maxSize + " but is " + a.length + ".");
53 var firstFailedValue = -1;
55 for (var i = 0; i < a.length; i++) {
56 if (a[i].value != i) {
57 firstFailedValue = i;
58 break;
62 if (firstFailedValue == -1)
63 testPassed("Array values are correct.");
64 else
65 testFailed("Array values are wrong, first bad value at index " + firstFailedValue + ", should be " + firstFailedValue + " was " + a[firstFailedValue].value + ".");
66 } catch(er) {
67 testFailed("Got exception processing sort()");
70 for (var i = 0; i < a.length; i++) {
71 a[i] = 0x42424242;
75 test();