summaryrefslogtreecommitdiff
path: root/jstests/core/numberlong.js
blob: e58929823913bc3b44caca5ba351dd1d9393376a (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
assert.eq.automsg("0", "new NumberLong()");

n = new NumberLong(4);
assert.eq.automsg("4", "n");
assert.eq.automsg("4", "n.toNumber()");
assert.eq.automsg("8", "n + 4");
assert.eq.automsg("'NumberLong(4)'", "n.toString()");
assert.eq.automsg("'NumberLong(4)'", "tojson( n )");
a = {};
a.a = n;
p = tojson(a);
assert.eq.automsg("'{ \"a\" : NumberLong(4) }'", "p");

assert.eq.automsg("NumberLong(4 )", "eval( tojson( NumberLong( 4 ) ) )");
assert.eq.automsg("a", "eval( tojson( a ) )");

n = new NumberLong(-4);
assert.eq.automsg("-4", "n");
assert.eq.automsg("-4", "n.toNumber()");
assert.eq.automsg("0", "n + 4");
assert.eq.automsg("'NumberLong(-4)'", "n.toString()");
assert.eq.automsg("'NumberLong(-4)'", "tojson( n )");
a = {};
a.a = n;
p = tojson(a);
assert.eq.automsg("'{ \"a\" : NumberLong(-4) }'", "p");

// too big to fit in double
n = new NumberLong("11111111111111111");
assert.eq.automsg("11111111111111112", "n.toNumber()");
assert.eq.automsg("11111111111111116", "n + 4");
assert.eq.automsg("'NumberLong(\"11111111111111111\")'", "n.toString()");
assert.eq.automsg("'NumberLong(\"11111111111111111\")'", "tojson( n )");
a = {};
a.a = n;
p = tojson(a);
assert.eq.automsg("'{ \"a\" : NumberLong(\"11111111111111111\") }'", "p");

assert.eq.automsg("NumberLong('11111111111111111' )",
                  "eval( tojson( NumberLong( '11111111111111111' ) ) )");
assert.eq.automsg("a", "eval( tojson( a ) )");

n = new NumberLong("-11111111111111111");
assert.eq.automsg("-11111111111111112", "n.toNumber()");
assert.eq.automsg("-11111111111111108", "n + 4");
assert.eq.automsg("'NumberLong(\"-11111111111111111\")'", "n.toString()");
assert.eq.automsg("'NumberLong(\"-11111111111111111\")'", "tojson( n )");
a = {};
a.a = n;
p = tojson(a);
assert.eq.automsg("'{ \"a\" : NumberLong(\"-11111111111111111\") }'", "p");

// parsing
assert.throws.automsg(function() {
    new NumberLong("");
});
assert.throws.automsg(function() {
    new NumberLong("y");
});
assert.throws.automsg(function() {
    new NumberLong("11111111111111111111");
});

// create NumberLong from NumberInt (SERVER-9973)
assert.doesNotThrow.automsg(function() {
    new NumberLong(NumberInt(1));
});

// check that creating a NumberLong from a NumberLong bigger than a double doesn't
// get a truncated value (SERVER-9973)
n = new NumberLong(NumberLong("11111111111111111"));
assert.eq.automsg("n.toString()", "'NumberLong(\"11111111111111111\")'");

//
// Test NumberLong.compare()
//

var left = new NumberLong("0");
var right = new NumberLong("0");
assert.eq(left.compare(right), 0);
assert.eq(right.compare(left), 0);

left = new NumberLong("20");
right = new NumberLong("10");
assert.gt(left.compare(right), 0);
assert.lt(right.compare(left), 0);

left = new NumberLong("-9223372036854775808");
right = new NumberLong("9223372036854775807");
assert.lt(left.compare(right), 0);
assert.gt(right.compare(left), 0);
assert.eq(left.compare(left), 0);
assert.eq(right.compare(right), 0);

// Bad input to .compare().
assert.throws(function() {
    NumberLong("0").compare();
});
assert.throws(function() {
    NumberLong("0").compare(null);
});
assert.throws(function() {
    NumberLong("0").compare(undefined);
});
assert.throws(function() {
    NumberLong("0").compare(3);
});
assert.throws(function() {
    NumberLong("0").compare("foo");
});
assert.throws(function() {
    NumberLong("0").compare(NumberLong("0"), 3);
});