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-10-08 13:18:02 +0000
commiteda5c7c568a611394135e3c8c0b06ff23ee4818d (patch)
tree3758a0087ffb8e959a4677ee865c3c881417539e
parent58971da1ef93435a9f62bf4708a81713def6e88c (diff)
downloadmongo-eda5c7c568a611394135e3c8c0b06ff23ee4818d.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 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