16
16
#include < cstring>
17
17
#include < iomanip>
18
18
#include < iostream>
19
+ #include < iterator>
19
20
#include < json/config.h>
20
21
#include < json/json.h>
21
22
#include < limits>
@@ -3394,18 +3395,85 @@ JSONTEST_FIXTURE_LOCAL(BuilderTest, settings) {
3394
3395
3395
3396
struct IteratorTest : JsonTest::TestCase {};
3396
3397
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) {
3398
3408
Json::Value json;
3399
3409
json[" k1" ] = " a" ;
3400
3410
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 );
3406
3450
}
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 ());
3409
3477
}
3410
3478
3411
3479
JSONTEST_FIXTURE_LOCAL (IteratorTest, names) {
@@ -3416,11 +3484,13 @@ JSONTEST_FIXTURE_LOCAL(IteratorTest, names) {
3416
3484
JSONTEST_ASSERT (it != json.end ());
3417
3485
JSONTEST_ASSERT_EQUAL (Json::Value (" k1" ), it.key ());
3418
3486
JSONTEST_ASSERT_STRING_EQUAL (" k1" , it.name ());
3487
+ JSONTEST_ASSERT_STRING_EQUAL (" k1" , it.memberName ());
3419
3488
JSONTEST_ASSERT_EQUAL (-1 , it.index ());
3420
3489
++it;
3421
3490
JSONTEST_ASSERT (it != json.end ());
3422
3491
JSONTEST_ASSERT_EQUAL (Json::Value (" k2" ), it.key ());
3423
3492
JSONTEST_ASSERT_STRING_EQUAL (" k2" , it.name ());
3493
+ JSONTEST_ASSERT_STRING_EQUAL (" k2" , it.memberName ());
3424
3494
JSONTEST_ASSERT_EQUAL (-1 , it.index ());
3425
3495
++it;
3426
3496
JSONTEST_ASSERT (it == json.end ());
@@ -3444,7 +3514,7 @@ JSONTEST_FIXTURE_LOCAL(IteratorTest, indexes) {
3444
3514
JSONTEST_ASSERT (it == json.end ());
3445
3515
}
3446
3516
3447
- JSONTEST_FIXTURE_LOCAL (IteratorTest, const ) {
3517
+ JSONTEST_FIXTURE_LOCAL (IteratorTest, constness ) {
3448
3518
Json::Value const v;
3449
3519
JSONTEST_ASSERT_THROWS (
3450
3520
Json::Value::iterator it (v.begin ())); // Compile, but throw.
0 commit comments