summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/TypedArray/map-and-filter.js
blob: 0d86fc02b2339bfacc3d999749668897399a7ea0 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Tests for TypedArray#map.
for (var constructor of anyTypedArrayConstructors) {
    assertEq(constructor.prototype.map.length, 1);

    // Basic tests.
    assertDeepEq(new constructor([1, 3, 5]).map(v => v * 2), new constructor([2,6,10]));
    assertDeepEq(new constructor([-1, 13, 5]).map(v => v - 2), new constructor([-3, 11, 3]));
    assertDeepEq(new constructor(10).map(v => v), new constructor(10));
    assertDeepEq(new constructor().map(v => v + 1), new constructor);
    assertDeepEq(new constructor([1,2,3]).map(v => v), new constructor([1,2,3]));

    var arr = new constructor([1, 2, 3, 4, 5]);
    var sum = 0;
    var count = 0;
    assertDeepEq(arr.map((v, k, o) => {
        count++;
        sum += v;
        assertEq(k, v - 1);
        assertEq(o, arr);
        return v;
    }), arr);
    assertEq(sum, 15);
    assertEq(count, 5);

    // Test that changing elements that have been visited does not affect the result.
    var changeArr = new constructor([1,2,3,4,5]);
    assertDeepEq(arr.map((v,k) => {
        changeArr[k] = v + 1;
        return v;
    }), new constructor([1,2,3,4,5]));

    // Tests for `thisArg` argument.
    function assertThisArg(thisArg, thisValue) {
        // In sloppy mode, `this` could be global object or a wrapper of `thisArg`.
        assertDeepEq(arr.map(function(v) {
            assertDeepEq(this, thisValue);
            return v;
        }, thisArg), arr);

        // In strict mode, `this` strictly equals `thisArg`.
        assertDeepEq(arr.map(function(v) {
            "use strict";
            assertDeepEq(this, thisArg);
            return v;
        }, thisArg), arr);

        // Passing `thisArg` has no effect if callback is an arrow function.
        var self = this;
        assertDeepEq(arr.map((v) => {
            assertEq(this, self);
            return v;
        }, thisArg), arr);
    }
    assertThisArg([1, 2, 3], [1, 2, 3]);
    assertThisArg(Object, Object);
    assertThisArg(1, Object(1));
    assertThisArg("1", Object("1"));
    assertThisArg(false, Object(false));
    assertThisArg(undefined, this);
    assertThisArg(null, this);

    // Throw an exception in the callback.
    var sum = 0;
    var count = 0;
    var thrown = false;
    try {
        arr.map((v, k, o) => {
            count++;
            sum += v;
            assertEq(k, v - 1);
            assertEq(o, arr);
            if (v === 3) {
                throw "map";
            }
            return v;
        })
    } catch(e) {
        assertEq(e, "map");
        thrown = true;
    }
    assertEq(thrown, true);
    assertEq(sum, 6);
    assertEq(count, 3);

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

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

    // Called from other globals.
    if (typeof newGlobal === "function") {
        var map = newGlobal()[constructor.name].prototype.map;
        var sum = 0;
        assertDeepEq(map.call(new constructor([1, 2, 3]), v => sum += v), new constructor([1,3,6]));
        assertEq(sum, 6);
    }

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

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

// Test For TypedArray#filter.
for (var constructor of anyTypedArrayConstructors) {
    assertEq(constructor.prototype.filter.length, 1)

    // Basic tests.
    assertDeepEq(new constructor([1,2,3]).filter(x => x == x), new constructor([1,2,3]));
    assertDeepEq(new constructor([1,2,3,4]).filter(x => x % 2 == 0), new constructor([2,4]));
    assertDeepEq(new constructor([1,2,3,4,5]).filter(x => x < 4), new constructor([1,2,3]));
    assertDeepEq(new constructor().filter(x => x * 2 == 4), new constructor());

    var arr = new constructor([1,2,3,4,5]);
    var sum = 0;
    var count = 0;
    assertDeepEq(arr.filter((v, k, o) => {
        count++;
        sum += v;
        assertEq(k, v - 1);
        assertEq(o, arr);
        return (v < 4);
    }), new constructor([1,2,3]));
    assertEq(sum, 15);
    assertEq(count, 5);

    // Test that changing elements that have been visited does not affect the result.
    var changeArr = new constructor([1,2,3,4,5]);
    assertDeepEq(arr.filter((v,k) => {
        changeArr[k] = v + 1;
        return true;
    }), new constructor([1,2,3,4,5]));

    // Tests for `thisArg` argument.
    function assertThisArg(thisArg, thisValue) {
        // In sloppy mode, `this` could be global object or a wrapper of `thisArg`.
        assertDeepEq(arr.filter(function(v) {
            assertDeepEq(this, thisValue);
            return v;
        }, thisArg), arr);

        // In strict mode, `this` strictly equals `thisArg`.
        assertDeepEq(arr.filter(function(v) {
            "use strict";
            assertDeepEq(this, thisArg);
            return v;
        }, thisArg), arr);

        // Passing `thisArg` has no effect if callback is an arrow function.
        var self = this;
        assertDeepEq(arr.filter((v) => {
            assertEq(this, self);
            return v;
        }, thisArg), arr);
    }
    assertThisArg([1, 2, 3], [1, 2, 3]);
    assertThisArg(Object, Object);
    assertThisArg(1, Object(1));
    assertThisArg("1", Object("1"));
    assertThisArg(false, Object(false));
    assertThisArg(undefined, this);
    assertThisArg(null, this);

    // Throw an exception in the callback.
    var sum = 0;
    var count = 0;
    var thrown = false;
    try {
        arr.filter((v, k, o) => {
            count++;
            sum += v;
            assertEq(k, v - 1);
            assertEq(o, arr);
            if (v === 3) {
                throw "filter";
            }
            return v;
        })
    } catch(e) {
        assertEq(e, "filter");
        thrown = true;
    }
    assertEq(thrown, true);
    assertEq(sum, 6);
    assertEq(count, 3);

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

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

    // Called from other globals.
    if (typeof newGlobal === "function") {
        var filter = newGlobal()[constructor.name].prototype.filter;
        var sum = 0;
        assertDeepEq(filter.call(new constructor([1, 2, 3]), v => {sum += v; return true}),
        new constructor([1,2,3]));
        assertEq(sum, 6);
    }

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

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

// Test that changing Array.prototype[Symbol.iterator] does not affect the
// behaviour of filter. See https://bugzilla.mozilla.org/show_bug.cgi?id=1121936#c18
// for more details.

var arr = new Uint16Array([1,2,3]);

// save
var old = Array.prototype[Symbol.iterator];

Array.prototype[Symbol.iterator] = () => { throw new Error("unreachable"); };
assertDeepEq(arr.filter(v => true), arr);

// restore
Array.prototype[Symbol.iterator] = old;

// Test that defining accessors on Array.prototype doesn't affect the behaviour
// of filter. See https://bugzilla.mozilla.org/show_bug.cgi?id=1121936#c18
// for more details.
Object.defineProperty(Array.prototype, 0, {configurable: true, get: function() { return 1; }, set: function() { this.b = 1; }});
assertDeepEq(new Uint16Array([1,2,3]).filter(v => true), new Uint16Array([1,2,3]));
delete Array.prototype[0];

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