summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/ion/bug1101821.js
blob: e65e47b8cdc3d4f24479062239249de4b300df45 (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
// Makes it easier for fuzzers to get max or min statements with multiple arguments.
function test(a, b, c, d, e, f) {
    var r = 0
    r += Math.max(a);
    r += Math.max(a,b);
    r += Math.max(a,b,c);
    r += Math.max(a,b,c,d);
    r += Math.max(a,b,c,d,e);
    r += Math.max(a,b,c,d,e,f);
    r += Math.min(a);
    r += Math.min(a,b);
    r += Math.min(a,b,c);
    r += Math.min(a,b,c,d);
    r += Math.min(a,b,c,d,e);
    r += Math.min(a,b,c,d,e,f);
    return r;
}
for (var i=0; i<10; i++) {
    assertEq(test(12,5,32,6,18,2), 186);
    assertEq(test(1,5,3,6,18,-10), 48);
    assertEq(test(-19,5,20,6,18,1), -48);
}

// Test max/min result up to 5 arguments.
for (var i=1; i<5; i++) {
    var args = [];
    for (var j=0; j<i; j++)
        args[args.length] = "arg" + j;
    var max = new Function(args, "return Math.max("+args.join(",")+");");
    var min = new Function(args, "return Math.min("+args.join(",")+");");

    var input = [];
    for (var j=0; j<i; j++) {
        input[input.length] = j;
    }

    permutate(input, function (a) {
        var min_value = min.apply(undefined, a);
        var max_value = max.apply(undefined, a);
        assertEq(min_value, minimum(a));
        assertEq(max_value, maximum(a));
    });
}

function minimum(arr) {
    var min = arr[0]
    for (var i=1; i<arr.length; i++) {
        if (min > arr[i])
            min = arr[i]
    }
    return min
}
function maximum(arr) {
    var max = arr[0]
    for (var i=1; i<arr.length; i++) {
        if (max < arr[i])
            max = arr[i]
    }
    return max
}

function permutate(array, callback) {
    function p(array, index, callback) {
        function swap(a, i1, i2) {
            var t = a[i1];
            a[i1] = a[i2];
            a[i2] = t;
        }

        if (index == array.length - 1) {
            callback(array);
            return 1;
        } else {
            var count = p(array, index + 1, callback);
            for (var i = index + 1; i < array.length; i++) {
                swap(array, i, index);
                count += p(array, index + 1, callback);
                swap(array, i, index);
            }
            return count;
        }
    }

    if (!array || array.length == 0) {
        return 0;
    }
    return p(array, 0, callback);
}