summaryrefslogtreecommitdiff
path: root/jstests/core/mod_overflow.js
blob: 983e943b6dd997ed1619fc6dd55d2ddce06b6ca8 (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
/**
 * Tests that modding the smallest representable integer values by -1 does not result in integer
 * overflow. Exercises the fix for SERVER-43699.
 */
(function() {
    "use strict";

    const testDB = db.getSiblingDB(jsTestName());
    const testColl = testDB.test;
    testColl.drop();

    // Insert two documents, one with a value of -2^63 and the other with a value of -2^31.
    const insertedDocs = [
        {_id: 0, val: NumberLong("-9223372036854775808")},
        {_id: 1, val: NumberInt("-2147483648")}
    ];
    assert.writeOK(testColl.insert(insertedDocs));

    // For each possible integral representation of -1, confirm that overflow does not occur.
    for (let divisor of[-1.0, NumberInt("-1"), NumberLong("-1"), NumberDecimal("-1")]) {
        try {
            assert.docEq(testColl.find({val: {$mod: [divisor, 0]}}).sort({_id: 1}).toArray(),
                         insertedDocs);
            assert.docEq(testColl
                             .aggregate([
                                 {$match: {$expr: {$eq: [0, {$mod: ["$val", divisor]}]}}},
                                 {$sort: {_id: 1}}
                             ])
                             .toArray(),
                         insertedDocs);

            // Confirm that overflow does not occur during agg expression evaluation. Also confirm
            // that the correct type is returned for each combination of input types.
            const expectedResults = [
                Object.merge(insertedDocs[0], {
                    modVal:
                        (divisor instanceof NumberDecimal ? NumberDecimal("-0") : NumberLong("0"))
                }),
                Object.merge(insertedDocs[1], {
                    modVal: (divisor instanceof NumberLong
                                 ? NumberLong("0")
                                 : divisor instanceof NumberDecimal ? NumberDecimal("-0") : 0)
                })
            ];
            assert.docEq(testColl
                             .aggregate([
                                 {$project: {val: 1, modVal: {$mod: ["$val", divisor]}}},
                                 {$sort: {_id: 1}}
                             ])
                             .toArray(),
                         expectedResults);
        } catch (error) {
            jsTestLog("Failure encountered on divisor: " + tojson(divisor));
            jsTestLog("Explain output for find with $mod match expression: " +
                      tojson(testColl.find({val: {$mod: [divisor, 0]}}).sort({_id: 1}).explain()));
            jsTestLog("Explain output for aggregate with $mod aggregation expression in $expr: " +
                      tojson(testColl.explain().aggregate([
                          {$match: {$expr: {$eq: [0, {$mod: ["$val", divisor]}]}}},
                          {$sort: {_id: 1}}
                      ])));
            jsTestLog(
                "Explain output for aggregate with $mod aggregation expression in $project: " +
                tojson(testColl.explain().aggregate(
                    [{$project: {val: 1, modVal: {$mod: ["$val", divisor]}}}, {$sort: {_id: 1}}])));
            throw error;
        }
    }
})();