summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/TypedArray/fill.js
blob: 4349da06a986cf56fda7a7b23fa531e779c03ec2 (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
for (var constructor of anyTypedArrayConstructors) {
    assertDeepEq(constructor.prototype.fill.length, 1);

    assertDeepEq(new constructor([]).fill(1), new constructor([]));
    assertDeepEq(new constructor([1,1,1]).fill(2), new constructor([2,2,2]));
    assertDeepEq(new constructor([1,1,1]).fill(2, 1), new constructor([1,2,2]));
    assertDeepEq(new constructor([1,1,1]).fill(2, 1, 2), new constructor([1,2,1]));
    assertDeepEq(new constructor([1,1,1]).fill(2, -2), new constructor([1,2,2]));
    assertDeepEq(new constructor([1,1,1]).fill(2, -2, -1), new constructor([1,2,1]));
    assertDeepEq(new constructor([1,1,1]).fill(2, undefined), new constructor([2,2,2]));
    assertDeepEq(new constructor([1,1,1]).fill(2, undefined, undefined), new constructor([2,2,2]));
    assertDeepEq(new constructor([1,1,1]).fill(2, 1, undefined), new constructor([1,2,2]));
    assertDeepEq(new constructor([1,1,1]).fill(2, undefined, 1), new constructor([2,1,1]));
    assertDeepEq(new constructor([1,1,1]).fill(2, 2, 1), new constructor([1,1,1]));
    assertDeepEq(new constructor([1,1,1]).fill(2, -1, 1), new constructor([1,1,1]));
    assertDeepEq(new constructor([1,1,1]).fill(2, -2, 1), new constructor([1,1,1]));
    assertDeepEq(new constructor([1,1,1]).fill(2, 1, -2), new constructor([1,1,1]));
    assertDeepEq(new constructor([1,1,1]).fill(2, 0.1), new constructor([2,2,2]));
    assertDeepEq(new constructor([1,1,1]).fill(2, 0.9), new constructor([2,2,2]));
    assertDeepEq(new constructor([1,1,1]).fill(2, 1.1), new constructor([1,2,2]));
    assertDeepEq(new constructor([1,1,1]).fill(2, 0.1, 0.9), new constructor([1,1,1]));
    assertDeepEq(new constructor([1,1,1]).fill(2, 0.1, 1.9), new constructor([2,1,1]));
    assertDeepEq(new constructor([1,1,1]).fill(2, 0.1, 1.9), new constructor([2,1,1]));
    assertDeepEq(new constructor([1,1,1]).fill(2, -0), new constructor([2,2,2]));
    assertDeepEq(new constructor([1,1,1]).fill(2, 0, -0), new constructor([1,1,1]));
    assertDeepEq(new constructor([1,1,1]).fill(2, NaN), new constructor([2,2,2]));
    assertDeepEq(new constructor([1,1,1]).fill(2, 0, NaN), new constructor([1,1,1]));
    assertDeepEq(new constructor([1,1,1]).fill(2, false), new constructor([2,2,2]));
    assertDeepEq(new constructor([1,1,1]).fill(2, true), new constructor([1,2,2]));
    assertDeepEq(new constructor([1,1,1]).fill(2, "0"), new constructor([2,2,2]));
    assertDeepEq(new constructor([1,1,1]).fill(2, "1"), new constructor([1,2,2]));
    assertDeepEq(new constructor([1,1,1]).fill(2, "-2"), new constructor([1,2,2]));
    assertDeepEq(new constructor([1,1,1]).fill(2, "-2", "-1"), new constructor([1,2,1]));
    assertDeepEq(new constructor([1,1,1]).fill(2, {valueOf: ()=>1}), new constructor([1,2,2]));
    assertDeepEq(new constructor([1,1,1]).fill(2, 0, {valueOf: ()=>1}), new constructor([2,1,1]));

    // Called from other globals.
    if (typeof newGlobal === "function") {
        var fill = newGlobal()[constructor.name].prototype.fill;
        assertDeepEq(fill.call(new constructor([3, 2, 1]), 2), new constructor([2, 2, 2]));
    }

    // Throws if `this` isn't a TypedArray.
    var invalidReceivers = [undefined, null, 1, false, "", Symbol(), [], {}, /./,
                            new Proxy(new constructor(), {})];
    invalidReceivers.forEach(invalidReceiver => {
        assertThrowsInstanceOf(() => {
            constructor.prototype.fill.call(invalidReceiver, 1);
        }, TypeError);
    });

    // Test that the length getter is never called.
    Object.defineProperty(new constructor([1, 2, 3]), "length", {
        get() {
            throw new Error("length accessor called");
        }
    }).fill(1);
}

for (let constructor of anyTypedArrayConstructors.filter(isFloatConstructor)) {
    assertDeepEq(new constructor([0, 0]).fill(NaN), new constructor([NaN, NaN]));
}

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