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-07-05 10:06:02 +0000
commit66f3c41018ac1850c98c0bef87eda74ff95a00de (patch)
tree88340f026cbab22ac03bfedc2d6f471a4d4f950b
parentb76f72590c66064f3fd69fc73c79636ce95566cf (diff)
downloadmongo-66f3c41018ac1850c98c0bef87eda74ff95a00de.tar.gz
SERVER-57605 Expose Decimal128 equality comparison helper to shell
-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 c0d256c5d21..321155d8b8e 100644
--- a/src/mongo/shell/shell_utils.cpp
+++ b/src/mongo/shell/shell_utils.cpp
@@ -454,6 +454,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);
@@ -467,6 +480,7 @@ void installShellUtils(Scope& scope) {
scope.injectNative("convertShardKeyToHashed", convertShardKeyToHashed);
scope.injectNative("fileExists", fileExistsJS);
scope.injectNative("isInteractive", isInteractive);
+ scope.injectNative("numberDecimalsEqual", numberDecimalsEqual);
installShellUtilsLauncher(scope);
installShellUtilsExtended(scope);
diff --git a/src/mongo/shell/types.js b/src/mongo/shell/types.js
index 85e4dce9a55..571f40d8af6 100644
--- a/src/mongo/shell/types.js
+++ b/src/mongo/shell/types.js
@@ -395,6 +395,10 @@ if (typeof NumberDecimal !== 'undefined') {
NumberDecimal.prototype.tojson = function() {
return this.toString();
};
+
+ NumberDecimal.prototype.equals = function(other) {
+ return compareNumberDecimals(this, other);
+ };
}
// ObjectId