💥 changed iterators to andom_access_iterator #593
This commit changes the iterator category to andom_access_iterator and allows offsets and subscript operators for object iterators.
This commit is contained in:
parent
aba8b58492
commit
c77a0be5f3
3 changed files with 68 additions and 138 deletions
|
@ -240,9 +240,8 @@ TEST_CASE("algorithms")
|
|||
SECTION("sorting an object")
|
||||
{
|
||||
json j({{"one", 1}, {"two", 2}});
|
||||
CHECK_THROWS_AS(std::sort(j.begin(), j.end()), json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(std::sort(j.begin(), j.end()),
|
||||
"[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
std::sort(j.begin(), j.end());
|
||||
CHECK(j == json({{"one", 1}, {"two", 2}}));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -240,7 +240,7 @@ TEST_CASE("iterators 2")
|
|||
|
||||
SECTION("iterator arithmetic")
|
||||
{
|
||||
json j_object = {{"one", 1}, {"two", 2}, {"three", 3}};
|
||||
json j_object = {{"one", 1}, {"two", 2}, {"three", 3}, {"four", 4}};
|
||||
json j_array = {1, 2, 3, 4, 5, 6};
|
||||
json j_null = nullptr;
|
||||
json j_value = 42;
|
||||
|
@ -251,63 +251,25 @@ TEST_CASE("iterators 2")
|
|||
{
|
||||
{
|
||||
auto it = j_object.begin();
|
||||
CHECK_THROWS_AS(it += 1, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it += 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
it += 3;
|
||||
CHECK((j_object.begin() + 3) == it);
|
||||
CHECK((3 + j_object.begin()) == it);
|
||||
CHECK((it - 3) == j_object.begin());
|
||||
CHECK((it - j_object.begin()) == 3);
|
||||
CHECK(*it == json(2));
|
||||
it -= 2;
|
||||
CHECK(*it == json(1));
|
||||
}
|
||||
{
|
||||
auto it = j_object.cbegin();
|
||||
CHECK_THROWS_AS(it += 1, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it += 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.begin();
|
||||
CHECK_THROWS_AS(it + 1, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it + 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.cbegin();
|
||||
CHECK_THROWS_AS(it + 1, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it + 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.begin();
|
||||
CHECK_THROWS_AS(1 + it, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(1 + it, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.cbegin();
|
||||
CHECK_THROWS_AS(1 + it, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(1 + it, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.begin();
|
||||
CHECK_THROWS_AS(it -= 1, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it -= 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.cbegin();
|
||||
CHECK_THROWS_AS(it -= 1, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it -= 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.begin();
|
||||
CHECK_THROWS_AS(it - 1, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it - 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.cbegin();
|
||||
CHECK_THROWS_AS(it - 1, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it - 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.begin();
|
||||
CHECK_THROWS_AS(it - it, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it - it, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.cbegin();
|
||||
CHECK_THROWS_AS(it - it, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it - it, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
it += 3;
|
||||
CHECK((j_object.cbegin() + 3) == it);
|
||||
CHECK((3 + j_object.cbegin()) == it);
|
||||
CHECK((it - 3) == j_object.cbegin());
|
||||
CHECK((it - j_object.cbegin()) == 3);
|
||||
CHECK(*it == json(2));
|
||||
it -= 2;
|
||||
CHECK(*it == json(1));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -396,17 +358,17 @@ TEST_CASE("iterators 2")
|
|||
{
|
||||
{
|
||||
auto it = j_object.begin();
|
||||
CHECK_THROWS_AS(it[0], json::invalid_iterator&);
|
||||
CHECK_THROWS_AS(it[1], json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it[0], "[json.exception.invalid_iterator.208] cannot use operator[] for object iterators");
|
||||
CHECK_THROWS_WITH(it[1], "[json.exception.invalid_iterator.208] cannot use operator[] for object iterators");
|
||||
CHECK(it[0] == json(4));
|
||||
CHECK(it[1] == json(1));
|
||||
CHECK(it[2] == json(3));
|
||||
CHECK(it[3] == json(2));
|
||||
}
|
||||
{
|
||||
auto it = j_object.cbegin();
|
||||
CHECK_THROWS_AS(it[0], json::invalid_iterator&);
|
||||
CHECK_THROWS_AS(it[1], json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it[0], "[json.exception.invalid_iterator.208] cannot use operator[] for object iterators");
|
||||
CHECK_THROWS_WITH(it[1], "[json.exception.invalid_iterator.208] cannot use operator[] for object iterators");
|
||||
CHECK(it[0] == json(4));
|
||||
CHECK(it[1] == json(1));
|
||||
CHECK(it[2] == json(3));
|
||||
CHECK(it[3] == json(2));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -675,7 +637,7 @@ TEST_CASE("iterators 2")
|
|||
|
||||
SECTION("reverse iterator arithmetic")
|
||||
{
|
||||
json j_object = {{"one", 1}, {"two", 2}, {"three", 3}};
|
||||
json j_object = {{"one", 1}, {"two", 2}, {"three", 3}, {"four", 4}};
|
||||
json j_array = {1, 2, 3, 4, 5, 6};
|
||||
json j_null = nullptr;
|
||||
json j_value = 42;
|
||||
|
@ -686,63 +648,25 @@ TEST_CASE("iterators 2")
|
|||
{
|
||||
{
|
||||
auto it = j_object.rbegin();
|
||||
CHECK_THROWS_AS(it += 1, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it += 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
it += 3;
|
||||
CHECK((j_object.rbegin() + 3) == it);
|
||||
CHECK(json::reverse_iterator(3 + j_object.rbegin()) == it);
|
||||
CHECK((it - 3) == j_object.rbegin());
|
||||
CHECK((it - j_object.rbegin()) == 3);
|
||||
CHECK(*it == json(4));
|
||||
it -= 2;
|
||||
CHECK(*it == json(3));
|
||||
}
|
||||
{
|
||||
auto it = j_object.crbegin();
|
||||
CHECK_THROWS_AS(it += 1, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it += 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.rbegin();
|
||||
CHECK_THROWS_AS(it + 1, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it + 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.crbegin();
|
||||
CHECK_THROWS_AS(it + 1, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it + 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.rbegin();
|
||||
CHECK_THROWS_AS(1 + it, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(1 + it, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.crbegin();
|
||||
CHECK_THROWS_AS(1 + it, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(1 + it, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.rbegin();
|
||||
CHECK_THROWS_AS(it -= 1, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it -= 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.crbegin();
|
||||
CHECK_THROWS_AS(it -= 1, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it -= 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.rbegin();
|
||||
CHECK_THROWS_AS(it - 1, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it - 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.crbegin();
|
||||
CHECK_THROWS_AS(it - 1, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it - 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.rbegin();
|
||||
CHECK_THROWS_AS(it - it, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it - it, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
}
|
||||
{
|
||||
auto it = j_object.crbegin();
|
||||
CHECK_THROWS_AS(it - it, json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it - it, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
it += 3;
|
||||
CHECK((j_object.crbegin() + 3) == it);
|
||||
CHECK(json::const_reverse_iterator(3 + j_object.crbegin()) == it);
|
||||
CHECK((it - 3) == j_object.crbegin());
|
||||
CHECK((it - j_object.crbegin()) == 3);
|
||||
CHECK(*it == json(4));
|
||||
it -= 2;
|
||||
CHECK(*it == json(3));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -831,17 +755,17 @@ TEST_CASE("iterators 2")
|
|||
{
|
||||
{
|
||||
auto it = j_object.rbegin();
|
||||
CHECK_THROWS_AS(it[0], json::invalid_iterator&);
|
||||
CHECK_THROWS_AS(it[1], json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it[0], "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
CHECK_THROWS_WITH(it[1], "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
CHECK(it[0] == json(2));
|
||||
CHECK(it[1] == json(3));
|
||||
CHECK(it[2] == json(1));
|
||||
CHECK(it[3] == json(4));
|
||||
}
|
||||
{
|
||||
auto it = j_object.crbegin();
|
||||
CHECK_THROWS_AS(it[0], json::invalid_iterator&);
|
||||
CHECK_THROWS_AS(it[1], json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH(it[0], "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
CHECK_THROWS_WITH(it[1], "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
|
||||
CHECK(it[0] == json(2));
|
||||
CHECK(it[1] == json(3));
|
||||
CHECK(it[2] == json(1));
|
||||
CHECK(it[3] == json(4));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue