js/src/tests/ecma_5/Array/length-truncate-nonconfigurable-sparse.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-nonconfigurable-sparse.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,110 @@
     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 redefinition behavior with non-configurable elements";
    1.15 +
    1.16 +print(BUGNUMBER + ": " + summary);
    1.17 +
    1.18 +/**************
    1.19 + * BEGIN TEST *
    1.20 + **************/
    1.21 +
    1.22 +function addDataProperty(obj, prop, value, enumerable, configurable, writable)
    1.23 +{
    1.24 +  var desc =
    1.25 +    { enumerable: enumerable,
    1.26 +      configurable: configurable,
    1.27 +      writable: writable,
    1.28 +      value: value };
    1.29 +  Object.defineProperty(obj, prop, desc);
    1.30 +}
    1.31 +
    1.32 +function nonstrict()
    1.33 +{
    1.34 +  var arr = [0, , 2, , , 5];
    1.35 +
    1.36 +  addDataProperty(arr,  31415926, "foo", true,  true,  true);
    1.37 +  addDataProperty(arr, 123456789, "bar", true,  true,  false);
    1.38 +  addDataProperty(arr,   8675309, "qux", false, true,  true);
    1.39 +  addDataProperty(arr,   1735039, "eit", false, true,  false);
    1.40 +  addDataProperty(arr, 987654321, "fun", false, true,  false);
    1.41 +
    1.42 +  // non-array indexes to spice things up
    1.43 +  addDataProperty(arr, "foopy", "sdfsd", false, false, false);
    1.44 +  addDataProperty(arr, 4294967296, "psych", true, false, false);
    1.45 +  addDataProperty(arr, 4294967295, "psych", true, false, false);
    1.46 +
    1.47 +  addDataProperty(arr,  27182818, "eep", false, false, false);
    1.48 +
    1.49 +  // Truncate...but only as far as possible.
    1.50 +  arr.length = 1;
    1.51 +
    1.52 +  assertEq(arr.length, 27182819);
    1.53 +
    1.54 +  var props = Object.getOwnPropertyNames(arr).sort();
    1.55 +  var expected =
    1.56 +    ["0", "2", "5", "1735039", "8675309", "27182818",
    1.57 +     "foopy", "4294967296", "4294967295", "length"].sort();
    1.58 +
    1.59 +  assertEq(props.length, expected.length);
    1.60 +  for (var i = 0; i < props.length; i++)
    1.61 +    assertEq(props[i], expected[i], "unexpected property: " + props[i]);
    1.62 +}
    1.63 +nonstrict();
    1.64 +
    1.65 +function strict()
    1.66 +{
    1.67 +  "use strict";
    1.68 +
    1.69 +  var arr = [0, , 2, , , 5];
    1.70 +
    1.71 +  addDataProperty(arr,  31415926, "foo", true,  true,  true);
    1.72 +  addDataProperty(arr, 123456789, "bar", true,  true,  false);
    1.73 +  addDataProperty(arr,   8675309, "qux", false, true,  true);
    1.74 +  addDataProperty(arr,   1735039, "eit", false, true,  false);
    1.75 +  addDataProperty(arr, 987654321, "fun", false, true,  false);
    1.76 +
    1.77 +  // non-array indexes to spice things up
    1.78 +  addDataProperty(arr, "foopy", "sdfsd", false, false, false);
    1.79 +  addDataProperty(arr, 4294967296, "psych", true, false, false);
    1.80 +  addDataProperty(arr, 4294967295, "psych", true, false, false);
    1.81 +
    1.82 +  addDataProperty(arr,  27182818, "eep", false, false, false);
    1.83 +
    1.84 +  try
    1.85 +  {
    1.86 +    arr.length = 1;
    1.87 +    throw new Error("didn't throw?!");
    1.88 +  }
    1.89 +  catch (e)
    1.90 +  {
    1.91 +    assertEq(e instanceof TypeError, true,
    1.92 +             "non-configurable property should trigger TypeError, got " + e);
    1.93 +  }
    1.94 +
    1.95 +  assertEq(arr.length, 27182819);
    1.96 +
    1.97 +  var props = Object.getOwnPropertyNames(arr).sort();
    1.98 +  var expected =
    1.99 +    ["0", "2", "5", "1735039", "8675309", "27182818",
   1.100 +     "foopy", "4294967296", "4294967295", "length"].sort();
   1.101 +
   1.102 +  assertEq(props.length, expected.length);
   1.103 +  for (var i = 0; i < props.length; i++)
   1.104 +    assertEq(props[i], expected[i], "unexpected property: " + props[i]);
   1.105 +}
   1.106 +strict();
   1.107 +
   1.108 +/******************************************************************************/
   1.109 +
   1.110 +if (typeof reportCompare === "function")
   1.111 +  reportCompare(true, true);
   1.112 +
   1.113 +print("Tests complete");

mercurial