From eda5c7c568a611394135e3c8c0b06ff23ee4818d Mon Sep 17 00:00:00 2001 From: Arun Banala Date: Thu, 20 May 2021 12:55:12 +0100 Subject: SERVER-57605 Expose Decimal128 equality comparison helper to shell (cherry picked from commit 66f3c41018ac1850c98c0bef87eda74ff95a00de) --- jstests/decimal/decimal_constructors.js | 10 ++++++++++ src/mongo/shell/shell_utils.cpp | 14 ++++++++++++++ src/mongo/shell/types.js | 4 ++++ 3 files changed, 28 insertions(+) diff --git a/jstests/decimal/decimal_constructors.js b/jstests/decimal/decimal_constructors.js index 387a1bee1a9..8af1b6d0f62 100644 --- a/jstests/decimal/decimal_constructors.js +++ b/jstests/decimal/decimal_constructors.js @@ -37,4 +37,14 @@ 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")); }()); diff --git a/src/mongo/shell/shell_utils.cpp b/src/mongo/shell/shell_utils.cpp index 84e4c1ef4fd..396ca6ab655 100644 --- a/src/mongo/shell/shell_utils.cpp +++ b/src/mongo/shell/shell_utils.cpp @@ -461,6 +461,19 @@ BSONObj isInteractive(const BSONObj& a, void*) { return BSON("" << shellGlobalParams.runShell); } +BSONObj numberDecimalsEqual(const BSONObj& input, void*) { + uassert(5760500, "numberDecimalsEqual expects two arguments", input.nFields() == 2); + + BSONObjIterator i(input); + auto first = i.next(); + auto second = i.next(); + uassert(5760501, + "Both the arguments of numberDecimalsEqual should be of type 'NumberDecimal'", + first.type() == BSONType::NumberDecimal && second.type() == BSONType::NumberDecimal); + + return BSON("" << first.numberDecimal().isEqual(second.numberDecimal())); +} + void installShellUtils(Scope& scope) { scope.injectNative("getMemInfo", JSGetMemInfo); scope.injectNative("_replMonitorStats", replMonitorStats); @@ -474,6 +487,7 @@ void installShellUtils(Scope& scope) { scope.injectNative("convertShardKeyToHashed", convertShardKeyToHashed); scope.injectNative("fileExists", fileExistsJS); scope.injectNative("isInteractive", isInteractive); + scope.injectNative("numberDecimalsEqual", numberDecimalsEqual); #ifndef MONGO_SAFE_SHELL // can't launch programs diff --git a/src/mongo/shell/types.js b/src/mongo/shell/types.js index faaa5d00499..c371c3a6b95 100644 --- a/src/mongo/shell/types.js +++ b/src/mongo/shell/types.js @@ -389,6 +389,10 @@ if (typeof NumberDecimal !== 'undefined') { NumberDecimal.prototype.tojson = function() { return this.toString(); }; + + NumberDecimal.prototype.equals = function(other) { + return compareNumberDecimals(this, other); + }; } // ObjectId -- cgit v1.2.1