js/src/jit-test/tests/collections/Map-forEach.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* test Map.prototype.forEach */
     3 load(libdir + 'asserts.js');
     4 load(libdir + 'iteration.js');
     6 // testing success conditions of Map.prototype.forEach
     8 var testMap = new Map();
    10 function callback(value, key, map) {
    11     testMap.set(key, value);
    12     assertEq(map.has(key), true);
    13     assertEq(map.get(key), value);
    14 }
    16 var initialMap = new Map([['a', 1], ['b', 2.3], [false, undefined]]);
    17 initialMap.forEach(callback);
    19 // test that both the Maps are equal and are in same order
    20 var iterator = initialMap[std_iterator]();
    21 var count = 0;
    22 for (var [k, v] of testMap) {
    23     assertEq(initialMap.has(k), true);
    24     assertEq(initialMap.get(k), testMap.get(k));
    25     assertIteratorNext(iterator, [k, testMap.get(k)]);
    26     count++;
    27 }
    29 //check both the Maps we have are equal in size
    30 assertEq(initialMap.size, testMap.size);
    31 assertEq(initialMap.size, count);
    33 var x = { abc: 'test'};
    34 function callback2(value, key, map) {
    35     assertEq(x, this);
    36 }
    37 initialMap = new Map([['a', 1]]);
    38 initialMap.forEach(callback2, x);
    40 // testing failure conditions of Map.prototype.forEach
    42 var s = new Set([1, 2, 3]);
    43 assertThrowsInstanceOf(function() {
    44     Map.prototype.forEach.call(s, callback);
    45 }, TypeError, "Map.prototype.forEach should raise TypeError if not used on a Map");
    47 var fn = 2;
    48 assertThrowsInstanceOf(function() {
    49     initialMap.forEach(fn);
    50 }, TypeError, "Map.prototype.forEach should raise TypeError if callback is not a function");
    52 // testing that Map#forEach uses internal next() function and does not stop when
    53 // StopIteration exception is thrown
    55 var m = new Map([["one", 1]]);
    56 Object.getPrototypeOf(m[std_iterator]()).next = function () { throw "FAIL"; };
    57 assertThrowsInstanceOf(function () {
    58   m.forEach(function () { throw StopIteration; });
    59 }, StopIteration, "Map.prototype.forEach should use intrinsic next method.");

mercurial