js/src/tests/ecma_5/Array/length-truncate-with-indexed.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:d58048ec9618
1 /*
2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/licenses/publicdomain/
4 * Contributor:
5 * Jeff Walden <jwalden+code@mit.edu>
6 */
7
8 //-----------------------------------------------------------------------------
9 var BUGNUMBER = 858381;
10 var summary =
11 "Array length setting/truncating with non-dense, indexed elements";
12
13 print(BUGNUMBER + ": " + summary);
14
15 /**************
16 * BEGIN TEST *
17 **************/
18
19 function testTruncateDenseAndSparse()
20 {
21 var arr;
22
23 // initialized length 16, capacity same
24 arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
25
26 // plus a sparse element
27 arr[987654321] = 987654321;
28
29 // lop off the sparse element and half the dense elements, shrink capacity
30 arr.length = 8;
31
32 assertEq(987654321 in arr, false);
33 assertEq(arr[987654321], undefined);
34 assertEq(arr.length, 8);
35 }
36 testTruncateDenseAndSparse();
37
38 function testTruncateSparse()
39 {
40 // initialized length 8, capacity same
41 var arr = [0, 1, 2, 3, 4, 5, 6, 7];
42
43 // plus a sparse element
44 arr[987654321] = 987654321;
45
46 // lop off the sparse element, leave initialized length/capacity unchanged
47 arr.length = 8;
48
49 assertEq(987654321 in arr, false);
50 assertEq(arr[987654321], undefined);
51 assertEq(arr.length, 8);
52 }
53 testTruncateSparse();
54
55 function testTruncateDenseAndSparseShrinkCapacity()
56 {
57 // initialized length 11, capacity...somewhat larger, likely 16
58 var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
59
60 // plus a sparse element
61 arr[987654321] = 987654321;
62
63 // lop off the sparse element, reduce initialized length, reduce capacity
64 arr.length = 8;
65
66 assertEq(987654321 in arr, false);
67 assertEq(arr[987654321], undefined);
68 assertEq(arr.length, 8);
69 }
70 testTruncateDenseAndSparseShrinkCapacity();
71
72 function testTruncateSparseShrinkCapacity()
73 {
74 // initialized length 8, capacity same
75 var arr = [0, 1, 2, 3, 4, 5, 6, 7];
76
77 // capacity expands to accommodate, initialized length remains same (not equal
78 // to capacity or length)
79 arr[15] = 15;
80
81 // now no elements past initialized length
82 delete arr[15];
83
84 // ...except a sparse element
85 arr[987654321] = 987654321;
86
87 // trims sparse element, doesn't change initialized length, shrinks capacity
88 arr.length = 8;
89
90 assertEq(987654321 in arr, false);
91 assertEq(arr[987654321], undefined);
92 assertEq(arr.length, 8);
93 }
94 testTruncateSparseShrinkCapacity();
95
96 /******************************************************************************/
97
98 if (typeof reportCompare === "function")
99 reportCompare(true, true);
100
101 print("Tests complete");

mercurial