diff options
author | Vincent Do <do.vincent@live.com> | 2016-05-18 16:00:10 -0400 |
---|---|---|
committer | Vincent Do <vincent.do@mongodb.com> | 2016-05-27 10:31:19 -0400 |
commit | ba23c68f0bf0a594a4af7080900ea03bda133fa4 (patch) | |
tree | 37d2a1f38df2f00615c8898dc395a524c564eeb1 /jstests/decimal | |
parent | 9b3fc54ceb6d1066e423f7c562c22de2f5c6480e (diff) | |
download | mongo-ba23c68f0bf0a594a4af7080900ea03bda133fa4.tar.gz |
SERVER-23703 Make NumberDecimal accepts only valid strings
Diffstat (limited to 'jstests/decimal')
-rw-r--r-- | jstests/decimal/decimal_constructors.js | 44 | ||||
-rw-r--r-- | jstests/decimal/decimal_find_basic.js | 4 | ||||
-rw-r--r-- | jstests/decimal/decimal_find_mixed.js | 22 | ||||
-rw-r--r-- | jstests/decimal/decimal_find_query.js | 2 |
4 files changed, 33 insertions, 39 deletions
diff --git a/jstests/decimal/decimal_constructors.js b/jstests/decimal/decimal_constructors.js index d83568faf39..dd8e6cfef75 100644 --- a/jstests/decimal/decimal_constructors.js +++ b/jstests/decimal/decimal_constructors.js @@ -1,26 +1,42 @@ // Tests constructing NumberDecimal with various types (function() { - "use strict"; - var col = db.decimal_constructors; + 'use strict'; + var col = db.d_constructors; col.drop(); // Insert some sample data. assert.writeOK(col.insert([ - {"decimal": NumberDecimal("1")}, - {"decimal": NumberDecimal(1)}, - {"decimal": NumberDecimal(NumberLong("1"))}, - {"decimal": NumberDecimal(NumberInt("1"))}, + {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"); + '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({"decimal": NumberDecimal("1")}).count(), "4"); - assert.eq(col.find({"decimal": NumberDecimal(1)}).count(), "4"); - assert.eq(col.find({"decimal": NumberDecimal(NumberLong(1))}).count(), "4"); - assert.eq(col.find({"decimal": NumberDecimal(NumberInt(1))}).count(), "4"); - assert.eq(col.find({"decimal": 1}).count(), "4"); - assert.eq(col.find({"decimal": NumberLong(1)}).count(), "4"); - assert.eq(col.find({"decimal": NumberInt(1)}).count(), "4"); + 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); }()); diff --git a/jstests/decimal/decimal_find_basic.js b/jstests/decimal/decimal_find_basic.js index 6527957a93f..a6a92419616 100644 --- a/jstests/decimal/decimal_find_basic.js +++ b/jstests/decimal/decimal_find_basic.js @@ -45,11 +45,11 @@ // Maximum and Minimum Values assert.writeOK(col.insert([ - {"max": NumberDecimal("9999999999999999999999999999999999E6144")}, + {"max": NumberDecimal("9999999999999999999999999999999999E6111")}, {"min": NumberDecimal("1E-6176")} ])); - assert.eq(col.find({"max": NumberDecimal("9999999999999999999999999999999999E6144")}).count(), + assert.eq(col.find({"max": NumberDecimal("9999999999999999999999999999999999E6111")}).count(), 1); assert.eq(col.find({"min": NumberDecimal("1E-6176")}).count(), 1); }()); diff --git a/jstests/decimal/decimal_find_mixed.js b/jstests/decimal/decimal_find_mixed.js index 02efc213656..078ada3ad0f 100644 --- a/jstests/decimal/decimal_find_mixed.js +++ b/jstests/decimal/decimal_find_mixed.js @@ -67,26 +67,4 @@ // Not representable as double assert.eq(col.find({"a": 36028797018963967}).count(), 0, "E2"); assert.eq(col.find({"a": NumberLong("36028797018963967")}).count(), 1, "E3"); - - // Doubles not representable as decimal - col.drop(); - var exactDouble = 1427247692705959881058285969449495136382746624; - var exactDoubleString = "1427247692705959881058285969449495136382746624"; - - assert.writeOK(col.insert([{"a": exactDouble} // Exact double (46 digits) - ], - "4 insertion failed")); - - assert.eq(col.find({"a": NumberDecimal(exactDoubleString)}).count(), 0, "F1"); - assert.eq(col.find({"a": {$gt: NumberDecimal(exactDoubleString)}}).count(), 1, "E2"); - - var exactDoubleTiny = 1 / 1606938044258990275541962092341162602522202993782792835301376; - var exactDoubleTinyString = - "0.00000000000000000000000000000000000000000000000000000000000062230152778611417071440640537801242405902521687211671331011166147896988340353834411839448231257136169569665895551224821247160434722900390625"; - - col.drop(); - assert.writeOK(col.insert([{"a": exactDoubleTiny}], "5 insertion failed")); - - assert.eq(col.find({"a": NumberDecimal(exactDoubleTinyString)}).count(), 0, "F1"); - assert.eq(col.find({"a": {$gt: NumberDecimal(exactDoubleTinyString)}}).count(), 1, "F2"); }()); diff --git a/jstests/decimal/decimal_find_query.js b/jstests/decimal/decimal_find_query.js index dbdc3b69d0c..47e5c7167d0 100644 --- a/jstests/decimal/decimal_find_query.js +++ b/jstests/decimal/decimal_find_query.js @@ -14,7 +14,7 @@ {'decimal': NumberDecimal('1.0')}, {'decimal': NumberDecimal('1.00')}, {'decimal': NumberDecimal('2.00')}, - {'decimal': NumberDecimal('1234567890123456789012.12345678901234')}, + {'decimal': NumberDecimal('12345678901234.56789012345678901234')}, {'decimal': NumberDecimal('NaN')}, {'decimal': NumberDecimal('-NaN')}, {'decimal': NumberDecimal('Infinity')}, |