From d6c78dacd8f2d7461073f0956a2b68e4145016f7 Mon Sep 17 00:00:00 2001 From: Jay Sistar Date: Wed, 10 May 2017 17:53:27 +0000 Subject: [PATCH 1/3] Fixing assignement for iterator wrapper second, and adding unit test --- src/json.hpp | 8 + test/CMakeLists.txt | 1 + .../unit-iterator_wrapper_first_second.cpp | 729 ++++++++++++++++++ 3 files changed, 738 insertions(+) create mode 100644 test/src/unit-iterator_wrapper_first_second.cpp diff --git a/src/json.hpp b/src/json.hpp index 8e2d8749c5..a4e1275ea9 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -7913,6 +7913,14 @@ class basic_json { return proxy.value(); } + + /// assignment operator (calls value()) + template + iterator_value_property& operator=(const ValueType& value) + { + proxy.value() = value; + return *this; + } }; /// helper class for iteration diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 25e345c2e9..36cc52b5d4 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -30,6 +30,7 @@ add_executable(${JSON_UNITTEST_TARGET_NAME} "src/unit-element_access2.cpp" "src/unit-inspection.cpp" "src/unit-iterator_wrapper.cpp" + "src/unit-iterator_wrapper_first_second.cpp" "src/unit-iterators1.cpp" "src/unit-iterators2.cpp" "src/unit-json_patch.cpp" diff --git a/test/src/unit-iterator_wrapper_first_second.cpp b/test/src/unit-iterator_wrapper_first_second.cpp new file mode 100644 index 0000000000..6dceef8bee --- /dev/null +++ b/test/src/unit-iterator_wrapper_first_second.cpp @@ -0,0 +1,729 @@ +/* + __ _____ _____ _____ + __| | __| | | | JSON for Modern C++ (test suite) +| | |__ | | | | | | version 2.1.1 +|_____|_____|_____|_|___| https://github.com/nlohmann/json + +Licensed under the MIT License . +Copyright (c) 2013-2017 Niels Lohmann . + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "catch.hpp" + +#include "json.hpp" +using nlohmann::json; + +TEST_CASE("iterator_wrapper_first_second") +{ + SECTION("object") + { + SECTION("value") + { + json j = {{"A", 1}, {"B", 2}}; + int counter = 1; + + for (auto i : json::iterator_wrapper(j)) + { + switch (counter++) + { + case 1: + { + CHECK(i.first == "A"); + CHECK(i.second == json(1)); + break; + } + + case 2: + { + CHECK(i.first == "B"); + CHECK(i.second == json(2)); + break; + } + + default: + { + break; + } + } + } + + CHECK(counter == 3); + } + + SECTION("reference") + { + json j = {{"A", 1}, {"B", 2}}; + int counter = 1; + + for (auto& i : json::iterator_wrapper(j)) + { + switch (counter++) + { + case 1: + { + CHECK(i.first == "A"); + CHECK(i.second == json(1)); + + // change the value + i.second = json(11); + CHECK(i.second == json(11)); + break; + } + + case 2: + { + CHECK(i.first == "B"); + CHECK(i.second == json(2)); + + // change the value + i.second = json(22); + CHECK(i.second == json(22)); + break; + } + + default: + { + break; + } + } + } + + CHECK(counter == 3); + + // check if values where changed + CHECK(j == json({{"A", 11}, {"B", 22}})); + } + + SECTION("const value") + { + json j = {{"A", 1}, {"B", 2}}; + int counter = 1; + + for (const auto i : json::iterator_wrapper(j)) + { + switch (counter++) + { + case 1: + { + CHECK(i.first == "A"); + CHECK(i.second == json(1)); + break; + } + + case 2: + { + CHECK(i.first == "B"); + CHECK(i.second == json(2)); + break; + } + + default: + { + break; + } + } + } + + CHECK(counter == 3); + } + + SECTION("const reference") + { + json j = {{"A", 1}, {"B", 2}}; + int counter = 1; + + for (const auto& i : json::iterator_wrapper(j)) + { + switch (counter++) + { + case 1: + { + CHECK(i.first == "A"); + CHECK(i.second == json(1)); + break; + } + + case 2: + { + CHECK(i.first == "B"); + CHECK(i.second == json(2)); + break; + } + + default: + { + break; + } + } + } + + CHECK(counter == 3); + } + } + + SECTION("const object") + { + SECTION("value") + { + const json j = {{"A", 1}, {"B", 2}}; + int counter = 1; + + for (auto i : json::iterator_wrapper(j)) + { + switch (counter++) + { + case 1: + { + CHECK(i.first == "A"); + CHECK(i.second == json(1)); + break; + } + + case 2: + { + CHECK(i.first == "B"); + CHECK(i.second == json(2)); + break; + } + + default: + { + break; + } + } + } + + CHECK(counter == 3); + } + + SECTION("reference") + { + const json j = {{"A", 1}, {"B", 2}}; + int counter = 1; + + for (auto& i : json::iterator_wrapper(j)) + { + switch (counter++) + { + case 1: + { + CHECK(i.first == "A"); + CHECK(i.second == json(1)); + break; + } + + case 2: + { + CHECK(i.first == "B"); + CHECK(i.second == json(2)); + break; + } + + default: + { + break; + } + } + } + + CHECK(counter == 3); + } + + SECTION("const value") + { + const json j = {{"A", 1}, {"B", 2}}; + int counter = 1; + + for (const auto i : json::iterator_wrapper(j)) + { + switch (counter++) + { + case 1: + { + CHECK(i.first == "A"); + CHECK(i.second == json(1)); + break; + } + + case 2: + { + CHECK(i.first == "B"); + CHECK(i.second == json(2)); + break; + } + + default: + { + break; + } + } + } + + CHECK(counter == 3); + } + + SECTION("const reference") + { + const json j = {{"A", 1}, {"B", 2}}; + int counter = 1; + + for (const auto& i : json::iterator_wrapper(j)) + { + switch (counter++) + { + case 1: + { + CHECK(i.first == "A"); + CHECK(i.second == json(1)); + break; + } + + case 2: + { + CHECK(i.first == "B"); + CHECK(i.second == json(2)); + break; + } + + default: + { + break; + } + } + } + + CHECK(counter == 3); + } + } + + SECTION("array") + { + SECTION("value") + { + json j = {"A", "B"}; + int counter = 1; + + for (auto i : json::iterator_wrapper(j)) + { + switch (counter++) + { + case 1: + { + CHECK(i.first == "0"); + CHECK(i.second == "A"); + break; + } + + case 2: + { + CHECK(i.first == "1"); + CHECK(i.second == "B"); + break; + } + + default: + { + break; + } + } + } + + CHECK(counter == 3); + } + + SECTION("reference") + { + json j = {"A", "B"}; + int counter = 1; + + for (auto& i : json::iterator_wrapper(j)) + { + switch (counter++) + { + case 1: + { + CHECK(i.first == "0"); + CHECK(i.second == "A"); + + // change the value + i.second = "AA"; + CHECK(i.second == "AA"); + break; + } + + case 2: + { + CHECK(i.first == "1"); + CHECK(i.second == "B"); + + // change the value + i.second = "BB"; + CHECK(i.second == "BB"); + break; + } + + default: + { + break; + } + } + } + + CHECK(counter == 3); + + // check if values where changed + CHECK(j == json({"AA", "BB"})); + } + + SECTION("const value") + { + json j = {"A", "B"}; + int counter = 1; + + for (const auto i : json::iterator_wrapper(j)) + { + switch (counter++) + { + case 1: + { + CHECK(i.first == "0"); + CHECK(i.second == "A"); + break; + } + + case 2: + { + CHECK(i.first == "1"); + CHECK(i.second == "B"); + break; + } + + default: + { + break; + } + } + } + + CHECK(counter == 3); + } + + SECTION("const reference") + { + json j = {"A", "B"}; + int counter = 1; + + for (const auto& i : json::iterator_wrapper(j)) + { + switch (counter++) + { + case 1: + { + CHECK(i.first == "0"); + CHECK(i.second == "A"); + break; + } + + case 2: + { + CHECK(i.first == "1"); + CHECK(i.second == "B"); + break; + } + + default: + { + break; + } + } + } + + CHECK(counter == 3); + } + } + + SECTION("const array") + { + SECTION("value") + { + const json j = {"A", "B"}; + int counter = 1; + + for (auto i : json::iterator_wrapper(j)) + { + switch (counter++) + { + case 1: + { + CHECK(i.first == "0"); + CHECK(i.second == "A"); + break; + } + + case 2: + { + CHECK(i.first == "1"); + CHECK(i.second == "B"); + break; + } + + default: + { + break; + } + } + } + + CHECK(counter == 3); + } + + SECTION("reference") + { + const json j = {"A", "B"}; + int counter = 1; + + for (auto& i : json::iterator_wrapper(j)) + { + switch (counter++) + { + case 1: + { + CHECK(i.first == "0"); + CHECK(i.second == "A"); + break; + } + + case 2: + { + CHECK(i.first == "1"); + CHECK(i.second == "B"); + break; + } + + default: + { + break; + } + } + } + + CHECK(counter == 3); + } + + SECTION("const value") + { + const json j = {"A", "B"}; + int counter = 1; + + for (const auto i : json::iterator_wrapper(j)) + { + switch (counter++) + { + case 1: + { + CHECK(i.first == "0"); + CHECK(i.second == "A"); + break; + } + + case 2: + { + CHECK(i.first == "1"); + CHECK(i.second == "B"); + break; + } + + default: + { + break; + } + } + } + + CHECK(counter == 3); + } + + SECTION("const reference") + { + const json j = {"A", "B"}; + int counter = 1; + + for (const auto& i : json::iterator_wrapper(j)) + { + switch (counter++) + { + case 1: + { + CHECK(i.first == "0"); + CHECK(i.second == "A"); + break; + } + + case 2: + { + CHECK(i.first == "1"); + CHECK(i.second == "B"); + break; + } + + default: + { + break; + } + } + } + + CHECK(counter == 3); + } + } + + SECTION("primitive") + { + SECTION("value") + { + json j = 1; + int counter = 1; + + for (auto i : json::iterator_wrapper(j)) + { + ++counter; + CHECK(i.first == ""); + CHECK(i.second == json(1)); + } + + CHECK(counter == 2); + } + + SECTION("reference") + { + json j = 1; + int counter = 1; + + for (auto& i : json::iterator_wrapper(j)) + { + ++counter; + CHECK(i.first == ""); + CHECK(i.second == json(1)); + + // change value + i.second = json(2); + } + + CHECK(counter == 2); + + // check if value has changed + CHECK(j == json(2)); + } + + SECTION("const value") + { + json j = 1; + int counter = 1; + + for (const auto i : json::iterator_wrapper(j)) + { + ++counter; + CHECK(i.first == ""); + CHECK(i.second == json(1)); + } + + CHECK(counter == 2); + } + + SECTION("const reference") + { + json j = 1; + int counter = 1; + + for (const auto& i : json::iterator_wrapper(j)) + { + ++counter; + CHECK(i.first == ""); + CHECK(i.second == json(1)); + } + + CHECK(counter == 2); + } + } + + SECTION("const primitive") + { + SECTION("value") + { + const json j = 1; + int counter = 1; + + for (auto i : json::iterator_wrapper(j)) + { + ++counter; + CHECK(i.first == ""); + CHECK(i.second == json(1)); + } + + CHECK(counter == 2); + } + + SECTION("reference") + { + const json j = 1; + int counter = 1; + + for (auto& i : json::iterator_wrapper(j)) + { + ++counter; + CHECK(i.first == ""); + CHECK(i.second == json(1)); + } + + CHECK(counter == 2); + } + + SECTION("const value") + { + const json j = 1; + int counter = 1; + + for (const auto i : json::iterator_wrapper(j)) + { + ++counter; + CHECK(i.first == ""); + CHECK(i.second == json(1)); + } + + CHECK(counter == 2); + } + + SECTION("const reference") + { + const json j = 1; + int counter = 1; + + for (const auto& i : json::iterator_wrapper(j)) + { + ++counter; + CHECK(i.first == ""); + CHECK(i.second == json(1)); + } + + CHECK(counter == 2); + } + } +} From 00d841bfda5dea2d21261bdefa62c9274554bd05 Mon Sep 17 00:00:00 2001 From: Jay Sistar Date: Thu, 11 May 2017 14:50:59 +0000 Subject: [PATCH 2/3] Adding equal and not equal operators to proxys. --- src/json.hpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/json.hpp b/src/json.hpp index a4e1275ea9..b799f63bb5 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -7894,6 +7894,20 @@ class basic_json { return proxy.key(); } + + /// equal operator (calls key()) + template + bool operator==(const KeyType& key) + { + return proxy.key() == key; + } + + /// not equal operator (calls key()) + template + bool operator!=(const KeyType& key) + { + return proxy.key() != key; + } }; /// helper class for second "property" @@ -7914,6 +7928,20 @@ class basic_json return proxy.value(); } + /// equal operator (calls value()) + template + bool operator==(const ValueType& value) + { + return proxy.value() == value; + } + + /// not equal operator (calls value()) + template + bool operator!=(const ValueType& value) + { + return proxy.value() != value; + } + /// assignment operator (calls value()) template iterator_value_property& operator=(const ValueType& value) From b78457b792501803eadb7b48e04178a1ec570b1e Mon Sep 17 00:00:00 2001 From: Jay Sistar Date: Thu, 11 May 2017 13:22:47 -0400 Subject: [PATCH 3/3] Making comparison operators const. --- src/json.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index b799f63bb5..6e5803ca27 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -7897,14 +7897,14 @@ class basic_json /// equal operator (calls key()) template - bool operator==(const KeyType& key) + bool operator==(const KeyType& key) const { return proxy.key() == key; } /// not equal operator (calls key()) template - bool operator!=(const KeyType& key) + bool operator!=(const KeyType& key) const { return proxy.key() != key; } @@ -7930,14 +7930,14 @@ class basic_json /// equal operator (calls value()) template - bool operator==(const ValueType& value) + bool operator==(const ValueType& value) const { return proxy.value() == value; } /// not equal operator (calls value()) template - bool operator!=(const ValueType& value) + bool operator!=(const ValueType& value) const { return proxy.value() != value; }