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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jit-test/tests/collections/Set-forEach.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,49 @@
     1.4 +/* test Set.prototype.forEach */
     1.5 +
     1.6 +load(libdir + 'asserts.js');
     1.7 +load(libdir + 'iteration.js');
     1.8 +
     1.9 +// testing success conditions of Set.prototype.forEach
    1.10 +
    1.11 +var testSet = new Set();
    1.12 +
    1.13 +function callback(value, key, set) {
    1.14 +    assertEq(value, key);
    1.15 +    testSet.add(value);
    1.16 +    assertEq(set.has(key), true);
    1.17 +}
    1.18 +
    1.19 +var initialSet = new Set(['a', 1, undefined]);
    1.20 +initialSet.forEach(callback);
    1.21 +
    1.22 +// test that both the Sets are equal and are in same order
    1.23 +var iterator = initialSet[std_iterator]();
    1.24 +var count = 0;
    1.25 +for (var v of testSet) {
    1.26 +    assertEq(initialSet.has(v), true);
    1.27 +    assertIteratorNext(iterator, v);
    1.28 +    count++;
    1.29 +}
    1.30 +
    1.31 +//check both the Sets we have are equal in size
    1.32 +assertEq(initialSet.size, testSet.size);
    1.33 +assertEq(initialSet.size, count);
    1.34 +
    1.35 +var x = { abc: 'test'};
    1.36 +function callback2(value, key, set) {
    1.37 +    assertEq(x, this);
    1.38 +}
    1.39 +initialSet = new Set(['a']);
    1.40 +initialSet.forEach(callback2, x);
    1.41 +
    1.42 +// testing failure conditions of Set.prototype.forEach
    1.43 +
    1.44 +var m = new Map([['a', 1], ['b', 2.3], ['c', undefined]]);
    1.45 +assertThrowsInstanceOf(function() {
    1.46 +    Set.prototype.forEach.call(m, callback);
    1.47 +}, TypeError, "Set.prototype.forEach should raise TypeError if not a used on a Set");
    1.48 +
    1.49 +var fn = 2;
    1.50 +assertThrowsInstanceOf(function() {
    1.51 +    initialSet.forEach(fn);
    1.52 +}, TypeError, "Set.prototype.forEach should raise TypeError if callback is not a function");

mercurial