summaryrefslogtreecommitdiff
path: root/jstests/core/type7.js
blob: a9e0d67c3b0f5a481e21397c3b6c543c2ab1a799 (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
(function() {
"use strict";

// SERVER-20332 make JS NumberLong more robust
//
// Make sure swapping floatApprox, top and bottom don't break NumberLong

// Picking 2^54 because it's representable as a double (as a power of
// two), but big enough that the NumberLong code doesn't know it (numbers
// over 2^53 can lose precision)
var number = NumberLong("18014398509481984");

{
    // Make sure all elements in a new NumberLong are valid

    assert.eq(number.floatApprox, 18014398509481984);
    assert.eq(number.top, 4194304);
    assert.eq(number.bottom, 0);
    assert.eq(number.valueOf(), 18014398509481984);
}

{
    // Make sure that floatApprox, top and bottom cannot be set

    assert.throws(function() {
        number.floatApprox = "a";
    }, [], "floatApprox should not be setable.");

    assert.throws(function() {
        number.top = "a";
    }, [], "top should not be setable.");

    assert.throws(function() {
        number.bottom = "a";
    }, [], "bottom should not be setable.");
}

{
    // Make sure we fall back to floatApprox

    delete number.top;
    delete number.bottom;

    assert.eq(number.valueOf(), 18014398509481984);
}
})();