summaryrefslogtreecommitdiff
path: root/jstests/aggregation/bugs/server6074.js
blob: 8e53459ba9e31feec5daf27fcfdd16f4918c88fc (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
// SERVER-6074: Add $slice aggregation expression.

// For assertErrorCode.
load('jstests/aggregation/extras/utils.js');

(function() {
'use strict';

var coll = db.agg_slice_expr;
coll.drop();

// Need to have at least one document to ensure the pipeline executes.
assert.writeOK(coll.insert({}));

function testSlice(sliceArgs, expArray) {
    var pipeline = [{$project: {_id: 0, slice: {$slice: sliceArgs}}}];
    assert.eq(coll.aggregate(pipeline).toArray(), [{slice: expArray}]);
}

// Two argument form.

testSlice([[0, 1, 2, 3, 4], 2], [0, 1]);
testSlice([[0, 1, 2, 3, 4], 2.0], [0, 1]);
// Negative count
testSlice([[0, 1, 2, 3, 4], -2], [3, 4]);
testSlice([[0, 1, 2, 3, 4], -2.0], [3, 4]);
// Zero count.
testSlice([[0, 1, 2, 3, 4], 0], []);
// Out of bounds positive.
testSlice([[0, 1, 2, 3, 4], 10], [0, 1, 2, 3, 4]);
// Out of bounds negative.
testSlice([[0, 1, 2, 3, 4], -10], [0, 1, 2, 3, 4]);
// Null arguments
testSlice([null, -10], null);
testSlice([[0, 1, 2, 3, 4], null], null);

// Three argument form.

testSlice([[0, 1, 2, 3, 4], 1, 2], [1, 2]);
testSlice([[0, 1, 2, 3, 4], 1.0, 2.0], [1, 2]);
// Negative start index.
testSlice([[0, 1, 2, 3, 4], -3, 2], [2, 3]);
testSlice([[0, 1, 2, 3, 4], -5, 2], [0, 1]);
// Slice starts out of bounds.
testSlice([[0, 1, 2, 3, 4], -10, 2], [0, 1]);
testSlice([[0, 1, 2, 3, 4], 10, 2], []);
// Slice ends out of bounds.
testSlice([[0, 1, 2, 3, 4], 4, 3], [4]);
testSlice([[0, 1, 2, 3, 4], -1, 3], [4]);
// Null arguments
testSlice([[0, 1, 2, 3, 4], -1, null], null);

// Error cases.

// Wrong number of arguments.
assertErrorCode(coll, [{$project: {x: {$slice: [[1, 2, 3]]}}}], 28667);
assertErrorCode(coll, [{$project: {x: {$slice: [[1, 2, 3], 4, 5, 6]}}}], 28667);

// First argument is not an array.
assertErrorCode(coll, [{$project: {x: {$slice: ['one', 2]}}}], 28724);

// Second argument is not numeric.
assertErrorCode(coll, [{$project: {x: {$slice: [[1, 2], '2']}}}], 28725);

// Second argument is not integral.
assertErrorCode(coll, [{$project: {x: {$slice: [[1, 2], 1.5]}}}], 28726);
assertErrorCode(coll, [{$project: {x: {$slice: [[1, 2], Math.pow(2, 32)]}}}], 28726);
assertErrorCode(coll, [{$project: {x: {$slice: [[1, 2], -Math.pow(2, 31) - 1]}}}], 28726);

// Third argument is not numeric.
assertErrorCode(coll, [{$project: {x: {$slice: [[1, 2], 0, '2']}}}], 28727);

// Third argument is not integral.
assertErrorCode(coll, [{$project: {x: {$slice: [[1, 2], 0, 1.5]}}}], 28728);
assertErrorCode(coll, [{$project: {x: {$slice: [[1, 2], 0, Math.pow(2, 32)]}}}], 28728);
assertErrorCode(coll, [{$project: {x: {$slice: [[1, 2], 0, -Math.pow(2, 31) - 1]}}}], 28728);

// Third argument is not positive.
assertErrorCode(coll, [{$project: {x: {$slice: [[1, 2], 0, 0]}}}], 28729);
assertErrorCode(coll, [{$project: {x: {$slice: [[1, 2], 0, -1]}}}], 28729);
}());