summaryrefslogtreecommitdiff
path: root/jstests/decimal/decimal_constructors.js
blob: 8af1b6d0f626a5387272e06c99d8cb0076a3b675 (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
// Tests constructing NumberDecimal with various types

(function() {
'use strict';
var col = db.d_constructors;
col.drop();

// Insert some sample data.

assert.commandWorked(col.insert([
    {d: NumberDecimal('1')},
    {d: NumberDecimal(1)},
    {d: NumberDecimal(NumberLong('1'))},
    {d: NumberDecimal(NumberInt('1'))},
    {d: NumberDecimal('NaN')},
    {d: NumberDecimal('-NaN')}
]),
                     'Initial insertion of decimals failed');

var exactDoubleString = "1427247692705959881058285969449495136382746624";
var exactDoubleTinyString =
    "0.00000000000000000000000000000000000000000000000000000000000062230152778611417071440640537801242405902521687211671331011166147896988340353834411839448231257136169569665895551224821247160434722900390625";

assert.throws(
    NumberDecimal, [exactDoubleString], 'Unexpected success in creating invalid Decimal128');
assert.throws(
    NumberDecimal, [exactDoubleTinyString], 'Unexpected success in creating invalid Decimal128');
assert.throws(NumberDecimal, ['some garbage'], 'Unexpected success in creating invalid Decimal128');

// Find values with various types and NumberDecimal constructed types
assert.eq(col.find({'d': NumberDecimal('1')}).count(), '4');
assert.eq(col.find({'d': NumberDecimal(1)}).count(), '4');
assert.eq(col.find({'d': NumberDecimal(NumberLong(1))}).count(), '4');
assert.eq(col.find({'d': NumberDecimal(NumberInt(1))}).count(), '4');
assert.eq(col.find({'d': 1}).count(), '4');
assert.eq(col.find({'d': NumberLong(1)}).count(), '4');
assert.eq(col.find({'d': NumberInt(1)}).count(), '4');
// NaN and -NaN are both evaluated to NaN
assert.eq(col.find({'d': NumberDecimal('NaN')}).count(), 2);

// Verify that shell 'assert.eq' considers precision during comparison.
assert.neq(NumberDecimal('1'), NumberDecimal('1.000'));
assert.neq(NumberDecimal('0'), NumberDecimal('-0'));

// Verify the behavior of 'numberDecimalsEqual' helper.
assert(numberDecimalsEqual(NumberDecimal('10.20'), NumberDecimal('10.2')));
assert.throws(
    () => numberDecimalsEqual(NumberDecimal('10.20'), NumberDecimal('10.2'), "Third parameter"));
assert.throws(() => numberDecimalsEqual(NumberDecimal('10.20'), "Wrong parameter type"));
}());