diff options
author | Benety Goh <benety@mongodb.com> | 2022-08-02 13:48:25 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-08-02 15:29:23 +0000 |
commit | ed25ebe0d87dd2521fbfc5534ba7af7de69aad4d (patch) | |
tree | 7a4904289fc5ffcf183f302434a0cea16b7151ae /src/mongo | |
parent | fc5d35bb08d81f0cd165296760c4ab7506fcff1c (diff) | |
download | mongo-ed25ebe0d87dd2521fbfc5534ba7af7de69aad4d.tar.gz |
SERVER-68487 add BSONElement::isNaN()
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/bson/bsonelement.h | 20 | ||||
-rw-r--r-- | src/mongo/bson/bsonelement_test.cpp | 19 |
2 files changed, 39 insertions, 0 deletions
diff --git a/src/mongo/bson/bsonelement.h b/src/mongo/bson/bsonelement.h index e0b3b8e4414..1d31c647901 100644 --- a/src/mongo/bson/bsonelement.h +++ b/src/mongo/bson/bsonelement.h @@ -373,6 +373,11 @@ public: 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 { @@ -1005,6 +1010,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 8a2a7c3fc52..4e5bc3f6f07 100644 --- a/src/mongo/bson/bsonelement_test.cpp +++ b/src/mongo/bson/bsonelement_test.cpp @@ -283,6 +283,25 @@ TEST(BSONElement, SafeNumberDoubleNegativeBound) { (double)BSONElement::kSmallestSafeLongLongAsDouble); } +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()); |