diff options
-rw-r--r-- | db/jsobj.cpp | 20 | ||||
-rw-r--r-- | dbtests/jsobjtests.cpp | 26 |
2 files changed, 44 insertions, 2 deletions
diff --git a/db/jsobj.cpp b/db/jsobj.cpp index 0b9e086b217..2da9ce37be2 100644 --- a/db/jsobj.cpp +++ b/db/jsobj.cpp @@ -455,10 +455,26 @@ namespace mongo { return -1; return l.date() == r.date() ? 0 : 1; case NumberInt: - case NumberDouble: - x = l.number() - r.number(); + case NumberDouble: { + double left = l.number(); + double right = r.number(); + bool lNan = !( left <= numeric_limits< double >::max() && + left >= -numeric_limits< double >::max() ); + bool rNan = !( right <= numeric_limits< double >::max() && + right >= -numeric_limits< double >::max() ); + if ( lNan ) { + if ( rNan ) { + return 0; + } else { + return -1; + } + } else if ( rNan ) { + return 1; + } + x = left - right; if ( x < 0 ) return -1; return x == 0 ? 0 : 1; + } case jstOID: return memcmp(l.value(), r.value(), 12); case Code: diff --git a/dbtests/jsobjtests.cpp b/dbtests/jsobjtests.cpp index f004ec23f71..e157f956152 100644 --- a/dbtests/jsobjtests.cpp +++ b/dbtests/jsobjtests.cpp @@ -175,6 +175,31 @@ namespace JsobjTests { } }; + class Nan : public Base { + public: + void run() { + double inf = numeric_limits< double >::infinity(); + double nan = numeric_limits< double >::quiet_NaN(); + double nan2 = numeric_limits< double >::signaling_NaN(); + + ASSERT( BSON( "a" << inf ).woCompare( BSON( "a" << inf ) ) == 0 ); + ASSERT( BSON( "a" << inf ).woCompare( BSON( "a" << 1 ) ) < 0 ); + ASSERT( BSON( "a" << 1 ).woCompare( BSON( "a" << inf ) ) > 0 ); + + ASSERT( BSON( "a" << nan ).woCompare( BSON( "a" << nan ) ) == 0 ); + ASSERT( BSON( "a" << nan ).woCompare( BSON( "a" << 1 ) ) < 0 ); + ASSERT( BSON( "a" << 1 ).woCompare( BSON( "a" << nan ) ) > 0 ); + + ASSERT( BSON( "a" << nan2 ).woCompare( BSON( "a" << nan2 ) ) == 0 ); + ASSERT( BSON( "a" << nan2 ).woCompare( BSON( "a" << 1 ) ) < 0 ); + ASSERT( BSON( "a" << 1 ).woCompare( BSON( "a" << nan2 ) ) > 0 ); + + ASSERT( BSON( "a" << inf ).woCompare( BSON( "a" << nan ) ) == 0 ); + ASSERT( BSON( "a" << inf ).woCompare( BSON( "a" << nan2 ) ) == 0 ); + ASSERT( BSON( "a" << nan ).woCompare( BSON( "a" << nan2 ) ) == 0 ); + } + }; + namespace Validation { class Base { @@ -619,6 +644,7 @@ namespace JsobjTests { add< BSONObjTests::WoCompareDifferentLength >(); add< BSONObjTests::WoSortOrder >(); add< BSONObjTests::TimestampTest >(); + add< BSONObjTests::Nan >(); add< BSONObjTests::Validation::BadType >(); add< BSONObjTests::Validation::EooBeforeEnd >(); add< BSONObjTests::Validation::Undefined >(); |