mirror of
https://github.com/nlohmann/json.git
synced 2026-10-03 13:10:33 +00:00
Assert on the result of STL container conversions in tests
The "object-like STL containers" and "array-like STL containers" sections converted json values into std::map, unordered_map, multimap, unordered_multimap, list, forward_list, array, valarray, vector, deque, set and unordered_set and discarded the result, so these ~60 conversions only proved that the code compiles and does not throw; a conversion that dropped or reordered elements would still pass. Bind each result and compare it against the expected container. Also fix a copy-paste slip in the deque section (`j2.get<std::deque<double>>()` instead of j3, so j3's doubles were never converted to a deque), and remove the dead `// CHECK(m5["one"] == "eins")` comments that referred to a variable that did not exist by asserting the equivalent through the bound result. Part of #5713 Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -1372,21 +1372,22 @@ TEST_CASE("value conversion")
|
||||
|
||||
SECTION("std::map")
|
||||
{
|
||||
j1.get<std::map<std::string, int>>();
|
||||
j2.get<std::map<std::string, unsigned int>>();
|
||||
j3.get<std::map<std::string, double>>();
|
||||
j4.get<std::map<std::string, bool>>();
|
||||
j5.get<std::map<std::string, std::string>>();
|
||||
CHECK(j1.get<std::map<std::string, int>>() == (std::map<std::string, int> {{"one", 1}, {"two", 2}, {"three", 3}}));
|
||||
CHECK(j2.get<std::map<std::string, unsigned int>>() == (std::map<std::string, unsigned int> {{"one", 1u}, {"two", 2u}, {"three", 3u}}));
|
||||
CHECK(j3.get<std::map<std::string, double>>() == (std::map<std::string, double> {{"one", 1.1}, {"two", 2.2}, {"three", 3.3}}));
|
||||
CHECK(j4.get<std::map<std::string, bool>>() == (std::map<std::string, bool> {{"one", true}, {"two", false}, {"three", true}}));
|
||||
CHECK(j5.get<std::map<std::string, std::string>>() == (std::map<std::string, std::string> {{"one", "eins"}, {"two", "zwei"}, {"three", "drei"}}));
|
||||
}
|
||||
|
||||
SECTION("std::unordered_map")
|
||||
{
|
||||
j1.get<std::unordered_map<std::string, int>>();
|
||||
j2.get<std::unordered_map<std::string, unsigned int>>();
|
||||
j3.get<std::unordered_map<std::string, double>>();
|
||||
j4.get<std::unordered_map<std::string, bool>>();
|
||||
j5.get<std::unordered_map<std::string, std::string>>();
|
||||
// CHECK(m5["one"] == "eins");
|
||||
CHECK(j1.get<std::unordered_map<std::string, int>>() == (std::unordered_map<std::string, int> {{"one", 1}, {"two", 2}, {"three", 3}}));
|
||||
CHECK(j2.get<std::unordered_map<std::string, unsigned int>>() == (std::unordered_map<std::string, unsigned int> {{"one", 1u}, {"two", 2u}, {"three", 3u}}));
|
||||
CHECK(j3.get<std::unordered_map<std::string, double>>() == (std::unordered_map<std::string, double> {{"one", 1.1}, {"two", 2.2}, {"three", 3.3}}));
|
||||
CHECK(j4.get<std::unordered_map<std::string, bool>>() == (std::unordered_map<std::string, bool> {{"one", true}, {"two", false}, {"three", true}}));
|
||||
const auto m5 = j5.get<std::unordered_map<std::string, std::string>>();
|
||||
CHECK(m5 == (std::unordered_map<std::string, std::string> {{"one", "eins"}, {"two", "zwei"}, {"three", "drei"}}));
|
||||
CHECK(m5.at("one") == "eins");
|
||||
}
|
||||
|
||||
SECTION("reserve is called on containers that support it (#5406)")
|
||||
@@ -1422,22 +1423,24 @@ TEST_CASE("value conversion")
|
||||
|
||||
SECTION("std::multimap")
|
||||
{
|
||||
j1.get<std::multimap<std::string, int>>();
|
||||
j2.get<std::multimap<std::string, unsigned int>>();
|
||||
j3.get<std::multimap<std::string, double>>();
|
||||
j4.get<std::multimap<std::string, bool>>();
|
||||
j5.get<std::multimap<std::string, std::string>>();
|
||||
// CHECK(m5["one"] == "eins");
|
||||
CHECK(j1.get<std::multimap<std::string, int>>() == (std::multimap<std::string, int> {{"one", 1}, {"two", 2}, {"three", 3}}));
|
||||
CHECK(j2.get<std::multimap<std::string, unsigned int>>() == (std::multimap<std::string, unsigned int> {{"one", 1u}, {"two", 2u}, {"three", 3u}}));
|
||||
CHECK(j3.get<std::multimap<std::string, double>>() == (std::multimap<std::string, double> {{"one", 1.1}, {"two", 2.2}, {"three", 3.3}}));
|
||||
CHECK(j4.get<std::multimap<std::string, bool>>() == (std::multimap<std::string, bool> {{"one", true}, {"two", false}, {"three", true}}));
|
||||
const auto m5 = j5.get<std::multimap<std::string, std::string>>();
|
||||
CHECK(m5 == (std::multimap<std::string, std::string> {{"one", "eins"}, {"two", "zwei"}, {"three", "drei"}}));
|
||||
CHECK(m5.find("one")->second == "eins");
|
||||
}
|
||||
|
||||
SECTION("std::unordered_multimap")
|
||||
{
|
||||
j1.get<std::unordered_multimap<std::string, int>>();
|
||||
j2.get<std::unordered_multimap<std::string, unsigned int>>();
|
||||
j3.get<std::unordered_multimap<std::string, double>>();
|
||||
j4.get<std::unordered_multimap<std::string, bool>>();
|
||||
j5.get<std::unordered_multimap<std::string, std::string>>();
|
||||
// CHECK(m5["one"] == "eins");
|
||||
CHECK(j1.get<std::unordered_multimap<std::string, int>>() == (std::unordered_multimap<std::string, int> {{"one", 1}, {"two", 2}, {"three", 3}}));
|
||||
CHECK(j2.get<std::unordered_multimap<std::string, unsigned int>>() == (std::unordered_multimap<std::string, unsigned int> {{"one", 1u}, {"two", 2u}, {"three", 3u}}));
|
||||
CHECK(j3.get<std::unordered_multimap<std::string, double>>() == (std::unordered_multimap<std::string, double> {{"one", 1.1}, {"two", 2.2}, {"three", 3.3}}));
|
||||
CHECK(j4.get<std::unordered_multimap<std::string, bool>>() == (std::unordered_multimap<std::string, bool> {{"one", true}, {"two", false}, {"three", true}}));
|
||||
const auto m5 = j5.get<std::unordered_multimap<std::string, std::string>>();
|
||||
CHECK(m5 == (std::unordered_multimap<std::string, std::string> {{"one", "eins"}, {"two", "zwei"}, {"three", "drei"}}));
|
||||
CHECK(m5.find("one")->second == "eins");
|
||||
}
|
||||
|
||||
SECTION("exception in case of a non-object type")
|
||||
@@ -1458,29 +1461,30 @@ TEST_CASE("value conversion")
|
||||
|
||||
SECTION("std::list")
|
||||
{
|
||||
j1.get<std::list<int>>();
|
||||
j2.get<std::list<unsigned int>>();
|
||||
j3.get<std::list<double>>();
|
||||
j4.get<std::list<bool>>();
|
||||
j5.get<std::list<std::string>>();
|
||||
CHECK(j1.get<std::list<int>>() == (std::list<int> {1, 2, 3, 4}));
|
||||
CHECK(j2.get<std::list<unsigned int>>() == (std::list<unsigned int> {1u, 2u, 3u, 4u}));
|
||||
CHECK(j3.get<std::list<double>>() == (std::list<double> {1.2, 2.3, 3.4, 4.5}));
|
||||
CHECK(j4.get<std::list<bool>>() == (std::list<bool> {true, false, true}));
|
||||
CHECK(j5.get<std::list<std::string>>() == (std::list<std::string> {"one", "two", "three"}));
|
||||
}
|
||||
|
||||
SECTION("std::forward_list")
|
||||
{
|
||||
j1.get<std::forward_list<int>>();
|
||||
j2.get<std::forward_list<unsigned int>>();
|
||||
j3.get<std::forward_list<double>>();
|
||||
j4.get<std::forward_list<bool>>();
|
||||
j5.get<std::forward_list<std::string>>();
|
||||
CHECK(j1.get<std::forward_list<int>>() == (std::forward_list<int> {1, 2, 3, 4}));
|
||||
CHECK(j2.get<std::forward_list<unsigned int>>() == (std::forward_list<unsigned int> {1u, 2u, 3u, 4u}));
|
||||
CHECK(j3.get<std::forward_list<double>>() == (std::forward_list<double> {1.2, 2.3, 3.4, 4.5}));
|
||||
CHECK(j4.get<std::forward_list<bool>>() == (std::forward_list<bool> {true, false, true}));
|
||||
CHECK(j5.get<std::forward_list<std::string>>() == (std::forward_list<std::string> {"one", "two", "three"}));
|
||||
}
|
||||
|
||||
SECTION("std::array")
|
||||
{
|
||||
j1.get<std::array<int, 4>>();
|
||||
j2.get<std::array<unsigned int, 3>>();
|
||||
j3.get<std::array<double, 4>>();
|
||||
j4.get<std::array<bool, 3>>();
|
||||
j5.get<std::array<std::string, 3>>();
|
||||
CHECK(j1.get<std::array<int, 4>>() == (std::array<int, 4> {{1, 2, 3, 4}}));
|
||||
// only the first 3 elements of j2 are converted, since the target array is smaller
|
||||
CHECK(j2.get<std::array<unsigned int, 3>>() == (std::array<unsigned int, 3> {{1u, 2u, 3u}}));
|
||||
CHECK(j3.get<std::array<double, 4>>() == (std::array<double, 4> {{1.2, 2.3, 3.4, 4.5}}));
|
||||
CHECK(j4.get<std::array<bool, 3>>() == (std::array<bool, 3> {{true, false, true}}));
|
||||
CHECK(j5.get<std::array<std::string, 3>>() == (std::array<std::string, 3> {{"one", "two", "three"}}));
|
||||
|
||||
SECTION("std::array is larger than JSON")
|
||||
{
|
||||
@@ -1500,47 +1504,53 @@ TEST_CASE("value conversion")
|
||||
|
||||
SECTION("std::valarray")
|
||||
{
|
||||
j1.get<std::valarray<int>>();
|
||||
j2.get<std::valarray<unsigned int>>();
|
||||
j3.get<std::valarray<double>>();
|
||||
j4.get<std::valarray<bool>>();
|
||||
j5.get<std::valarray<std::string>>();
|
||||
// valarray has no operator== that returns bool, so compare via a vector copy
|
||||
const auto v1 = j1.get<std::valarray<int>>();
|
||||
CHECK((std::vector<int>(std::begin(v1), std::end(v1)) == std::vector<int> {1, 2, 3, 4}));
|
||||
const auto v2 = j2.get<std::valarray<unsigned int>>();
|
||||
CHECK((std::vector<unsigned int>(std::begin(v2), std::end(v2)) == std::vector<unsigned int> {1u, 2u, 3u, 4u}));
|
||||
const auto v3 = j3.get<std::valarray<double>>();
|
||||
CHECK((std::vector<double>(std::begin(v3), std::end(v3)) == std::vector<double> {1.2, 2.3, 3.4, 4.5}));
|
||||
const auto v4 = j4.get<std::valarray<bool>>();
|
||||
CHECK((std::vector<bool>(std::begin(v4), std::end(v4)) == std::vector<bool> {true, false, true}));
|
||||
const auto v5 = j5.get<std::valarray<std::string>>();
|
||||
CHECK((std::vector<std::string>(std::begin(v5), std::end(v5)) == std::vector<std::string> {"one", "two", "three"}));
|
||||
}
|
||||
|
||||
SECTION("std::vector")
|
||||
{
|
||||
j1.get<std::vector<int>>();
|
||||
j2.get<std::vector<unsigned int>>();
|
||||
j3.get<std::vector<double>>();
|
||||
j4.get<std::vector<bool>>();
|
||||
j5.get<std::vector<std::string>>();
|
||||
CHECK(j1.get<std::vector<int>>() == (std::vector<int> {1, 2, 3, 4}));
|
||||
CHECK(j2.get<std::vector<unsigned int>>() == (std::vector<unsigned int> {1u, 2u, 3u, 4u}));
|
||||
CHECK(j3.get<std::vector<double>>() == (std::vector<double> {1.2, 2.3, 3.4, 4.5}));
|
||||
CHECK(j4.get<std::vector<bool>>() == (std::vector<bool> {true, false, true}));
|
||||
CHECK(j5.get<std::vector<std::string>>() == (std::vector<std::string> {"one", "two", "three"}));
|
||||
}
|
||||
|
||||
SECTION("std::deque")
|
||||
{
|
||||
j1.get<std::deque<int>>();
|
||||
j2.get<std::deque<unsigned int>>();
|
||||
j2.get<std::deque<double>>();
|
||||
j4.get<std::deque<bool>>();
|
||||
j5.get<std::deque<std::string>>();
|
||||
CHECK(j1.get<std::deque<int>>() == (std::deque<int> {1, 2, 3, 4}));
|
||||
CHECK(j2.get<std::deque<unsigned int>>() == (std::deque<unsigned int> {1u, 2u, 3u, 4u}));
|
||||
CHECK(j3.get<std::deque<double>>() == (std::deque<double> {1.2, 2.3, 3.4, 4.5}));
|
||||
CHECK(j4.get<std::deque<bool>>() == (std::deque<bool> {true, false, true}));
|
||||
CHECK(j5.get<std::deque<std::string>>() == (std::deque<std::string> {"one", "two", "three"}));
|
||||
}
|
||||
|
||||
SECTION("std::set")
|
||||
{
|
||||
j1.get<std::set<int>>();
|
||||
j2.get<std::set<unsigned int>>();
|
||||
j3.get<std::set<double>>();
|
||||
j4.get<std::set<bool>>();
|
||||
j5.get<std::set<std::string>>();
|
||||
CHECK(j1.get<std::set<int>>() == (std::set<int> {1, 2, 3, 4}));
|
||||
CHECK(j2.get<std::set<unsigned int>>() == (std::set<unsigned int> {1u, 2u, 3u, 4u}));
|
||||
CHECK(j3.get<std::set<double>>() == (std::set<double> {1.2, 2.3, 3.4, 4.5}));
|
||||
CHECK(j4.get<std::set<bool>>() == (std::set<bool> {true, false, true}));
|
||||
CHECK(j5.get<std::set<std::string>>() == (std::set<std::string> {"one", "two", "three"}));
|
||||
}
|
||||
|
||||
SECTION("std::unordered_set")
|
||||
{
|
||||
j1.get<std::unordered_set<int>>();
|
||||
j2.get<std::unordered_set<unsigned int>>();
|
||||
j3.get<std::unordered_set<double>>();
|
||||
j4.get<std::unordered_set<bool>>();
|
||||
j5.get<std::unordered_set<std::string>>();
|
||||
CHECK(j1.get<std::unordered_set<int>>() == (std::unordered_set<int> {1, 2, 3, 4}));
|
||||
CHECK(j2.get<std::unordered_set<unsigned int>>() == (std::unordered_set<unsigned int> {1u, 2u, 3u, 4u}));
|
||||
CHECK(j3.get<std::unordered_set<double>>() == (std::unordered_set<double> {1.2, 2.3, 3.4, 4.5}));
|
||||
CHECK(j4.get<std::unordered_set<bool>>() == (std::unordered_set<bool> {true, false, true}));
|
||||
CHECK(j5.get<std::unordered_set<std::string>>() == (std::unordered_set<std::string> {"one", "two", "three"}));
|
||||
}
|
||||
|
||||
SECTION("std::map (array of pairs)")
|
||||
|
||||
Reference in New Issue
Block a user