summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArun Banala <arun.banala@mongodb.com>2021-05-20 12:55:12 +0100
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-11-03 14:25:28 +0000
commitf671990a519d035566aef84df2bae540d0f085a1 (patch)
treedecac8727185d22f95a142c25ee2b4c44a466b8c
parentc6902fc64ef1f8870fb172ee655dc37ef1d105b8 (diff)
downloadmongo-f671990a519d035566aef84df2bae540d0f085a1.tar.gz
SERVER-57605 Expose Decimal128 equality comparison helper to shell
(cherry picked from commit 66f3c41018ac1850c98c0bef87eda74ff95a00de)
-rw-r--r--jstests/decimal/decimal_constructors.js10
-rw-r--r--src/mongo/shell/shell_utils.cpp14
-rw-r--r--src/mongo/shell/types.js4
3 files changed, 28 insertions, 0 deletions
diff --git a/jstests/decimal/decimal_constructors.js b/jstests/decimal/decimal_constructors.js
index 93a5bfe9703..b73db321267 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 6a4747d6c45..992f658f5f5 100644
--- a/src/mongo/shell/shell_utils.cpp
+++ b/src/mongo/shell/shell_utils.cpp
@@ -360,6 +360,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);
@@ -373,6 +386,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