summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/TypedArray/reduce-and-reduceRight.js
blob: 21a7d99e9075aaea68c84cc1f5e00799e2dbe3e5 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Tests for TypedArray#reduce.
for (var constructor of anyTypedArrayConstructors) {
    assertEq(constructor.prototype.reduce.length, 1);

    // Basic tests.
    var arr = new constructor([1, 2, 3, 4, 5]);

    assertEq(arr.reduce((previous, current) => previous + current), 15);
    assertEq(arr.reduce((previous, current) => current - previous), 3);

    var count = 0;
    var sum = 0;
    assertEq(arr.reduce((previous, current, index, array) => {
        count++;
        sum += current;
        assertEq(current - 1, index);
        assertEq(current, arr[index]);
        assertEq(array, arr);
        return previous * current;
    }), 120);
    assertEq(count, 4);
    assertEq(sum, 14);

    // Tests for `initialValue` argument.
    assertEq(arr.reduce((previous, current) => previous + current, -15), 0);
    assertEq(arr.reduce((previous, current) => previous + current, ""), "12345");
    assertDeepEq(arr.reduce((previous, current) => previous.concat(current), []), [1, 2, 3, 4, 5]);

    // Tests for `this` value.
    var global = this;
    arr.reduce(function(){
        assertEq(this, global);
    });
    arr.reduce(function(){
        "use strict";
        assertEq(this, undefined);
    });
    arr.reduce(() => assertEq(this, global));

    // Throw an exception in the callback.
    var count = 0;
    var sum = 0;
    assertThrowsInstanceOf(() => {
        arr.reduce((previous, current, index, array) => {
            count++;
            sum += current;
            if (index === 3) {
                throw TypeError("reduce");
            }
        })
    }, TypeError);
    assertEq(count, 3);
    assertEq(sum, 9);

    // There is no callback or callback is not a function.
    assertThrowsInstanceOf(() => {
        arr.reduce();
    }, TypeError);
    var invalidCallbacks = [undefined, null, 1, false, "", Symbol(), [], {}, /./];
    invalidCallbacks.forEach(callback => {
        assertThrowsInstanceOf(() => {
            arr.reduce(callback);
        }, TypeError);
    })

    // Callback is a generator.
    arr.reduce(function*(){
        throw "This line will not be executed";
    });

    // Called from other globals.
    if (typeof newGlobal === "function") {
        var reduce = newGlobal()[constructor.name].prototype.reduce;
        assertEq(reduce.call(arr, (previous, current) => Math.min(previous, current)), 1);
    }

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

    // Test that the length getter is never called.
    assertEq(Object.defineProperty(arr, "length", {
        get() {
            throw new Error("length accessor called");
        }
    }).reduce((previous, current) => Math.max(previous, current)), 5);
}

// Tests for TypedArray#reduceRight.
for (var constructor of anyTypedArrayConstructors) {
    assertEq(constructor.prototype.reduceRight.length, 1);

    // Basic tests.
    var arr = new constructor([1, 2, 3, 4, 5]);

    assertEq(arr.reduceRight((previous, current) => previous + current), 15);
    assertEq(arr.reduceRight((previous, current) => current - previous), 3);

    var count = 0;
    var sum = 0;
    assertEq(arr.reduceRight((previous, current, index, array) => {
        count++;
        sum += current;
        assertEq(current - 1, index);
        assertEq(current, arr[index]);
        assertEq(array, arr);
        return previous * current;
    }), 120);
    assertEq(count, 4);
    assertEq(sum, 10);

    // Tests for `initialValue` argument.
    assertEq(arr.reduceRight((previous, current) => previous + current, -15), 0);
    assertEq(arr.reduceRight((previous, current) => previous + current, ""), "54321");
    assertDeepEq(arr.reduceRight((previous, current) => previous.concat(current), []), [5, 4, 3, 2, 1]);

    // Tests for `this` value.
    var global = this;
    arr.reduceRight(function(){
        assertEq(this, global);
    });
    arr.reduceRight(function(){
        "use strict";
        assertEq(this, undefined);
    });
    arr.reduceRight(() => assertEq(this, global));

    // Throw an exception in the callback.
    var count = 0;
    var sum = 0;
    assertThrowsInstanceOf(() => {
        arr.reduceRight((previous, current, index, array) => {
            count++;
            sum += current;
            if (index === 1) {
                throw TypeError("reduceRight");
            }
        })
    }, TypeError);
    assertEq(count, 3);
    assertEq(sum, 9);

    // There is no callback or callback is not a function.
    assertThrowsInstanceOf(() => {
        arr.reduceRight();
    }, TypeError);
    var invalidCallbacks = [undefined, null, 1, false, "", Symbol(), [], {}, /./];
    invalidCallbacks.forEach(callback => {
        assertThrowsInstanceOf(() => {
            arr.reduceRight(callback);
        }, TypeError);
    })

    // Callback is a generator.
    arr.reduceRight(function*(){
        throw "This line will not be executed";
    });

    // Called from other globals.
    if (typeof newGlobal === "function") {
        var reduceRight = newGlobal()[constructor.name].prototype.reduceRight;
        assertEq(reduceRight.call(arr, (previous, current) => Math.min(previous, current)), 1);
    }

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

    // Test that the length getter is never called.
    assertEq(Object.defineProperty(arr, "length", {
        get() {
            throw new Error("length accessor called");
        }
    }).reduceRight((previous, current) => Math.max(previous, current)), 5);
}

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