summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/basic/mathImul.js
blob: aa1861e5a783a030c327bace0f6f8856097c37b2 (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
var table = [
    [NaN, 0, 0],
    [Infinity, Infinity, 0],
    [NaN, 1000, 0],

    [-1, -2, 2],
    [1, 2, 2],
    [-1, 2, -2],
    [1, -2, -2],
    [-0, 0, 0],
    [0, -0, 0],
    [-1, -0, 0],
    [1, -0, 0],

    [0xffffffff, 1, -1],

    [0xffffffff, 0xffffffff, 1],
    [0xffffffff, -0xffffffff, -1],
    [0xffffffff, 0xfffffffe, 2],
    [0xffffffff, -0xfffffffe, -2],
    [0x10000, 0x10000, 0],

    [{}, {}, 0],
    [[], [], 0],
    [{}, [], 0],
    [[], {}, 0],

    [{valueOf: function() { return -1; }}, 0x100000, -1048576],
    ["3", "-4", -12],
    [3.4, 6, 18]
];

try {
    Math.imul({ valueOf: function() { throw "ha ha ha"; } });
    assertEq(true, false, "no error thrown");
} catch (e) {
    assertEq(e, "ha ha ha");
}

var order = [];
assertEq(Math.imul({ valueOf: function() { order.push("first"); return 0; } },
                   { valueOf: function() { order.push("second"); return 0; } }),
         0);
assertEq(order[0], "first");
assertEq(order[1], "second");

var seen = [];
try
{
    Math.imul({ valueOf: function() { seen.push("one"); return 17; } },
              { valueOf: function() { throw "abort!"; } });
    assertEq(true, false, "no error thrown");
}
catch (e)
{
  assertEq(e, "abort!", "should have thrown partway through, instead threw " + e);
}
assertEq(seen.length, 1);
assertEq(seen[0], "one");

assertEq(Math.imul(), 0);
assertEq(Math.imul(100), 0);
assertEq(Math.imul(NaN, 100), 0);
assertEq(Math.imul(NaN, NaN), 0);
assertEq(Math.imul(5, Infinity), 0);

for (var i = 0; i < table.length; i++) {
    assertEq(Math.imul(table[i][0], table[i][1]), table[i][2]);
    assertEq(Math.imul(table[i][1], table[i][0]), table[i][2]);
}