summaryrefslogtreecommitdiff
path: root/src/mongo/bson/bsonobjbuilder_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/bson/bsonobjbuilder_test.cpp')
-rw-r--r--src/mongo/bson/bsonobjbuilder_test.cpp400
1 files changed, 202 insertions, 198 deletions
diff --git a/src/mongo/bson/bsonobjbuilder_test.cpp b/src/mongo/bson/bsonobjbuilder_test.cpp
index d18fb900537..0f87d966fd8 100644
--- a/src/mongo/bson/bsonobjbuilder_test.cpp
+++ b/src/mongo/bson/bsonobjbuilder_test.cpp
@@ -38,226 +38,230 @@
namespace {
- using std::string;
- using std::stringstream;
- using mongo::BSONElement;
- using mongo::BSONObj;
- using mongo::BSONObjBuilder;
- using mongo::BSONType;
+using std::string;
+using std::stringstream;
+using mongo::BSONElement;
+using mongo::BSONObj;
+using mongo::BSONObjBuilder;
+using mongo::BSONType;
- const long long maxEncodableInt = (1 << 30) - 1;
- const long long minEncodableInt = -maxEncodableInt;
+const long long maxEncodableInt = (1 << 30) - 1;
+const long long minEncodableInt = -maxEncodableInt;
- const long long maxInt = (std::numeric_limits<int>::max)();
- const long long minInt = (std::numeric_limits<int>::min)();
+const long long maxInt = (std::numeric_limits<int>::max)();
+const long long minInt = (std::numeric_limits<int>::min)();
- const long long maxEncodableDouble = (1LL << 40) - 1;
- const long long minEncodableDouble = -maxEncodableDouble;
+const long long maxEncodableDouble = (1LL << 40) - 1;
+const long long minEncodableDouble = -maxEncodableDouble;
- const long long maxDouble = (1LL << std::numeric_limits<double>::digits) - 1;
- const long long minDouble = -maxDouble;
+const long long maxDouble = (1LL << std::numeric_limits<double>::digits) - 1;
+const long long minDouble = -maxDouble;
- const long long maxLongLong = (std::numeric_limits<long long>::max)();
- const long long minLongLong = (std::numeric_limits<long long>::min)();
+const long long maxLongLong = (std::numeric_limits<long long>::max)();
+const long long minLongLong = (std::numeric_limits<long long>::min)();
- template<typename T> void assertBSONTypeEquals(BSONType actual, BSONType expected, T value, int i) {
- if (expected != actual) {
- stringstream ss;
- ss << "incorrect type in bson object for " << (i+1) << "-th test value " << value
- << ". actual: " << mongo::typeName(actual)
- << "; expected: " << mongo::typeName(expected);
- const string msg = ss.str();
- FAIL(msg);
- }
+template <typename T>
+void assertBSONTypeEquals(BSONType actual, BSONType expected, T value, int i) {
+ if (expected != actual) {
+ stringstream ss;
+ ss << "incorrect type in bson object for " << (i + 1) << "-th test value " << value
+ << ". actual: " << mongo::typeName(actual)
+ << "; expected: " << mongo::typeName(expected);
+ const string msg = ss.str();
+ FAIL(msg);
}
+}
- /**
- * current conversion ranges in append(unsigned n)
- * dbl/int max/min in comments refer to max/min encodable constants
- * 0 <= n <= uint_max -----> int
- */
+/**
+ * current conversion ranges in append(unsigned n)
+ * dbl/int max/min in comments refer to max/min encodable constants
+ * 0 <= n <= uint_max -----> int
+ */
- TEST(BSONObjBuilderTest, AppendUnsignedInt) {
- struct { unsigned int v; BSONType t; } data[] = {
- { 0, mongo::NumberInt },
- { 100, mongo::NumberInt },
- { maxEncodableInt, mongo::NumberInt },
- { maxEncodableInt + 1, mongo::NumberInt },
- { static_cast<unsigned int>(maxInt), mongo::NumberInt },
- { static_cast<unsigned int>(maxInt) + 1U, mongo::NumberInt },
- { (std::numeric_limits<unsigned int>::max)(), mongo::NumberInt },
- { 0, mongo::Undefined }
- };
- for (int i=0; data[i].t != mongo::Undefined; i++) {
- unsigned int v = data[i].v;
- BSONObjBuilder b;
- b.append("a", v);
- BSONObj o = b.obj();
- ASSERT_EQUALS(o.nFields(), 1);
- BSONElement e = o.getField("a");
- unsigned int n = e.numberLong();
- ASSERT_EQUALS(n, v);
- assertBSONTypeEquals(e.type(), data[i].t, v, i);
- }
+TEST(BSONObjBuilderTest, AppendUnsignedInt) {
+ struct {
+ unsigned int v;
+ BSONType t;
+ } data[] = {{0, mongo::NumberInt},
+ {100, mongo::NumberInt},
+ {maxEncodableInt, mongo::NumberInt},
+ {maxEncodableInt + 1, mongo::NumberInt},
+ {static_cast<unsigned int>(maxInt), mongo::NumberInt},
+ {static_cast<unsigned int>(maxInt) + 1U, mongo::NumberInt},
+ {(std::numeric_limits<unsigned int>::max)(), mongo::NumberInt},
+ {0, mongo::Undefined}};
+ for (int i = 0; data[i].t != mongo::Undefined; i++) {
+ unsigned int v = data[i].v;
+ BSONObjBuilder b;
+ b.append("a", v);
+ BSONObj o = b.obj();
+ ASSERT_EQUALS(o.nFields(), 1);
+ BSONElement e = o.getField("a");
+ unsigned int n = e.numberLong();
+ ASSERT_EQUALS(n, v);
+ assertBSONTypeEquals(e.type(), data[i].t, v, i);
}
+}
- /**
- * current conversion ranges in appendIntOrLL(long long n)
- * dbl/int max/min in comments refer to max/min encodable constants
- * n < dbl_min -----> long long
- * dbl_min <= n < int_min -----> double
- * int_min <= n <= int_max -----> int
- * int_max < n <= dbl_max -----> double
- * dbl_max < n -----> long long
- */
-
- TEST(BSONObjBuilderTest, AppendIntOrLL) {
- struct { long long v; BSONType t; } data[] = {
- { 0, mongo::NumberInt },
- { -100, mongo::NumberInt },
- { 100, mongo::NumberInt },
- { -(maxInt / 2 - 1), mongo::NumberInt },
- { maxInt / 2 - 1, mongo::NumberInt },
- { -(maxInt / 2), mongo::NumberLong },
- { maxInt / 2, mongo::NumberLong },
- { minEncodableInt, mongo::NumberLong },
- { maxEncodableInt, mongo::NumberLong },
- { minEncodableInt - 1, mongo::NumberLong },
- { maxEncodableInt + 1, mongo::NumberLong },
- { minInt, mongo::NumberLong },
- { maxInt, mongo::NumberLong },
- { minInt - 1, mongo::NumberLong },
- { maxInt + 1, mongo::NumberLong },
- { minLongLong, mongo::NumberLong },
- { maxLongLong, mongo::NumberLong },
- { 0, mongo::Undefined }
- };
- for (int i=0; data[i].t != mongo::Undefined; i++) {
- long long v = data[i].v;
- BSONObjBuilder b;
- b.appendIntOrLL("a", v);
- BSONObj o = b.obj();
- ASSERT_EQUALS(o.nFields(), 1);
- BSONElement e = o.getField("a");
- long long n = e.numberLong();
- ASSERT_EQUALS(n, v);
- assertBSONTypeEquals(e.type(), data[i].t, v, i);
- }
- }
+/**
+ * current conversion ranges in appendIntOrLL(long long n)
+ * dbl/int max/min in comments refer to max/min encodable constants
+ * n < dbl_min -----> long long
+ * dbl_min <= n < int_min -----> double
+ * int_min <= n <= int_max -----> int
+ * int_max < n <= dbl_max -----> double
+ * dbl_max < n -----> long long
+ */
- /**
- * current conversion ranges in appendNumber(size_t n)
- * dbl/int max/min in comments refer to max/min encodable constants
- * 0 <= n <= int_max -----> int
- * int_max < n -----> long long
- */
-
- TEST(BSONObjBuilderTest, AppendNumberSizeT) {
- struct { size_t v; BSONType t; } data[] = {
- { 0, mongo::NumberInt },
- { 100, mongo::NumberInt },
- { maxEncodableInt, mongo::NumberInt },
- { maxEncodableInt + 1, mongo::NumberLong },
- { size_t(maxInt), mongo::NumberLong },
- { size_t(maxInt) + 1U, mongo::NumberLong },
- { (std::numeric_limits<size_t>::max)(), mongo::NumberLong },
- { 0, mongo::Undefined }
- };
- for (int i=0; data[i].t != mongo::Undefined; i++) {
- size_t v = data[i].v;
- BSONObjBuilder b;
- b.appendNumber("a", v);
- BSONObj o = b.obj();
- ASSERT_EQUALS(o.nFields(), 1);
- BSONElement e = o.getField("a");
- size_t n = e.numberLong();
- ASSERT_EQUALS(n, v);
- assertBSONTypeEquals(e.type(), data[i].t, v, i);
- }
+TEST(BSONObjBuilderTest, AppendIntOrLL) {
+ struct {
+ long long v;
+ BSONType t;
+ } data[] = {{0, mongo::NumberInt},
+ {-100, mongo::NumberInt},
+ {100, mongo::NumberInt},
+ {-(maxInt / 2 - 1), mongo::NumberInt},
+ {maxInt / 2 - 1, mongo::NumberInt},
+ {-(maxInt / 2), mongo::NumberLong},
+ {maxInt / 2, mongo::NumberLong},
+ {minEncodableInt, mongo::NumberLong},
+ {maxEncodableInt, mongo::NumberLong},
+ {minEncodableInt - 1, mongo::NumberLong},
+ {maxEncodableInt + 1, mongo::NumberLong},
+ {minInt, mongo::NumberLong},
+ {maxInt, mongo::NumberLong},
+ {minInt - 1, mongo::NumberLong},
+ {maxInt + 1, mongo::NumberLong},
+ {minLongLong, mongo::NumberLong},
+ {maxLongLong, mongo::NumberLong},
+ {0, mongo::Undefined}};
+ for (int i = 0; data[i].t != mongo::Undefined; i++) {
+ long long v = data[i].v;
+ BSONObjBuilder b;
+ b.appendIntOrLL("a", v);
+ BSONObj o = b.obj();
+ ASSERT_EQUALS(o.nFields(), 1);
+ BSONElement e = o.getField("a");
+ long long n = e.numberLong();
+ ASSERT_EQUALS(n, v);
+ assertBSONTypeEquals(e.type(), data[i].t, v, i);
}
+}
- /**
- * current conversion ranges in appendNumber(long long n)
- * dbl/int max/min in comments refer to max/min encodable constants
- * n < dbl_min -----> long long
- * dbl_min <= n < int_min -----> double
- * int_min <= n <= int_max -----> int
- * int_max < n <= dbl_max -----> double
- * dbl_max < n -----> long long
- */
-
- TEST(BSONObjBuilderTest, AppendNumberLongLong) {
- struct { long long v; BSONType t; } data[] = {
- { 0, mongo::NumberInt },
- { -100, mongo::NumberInt },
- { 100, mongo::NumberInt },
- { minEncodableInt, mongo::NumberInt },
- { maxEncodableInt, mongo::NumberInt },
- { minEncodableInt - 1, mongo::NumberDouble },
- { maxEncodableInt + 1, mongo::NumberDouble },
- { minInt, mongo::NumberDouble },
- { maxInt, mongo::NumberDouble },
- { minInt - 1, mongo::NumberDouble },
- { maxInt + 1, mongo::NumberDouble },
- { minEncodableDouble, mongo::NumberDouble },
- { maxEncodableDouble, mongo::NumberDouble },
- { minEncodableDouble - 1, mongo::NumberLong },
- { maxEncodableDouble + 1, mongo::NumberLong },
- { minDouble, mongo::NumberLong },
- { maxDouble, mongo::NumberLong },
- { minDouble - 1, mongo::NumberLong },
- { maxDouble + 1, mongo::NumberLong },
- { minLongLong, mongo::NumberLong },
- { maxLongLong, mongo::NumberLong },
- { 0, mongo::Undefined }
- };
- for (int i=0; data[i].t != mongo::Undefined; i++) {
- long long v = data[i].v;
- BSONObjBuilder b;
- b.appendNumber("a", v);
- BSONObj o = b.obj();
- ASSERT_EQUALS(o.nFields(), 1);
- BSONElement e = o.getField("a");
- if (data[i].t != mongo::NumberDouble) {
- long long n = e.numberLong();
- ASSERT_EQUALS(n, v);
- }
- else {
- double n = e.numberDouble();
- ASSERT_APPROX_EQUAL(n, static_cast<double>(v), 0.001);
- }
- assertBSONTypeEquals(e.type(), data[i].t, v, i);
- }
- }
+/**
+ * current conversion ranges in appendNumber(size_t n)
+ * dbl/int max/min in comments refer to max/min encodable constants
+ * 0 <= n <= int_max -----> int
+ * int_max < n -----> long long
+ */
- TEST(BSONObjBuilderTest, StreamLongLongMin) {
- BSONObj o = BSON("a" << std::numeric_limits<long long>::min());
+TEST(BSONObjBuilderTest, AppendNumberSizeT) {
+ struct {
+ size_t v;
+ BSONType t;
+ } data[] = {{0, mongo::NumberInt},
+ {100, mongo::NumberInt},
+ {maxEncodableInt, mongo::NumberInt},
+ {maxEncodableInt + 1, mongo::NumberLong},
+ {size_t(maxInt), mongo::NumberLong},
+ {size_t(maxInt) + 1U, mongo::NumberLong},
+ {(std::numeric_limits<size_t>::max)(), mongo::NumberLong},
+ {0, mongo::Undefined}};
+ for (int i = 0; data[i].t != mongo::Undefined; i++) {
+ size_t v = data[i].v;
+ BSONObjBuilder b;
+ b.appendNumber("a", v);
+ BSONObj o = b.obj();
ASSERT_EQUALS(o.nFields(), 1);
BSONElement e = o.getField("a");
- long long n = e.numberLong();
- ASSERT_EQUALS(n, std::numeric_limits<long long>::min());
+ size_t n = e.numberLong();
+ ASSERT_EQUALS(n, v);
+ assertBSONTypeEquals(e.type(), data[i].t, v, i);
}
+}
- TEST(BSONObjBuilderTest, AppendNumberLongLongMinCompareObject) {
+/**
+ * current conversion ranges in appendNumber(long long n)
+ * dbl/int max/min in comments refer to max/min encodable constants
+ * n < dbl_min -----> long long
+ * dbl_min <= n < int_min -----> double
+ * int_min <= n <= int_max -----> int
+ * int_max < n <= dbl_max -----> double
+ * dbl_max < n -----> long long
+ */
+
+TEST(BSONObjBuilderTest, AppendNumberLongLong) {
+ struct {
+ long long v;
+ BSONType t;
+ } data[] = {{0, mongo::NumberInt},
+ {-100, mongo::NumberInt},
+ {100, mongo::NumberInt},
+ {minEncodableInt, mongo::NumberInt},
+ {maxEncodableInt, mongo::NumberInt},
+ {minEncodableInt - 1, mongo::NumberDouble},
+ {maxEncodableInt + 1, mongo::NumberDouble},
+ {minInt, mongo::NumberDouble},
+ {maxInt, mongo::NumberDouble},
+ {minInt - 1, mongo::NumberDouble},
+ {maxInt + 1, mongo::NumberDouble},
+ {minEncodableDouble, mongo::NumberDouble},
+ {maxEncodableDouble, mongo::NumberDouble},
+ {minEncodableDouble - 1, mongo::NumberLong},
+ {maxEncodableDouble + 1, mongo::NumberLong},
+ {minDouble, mongo::NumberLong},
+ {maxDouble, mongo::NumberLong},
+ {minDouble - 1, mongo::NumberLong},
+ {maxDouble + 1, mongo::NumberLong},
+ {minLongLong, mongo::NumberLong},
+ {maxLongLong, mongo::NumberLong},
+ {0, mongo::Undefined}};
+ for (int i = 0; data[i].t != mongo::Undefined; i++) {
+ long long v = data[i].v;
BSONObjBuilder b;
- b.appendNumber("a", std::numeric_limits<long long>::min());
- BSONObj o1 = b.obj();
-
- BSONObj o2 = BSON("a" << std::numeric_limits<long long>::min());
-
- ASSERT_EQUALS(o1, o2);
+ b.appendNumber("a", v);
+ BSONObj o = b.obj();
+ ASSERT_EQUALS(o.nFields(), 1);
+ BSONElement e = o.getField("a");
+ if (data[i].t != mongo::NumberDouble) {
+ long long n = e.numberLong();
+ ASSERT_EQUALS(n, v);
+ } else {
+ double n = e.numberDouble();
+ ASSERT_APPROX_EQUAL(n, static_cast<double>(v), 0.001);
+ }
+ assertBSONTypeEquals(e.type(), data[i].t, v, i);
}
+}
- TEST(BSONObjBuilderTest, AppendMaxTimestampConversion) {
- BSONObjBuilder b;
- b.appendMaxForType("a", mongo::bsonTimestamp);
- BSONObj o1 = b.obj();
+TEST(BSONObjBuilderTest, StreamLongLongMin) {
+ BSONObj o = BSON("a" << std::numeric_limits<long long>::min());
+ ASSERT_EQUALS(o.nFields(), 1);
+ BSONElement e = o.getField("a");
+ long long n = e.numberLong();
+ ASSERT_EQUALS(n, std::numeric_limits<long long>::min());
+}
- BSONElement e = o1.getField("a");
- ASSERT_FALSE(e.eoo());
+TEST(BSONObjBuilderTest, AppendNumberLongLongMinCompareObject) {
+ BSONObjBuilder b;
+ b.appendNumber("a", std::numeric_limits<long long>::min());
+ BSONObj o1 = b.obj();
- mongo::Timestamp timestamp = e.timestamp();
- ASSERT_FALSE(timestamp.isNull());
- }
+ BSONObj o2 = BSON("a" << std::numeric_limits<long long>::min());
+
+ ASSERT_EQUALS(o1, o2);
+}
+
+TEST(BSONObjBuilderTest, AppendMaxTimestampConversion) {
+ BSONObjBuilder b;
+ b.appendMaxForType("a", mongo::bsonTimestamp);
+ BSONObj o1 = b.obj();
+
+ BSONElement e = o1.getField("a");
+ ASSERT_FALSE(e.eoo());
+
+ mongo::Timestamp timestamp = e.timestamp();
+ ASSERT_FALSE(timestamp.isNull());
+}
-} // unnamed namespace
+} // unnamed namespace