summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Array/with-dense.js
blob: 4f457fbeddde312cc2e24077c522428d6354e1cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// |reftest| 

const indices = [
  -10, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 10,
];

const arrays = [
  // Dense no holes.
  [],
  [1],
  [1,2],
  [1,2,3],
  [1,2,3,4],
  [1,2,3,4,5,6,7,8],

  // Dense trailing holes.
  [,],
  [1,,],
  [1,2,,],
  [1,2,3,,],
  [1,2,3,4,,],
  [1,2,3,4,5,6,7,8,,],

  // Dense leading holes.
  [,],
  [,1],
  [,1,2],
  [,1,2,3],
  [,1,2,3,4],
  [,1,2,3,4,5,6,7,8],

  // Dense with holes.
  [1,,3],
  [1,2,,4],
  [1,,3,,5,6,,8],
];

const objects = arrays.map(array => {
  let obj = {
    length: array.length,
  };
  for (let i = 0; i < array.length; ++i) {
    if (i in array) {
      obj[i] = array[i];
    }
  }
  return obj;
});

const objectsWithLargerDenseInitializedLength = arrays.map(array => {
  let obj = {
    length: array.length,
  };
  for (let i = 0; i < array.length; ++i) {
    if (i in array) {
      obj[i] = array[i];
    }
  }

  // Add some extra dense elements after |length|.
  for (let i = 0; i < 5; ++i) {
    obj[array.length + i] = "extra";
  }

  return obj;
});

const thisValues = [
  ...arrays,
  ...objects,
  ...objectsWithLargerDenseInitializedLength,
];

const replacement = {};

for (let thisValue of thisValues) {
  for (let index of indices) {
    let actualIndex = index;
    if (actualIndex < 0) {
      actualIndex += thisValue.length;
    }

    if (actualIndex < 0 || actualIndex >= thisValue.length) {
      continue;
    }

    let res = Array.prototype.with.call(thisValue, index, replacement);
    assertEq(res.length, thisValue.length);

    for (let i = 0; i < thisValue.length; ++i) {
      assertEq(Object.hasOwn(res, i), true);

      if (i === actualIndex) {
        assertEq(res[i], replacement);
      } else {
        assertEq(res[i], thisValue[i]);
      }
    }
  }
}

if (typeof reportCompare === "function")
  reportCompare(0, 0);