summaryrefslogtreecommitdiff
path: root/jstests/core/bittest.js
blob: 1ac5ddbe59b26ccc2caec91c8e814974b3603d6a (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
 * This test ensures that bit test query operators work.
 */
(function() {
'use strict';

load("jstests/libs/analyze_plan.js");

var coll = db.jstests_bitwise;

function assertQueryCorrect(query, count) {
    var explain = coll.find(query).explain("executionStats");
    assert(isCollscan(db, explain.queryPlanner.winningPlan),
           "expected bit test query plan to be COLLSCAN");
    assert.eq(
        count, explain.executionStats.nReturned, "bit test query not returning correct documents");
}

// Tests on numbers.

coll.drop();
assert.commandWorked(coll.insert({a: 0}));
assert.commandWorked(coll.insert({a: 1}));
assert.commandWorked(coll.insert({a: 54}));
assert.commandWorked(coll.insert({a: 88}));
assert.commandWorked(coll.insert({a: 255}));
assert.commandWorked(coll.createIndex({a: 1}));

// Tests with bitmask.
assertQueryCorrect({a: {$bitsAllSet: 0}}, 5);
assertQueryCorrect({a: {$bitsAllSet: 1}}, 2);
assertQueryCorrect({a: {$bitsAllSet: 16}}, 3);
assertQueryCorrect({a: {$bitsAllSet: 54}}, 2);
assertQueryCorrect({a: {$bitsAllSet: 55}}, 1);
assertQueryCorrect({a: {$bitsAllSet: 88}}, 2);
assertQueryCorrect({a: {$bitsAllSet: 255}}, 1);
assertQueryCorrect({a: {$bitsAllClear: 0}}, 5);
assertQueryCorrect({a: {$bitsAllClear: 1}}, 3);
assertQueryCorrect({a: {$bitsAllClear: 16}}, 2);
assertQueryCorrect({a: {$bitsAllClear: 129}}, 3);
assertQueryCorrect({a: {$bitsAllClear: 255}}, 1);
assertQueryCorrect({a: {$bitsAnySet: 0}}, 0);
assertQueryCorrect({a: {$bitsAnySet: 9}}, 3);
assertQueryCorrect({a: {$bitsAnySet: 255}}, 4);
assertQueryCorrect({a: {$bitsAnyClear: 0}}, 0);
assertQueryCorrect({a: {$bitsAnyClear: 18}}, 3);
assertQueryCorrect({a: {$bitsAnyClear: 24}}, 3);
assertQueryCorrect({a: {$bitsAnyClear: 255}}, 4);

// Tests with array of bit positions.
assertQueryCorrect({a: {$bitsAllSet: []}}, 5);
assertQueryCorrect({a: {$bitsAllSet: [0]}}, 2);
assertQueryCorrect({a: {$bitsAllSet: [4]}}, 3);
assertQueryCorrect({a: {$bitsAllSet: [1, 2, 4, 5]}}, 2);
assertQueryCorrect({a: {$bitsAllSet: [0, 1, 2, 4, 5]}}, 1);
assertQueryCorrect({a: {$bitsAllSet: [3, 4, 6]}}, 2);
assertQueryCorrect({a: {$bitsAllSet: [0, 1, 2, 3, 4, 5, 6, 7]}}, 1);
assertQueryCorrect({a: {$bitsAllClear: []}}, 5);
assertQueryCorrect({a: {$bitsAllClear: [0]}}, 3);
assertQueryCorrect({a: {$bitsAllClear: [4]}}, 2);
assertQueryCorrect({a: {$bitsAllClear: [1, 7]}}, 3);
assertQueryCorrect({a: {$bitsAllClear: [0, 1, 2, 3, 4, 5, 6, 7]}}, 1);
assertQueryCorrect({a: {$bitsAnySet: []}}, 0);
assertQueryCorrect({a: {$bitsAnySet: [1, 3]}}, 3);
assertQueryCorrect({a: {$bitsAnySet: [0, 1, 2, 3, 4, 5, 6, 7]}}, 4);
assertQueryCorrect({a: {$bitsAnyClear: []}}, 0);
assertQueryCorrect({a: {$bitsAnyClear: [1, 4]}}, 3);
assertQueryCorrect({a: {$bitsAnyClear: [3, 4]}}, 3);
assertQueryCorrect({a: {$bitsAnyClear: [0, 1, 2, 3, 4, 5, 6, 7]}}, 4);

// Tests with multiple predicates.
assertQueryCorrect({a: {$bitsAllSet: 54, $bitsAllClear: 201}}, 1);

// Tests on negative numbers.

coll.drop();
assert.commandWorked(coll.insert({a: -0}));
assert.commandWorked(coll.insert({a: -1}));
assert.commandWorked(coll.insert({a: -54}));

// Tests with bitmask.
assertQueryCorrect({a: {$bitsAllSet: 0}}, 3);
assertQueryCorrect({a: {$bitsAllSet: 2}}, 2);
assertQueryCorrect({a: {$bitsAllSet: 127}}, 1);
assertQueryCorrect({a: {$bitsAllSet: 74}}, 2);
assertQueryCorrect({a: {$bitsAllClear: 0}}, 3);
assertQueryCorrect({a: {$bitsAllClear: 53}}, 2);
assertQueryCorrect({a: {$bitsAllClear: 127}}, 1);
assertQueryCorrect({a: {$bitsAnySet: 0}}, 0);
assertQueryCorrect({a: {$bitsAnySet: 2}}, 2);
assertQueryCorrect({a: {$bitsAnySet: 127}}, 2);
assertQueryCorrect({a: {$bitsAnyClear: 0}}, 0);
assertQueryCorrect({a: {$bitsAnyClear: 53}}, 2);
assertQueryCorrect({a: {$bitsAnyClear: 127}}, 2);

// Tests with array of bit positions.
var allPositions = [];
for (var i = 0; i < 64; i++) {
    allPositions.push(i);
}
assertQueryCorrect({a: {$bitsAllSet: []}}, 3);
assertQueryCorrect({a: {$bitsAllSet: [1]}}, 2);
assertQueryCorrect({a: {$bitsAllSet: allPositions}}, 1);
assertQueryCorrect({a: {$bitsAllSet: [1, 7, 6, 3, 100]}}, 2);
assertQueryCorrect({a: {$bitsAllClear: []}}, 3);
assertQueryCorrect({a: {$bitsAllClear: [5, 4, 2, 0]}}, 2);
assertQueryCorrect({a: {$bitsAllClear: allPositions}}, 1);
assertQueryCorrect({a: {$bitsAnySet: []}}, 0);
assertQueryCorrect({a: {$bitsAnySet: [1]}}, 2);
assertQueryCorrect({a: {$bitsAnySet: allPositions}}, 2);
assertQueryCorrect({a: {$bitsAnyClear: []}}, 0);
assertQueryCorrect({a: {$bitsAnyClear: [0, 2, 4, 5, 100]}}, 2);
assertQueryCorrect({a: {$bitsAnyClear: allPositions}}, 2);

// Tests with multiple predicates.
assertQueryCorrect({a: {$bitsAllSet: 74, $bitsAllClear: 53}}, 1);

// Tests on BinData.

coll.drop();
assert.commandWorked(coll.insert({a: BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAAA")}));
assert.commandWorked(coll.insert({a: BinData(0, "AANgAAAAAAAAAAAAAAAAAAAAAAAA")}));
assert.commandWorked(coll.insert({a: BinData(0, "JANgqwetkqwklEWRbWERKKJREtbq")}));
assert.commandWorked(coll.insert({a: BinData(0, "////////////////////////////")}));
assert.commandWorked(coll.createIndex({a: 1}));

// Tests with binary string bitmask.
assertQueryCorrect({a: {$bitsAllSet: BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAAA")}}, 4);
assertQueryCorrect({a: {$bitsAllSet: BinData(0, "AANgAAAAAAAAAAAAAAAAAAAAAAAA")}}, 3);
assertQueryCorrect({a: {$bitsAllSet: BinData(0, "JANgqwetkqwklEWRbWERKKJREtbq")}}, 2);
assertQueryCorrect({a: {$bitsAllSet: BinData(0, "////////////////////////////")}}, 1);
assertQueryCorrect({a: {$bitsAllClear: BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAAA")}}, 4);
assertQueryCorrect({a: {$bitsAllClear: BinData(0, "AAyfAAAAAAAAAAAAAAAAAAAAAAAA")}}, 3);
assertQueryCorrect({a: {$bitsAllClear: BinData(0, "JAyfqwetkqwklEWRbWERKKJREtbq")}}, 2);
assertQueryCorrect({a: {$bitsAllClear: BinData(0, "////////////////////////////")}}, 1);
assertQueryCorrect({a: {$bitsAnySet: BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAAA")}}, 0);
assertQueryCorrect({a: {$bitsAnySet: BinData(0, "AAyfAAAAAAAAAAAAAAAAAAAAAAAA")}}, 1);
assertQueryCorrect({a: {$bitsAnySet: BinData(0, "JAyfqwetkqwklEWRbWERKKJREtbq")}}, 2);
assertQueryCorrect({a: {$bitsAnySet: BinData(0, "////////////////////////////")}}, 3);
assertQueryCorrect({a: {$bitsAnyClear: BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAAA")}}, 0);
assertQueryCorrect({a: {$bitsAnyClear: BinData(0, "AANgAAAAAAAAAAAAAAAAAAAAAAAA")}}, 1);
assertQueryCorrect({a: {$bitsAnyClear: BinData(0, "JANgqwetkqwklEWRbWERKKJREtbq")}}, 2);
assertQueryCorrect({a: {$bitsAnyClear: BinData(0, "////////////////////////////")}}, 3);

// Tests with multiple predicates.
assertQueryCorrect({
    a: {
        $bitsAllSet: BinData(0, "AANgAAAAAAAAAAAAAAAAAAAAAAAA"),
        $bitsAllClear: BinData(0, "//yf////////////////////////")
    }
},
                   1);

coll.drop();
})();