summaryrefslogtreecommitdiff
path: root/src/mongo/bson/bson_obj_test.cpp
diff options
context:
space:
mode:
authorDavid Storch <david.storch@10gen.com>2016-09-06 18:16:43 -0400
committerDavid Storch <david.storch@10gen.com>2016-09-09 15:05:13 -0400
commit613134ad5960767276df350db1f90cc919225dd5 (patch)
treeb3af405211503717a2524813237734b3af2613a8 /src/mongo/bson/bson_obj_test.cpp
parentf6397011fa6f607a80a6bde1408bf6afddaf20a7 (diff)
downloadmongo-613134ad5960767276df350db1f90cc919225dd5.tar.gz
SERVER-23990 move BSONObj/BSONElement hashing into {BSONObj,BSONElement}::ComparatorInterface
Diffstat (limited to 'src/mongo/bson/bson_obj_test.cpp')
-rw-r--r--src/mongo/bson/bson_obj_test.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/mongo/bson/bson_obj_test.cpp b/src/mongo/bson/bson_obj_test.cpp
index bf38662a321..890d3c4580f 100644
--- a/src/mongo/bson/bson_obj_test.cpp
+++ b/src/mongo/bson/bson_obj_test.cpp
@@ -25,6 +25,10 @@
* then also delete it in the license file.
*/
+#include "mongo/bson/bsonelement_comparator.h"
+#include "mongo/bson/bsonobj_comparator.h"
+#include "mongo/bson/simple_bsonelement_comparator.h"
+#include "mongo/bson/simple_bsonobj_comparator.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/json.h"
#include "mongo/platform/decimal128.h"
@@ -509,6 +513,37 @@ TEST(BSONObjCompare, NumericBounds) {
ASSERT_GT(r.woCompare(l), 0);
}
+TEST(BSONObjCompare, BSONObjHashingIgnoresTopLevelFieldNamesWhenRequested) {
+ BSONObj obj1 = fromjson("{a: {b: 1}, c: {d: 1}}");
+ BSONObj obj2 = fromjson("{A: {b: 1}, C: {d: 1}}");
+ BSONObj obj3 = fromjson("{A: {B: 1}, C: {D: 1}}");
+
+ SimpleBSONObjComparator bsonCmpConsiderFieldNames;
+ BSONObjComparator bsonCmpIgnoreFieldNames(
+ BSONObj(), BSONObjComparator::FieldNamesMode::kIgnore, nullptr);
+
+ ASSERT_NE(bsonCmpConsiderFieldNames.hash(obj1), bsonCmpConsiderFieldNames.hash(obj2));
+ ASSERT_EQ(bsonCmpIgnoreFieldNames.hash(obj1), bsonCmpIgnoreFieldNames.hash(obj2));
+ ASSERT_NE(bsonCmpIgnoreFieldNames.hash(obj1), bsonCmpIgnoreFieldNames.hash(obj3));
+}
+
+TEST(BSONObjCompare, BSONElementHashingIgnoresEltFieldNameWhenRequested) {
+ BSONObj obj1 = fromjson("{a: {b: 1}}");
+ BSONObj obj2 = fromjson("{A: {b: 1}}");
+ BSONObj obj3 = fromjson("{A: {B: 1}}");
+
+ SimpleBSONElementComparator bsonCmpConsiderFieldNames;
+ BSONElementComparator bsonCmpIgnoreFieldNames(BSONElementComparator::FieldNamesMode::kIgnore,
+ nullptr);
+
+ ASSERT_NE(bsonCmpConsiderFieldNames.hash(obj1.firstElement()),
+ bsonCmpConsiderFieldNames.hash(obj2.firstElement()));
+ ASSERT_EQ(bsonCmpIgnoreFieldNames.hash(obj1.firstElement()),
+ bsonCmpIgnoreFieldNames.hash(obj2.firstElement()));
+ ASSERT_NE(bsonCmpIgnoreFieldNames.hash(obj1.firstElement()),
+ bsonCmpIgnoreFieldNames.hash(obj3.firstElement()));
+}
+
TEST(Looping, Cpp11Basic) {
int count = 0;
for (BSONElement e : BSON("a" << 1 << "a" << 2 << "a" << 3)) {