summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/cacheir/binaryarith-null-undef-bool.js
blob: 140db2657200e184f6acd8d42f08592be6b1392e (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
// Pass |null| as argument to be more robust against code folding.
function testNullWithInt32OrBool(nullVal) {
    var vals = [0, 1, true, false, null];
    for (var v of vals) {
        assertEq(v + nullVal, Number(v));
        assertEq(v - nullVal, Number(v));
        assertEq(v * nullVal, 0);
        var res = v / nullVal;
        assertEq(isNaN(res) || res === Infinity, true);
        assertEq(v % nullVal, NaN);
        assertEq(v ** nullVal, 1);

        assertEq(nullVal + v, Number(v));
        assertEq(nullVal - v, 0 - Number(v));
        assertEq(nullVal * v, 0);
        res = nullVal / v;
        assertEq(isNaN(res) || res === 0, true);
        res = nullVal % v;
        assertEq(isNaN(res) || res === 0, true);
        res = nullVal ** v;
        assertEq(res === 0 || res === 1, true);
    }
}
for (var i = 0; i < 15; i++) {
    testNullWithInt32OrBool(null);
}

function testUndefinedWithOther(undefinedVal) {
    var vals = [1.1, NaN, true, false, null, undefined];
    for (var v of vals) {
        assertEq(v + undefinedVal, NaN);
        assertEq(v - undefinedVal, NaN);
        assertEq(v * undefinedVal, NaN);
        assertEq(v / undefinedVal, NaN);
        assertEq(v % undefinedVal, NaN);
        assertEq(v ** undefinedVal, NaN);

        assertEq(undefinedVal + v, NaN);
        assertEq(undefinedVal - v, NaN);
        assertEq(undefinedVal * v, NaN);
        assertEq(undefinedVal / v, NaN);
        assertEq(undefinedVal % v, NaN);
        var res = undefinedVal ** v;
        if (v === false || v === null) {
            assertEq(res, 1);
        } else {
            assertEq(res, NaN);
        }
    }
}
for (var i = 0; i < 15; i++) {
    testUndefinedWithOther(undefined);
}

function testBooleanWithDouble(trueVal, falseVal) {
    var vals = [1.1, 2.2, 5, 6, 3.14];
    for (var v of vals) {
        assertEq(v + falseVal, v);
        assertEq(v - falseVal, v);
        assertEq(v * falseVal, 0);
        assertEq(v / falseVal, Infinity);
        assertEq(v % falseVal, NaN);
        assertEq(v ** falseVal, 1);

        assertEq(trueVal + v, v + 1);
        assertEq(trueVal - v, 1 - v);
        assertEq(trueVal * v, v);
        assertEq(trueVal / v, 1 / v);
        assertEq(trueVal % v, 1);
        assertEq(trueVal ** v, 1);
    }
}
for (var i = 0; i < 15; i++) {
    testBooleanWithDouble(true, false);
}