summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/TypedArray/set-tointeger.js
blob: 8c1e9c241832ccb20ef5004cfa8e670a7cc35483 (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
// Test ToInteger conversion in %TypedArray%.prototype.set(array|typedArray, offset).

let ta = new Int32Array(4);

// %TypedArray%.prototype.set has two different implementations for typed array
// and non-typed array arguments. Test with both input types.
let emptySources = [[], new Int32Array(0)];
let nonEmptySource = [[0], new Int32Array(1)];
let sources = [...emptySources, ...nonEmptySource];

// Test when ToInteger(offset) is in (-1, 4).
let validOffsets = [
    // Values in [+0, 4).
    0,
    0.1,
    3,
    3.9,

    // Values in (-1, -0].
    -0,
    -0.1,
    -0.9,

    NaN,

    // Also include some non-number values.
    undefined,
    null,
    true,
    "",
    "3",
    "  1\t\n",
    "some string",
    {valueOf() { return 2; }},
];

for (let offset of validOffsets) {
    for (let source of sources) {
        ta.set(source, offset);
    }
}

// Test when ToInteger(offset) isn't in (-1, 4).
let invalidOffsets = [
    // Values exceeding the typed array's length.
    5,
    2147483647,
    2147483648,
    2147483649,
    4294967295,
    4294967296,
    4294967297,
    Infinity,

    // Negative values.
    -1,
    -1.1,
    -2147483647,
    -2147483648,
    -2147483649,
    -4294967295,
    -4294967296,
    -4294967297,
    -Infinity,

    // Also include some non-number values.
    "8",
    "Infinity",
    "  Infinity  ",
    {valueOf() { return 10; }},
];

for (let offset of invalidOffsets) {
    for (let source of sources) {
        assertThrowsInstanceOf(() => ta.set(source, offset), RangeError);
    }
}

// Test when ToInteger(offset) is in [4, 5).
for (let source of emptySources) {
    ta.set(source, 4);
    ta.set(source, 4.9);
}
for (let source of nonEmptySource) {
    assertThrowsInstanceOf(() => ta.set(source, 4), RangeError);
    assertThrowsInstanceOf(() => ta.set(source, 4.9), RangeError);
}

// ToInteger(symbol value) throws a TypeError.
for (let source of sources) {
    assertThrowsInstanceOf(() => ta.set(source, Symbol()), TypeError);
}

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