diff options
-rw-r--r-- | src/mongo/bson/mutable/document.cpp | 8 | ||||
-rw-r--r-- | src/mongo/bson/mutable/mutable_bson_test.cpp | 62 |
2 files changed, 61 insertions, 9 deletions
diff --git a/src/mongo/bson/mutable/document.cpp b/src/mongo/bson/mutable/document.cpp index 1df190cc613..f8cee4b8913 100644 --- a/src/mongo/bson/mutable/document.cpp +++ b/src/mongo/bson/mutable/document.cpp @@ -1344,6 +1344,10 @@ namespace mutablebson { return fnamesComp; } + const bool considerChildFieldNames = + (impl.getType(thisRep) != mongo::Array) && + (oimpl.getType(otherRep) != mongo::Array); + // We are dealing with either two objects, or two arrays. We need to consider the child // elements individually. We walk two iterators forward over the children and compare // them. Length mismatches are handled by checking early for reaching the end of the @@ -1351,10 +1355,6 @@ namespace mutablebson { ConstElement thisIter = leftChild(); ConstElement otherIter = other.leftChild(); - const bool considerChildFieldNames = - (impl.getType(thisRep) != mongo::Array) && - (impl.getType(otherRep) != mongo::Array); - while (true) { if (!thisIter.ok()) return !otherIter.ok() ? 0 : -1; diff --git a/src/mongo/bson/mutable/mutable_bson_test.cpp b/src/mongo/bson/mutable/mutable_bson_test.cpp index 5b0f5436966..a4a11b4dcda 100644 --- a/src/mongo/bson/mutable/mutable_bson_test.cpp +++ b/src/mongo/bson/mutable/mutable_bson_test.cpp @@ -373,8 +373,8 @@ namespace { mmb::Element foo = doc.root().rightChild(); ASSERT_TRUE(foo.ok()); ASSERT_OK(foo.remove()); - ASSERT_OK(doc.root().appendString("bar", "bar")); - mmb::Element bar = doc.root().rightChild(); + ASSERT_FALSE(foo.parent().ok()); + mmb::Element bar = doc.makeElementString("bar", "bar"); ASSERT_TRUE(bar.ok()); ASSERT_NOT_OK(foo.addSiblingLeft(bar)); } @@ -385,8 +385,8 @@ namespace { mmb::Element foo = doc.root().rightChild(); ASSERT_TRUE(foo.ok()); ASSERT_OK(foo.remove()); - ASSERT_OK(doc.root().appendString("bar", "bar")); - mmb::Element bar = doc.root().rightChild(); + ASSERT_FALSE(foo.parent().ok()); + mmb::Element bar = doc.makeElementString("bar", "bar"); ASSERT_TRUE(bar.ok()); ASSERT_NOT_OK(foo.addSiblingRight(bar)); } @@ -580,7 +580,7 @@ namespace { docChild = docChild.rightSibling(); ASSERT_TRUE(docChild.ok()); ASSERT_TRUE(iter.more()); - ASSERT_EQUALS(iter.next().toString(), docChild.toString()); + ASSERT_EQUALS(iter.next().toString(), mmb::ConstElement(docChild).toString()); // 'c' docChild = docChild.rightSibling(); @@ -2700,4 +2700,56 @@ namespace { // ASSERT_EQUALS(value1, x.getValueDouble()); } + TEST(DocumentComparison, SimpleComparison) { + const mongo::BSONObj obj = + mongo::fromjson("{ a : 'a', b : ['b', 'b', 'b'], c : { one : 1.0 } }"); + + const mmb::Document doc1(obj.getOwned()); + ASSERT_EQUALS(0, doc1.compareWithBSONObj(obj)); + const mmb::Document doc2(obj.getOwned()); + ASSERT_EQUALS(0, doc1.compareWith(doc2)); + ASSERT_EQUALS(0, doc2.compareWith(doc1)); + } + + TEST(DocumentComparison, SimpleComparisonWithDeserializedElements) { + const mongo::BSONObj obj = + mongo::fromjson("{ a : 'a', b : ['b', 'b', 'b'], c : { one : 1.0 } }"); + + // Perform an operation on 'b' that doesn't change the serialized value, but + // deserializes the node. + mmb::Document doc1(obj.getOwned()); + const mmb::Document doc1Copy(obj.getOwned()); + mmb::Element b = doc1.root()["b"]; + ASSERT_TRUE(b.ok()); + mmb::Element b0 = b[0]; + ASSERT_TRUE(b0.ok()); + ASSERT_OK(b0.remove()); + ASSERT_OK(b.pushBack(b0)); + // Ensure that it compares correctly against the source object. + ASSERT_EQUALS(0, doc1.compareWithBSONObj(obj)); + // Ensure that it compares correctly against a pristine document. + ASSERT_EQUALS(0, doc1.compareWith(doc1Copy)); + ASSERT_EQUALS(0, doc1Copy.compareWith(doc1)); + + // Perform an operation on 'c' that doesn't change the serialized value, but + // deserializeds the node. + mmb::Document doc2(obj.getOwned()); + const mmb::Document doc2Copy(obj.getOwned()); + mmb::Element c = doc2.root()["c"]; + ASSERT_TRUE(c.ok()); + mmb::Element c1 = c.leftChild(); + ASSERT_TRUE(c1.ok()); + ASSERT_OK(c1.remove()); + ASSERT_OK(c.pushBack(c1)); + // Ensure that it compares correctly against the source object + ASSERT_EQUALS(0, doc2.compareWithBSONObj(obj)); + // Ensure that it compares correctly against a pristine document. + ASSERT_EQUALS(0, doc2.compareWith(doc2Copy)); + ASSERT_EQUALS(0, doc2Copy.compareWith(doc2)); + + // Ensure that the two deserialized documents compare with each other correctly. + ASSERT_EQUALS(0, doc1.compareWith(doc2)); + ASSERT_EQUALS(0, doc2.compareWith(doc1)); + } + } // namespace |