summaryrefslogtreecommitdiff
path: root/jstests/aggregation/bugs/server6165.js
blob: c2ad5fc0fe6a10ea1d7364ee6768fe1b05f89883 (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
/*
 * SERVER-6165: use of an integer valued double in a $mod expression can cause an incorrect result
 *
 * This test validates the SERVER-6165 ticket. Return a long if $mod is operating on a long and a
 * double. Previously it would be forced into an int and convert improperly.
 */

/*
 * 1) Clear and create testing db
 * 2) Run aggregations testing various combinations of number types with the $mod operator
 * 3) Assert that the results are what we expected
 */

// Clear db
db.s6165.drop();
db.s6165.save({});

// Aggregate checking various combinations of number types
// The $match portion ensures they are of the correct type as the shell turns 
// the ints back to doubles at the end so we can not check types with asserts
var s6165 = db.s6165.aggregate(
        { $project: {
            _id: 0,
            dub_dub: {$mod: [138.5, 3.0]},
            dub_int: {$mod: [138.5, NumberLong(3)]},
            dub_long: {$mod: [138.5, NumberInt(3)]},
            int_dub: {$mod: [NumberInt(8), 3.25]},
            int_dubint: {$mod: [NumberInt(8), 3.0]},
            int_int: {$mod: [NumberInt(8), NumberInt(3)]},
            int_long: {$mod: [NumberInt(8), NumberLong(3)]},
            long_dub: {$mod: [NumberLong(8), 3.25]},
            long_dubint: {$mod: [NumberLong(8), 3.0]},
            long_dublong: {$mod: [NumberLong(500000000000), 450000000000.0]},
            long_int: {$mod: [NumberLong(8), NumberInt(3)]},
            long_long: {$mod: [NumberLong(8), NumberLong(3)]},
            verylong_verylong: {$mod: [NumberLong(800000000000), NumberLong(300000000000)]}
        }},
        { $match: {
            // 1 is NumberDouble
            dub_dub: {$type: 1},
            dub_int: {$type: 1},
            dub_long: {$type: 1},
            int_dub: {$type: 1},
            // 16 is NumberInt
            int_dubint: {$type: 16},
            int_int: {$type: 16},
            // 18 is NumberLong
            int_long: {$type: 18},
            long_dub: {$type: 1},
            long_dubint: {$type: 18},
            long_dublong: {$type: 1},
            long_int: {$type: 18},
            long_long: {$type: 18},
            verylong_verylong: {$type: 18}
        }}
);

// Correct answers (it is mainly the types that are important here)
var s6165result = [
    {
        dub_dub: 0.5,
        dub_int: 0.5,
        dub_long: 0.5,
        int_dub: 1.5,
        int_dubint: 2,
        int_int: 2,
        int_long: NumberLong(2),
        long_dub: 1.5,
        long_dubint: NumberLong(2),
        long_dublong: 50000000000,
        long_int: NumberLong(2),
        long_long: NumberLong(2),
        verylong_verylong: NumberLong(200000000000)
    }
];

// Assert
assert.eq(s6165.toArray(), s6165result, 's6165 failed');