From 4feb8211caf7adda630103b7c5dbfc99238c743e Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Mon, 11 Jun 2018 18:03:46 +0300 Subject: [PATCH 1/3] test (non)equality for alt_string implementation --- test/src/unit-alt-string.cpp | 45 ++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/test/src/unit-alt-string.cpp b/test/src/unit-alt-string.cpp index cc05039c2..4fe7123da 100644 --- a/test/src/unit-alt-string.cpp +++ b/test/src/unit-alt-string.cpp @@ -60,17 +60,36 @@ class alt_string } template - bool operator==(op_type&& op) const + typename std::enable_if< // disable for alt_string + !std::is_same< alt_string, + typename std::remove_reference::type + >::value, + bool>::type + operator==(op_type&& op) const { return str_impl == op; } + bool operator==(const alt_string& op) const + { + return str_impl == op.str_impl; + } + template - bool operator!=(op_type&& op) const + typename std::enable_if< // disable for alt_string + !std::is_same< alt_string, + typename std::remove_reference::type + >::value, + bool>::type + operator!=(op_type&& op) const { return str_impl != op; } + bool operator!=(const alt_string& op) const { + return str_impl != op.str_impl; + } + std::size_t size() const noexcept { return str_impl.size(); @@ -210,4 +229,26 @@ TEST_CASE("alternative string type") alt_string dump = doc.dump(); CHECK(dump == R"({"foo":"bar"})"); } + + SECTION("equality") + { + alt_json doc; + doc["Who are you?"] = "I'm Batman"; + + CHECK("I'm Batman" == doc["Who are you?"]); + CHECK(doc["Who are you?"] == "I'm Batman"); + + CHECK("I'm Bruce Wayne" != doc["Who are you?"]); + CHECK(doc["Who are you?"] != "I'm Bruce Wayne"); + + { + const alt_json& const_doc = doc; + + CHECK("I'm Batman" == const_doc["Who are you?"]); + CHECK(const_doc["Who are you?"] == "I'm Batman"); + + CHECK("I'm Bruce Wayne" != const_doc["Who are you?"]); + CHECK(const_doc["Who are you?"] != "I'm Bruce Wayne"); + } + } } From 3d3055909cf83250e2b610b84956fc444079d68f Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Mon, 11 Jun 2018 19:09:57 +0300 Subject: [PATCH 2/3] define global operator< for const char* and alt_string --- test/src/unit-alt-string.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/test/src/unit-alt-string.cpp b/test/src/unit-alt-string.cpp index 4fe7123da..8f1a64b03 100644 --- a/test/src/unit-alt-string.cpp +++ b/test/src/unit-alt-string.cpp @@ -106,7 +106,12 @@ class alt_string } template - bool operator<(op_type&& op) const + typename std::enable_if< // disable for alt_string + !std::is_same< alt_string, + typename std::remove_reference::type + >::value, + bool>::type + operator<(op_type&& op) const { return str_impl < op; } @@ -153,6 +158,8 @@ class alt_string private: std::string str_impl; + + friend bool ::operator<(const char*, const alt_string&); }; @@ -168,6 +175,11 @@ using alt_json = nlohmann::basic_json < nlohmann::adl_serializer >; +bool operator<(const char* op1, const alt_string& op2) { + return op1 < op2.str_impl; +} + + TEST_CASE("alternative string type") { From cd28d872e70072fba0eb3b61143c0b7af7563136 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Mon, 11 Jun 2018 20:50:39 +0300 Subject: [PATCH 3/3] forward declarations to make new compilers happy --- test/src/unit-alt-string.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/src/unit-alt-string.cpp b/test/src/unit-alt-string.cpp index 8f1a64b03..4371d3ff9 100644 --- a/test/src/unit-alt-string.cpp +++ b/test/src/unit-alt-string.cpp @@ -33,6 +33,12 @@ SOFTWARE. #include #include + +/* forward declarations */ +class alt_string; +bool operator<(const char* op1, const alt_string& op2); + + /* * This is virtually a string class. * It covers std::string under the hood.