summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenety Goh <benety@mongodb.com>2022-08-02 13:48:25 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-08-03 16:29:36 +0000
commit9f3fe4b1546b6b3ab09345905a3163f19771cc70 (patch)
tree668e94a613197ba1178820e4d4644ad3f6685bd3
parenteef44cd56b1cc11e5771736fa6cb3077e0228be2 (diff)
downloadmongo-9f3fe4b1546b6b3ab09345905a3163f19771cc70.tar.gz
SERVER-68487 add BSONElement::isNaN()
(cherry picked from commit ed25ebe0d87dd2521fbfc5534ba7af7de69aad4d)
-rw-r--r--src/mongo/bson/bsonelement.h20
-rw-r--r--src/mongo/bson/bsonelement_test.cpp19
2 files changed, 39 insertions, 0 deletions
diff --git a/src/mongo/bson/bsonelement.h b/src/mongo/bson/bsonelement.h
index 86bd8ed9408..84230f95615 100644
--- a/src/mongo/bson/bsonelement.h
+++ b/src/mongo/bson/bsonelement.h
@@ -317,6 +317,11 @@ public:
/** True if element is of a numeric type. */
bool isNumber() const;
+ /**
+ * True if element is a NaN double or decimal.
+ */
+ bool isNaN() const;
+
/** Return double value for this field. MUST be NumberDouble type. */
double _numberDouble() const {
return ConstDataView(value()).read<LittleEndian<double>>();
@@ -824,6 +829,21 @@ inline bool BSONElement::isNumber() const {
}
}
+inline bool BSONElement::isNaN() const {
+ switch (type()) {
+ case NumberDouble: {
+ double d = _numberDouble();
+ return std::isnan(d);
+ }
+ case NumberDecimal: {
+ Decimal128 d = _numberDecimal();
+ return d.isNaN();
+ }
+ default:
+ return false;
+ }
+}
+
inline Decimal128 BSONElement::numberDecimal() const {
switch (type()) {
case NumberDouble:
diff --git a/src/mongo/bson/bsonelement_test.cpp b/src/mongo/bson/bsonelement_test.cpp
index f98ccf93894..d47e91dab9f 100644
--- a/src/mongo/bson/bsonelement_test.cpp
+++ b/src/mongo/bson/bsonelement_test.cpp
@@ -203,6 +203,25 @@ TEST(BSONElement, SafeNumberLongNegativeBound) {
ASSERT_EQ(obj["negativeInfinity"].safeNumberLong(), std::numeric_limits<long long>::lowest());
}
+TEST(BSONElement, IsNaN) {
+ ASSERT(BSON("" << std::numeric_limits<double>::quiet_NaN()).firstElement().isNaN());
+ ASSERT(BSON("" << -std::numeric_limits<double>::quiet_NaN()).firstElement().isNaN());
+ ASSERT(BSON("" << Decimal128::kPositiveNaN).firstElement().isNaN());
+ ASSERT(BSON("" << Decimal128::kNegativeNaN).firstElement().isNaN());
+
+ ASSERT_FALSE(BSON("" << std::numeric_limits<double>::infinity()).firstElement().isNaN());
+ ASSERT_FALSE(BSON("" << -std::numeric_limits<double>::infinity()).firstElement().isNaN());
+ ASSERT_FALSE(BSON("" << Decimal128::kPositiveInfinity).firstElement().isNaN());
+ ASSERT_FALSE(BSON("" << Decimal128::kNegativeInfinity).firstElement().isNaN());
+ ASSERT_FALSE(BSON("" << Decimal128{"9223372036854775808.5"}).firstElement().isNaN());
+ ASSERT_FALSE(BSON("" << Decimal128{"-9223372036854775809.99"}).firstElement().isNaN());
+ ASSERT_FALSE(BSON("" << 12345LL).firstElement().isNaN());
+ ASSERT_FALSE(BSON(""
+ << "foo")
+ .firstElement()
+ .isNaN());
+}
+
TEST(BSONElementIntegerParseTest, ParseIntegerElementToNonNegativeLongRejectsNegative) {
BSONObj query = BSON("" << -2LL);
ASSERT_NOT_OK(query.firstElement().parseIntegerElementToNonNegativeLong());