summaryrefslogtreecommitdiff
path: root/jstests/inc-SERVER-7446.js
blob: 73cdef3dbd517c4e049233fbe1bfbd4c1d7d2e7a (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
var c = db.incSERVER7446

// A 32 bit overflow spills to 64 bits
c.drop();
c.save( { a: NumberInt( "2147483647" ) } );
c.update( {}, { $inc:{ a:NumberInt( 1 ) } } );
var gle = db.getLastErrorObj();
assert.eq(1, gle.n, "Object not inserted");
var res = c.findOne();
assert.eq(NumberLong, res.a.constructor,
          "NumberInt incremented beyond std::numeric_limits<in32_t>::max() not NumberLong");
assert.eq(NumberLong("2147483648"), res.a,
          "NumberInt incremented beyond std::numeric_limits<in32_t>::max() has wrong value");

// A 32 bit underflow spills to 64 bits
c.drop();
c.save( { a: NumberInt( "-2147483648" ) } );
c.update( {}, { $inc:{ a:NumberInt( -1 ) } } );
gle = db.getLastErrorObj();
assert.eq(1, gle.n, "Object not inserted");
res = c.findOne();
assert.eq(NumberLong, res.a.constructor,
          "NumberInt decremented beyond std::numeric_limits<in32_t>::min() not NumberLong");
assert.eq(NumberLong("-2147483649"), res.a,
          "NumberInt decremented beyond std::numeric_limits<in32_t>::min() has wrong value");

// A 64 bit overflow is an error
c.drop();
c.save( { a: NumberLong( "9223372036854775807" ) } );
c.update( {}, { $inc:{ a:NumberInt( 1 ) } } );
gle = db.getLastErrorObj();
assert.eq(0, gle.n,
       "Did not fail to increment a NumberLong past std::numeric_limits<int64_t>::max()");

// A 64 bit underflow is an error
c.drop();
c.save( { a: NumberLong( "-9223372036854775808" ) } );
c.update( {}, { $inc:{ a:NumberInt( -1 ) } } );
gle = db.getLastErrorObj();
assert.eq(0, gle.n,
       "Did not fail to decrement a NumberLong past std::numeric_limits<int64_t>::min()");

c.drop()