summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/built-ins/Array/prototype/concat/Array.prototype.concat_small-typed-array.js
blob: 2bb8b17ef308f19a7b269a4ccd413523375ee9eb (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
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


/*---
esid: sec-array.prototype.concat
description: Array.prototype.concat small typed array
includes: [compareArray.js]
features: [Symbol.isConcatSpreadable]
---*/
function concatTypedArray(type, elems, modulo) {
  var items = new Array(elems);
  var ta_by_len = new type(elems);
  for (var i = 0; i < elems; ++i) {
    ta_by_len[i] = items[i] = modulo === false ? i : elems % modulo;
  }
  var ta = new type(items);
  assert.compareArray([].concat(ta, ta), [ta, ta]);
  ta[Symbol.isConcatSpreadable] = true;
  assert.compareArray([].concat(ta), items);

  assert.compareArray([].concat(ta_by_len, ta_by_len), [ta_by_len, ta_by_len]);
  ta_by_len[Symbol.isConcatSpreadable] = true;
  assert.compareArray([].concat(ta_by_len), items);

  // TypedArray with fake `length`.
  ta = new type(1);
  var defValue = ta[0];
  var expected = new Array(4000);
  expected[0] = defValue;

  Object.defineProperty(ta, "length", {
    value: 4000
  });
  ta[Symbol.isConcatSpreadable] = true;
  assert.compareArray([].concat(ta), expected);
}
var max = [Math.pow(2, 8), Math.pow(2, 16), Math.pow(2, 32), false, false];
var TAs = [
  Uint8Array,
  Uint16Array,
  Uint32Array,
  Float32Array,
  Float64Array
];
if (typeof Float16Array !== 'undefined') {
  max.push(false);
  TAs.push(Float16Array);
}

TAs.forEach(function(ctor, i) {
  concatTypedArray(ctor, 1, max[i]);
});

reportCompare(0, 0);