diff options
Diffstat (limited to 'src/mongo/dbtests/jsobjtests.cpp')
-rw-r--r-- | src/mongo/dbtests/jsobjtests.cpp | 326 |
1 files changed, 186 insertions, 140 deletions
diff --git a/src/mongo/dbtests/jsobjtests.cpp b/src/mongo/dbtests/jsobjtests.cpp index 13579765dfb..ee99ae37d3b 100644 --- a/src/mongo/dbtests/jsobjtests.cpp +++ b/src/mongo/dbtests/jsobjtests.cpp @@ -501,7 +501,8 @@ public: ASSERT(BSON("" << "b" << "" - << "h").woSortOrder(o, key) < 0); + << "h") + .woSortOrder(o, key) < 0); } ASSERT(BSON("" @@ -754,135 +755,131 @@ public: keyTest(BSON("" << now << "" << 3 << "" << jstNULL << "" << true)); keyTest(BSON("" << now << "" << 3 << "" << BSONObj() << "" << true)); + {{// check signed dates with new key format + KeyV1Owned a(BSONObjBuilder().appendDate("", Date_t::fromMillisSinceEpoch(-50)).obj()); + KeyV1Owned b(BSONObjBuilder().appendDate("", Date_t::fromMillisSinceEpoch(50)).obj()); + ASSERT(a.woCompare(b, Ordering::make(BSONObj())) < 0); + } + { + // backward compatibility + KeyBson a(BSONObjBuilder().appendDate("", Date_t::fromMillisSinceEpoch(-50)).obj()); + KeyBson b(BSONObjBuilder().appendDate("", Date_t::fromMillisSinceEpoch(50)).obj()); + ASSERT(a.woCompare(b, Ordering::make(BSONObj())) > 0); + } + { + // this is an uncompactable key: + BSONObj uc1 = BSONObjBuilder() + .appendDate("", Date_t::fromMillisSinceEpoch(-50)) + .appendCode("", "abc") + .obj(); + BSONObj uc2 = BSONObjBuilder() + .appendDate("", Date_t::fromMillisSinceEpoch(55)) + .appendCode("", "abc") + .obj(); + ASSERT(uc1.woCompare(uc2, Ordering::make(BSONObj())) < 0); { - { - // check signed dates with new key format - KeyV1Owned a( - BSONObjBuilder().appendDate("", Date_t::fromMillisSinceEpoch(-50)).obj()); - KeyV1Owned b( - BSONObjBuilder().appendDate("", Date_t::fromMillisSinceEpoch(50)).obj()); - ASSERT(a.woCompare(b, Ordering::make(BSONObj())) < 0); - } - { - // backward compatibility - KeyBson a(BSONObjBuilder().appendDate("", Date_t::fromMillisSinceEpoch(-50)).obj()); - KeyBson b(BSONObjBuilder().appendDate("", Date_t::fromMillisSinceEpoch(50)).obj()); - ASSERT(a.woCompare(b, Ordering::make(BSONObj())) > 0); - } - { - // this is an uncompactable key: - BSONObj uc1 = BSONObjBuilder() - .appendDate("", Date_t::fromMillisSinceEpoch(-50)) - .appendCode("", "abc") - .obj(); - BSONObj uc2 = BSONObjBuilder() - .appendDate("", Date_t::fromMillisSinceEpoch(55)) - .appendCode("", "abc") - .obj(); - ASSERT(uc1.woCompare(uc2, Ordering::make(BSONObj())) < 0); - { - KeyV1Owned a(uc1); - KeyV1Owned b(uc2); - ASSERT(!a.isCompactFormat()); - ASSERT(a.woCompare(b, Ordering::make(BSONObj())) < 0); - } - { - KeyBson a(uc1); - KeyBson b(uc2); - ASSERT(!a.isCompactFormat()); - ASSERT(a.woCompare(b, Ordering::make(BSONObj())) > 0); - } - } + KeyV1Owned a(uc1); + KeyV1Owned b(uc2); + ASSERT(!a.isCompactFormat()); + ASSERT(a.woCompare(b, Ordering::make(BSONObj())) < 0); } - { - BSONObjBuilder b; - b.appendBinData("f", 8, (BinDataType)1, "aaaabbbb"); - b.appendBinData("e", 3, (BinDataType)1, "aaa"); - b.appendBinData("b", 1, (BinDataType)1, "x"); - BSONObj o = b.obj(); - keyTest(o, true); + KeyBson a(uc1); + KeyBson b(uc2); + ASSERT(!a.isCompactFormat()); + ASSERT(a.woCompare(b, Ordering::make(BSONObj())) > 0); } + } +} - { - // check (non)equality - BSONObj a = BSONObjBuilder().appendBinData("", 8, (BinDataType)1, "abcdefgh").obj(); - BSONObj b = BSONObjBuilder().appendBinData("", 8, (BinDataType)1, "abcdefgj").obj(); - ASSERT(!a.equal(b)); - int res_ab = a.woCompare(b); - ASSERT(res_ab != 0); - keyTest(a, true); - keyTest(b, true); - - // check subtypes do not equal - BSONObj c = BSONObjBuilder().appendBinData("", 8, (BinDataType)4, "abcdefgh").obj(); - BSONObj d = BSONObjBuilder().appendBinData("", 8, (BinDataType)0x81, "abcdefgh").obj(); - ASSERT(!a.equal(c)); - int res_ac = a.woCompare(c); - ASSERT(res_ac != 0); - keyTest(c, true); - ASSERT(!a.equal(d)); - int res_ad = a.woCompare(d); - ASSERT(res_ad != 0); - keyTest(d, true); - - KeyV1Owned A(a); - KeyV1Owned B(b); - KeyV1Owned C(c); - KeyV1Owned D(d); - ASSERT(!A.woEqual(B)); - ASSERT(A.woCompare(B, Ordering::make(BSONObj())) < 0 && res_ab < 0); - ASSERT(!A.woEqual(C)); - ASSERT(A.woCompare(C, Ordering::make(BSONObj())) < 0 && res_ac < 0); - ASSERT(!A.woEqual(D)); - ASSERT(A.woCompare(D, Ordering::make(BSONObj())) < 0 && res_ad < 0); - } +{ + BSONObjBuilder b; + b.appendBinData("f", 8, (BinDataType)1, "aaaabbbb"); + b.appendBinData("e", 3, (BinDataType)1, "aaa"); + b.appendBinData("b", 1, (BinDataType)1, "x"); + BSONObj o = b.obj(); + keyTest(o, true); +} - { - BSONObjBuilder b; - b.appendBinData("f", 33, (BinDataType)1, "123456789012345678901234567890123"); - BSONObj o = b.obj(); - keyTest(o, false); - } +{ + // check (non)equality + BSONObj a = BSONObjBuilder().appendBinData("", 8, (BinDataType)1, "abcdefgh").obj(); + BSONObj b = BSONObjBuilder().appendBinData("", 8, (BinDataType)1, "abcdefgj").obj(); + ASSERT(!a.equal(b)); + int res_ab = a.woCompare(b); + ASSERT(res_ab != 0); + keyTest(a, true); + keyTest(b, true); + + // check subtypes do not equal + BSONObj c = BSONObjBuilder().appendBinData("", 8, (BinDataType)4, "abcdefgh").obj(); + BSONObj d = BSONObjBuilder().appendBinData("", 8, (BinDataType)0x81, "abcdefgh").obj(); + ASSERT(!a.equal(c)); + int res_ac = a.woCompare(c); + ASSERT(res_ac != 0); + keyTest(c, true); + ASSERT(!a.equal(d)); + int res_ad = a.woCompare(d); + ASSERT(res_ad != 0); + keyTest(d, true); + + KeyV1Owned A(a); + KeyV1Owned B(b); + KeyV1Owned C(c); + KeyV1Owned D(d); + ASSERT(!A.woEqual(B)); + ASSERT(A.woCompare(B, Ordering::make(BSONObj())) < 0 && res_ab < 0); + ASSERT(!A.woEqual(C)); + ASSERT(A.woCompare(C, Ordering::make(BSONObj())) < 0 && res_ac < 0); + ASSERT(!A.woEqual(D)); + ASSERT(A.woCompare(D, Ordering::make(BSONObj())) < 0 && res_ad < 0); +} - { - for (int i = 1; i <= 3; i++) { - for (int j = 1; j <= 3; j++) { - BSONObjBuilder b; - b.appendBinData("f", i, (BinDataType)j, "abc"); - BSONObj o = b.obj(); - keyTest(o, j != ByteArrayDeprecated); - } - } - } +{ + BSONObjBuilder b; + b.appendBinData("f", 33, (BinDataType)1, "123456789012345678901234567890123"); + BSONObj o = b.obj(); + keyTest(o, false); +} - { +{ + for (int i = 1; i <= 3; i++) { + for (int j = 1; j <= 3; j++) { BSONObjBuilder b; - b.appendBinData("f", 1, (BinDataType)133, "a"); + b.appendBinData("f", i, (BinDataType)j, "abc"); BSONObj o = b.obj(); - keyTest(o, true); + keyTest(o, j != ByteArrayDeprecated); } + } +} - { - BSONObjBuilder b; - b.append("AA", 3); - b.appendBinData("f", 0, (BinDataType)0, ""); - b.appendBinData("e", 3, (BinDataType)7, "aaa"); - b.appendBinData("b", 1, (BinDataType)128, "x"); - b.append("z", 3); - b.appendBinData("bb", 0, (BinDataType)129, "x"); - BSONObj o = b.obj(); - keyTest(o, true); - } +{ + BSONObjBuilder b; + b.appendBinData("f", 1, (BinDataType)133, "a"); + BSONObj o = b.obj(); + keyTest(o, true); +} - { - // 9 is not supported in compact format. so test a non-compact case here. - BSONObjBuilder b; - b.appendBinData("f", 9, (BinDataType)0, "aaaabbbbc"); - BSONObj o = b.obj(); - keyTest(o); - } - } +{ + BSONObjBuilder b; + b.append("AA", 3); + b.appendBinData("f", 0, (BinDataType)0, ""); + b.appendBinData("e", 3, (BinDataType)7, "aaa"); + b.appendBinData("b", 1, (BinDataType)128, "x"); + b.append("z", 3); + b.appendBinData("bb", 0, (BinDataType)129, "x"); + BSONObj o = b.obj(); + keyTest(o, true); +} + +{ + // 9 is not supported in compact format. so test a non-compact case here. + BSONObjBuilder b; + b.appendBinData("f", 9, (BinDataType)0, "aaaabbbbc"); + BSONObj o = b.obj(); + keyTest(o); +} +} }; class ToStringNumber { @@ -1452,13 +1449,18 @@ class LabelShares : public LabelBase { BSONObj expected() { return BSON("z" << "q" - << "a" << (BSON("$gt" << 1)) << "x" + << "a" + << (BSON("$gt" << 1)) + << "x" << "p"); } BSONObj actual() { return BSON("z" << "q" - << "a" << GT << 1 << "x" + << "a" + << GT + << 1 + << "x" << "p"); } }; @@ -1477,14 +1479,20 @@ class LabelDoubleShares : public LabelBase { BSONObj expected() { return BSON("z" << "q" - << "a" << (BSON("$gt" << 1 << "$lte" - << "x")) << "x" + << "a" + << (BSON("$gt" << 1 << "$lte" + << "x")) + << "x" << "p"); } BSONObj actual() { return BSON("z" << "q" - << "a" << GT << 1 << LTE << "x" + << "a" + << GT + << 1 + << LTE + << "x" << "x" << "p"); } @@ -1503,17 +1511,33 @@ class LabelMulti : public LabelBase { BSONObj expected() { return BSON("z" << "q" - << "a" << BSON("$gt" << 1 << "$lte" - << "x") << "b" << BSON("$ne" << 1 << "$ne" - << "f" - << "$ne" << 22.3) << "x" + << "a" + << BSON("$gt" << 1 << "$lte" + << "x") + << "b" + << BSON("$ne" << 1 << "$ne" + << "f" + << "$ne" + << 22.3) + << "x" << "p"); } BSONObj actual() { return BSON("z" << "q" - << "a" << GT << 1 << LTE << "x" - << "b" << NE << 1 << NE << "f" << NE << 22.3 << "x" + << "a" + << GT + << 1 + << LTE + << "x" + << "b" + << NE + << 1 + << NE + << "f" + << NE + << 22.3 + << "x" << "p"); } }; @@ -1523,7 +1547,8 @@ class LabelishOr : public LabelBase { << "x")) << BSON("b" << BSON("$ne" << 1 << "$ne" << "f" - << "$ne" << 22.3)) + << "$ne" + << 22.3)) << BSON("x" << "p"))); } @@ -1960,9 +1985,12 @@ struct ArrayMacroTest { << "qux"))); BSONObj obj = BSON("0" << "hello" - << "1" << 1 << "2" << BSON("foo" << BSON_ARRAY("bar" - << "baz" - << "qux"))); + << "1" + << 1 + << "2" + << BSON("foo" << BSON_ARRAY("bar" + << "baz" + << "qux"))); ASSERT_EQUALS(arr, obj); ASSERT_EQUALS(arr["2"].type(), Object); @@ -2071,26 +2099,38 @@ public: // DBRef stuff -- json parser can't handle this yet good(BSON("a" << BSON("$ref" << "coll" - << "$id" << 1))); + << "$id" + << 1))); good(BSON("a" << BSON("$ref" << "coll" - << "$id" << 1 << "$db" + << "$id" + << 1 + << "$db" << "a"))); good(BSON("a" << BSON("$ref" << "coll" - << "$id" << 1 << "stuff" << 1))); + << "$id" + << 1 + << "stuff" + << 1))); good(BSON("a" << BSON("$ref" << "coll" - << "$id" << 1 << "$db" + << "$id" + << 1 + << "$db" << "a" - << "stuff" << 1))); + << "stuff" + << 1))); bad(BSON("a" << BSON("$ref" << 1 << "$id" << 1))); bad(BSON("a" << BSON("$ref" << 1 << "$id" << 1 << "$db" << "a"))); bad(BSON("a" << BSON("$ref" << "coll" - << "$id" << 1 << "$db" << 1))); + << "$id" + << 1 + << "$db" + << 1))); bad(BSON("a" << BSON("$ref" << "coll"))); bad(BSON("a" << BSON("$ref" @@ -2102,10 +2142,16 @@ public: << "coll"))); bad(BSON("a" << BSON("$ref" << "coll" - << "$id" << 1 << "$hater" << 1))); + << "$id" + << 1 + << "$hater" + << 1))); bad(BSON("a" << BSON("$ref" << "coll" - << "$id" << 1 << "dot.dot" << 1))); + << "$id" + << 1 + << "dot.dot" + << 1))); // _id isn't a RegEx, or Array good("{_id: 0}"); |