summaryrefslogtreecommitdiff
path: root/jstests/core/nan.js
blob: 4cca00c4b6634a6a26d70adea9924e7d4bc8bcd9 (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
/**
 * Tests basic NaN handling. Note that WiredTiger indexes handle -NaN and NaN differently.
 */
(function() {
"use strict";

const coll = db.jstests_nan;
coll.drop();

assert.writeOK(coll.insert({_id: 0, a: -Infinity}));
assert.writeOK(coll.insert({_id: 1, a: -3}));
assert.writeOK(coll.insert({_id: 2, a: 0}));
assert.writeOK(coll.insert({_id: 3, a: 3}));
assert.writeOK(coll.insert({_id: 4, a: Infinity}));
assert.writeOK(coll.insert({_id: 5, a: NaN}));
assert.writeOK(coll.insert({_id: 6, a: -NaN}));
assert.writeOK(coll.insert({_id: 7, a: undefined}));
assert.writeOK(coll.insert({_id: 8, a: null}));
assert.writeOK(coll.insert({_id: 9, a: []}));
assert.writeOK(coll.insert({_id: 10, a: {b: 1}}));
assert.writeOK(coll.insert({_id: 11, a: {b: 1}}));

/**
 * Ensures correct results for EQ, LT, LTE, GT, and GTE cases.
 */
var testNaNComparisons = function() {
    // EQ
    let cursor = coll.find({a: NaN}).sort({_id: 1});
    assert.eq(5, cursor.next()["_id"]);
    assert.eq(6, cursor.next()["_id"]);
    assert(!cursor.hasNext());

    // LT
    cursor = coll.find({a: {$lt: NaN}});
    assert(!cursor.hasNext());

    // LTE
    cursor = coll.find({a: {$lte: NaN}}).sort({_id: 1});
    assert.eq(5, cursor.next()["_id"]);
    assert.eq(6, cursor.next()["_id"]);
    assert(!cursor.hasNext());

    // GT
    cursor = coll.find({a: {$gt: NaN}});
    assert(!cursor.hasNext());

    // GTE
    cursor = coll.find({a: {$gte: NaN}}).sort({_id: 1});
    assert.eq(5, cursor.next()["_id"]);
    assert.eq(6, cursor.next()["_id"]);
    assert(!cursor.hasNext());
};

// Unindexed.
testNaNComparisons();

// Indexed.
assert.commandWorked(coll.createIndex({a: 1}));
testNaNComparisons();
}());