From bce4816275bc4b856643219a5418198ab258c522 Mon Sep 17 00:00:00 2001 From: Julian Becker Date: Mon, 24 Sep 2018 23:35:19 +0200 Subject: [PATCH] BSON: Added test case for the different input/output_adapters --- test/src/unit-alt-string.cpp | 10 +++++-- test/src/unit-bson.cpp | 58 ++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/test/src/unit-alt-string.cpp b/test/src/unit-alt-string.cpp index ba52d6a4..d866ed70 100644 --- a/test/src/unit-alt-string.cpp +++ b/test/src/unit-alt-string.cpp @@ -102,9 +102,15 @@ class alt_string str_impl.resize(n, c); } - auto begin() -> std::string::iterator { return str_impl.begin(); } + auto begin() -> std::string::iterator + { + return str_impl.begin(); + } - auto end() -> std::string::iterator { return str_impl.end(); } + auto end() -> std::string::iterator + { + return str_impl.end(); + } template bool operator<(const op_type& op) const diff --git a/test/src/unit-bson.cpp b/test/src/unit-bson.cpp index fcaf51d6..c701af40 100644 --- a/test/src/unit-bson.cpp +++ b/test/src/unit-bson.cpp @@ -476,3 +476,61 @@ TEST_CASE("BSON") } } } + +TEST_CASE("BSON input/output_adapters") +{ + json json_representation = + { + {"double", 42.5}, + {"entry", 4.2}, + {"number", 12345}, + {"object", {{ "string", "value" }}} + }; + + std::vector bson_representation = + { + /*size */ 0x4f, 0x00, 0x00, 0x00, + /*entry*/ 0x01, 'd', 'o', 'u', 'b', 'l', 'e', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x45, 0x40, + /*entry*/ 0x01, 'e', 'n', 't', 'r', 'y', 0x00, 0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x10, 0x40, + /*entry*/ 0x10, 'n', 'u', 'm', 'b', 'e', 'r', 0x00, 0x39, 0x30, 0x00, 0x00, + /*entry*/ 0x03, 'o', 'b', 'j', 'e', 'c', 't', 0x00, + /*entry: obj-size */ 0x17, 0x00, 0x00, 0x00, + /*entry: obj-entry*/0x02, 's', 't', 'r', 'i', 'n', 'g', 0x00, 0x06, 0x00, 0x00, 0x00, 'v', 'a', 'l', 'u', 'e', 0, + /*entry: obj-term.*/0x00, + /*obj-term*/ 0x00 + }; + + json j2; + CHECK_NOTHROW(j2 = json::from_bson(bson_representation)); + + // compare parsed JSON values + CHECK(json_representation == j2); + + SECTION("roundtrips") + { + SECTION("std::ostringstream") + { + std::ostringstream ss; + json::to_bson(json_representation, ss); + std::istringstream iss(ss.str()); + json j3 = json::from_bson(iss); + CHECK(json_representation == j3); + } + + SECTION("std::string") + { + std::string s; + json::to_bson(json_representation, s); + json j3 = json::from_bson(s); + CHECK(json_representation == j3); + } + + SECTION("std::vector") + { + std::vector v; + json::to_bson(json_representation, v); + json j3 = json::from_bson(v); + CHECK(json_representation == j3); + } + } +}