json/tests/src/unit-items.cpp

1430 lines
32 KiB
C++
Raw Normal View History

// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++ (supporting code)
2023-11-29 00:36:31 +03:00
// | | |__ | | | | | | version 3.11.3
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
//
// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT
#include "doctest_compatibility.h"
#include <nlohmann/json.hpp>
using nlohmann::json;
// This test suite uses range for loops where values are copied. This is inefficient in usual code, but required to achieve 100% coverage.
DOCTEST_GCC_SUPPRESS_WARNING_PUSH
#if DOCTEST_GCC >= DOCTEST_COMPILER(11, 0, 0)
2023-11-29 17:02:51 +03:00
DOCTEST_GCC_SUPPRESS_WARNING("-Wrange-loop-construct")
#endif
DOCTEST_CLANG_SUPPRESS_WARNING_PUSH
DOCTEST_CLANG_SUPPRESS_WARNING("-Wrange-loop-construct")
TEST_CASE("iterator_wrapper")
{
SECTION("object")
{
SECTION("value")
{
2023-11-29 17:02:51 +03:00
json j = {{"A", 1}, {"B", 2}};
int counter = 1;
2023-11-29 17:02:51 +03:00
for (auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy)
{
switch (counter++)
{
case 1:
{
CHECK(i.key() == "A");
CHECK(i.value() == json(1));
break;
}
case 2:
{
CHECK(i.key() == "B");
CHECK(i.value() == json(2));
break;
}
default:
{
break;
}
}
}
CHECK(counter == 3);
}
SECTION("reference")
{
2023-11-29 17:02:51 +03:00
json j = {{"A", 1}, {"B", 2}};
int counter = 1;
2023-11-29 17:02:51 +03:00
for (auto& i : json::iterator_wrapper(j)) // NOLINT(readability-qualified-auto)
{
switch (counter++)
{
case 1:
{
CHECK(i.key() == "A");
CHECK(i.value() == json(1));
// change the value
i.value() = json(11);
CHECK(i.value() == json(11));
break;
}
case 2:
{
CHECK(i.key() == "B");
CHECK(i.value() == json(2));
// change the value
i.value() = json(22);
CHECK(i.value() == json(22));
break;
}
default:
{
break;
}
}
}
CHECK(counter == 3);
// check if values where changed
2023-11-29 17:02:51 +03:00
CHECK(j == json({{"A", 11}, {"B", 22}}));
}
SECTION("const value")
{
2023-11-29 17:02:51 +03:00
json j = {{"A", 1}, {"B", 2}};
int counter = 1;
2023-11-29 17:02:51 +03:00
for (const auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy)
{
switch (counter++)
{
case 1:
{
CHECK(i.key() == "A");
CHECK(i.value() == json(1));
break;
}
case 2:
{
CHECK(i.key() == "B");
CHECK(i.value() == json(2));
break;
}
default:
{
break;
}
}
}
CHECK(counter == 3);
}
SECTION("const reference")
{
2023-11-29 17:02:51 +03:00
json j = {{"A", 1}, {"B", 2}};
int counter = 1;
for (const auto& i : json::iterator_wrapper(j))
{
switch (counter++)
{
case 1:
{
CHECK(i.key() == "A");
CHECK(i.value() == json(1));
break;
}
case 2:
{
CHECK(i.key() == "B");
CHECK(i.value() == json(2));
break;
}
default:
{
break;
}
}
}
CHECK(counter == 3);
}
}
SECTION("const object")
{
SECTION("value")
{
2023-11-29 17:02:51 +03:00
const json j = {{"A", 1}, {"B", 2}};
int counter = 1;
2023-11-29 17:02:51 +03:00
for (auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy)
{
switch (counter++)
{
case 1:
{
CHECK(i.key() == "A");
CHECK(i.value() == json(1));
break;
}
case 2:
{
CHECK(i.key() == "B");
CHECK(i.value() == json(2));
break;
}
default:
{
break;
}
}
}
CHECK(counter == 3);
}
SECTION("reference")
{
2023-11-29 17:02:51 +03:00
const json j = {{"A", 1}, {"B", 2}};
int counter = 1;
2023-11-29 17:02:51 +03:00
for (auto& i : json::iterator_wrapper(j)) // NOLINT(readability-qualified-auto)
{
switch (counter++)
{
case 1:
{
CHECK(i.key() == "A");
CHECK(i.value() == json(1));
break;
}
case 2:
{
CHECK(i.key() == "B");
CHECK(i.value() == json(2));
break;
}
default:
{
break;
}
}
}
CHECK(counter == 3);
}
SECTION("const value")
{
2023-11-29 17:02:51 +03:00
const json j = {{"A", 1}, {"B", 2}};
int counter = 1;
2023-11-29 17:02:51 +03:00
for (const auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy)
{
switch (counter++)
{
case 1:
{
CHECK(i.key() == "A");
CHECK(i.value() == json(1));
break;
}
case 2:
{
CHECK(i.key() == "B");
CHECK(i.value() == json(2));
break;
}
default:
{
break;
}
}
}
CHECK(counter == 3);
}
SECTION("const reference")
{
2023-11-29 17:02:51 +03:00
const json j = {{"A", 1}, {"B", 2}};
int counter = 1;
for (const auto& i : json::iterator_wrapper(j))
{
switch (counter++)
{
case 1:
{
CHECK(i.key() == "A");
CHECK(i.value() == json(1));
break;
}
case 2:
{
CHECK(i.key() == "B");
CHECK(i.value() == json(2));
break;
}
default:
{
break;
}
}
}
CHECK(counter == 3);
}
}
SECTION("array")
{
SECTION("value")
{
2023-11-29 17:02:51 +03:00
json j = {"A", "B"};
int counter = 1;
2023-11-29 17:02:51 +03:00
for (auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy)
{
switch (counter++)
{
case 1:
{
CHECK(i.key() == "0");
CHECK(i.value() == "A");
break;
}
case 2:
{
CHECK(i.key() == "1");
CHECK(i.value() == "B");
break;
}
default:
{
break;
}
}
}
CHECK(counter == 3);
}
SECTION("reference")
{
2023-11-29 17:02:51 +03:00
json j = {"A", "B"};
int counter = 1;
2023-11-29 17:02:51 +03:00
for (auto& i : json::iterator_wrapper(j)) // NOLINT(readability-qualified-auto)
{
switch (counter++)
{
case 1:
{
CHECK(i.key() == "0");
CHECK(i.value() == "A");
// change the value
i.value() = "AA";
CHECK(i.value() == "AA");
break;
}
case 2:
{
CHECK(i.key() == "1");
CHECK(i.value() == "B");
// change the value
i.value() = "BB";
CHECK(i.value() == "BB");
break;
}
default:
{
break;
}
}
}
CHECK(counter == 3);
// check if values where changed
2023-11-29 17:02:51 +03:00
CHECK(j == json({"AA", "BB"}));
}
SECTION("const value")
{
2023-11-29 17:02:51 +03:00
json j = {"A", "B"};
int counter = 1;
2023-11-29 17:02:51 +03:00
for (const auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy)
{
switch (counter++)
{
case 1:
{
CHECK(i.key() == "0");
CHECK(i.value() == "A");
break;
}
case 2:
{
CHECK(i.key() == "1");
CHECK(i.value() == "B");
break;
}
default:
{
break;
}
}
}
CHECK(counter == 3);
}
SECTION("const reference")
{
2023-11-29 17:02:51 +03:00
json j = {"A", "B"};
int counter = 1;
for (const auto& i : json::iterator_wrapper(j))
{
switch (counter++)
{
case 1:
{
CHECK(i.key() == "0");
CHECK(i.value() == "A");
break;
}
case 2:
{
CHECK(i.key() == "1");
CHECK(i.value() == "B");
break;
}
default:
{
break;
}
}
}
CHECK(counter == 3);
}
}
SECTION("const array")
{
SECTION("value")
{
2023-11-29 17:02:51 +03:00
const json j = {"A", "B"};
int counter = 1;
2023-11-29 17:02:51 +03:00
for (auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy)
{
switch (counter++)
{
case 1:
{
CHECK(i.key() == "0");
CHECK(i.value() == "A");
break;
}
case 2:
{
CHECK(i.key() == "1");
CHECK(i.value() == "B");
break;
}
default:
{
break;
}
}
}
CHECK(counter == 3);
}
SECTION("reference")
{
2023-11-29 17:02:51 +03:00
const json j = {"A", "B"};
int counter = 1;
2023-11-29 17:02:51 +03:00
for (auto& i : json::iterator_wrapper(j)) // NOLINT(readability-qualified-auto)
{
switch (counter++)
{
case 1:
{
CHECK(i.key() == "0");
CHECK(i.value() == "A");
break;
}
case 2:
{
CHECK(i.key() == "1");
CHECK(i.value() == "B");
break;
}
default:
{
break;
}
}
}
CHECK(counter == 3);
}
SECTION("const value")
{
2023-11-29 17:02:51 +03:00
const json j = {"A", "B"};
int counter = 1;
2023-11-29 17:02:51 +03:00
for (const auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy)
{
switch (counter++)
{
case 1:
{
CHECK(i.key() == "0");
CHECK(i.value() == "A");
break;
}
case 2:
{
CHECK(i.key() == "1");
CHECK(i.value() == "B");
break;
}
default:
{
break;
}
}
}
CHECK(counter == 3);
}
SECTION("const reference")
{
2023-11-29 17:02:51 +03:00
const json j = {"A", "B"};
int counter = 1;
for (const auto& i : json::iterator_wrapper(j))
{
switch (counter++)
{
case 1:
{
CHECK(i.key() == "0");
CHECK(i.value() == "A");
break;
}
case 2:
{
CHECK(i.key() == "1");
CHECK(i.value() == "B");
break;
}
default:
{
break;
}
}
}
CHECK(counter == 3);
}
}
SECTION("primitive")
{
SECTION("value")
{
json j = 1;
int counter = 1;
2023-11-29 17:02:51 +03:00
for (auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy)
{
++counter;
CHECK(i.key() == "");
CHECK(i.value() == json(1));
}
CHECK(counter == 2);
}
SECTION("reference")
{
json j = 1;
int counter = 1;
2023-11-29 17:02:51 +03:00
for (auto& i : json::iterator_wrapper(j)) // NOLINT(readability-qualified-auto)
{
++counter;
CHECK(i.key() == "");
CHECK(i.value() == json(1));
// change value
i.value() = json(2);
}
CHECK(counter == 2);
// check if value has changed
CHECK(j == json(2));
}
SECTION("const value")
{
json j = 1;
int counter = 1;
2023-11-29 17:02:51 +03:00
for (const auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy)
{
++counter;
CHECK(i.key() == "");
CHECK(i.value() == 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.key() == "");
CHECK(i.value() == json(1));
}
CHECK(counter == 2);
}
}
SECTION("const primitive")
{
SECTION("value")
{
const json j = 1;
int counter = 1;
2023-11-29 17:02:51 +03:00
for (auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy)
{
++counter;
CHECK(i.key() == "");
CHECK(i.value() == json(1));
}
CHECK(counter == 2);
}
SECTION("reference")
{
const json j = 1;
int counter = 1;
2023-11-29 17:02:51 +03:00
for (auto& i : json::iterator_wrapper(j)) // NOLINT(readability-qualified-auto)
{
++counter;
CHECK(i.key() == "");
CHECK(i.value() == json(1));
}
CHECK(counter == 2);
}
SECTION("const value")
{
const json j = 1;
int counter = 1;
2023-11-29 17:02:51 +03:00
for (const auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy)
{
++counter;
CHECK(i.key() == "");
CHECK(i.value() == 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.key() == "");
CHECK(i.value() == json(1));
}
CHECK(counter == 2);
}
}
}
2018-01-05 20:34:10 +03:00
TEST_CASE("items()")
{
2023-11-29 17:02:51 +03:00
SECTION("object"){SECTION("value"){json j = {{"A", 1}, {"B", 2}};
2023-11-29 17:02:51 +03:00
int counter = 1;
for (auto i : j.items()) // NOLINT(performance-for-range-copy)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
switch (counter++)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
case 1:
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
CHECK(i.key() == "A");
CHECK(i.value() == json(1));
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
case 2:
{
CHECK(i.key() == "B");
CHECK(i.value() == json(2));
break;
2018-01-05 20:34:10 +03:00
}
2023-11-29 17:02:51 +03:00
default:
{
break;
}
2018-01-05 20:34:10 +03:00
}
2023-11-29 17:02:51 +03:00
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
CHECK(counter == 3);
}
SECTION("reference")
{
json j = {{"A", 1}, {"B", 2}};
int counter = 1;
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
for (auto& i : j.items()) // NOLINT(readability-qualified-auto)
{
switch (counter++)
{
case 1:
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
CHECK(i.key() == "A");
CHECK(i.value() == json(1));
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
// change the value
i.value() = json(11);
CHECK(i.value() == json(11));
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
case 2:
{
CHECK(i.key() == "B");
CHECK(i.value() == json(2));
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
// change the value
i.value() = json(22);
CHECK(i.value() == json(22));
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
default:
{
break;
2018-01-05 20:34:10 +03:00
}
2023-11-29 17:02:51 +03:00
}
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
CHECK(counter == 3);
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
// check if values where changed
CHECK(j == json({{"A", 11}, {"B", 22}}));
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
SECTION("const value")
{
json j = {{"A", 1}, {"B", 2}};
int counter = 1;
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
for (const auto i : j.items()) // NOLINT(performance-for-range-copy)
{
switch (counter++)
{
case 1:
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
CHECK(i.key() == "A");
CHECK(i.value() == json(1));
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
case 2:
{
CHECK(i.key() == "B");
CHECK(i.value() == json(2));
break;
2018-01-05 20:34:10 +03:00
}
2023-11-29 17:02:51 +03:00
default:
{
break;
}
2018-01-05 20:34:10 +03:00
}
2023-11-29 17:02:51 +03:00
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
CHECK(counter == 3);
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
SECTION("const reference")
{
json j = {{"A", 1}, {"B", 2}};
int counter = 1;
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
for (const auto& i : j.items())
{
switch (counter++)
{
case 1:
{
CHECK(i.key() == "A");
CHECK(i.value() == json(1));
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
case 2:
{
CHECK(i.key() == "B");
CHECK(i.value() == json(2));
break;
2018-01-05 20:34:10 +03:00
}
2023-11-29 17:02:51 +03:00
default:
{
break;
}
2018-01-05 20:34:10 +03:00
}
2023-11-29 17:02:51 +03:00
}
CHECK(counter == 3);
}
#ifdef JSON_HAS_CPP_17
2023-11-29 17:02:51 +03:00
SECTION("structured bindings")
{
json j = {{"A", 1}, {"B", 2}};
2023-11-29 17:02:51 +03:00
std::map<std::string, int> m;
2023-11-29 17:02:51 +03:00
for (auto const& [key, value] : j.items())
{
m.emplace(key, value);
}
2023-11-29 17:02:51 +03:00
CHECK(j.get<decltype(m)>() == m);
}
#endif
2023-11-29 17:02:51 +03:00
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
SECTION("const object")
{
SECTION("value")
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
const json j = {{"A", 1}, {"B", 2}};
int counter = 1;
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
for (auto i : j.items()) // NOLINT(performance-for-range-copy)
{
switch (counter++)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
case 1:
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
CHECK(i.key() == "A");
CHECK(i.value() == json(1));
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
case 2:
{
CHECK(i.key() == "B");
CHECK(i.value() == json(2));
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
default:
{
break;
2018-01-05 20:34:10 +03:00
}
}
}
2023-11-29 17:02:51 +03:00
CHECK(counter == 3);
}
SECTION("reference")
{
const json j = {{"A", 1}, {"B", 2}};
int counter = 1;
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
for (auto& i : j.items()) // NOLINT(readability-qualified-auto)
{
switch (counter++)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
case 1:
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
CHECK(i.key() == "A");
CHECK(i.value() == json(1));
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
case 2:
{
CHECK(i.key() == "B");
CHECK(i.value() == json(2));
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
default:
{
break;
2018-01-05 20:34:10 +03:00
}
}
}
2023-11-29 17:02:51 +03:00
CHECK(counter == 3);
}
SECTION("const value")
{
const json j = {{"A", 1}, {"B", 2}};
int counter = 1;
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
for (const auto i : j.items()) // NOLINT(performance-for-range-copy)
{
switch (counter++)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
case 1:
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
CHECK(i.key() == "A");
CHECK(i.value() == json(1));
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
case 2:
{
CHECK(i.key() == "B");
CHECK(i.value() == json(2));
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
default:
{
break;
2018-01-05 20:34:10 +03:00
}
}
}
2023-11-29 17:02:51 +03:00
CHECK(counter == 3);
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
SECTION("const reference")
{
const json j = {{"A", 1}, {"B", 2}};
int counter = 1;
for (const auto& i : j.items())
{
switch (counter++)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
case 1:
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
CHECK(i.key() == "A");
CHECK(i.value() == json(1));
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
case 2:
{
CHECK(i.key() == "B");
CHECK(i.value() == json(2));
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
default:
{
break;
2018-01-05 20:34:10 +03:00
}
}
}
2023-11-29 17:02:51 +03:00
CHECK(counter == 3);
2018-01-05 20:34:10 +03:00
}
2023-11-29 17:02:51 +03:00
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
SECTION("array")
{
SECTION("value")
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
json j = {"A", "B"};
int counter = 1;
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
for (auto i : j.items()) // NOLINT(performance-for-range-copy)
{
switch (counter++)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
case 1:
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
CHECK(i.key() == "0");
CHECK(i.value() == "A");
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
case 2:
{
CHECK(i.key() == "1");
CHECK(i.value() == "B");
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
default:
{
break;
2018-01-05 20:34:10 +03:00
}
}
}
2023-11-29 17:02:51 +03:00
CHECK(counter == 3);
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
SECTION("reference")
{
json j = {"A", "B"};
int counter = 1;
for (auto& i : j.items()) // NOLINT(readability-qualified-auto)
{
switch (counter++)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
case 1:
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
CHECK(i.key() == "0");
CHECK(i.value() == "A");
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
// change the value
i.value() = "AA";
CHECK(i.value() == "AA");
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
case 2:
{
CHECK(i.key() == "1");
CHECK(i.value() == "B");
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
// change the value
i.value() = "BB";
CHECK(i.value() == "BB");
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
default:
{
break;
2018-01-05 20:34:10 +03:00
}
}
}
2023-11-29 17:02:51 +03:00
CHECK(counter == 3);
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
// check if values where changed
CHECK(j == json({"AA", "BB"}));
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
SECTION("const value")
{
json j = {"A", "B"};
int counter = 1;
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
for (const auto i : j.items()) // NOLINT(performance-for-range-copy)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
switch (counter++)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
case 1:
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
CHECK(i.key() == "0");
CHECK(i.value() == "A");
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
case 2:
{
CHECK(i.key() == "1");
CHECK(i.value() == "B");
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
default:
{
break;
2018-01-05 20:34:10 +03:00
}
}
}
2023-11-29 17:02:51 +03:00
CHECK(counter == 3);
2018-01-05 20:34:10 +03:00
}
2023-11-29 17:02:51 +03:00
SECTION("const reference")
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
json j = {"A", "B"};
int counter = 1;
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
for (const auto& i : j.items())
{
switch (counter++)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
case 1:
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
CHECK(i.key() == "0");
CHECK(i.value() == "A");
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
case 2:
{
CHECK(i.key() == "1");
CHECK(i.value() == "B");
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
default:
{
break;
2018-01-05 20:34:10 +03:00
}
}
}
2023-11-29 17:02:51 +03:00
CHECK(counter == 3);
}
}
SECTION("const array")
{
SECTION("value")
{
const json j = {"A", "B"};
int counter = 1;
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
for (auto i : j.items()) // NOLINT(performance-for-range-copy)
{
switch (counter++)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
case 1:
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
CHECK(i.key() == "0");
CHECK(i.value() == "A");
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
case 2:
{
CHECK(i.key() == "1");
CHECK(i.value() == "B");
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
default:
{
break;
2018-01-05 20:34:10 +03:00
}
}
}
2023-11-29 17:02:51 +03:00
CHECK(counter == 3);
}
SECTION("reference")
{
const json j = {"A", "B"};
int counter = 1;
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
for (auto& i : j.items()) // NOLINT(readability-qualified-auto)
{
switch (counter++)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
case 1:
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
CHECK(i.key() == "0");
CHECK(i.value() == "A");
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
case 2:
{
CHECK(i.key() == "1");
CHECK(i.value() == "B");
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
default:
{
break;
2018-01-05 20:34:10 +03:00
}
}
}
2023-11-29 17:02:51 +03:00
CHECK(counter == 3);
}
SECTION("const value")
{
const json j = {"A", "B"};
int counter = 1;
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
for (const auto i : j.items()) // NOLINT(performance-for-range-copy)
{
switch (counter++)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
case 1:
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
CHECK(i.key() == "0");
CHECK(i.value() == "A");
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
case 2:
{
CHECK(i.key() == "1");
CHECK(i.value() == "B");
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
default:
{
break;
2018-01-05 20:34:10 +03:00
}
}
}
2023-11-29 17:02:51 +03:00
CHECK(counter == 3);
2018-01-05 20:34:10 +03:00
}
2023-11-29 17:02:51 +03:00
SECTION("const reference")
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
const json j = {"A", "B"};
int counter = 1;
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
for (const auto& i : j.items())
{
switch (counter++)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
case 1:
{
CHECK(i.key() == "0");
CHECK(i.value() == "A");
break;
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
case 2:
{
CHECK(i.key() == "1");
CHECK(i.value() == "B");
break;
}
default:
{
break;
}
}
2018-01-05 20:34:10 +03:00
}
2023-11-29 17:02:51 +03:00
CHECK(counter == 3);
}
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
SECTION("primitive")
{
SECTION("value")
{
json j = 1;
int counter = 1;
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
for (auto i : j.items()) // NOLINT(performance-for-range-copy)
{
++counter;
CHECK(i.key() == "");
CHECK(i.value() == json(1));
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
CHECK(counter == 2);
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
SECTION("reference")
{
json j = 1;
int counter = 1;
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
for (auto& i : j.items()) // NOLINT(readability-qualified-auto)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
++counter;
CHECK(i.key() == "");
CHECK(i.value() == json(1));
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
// change value
i.value() = json(2);
2018-01-05 20:34:10 +03:00
}
2023-11-29 17:02:51 +03:00
CHECK(counter == 2);
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
// check if value has changed
CHECK(j == json(2));
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
SECTION("const value")
{
json j = 1;
int counter = 1;
for (const auto i : j.items()) // NOLINT(performance-for-range-copy)
{
++counter;
CHECK(i.key() == "");
CHECK(i.value() == json(1));
2018-01-05 20:34:10 +03:00
}
2023-11-29 17:02:51 +03:00
CHECK(counter == 2);
2018-01-05 20:34:10 +03:00
}
2023-11-29 17:02:51 +03:00
SECTION("const reference")
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
json j = 1;
int counter = 1;
for (const auto& i : j.items())
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
++counter;
CHECK(i.key() == "");
CHECK(i.value() == json(1));
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
CHECK(counter == 2);
}
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
SECTION("const primitive")
{
SECTION("value")
{
const json j = 1;
int counter = 1;
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
for (auto i : j.items()) // NOLINT(performance-for-range-copy)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
++counter;
CHECK(i.key() == "");
CHECK(i.value() == json(1));
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
CHECK(counter == 2);
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
SECTION("reference")
{
const json j = 1;
int counter = 1;
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
for (auto& i : j.items()) // NOLINT(readability-qualified-auto)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
++counter;
CHECK(i.key() == "");
CHECK(i.value() == json(1));
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
CHECK(counter == 2);
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
SECTION("const value")
{
const json j = 1;
int counter = 1;
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
for (const auto i : j.items()) // NOLINT(performance-for-range-copy)
2018-01-05 20:34:10 +03:00
{
2023-11-29 17:02:51 +03:00
++counter;
CHECK(i.key() == "");
CHECK(i.value() == json(1));
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
CHECK(counter == 2);
}
2018-01-05 20:34:10 +03:00
2023-11-29 17:02:51 +03:00
SECTION("const reference")
{
const json j = 1;
int counter = 1;
for (const auto& i : j.items())
{
++counter;
CHECK(i.key() == "");
CHECK(i.value() == json(1));
2018-01-05 20:34:10 +03:00
}
2023-11-29 17:02:51 +03:00
CHECK(counter == 2);
2018-01-05 20:34:10 +03:00
}
2018-12-20 23:40:47 +03:00
}
2023-11-29 17:02:51 +03:00
}
2020-12-29 22:16:51 +03:00
DOCTEST_GCC_SUPPRESS_WARNING_POP
DOCTEST_CLANG_SUPPRESS_WARNING_POP