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.

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

mercurial