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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_5/Array/length-truncate-with-indexed.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,101 @@
     1.4 +/*
     1.5 + * Any copyright is dedicated to the Public Domain.
     1.6 + * http://creativecommons.org/licenses/publicdomain/
     1.7 + * Contributor:
     1.8 + *   Jeff Walden <jwalden+code@mit.edu>
     1.9 + */
    1.10 +
    1.11 +//-----------------------------------------------------------------------------
    1.12 +var BUGNUMBER = 858381;
    1.13 +var summary =
    1.14 +  "Array length setting/truncating with non-dense, indexed elements";
    1.15 +
    1.16 +print(BUGNUMBER + ": " + summary);
    1.17 +
    1.18 +/**************
    1.19 + * BEGIN TEST *
    1.20 + **************/
    1.21 +
    1.22 +function testTruncateDenseAndSparse()
    1.23 +{
    1.24 +  var arr;
    1.25 +
    1.26 +  // initialized length 16, capacity same
    1.27 +  arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
    1.28 +
    1.29 +  // plus a sparse element
    1.30 +  arr[987654321] = 987654321;
    1.31 +
    1.32 +  // lop off the sparse element and half the dense elements, shrink capacity
    1.33 +  arr.length = 8;
    1.34 +
    1.35 +  assertEq(987654321 in arr, false);
    1.36 +  assertEq(arr[987654321], undefined);
    1.37 +  assertEq(arr.length, 8);
    1.38 +}
    1.39 +testTruncateDenseAndSparse();
    1.40 +
    1.41 +function testTruncateSparse()
    1.42 +{
    1.43 +  // initialized length 8, capacity same
    1.44 +  var arr = [0, 1, 2, 3, 4, 5, 6, 7];
    1.45 +
    1.46 +  // plus a sparse element
    1.47 +  arr[987654321] = 987654321;
    1.48 +
    1.49 +  // lop off the sparse element, leave initialized length/capacity unchanged
    1.50 +  arr.length = 8;
    1.51 +
    1.52 +  assertEq(987654321 in arr, false);
    1.53 +  assertEq(arr[987654321], undefined);
    1.54 +  assertEq(arr.length, 8);
    1.55 +}
    1.56 +testTruncateSparse();
    1.57 +
    1.58 +function testTruncateDenseAndSparseShrinkCapacity()
    1.59 +{
    1.60 +  // initialized length 11, capacity...somewhat larger, likely 16
    1.61 +  var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    1.62 +
    1.63 +  // plus a sparse element
    1.64 +  arr[987654321] = 987654321;
    1.65 +
    1.66 +  // lop off the sparse element, reduce initialized length, reduce capacity
    1.67 +  arr.length = 8;
    1.68 +
    1.69 +  assertEq(987654321 in arr, false);
    1.70 +  assertEq(arr[987654321], undefined);
    1.71 +  assertEq(arr.length, 8);
    1.72 +}
    1.73 +testTruncateDenseAndSparseShrinkCapacity();
    1.74 +
    1.75 +function testTruncateSparseShrinkCapacity()
    1.76 +{
    1.77 +  // initialized length 8, capacity same
    1.78 +  var arr = [0, 1, 2, 3, 4, 5, 6, 7];
    1.79 +
    1.80 +  // capacity expands to accommodate, initialized length remains same (not equal
    1.81 +  // to capacity or length)
    1.82 +  arr[15] = 15;
    1.83 +
    1.84 +  // now no elements past initialized length
    1.85 +  delete arr[15];
    1.86 +
    1.87 +  // ...except a sparse element
    1.88 +  arr[987654321] = 987654321;
    1.89 +
    1.90 +  // trims sparse element, doesn't change initialized length, shrinks capacity
    1.91 +  arr.length = 8;
    1.92 +
    1.93 +  assertEq(987654321 in arr, false);
    1.94 +  assertEq(arr[987654321], undefined);
    1.95 +  assertEq(arr.length, 8);
    1.96 +}
    1.97 +testTruncateSparseShrinkCapacity();
    1.98 +
    1.99 +/******************************************************************************/
   1.100 +
   1.101 +if (typeof reportCompare === "function")
   1.102 +  reportCompare(true, true);
   1.103 +
   1.104 +print("Tests complete");

mercurial