Skip to content

Commit d2e6a97

Browse files
authored
some test coverage for Value::iterator (#1093)
1 parent 53c8e2c commit d2e6a97

File tree

1 file changed

+79
-9
lines changed

1 file changed

+79
-9
lines changed

src/test_lib_json/main.cpp

+79-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <cstring>
1717
#include <iomanip>
1818
#include <iostream>
19+
#include <iterator>
1920
#include <json/config.h>
2021
#include <json/json.h>
2122
#include <limits>
@@ -3394,18 +3395,85 @@ JSONTEST_FIXTURE_LOCAL(BuilderTest, settings) {
33943395

33953396
struct IteratorTest : JsonTest::TestCase {};
33963397

3397-
JSONTEST_FIXTURE_LOCAL(IteratorTest, distance) {
3398+
JSONTEST_FIXTURE_LOCAL(IteratorTest, convert) {
3399+
Json::Value j;
3400+
const Json::Value& cj = j;
3401+
auto it = j.begin();
3402+
Json::Value::const_iterator cit;
3403+
cit = it;
3404+
JSONTEST_ASSERT(cit == cj.begin());
3405+
}
3406+
3407+
JSONTEST_FIXTURE_LOCAL(IteratorTest, decrement) {
33983408
Json::Value json;
33993409
json["k1"] = "a";
34003410
json["k2"] = "b";
3401-
int dist = 0;
3402-
Json::String str;
3403-
for (Json::ValueIterator it = json.begin(); it != json.end(); ++it) {
3404-
dist = it - json.begin();
3405-
str = it->asString().c_str();
3411+
std::vector<std::string> values;
3412+
for (auto it = json.end(); it != json.begin();) {
3413+
--it;
3414+
values.push_back(it->asString());
3415+
}
3416+
JSONTEST_ASSERT((values == std::vector<std::string>{"b", "a"}));
3417+
}
3418+
3419+
JSONTEST_FIXTURE_LOCAL(IteratorTest, reverseIterator) {
3420+
Json::Value json;
3421+
json["k1"] = "a";
3422+
json["k2"] = "b";
3423+
std::vector<std::string> values;
3424+
using Iter = decltype(json.begin());
3425+
auto re = std::reverse_iterator<Iter>(json.begin());
3426+
for (auto it = std::reverse_iterator<Iter>(json.end()); it != re; ++it) {
3427+
values.push_back(it->asString());
3428+
}
3429+
JSONTEST_ASSERT((values == std::vector<std::string>{"b", "a"}));
3430+
}
3431+
3432+
JSONTEST_FIXTURE_LOCAL(IteratorTest, distance) {
3433+
{
3434+
Json::Value json;
3435+
json["k1"] = "a";
3436+
json["k2"] = "b";
3437+
int i = 0;
3438+
auto it = json.begin();
3439+
for (;; ++it, ++i) {
3440+
auto dist = it - json.begin();
3441+
JSONTEST_ASSERT_EQUAL(i, dist);
3442+
if (it == json.end())
3443+
break;
3444+
}
3445+
}
3446+
{
3447+
Json::Value empty;
3448+
JSONTEST_ASSERT_EQUAL(empty.end() - empty.end(), 0);
3449+
JSONTEST_ASSERT_EQUAL(empty.end() - empty.begin(), 0);
34063450
}
3407-
JSONTEST_ASSERT_EQUAL(1, dist);
3408-
JSONTEST_ASSERT_STRING_EQUAL("b", str);
3451+
}
3452+
3453+
JSONTEST_FIXTURE_LOCAL(IteratorTest, nullValues) {
3454+
{
3455+
Json::Value json;
3456+
auto end = json.end();
3457+
auto endCopy = end;
3458+
JSONTEST_ASSERT(endCopy == end);
3459+
endCopy = end;
3460+
JSONTEST_ASSERT(endCopy == end);
3461+
}
3462+
{
3463+
// Same test, now with const Value.
3464+
const Json::Value json;
3465+
auto end = json.end();
3466+
auto endCopy = end;
3467+
JSONTEST_ASSERT(endCopy == end);
3468+
endCopy = end;
3469+
JSONTEST_ASSERT(endCopy == end);
3470+
}
3471+
}
3472+
3473+
JSONTEST_FIXTURE_LOCAL(IteratorTest, staticStringKey) {
3474+
Json::Value json;
3475+
json[Json::StaticString("k1")] = "a";
3476+
JSONTEST_ASSERT_EQUAL(Json::Value("k1"), json.begin().key());
34093477
}
34103478

34113479
JSONTEST_FIXTURE_LOCAL(IteratorTest, names) {
@@ -3416,11 +3484,13 @@ JSONTEST_FIXTURE_LOCAL(IteratorTest, names) {
34163484
JSONTEST_ASSERT(it != json.end());
34173485
JSONTEST_ASSERT_EQUAL(Json::Value("k1"), it.key());
34183486
JSONTEST_ASSERT_STRING_EQUAL("k1", it.name());
3487+
JSONTEST_ASSERT_STRING_EQUAL("k1", it.memberName());
34193488
JSONTEST_ASSERT_EQUAL(-1, it.index());
34203489
++it;
34213490
JSONTEST_ASSERT(it != json.end());
34223491
JSONTEST_ASSERT_EQUAL(Json::Value("k2"), it.key());
34233492
JSONTEST_ASSERT_STRING_EQUAL("k2", it.name());
3493+
JSONTEST_ASSERT_STRING_EQUAL("k2", it.memberName());
34243494
JSONTEST_ASSERT_EQUAL(-1, it.index());
34253495
++it;
34263496
JSONTEST_ASSERT(it == json.end());
@@ -3444,7 +3514,7 @@ JSONTEST_FIXTURE_LOCAL(IteratorTest, indexes) {
34443514
JSONTEST_ASSERT(it == json.end());
34453515
}
34463516

3447-
JSONTEST_FIXTURE_LOCAL(IteratorTest, const) {
3517+
JSONTEST_FIXTURE_LOCAL(IteratorTest, constness) {
34483518
Json::Value const v;
34493519
JSONTEST_ASSERT_THROWS(
34503520
Json::Value::iterator it(v.begin())); // Compile, but throw.

0 commit comments

Comments
 (0)