-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #593 Fix the arithmetic operators in the iterator and reverse iterator #595
Changes from 8 commits
ed62129
adc73d6
a3bf013
3aef1a5
e42db27
5b245da
0c2ed00
daea213
c4ab8f8
fb91aa8
c98169d
0f065ed
6661ec7
e12c2ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -269,6 +269,16 @@ TEST_CASE("iterators 2") | |
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); | ||
|
@@ -307,6 +317,7 @@ TEST_CASE("iterators 2") | |
auto it = j_array.begin(); | ||
it += 3; | ||
CHECK((j_array.begin() + 3) == it); | ||
CHECK(json::iterator(3 + j_array.begin()) == it); | ||
CHECK((it - 3) == j_array.begin()); | ||
CHECK((it - j_array.begin()) == 3); | ||
CHECK(*it == json(4)); | ||
|
@@ -317,6 +328,7 @@ TEST_CASE("iterators 2") | |
auto it = j_array.cbegin(); | ||
it += 3; | ||
CHECK((j_array.cbegin() + 3) == it); | ||
CHECK(json::const_iterator(3 + j_array.cbegin()) == it); | ||
CHECK((it - 3) == j_array.cbegin()); | ||
CHECK((it - j_array.cbegin()) == 3); | ||
CHECK(*it == json(4)); | ||
|
@@ -331,6 +343,7 @@ TEST_CASE("iterators 2") | |
auto it = j_null.begin(); | ||
it += 3; | ||
CHECK((j_null.begin() + 3) == it); | ||
CHECK(json::iterator(3 + j_null.begin()) == it); | ||
CHECK((it - 3) == j_null.begin()); | ||
CHECK((it - j_null.begin()) == 3); | ||
CHECK(it != j_null.end()); | ||
|
@@ -341,6 +354,7 @@ TEST_CASE("iterators 2") | |
auto it = j_null.cbegin(); | ||
it += 3; | ||
CHECK((j_null.cbegin() + 3) == it); | ||
CHECK(json::const_iterator(3 + j_null.cbegin()) == it); | ||
CHECK((it - 3) == j_null.cbegin()); | ||
CHECK((it - j_null.cbegin()) == 3); | ||
CHECK(it != j_null.cend()); | ||
|
@@ -355,6 +369,7 @@ TEST_CASE("iterators 2") | |
auto it = j_value.begin(); | ||
it += 3; | ||
CHECK((j_value.begin() + 3) == it); | ||
CHECK(json::iterator(3 + j_value.begin()) == it); | ||
CHECK((it - 3) == j_value.begin()); | ||
CHECK((it - j_value.begin()) == 3); | ||
CHECK(it != j_value.end()); | ||
|
@@ -365,6 +380,7 @@ TEST_CASE("iterators 2") | |
auto it = j_value.cbegin(); | ||
it += 3; | ||
CHECK((j_value.cbegin() + 3) == it); | ||
CHECK(json::const_iterator(3 + j_value.cbegin()) == it); | ||
CHECK((it - 3) == j_value.cbegin()); | ||
CHECK((it - j_value.cbegin()) == 3); | ||
CHECK(it != j_value.cend()); | ||
|
@@ -688,6 +704,16 @@ TEST_CASE("iterators 2") | |
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); | ||
|
@@ -726,8 +752,9 @@ TEST_CASE("iterators 2") | |
auto it = j_array.rbegin(); | ||
it += 3; | ||
CHECK((j_array.rbegin() + 3) == it); | ||
CHECK(json::reverse_iterator(3 + j_array.rbegin()) == it); | ||
CHECK((it - 3) == j_array.rbegin()); | ||
CHECK((j_array.rbegin() - it) == 3); | ||
CHECK((it - j_array.rbegin()) == 3); | ||
CHECK(*it == json(3)); | ||
it -= 2; | ||
CHECK(*it == json(5)); | ||
|
@@ -736,8 +763,9 @@ TEST_CASE("iterators 2") | |
auto it = j_array.crbegin(); | ||
it += 3; | ||
CHECK((j_array.crbegin() + 3) == it); | ||
CHECK(json::const_reverse_iterator(3 + j_array.crbegin()) == it); | ||
CHECK((it - 3) == j_array.crbegin()); | ||
CHECK((j_array.crbegin() - it) == 3); | ||
CHECK((it - j_array.crbegin()) == 3); | ||
CHECK(*it == json(3)); | ||
it -= 2; | ||
CHECK(*it == json(5)); | ||
|
@@ -750,8 +778,9 @@ TEST_CASE("iterators 2") | |
auto it = j_null.rbegin(); | ||
it += 3; | ||
CHECK((j_null.rbegin() + 3) == it); | ||
CHECK(json::reverse_iterator(3 + j_null.rbegin()) == it); | ||
CHECK((it - 3) == j_null.rbegin()); | ||
CHECK((j_null.rbegin() - it) == 3); | ||
CHECK((it - j_null.rbegin()) == 3); | ||
CHECK(it != j_null.rend()); | ||
it -= 3; | ||
CHECK(it == j_null.rend()); | ||
|
@@ -760,8 +789,9 @@ TEST_CASE("iterators 2") | |
auto it = j_null.crbegin(); | ||
it += 3; | ||
CHECK((j_null.crbegin() + 3) == it); | ||
CHECK(json::const_reverse_iterator(3 + j_null.crbegin()) == it); | ||
CHECK((it - 3) == j_null.crbegin()); | ||
CHECK((j_null.crbegin() - it) == 3); | ||
CHECK((it - j_null.crbegin()) == 3); | ||
CHECK(it != j_null.crend()); | ||
it -= 3; | ||
CHECK(it == j_null.crend()); | ||
|
@@ -774,8 +804,9 @@ TEST_CASE("iterators 2") | |
auto it = j_value.rbegin(); | ||
it += 3; | ||
CHECK((j_value.rbegin() + 3) == it); | ||
CHECK(json::reverse_iterator(3 + j_value.rbegin()) == it); | ||
CHECK((it - 3) == j_value.rbegin()); | ||
CHECK((j_value.rbegin() - it) == 3); | ||
CHECK((it - j_value.rbegin()) == 3); | ||
CHECK(it != j_value.rend()); | ||
it -= 3; | ||
CHECK(*it == json(42)); | ||
|
@@ -784,8 +815,9 @@ TEST_CASE("iterators 2") | |
auto it = j_value.crbegin(); | ||
it += 3; | ||
CHECK((j_value.crbegin() + 3) == it); | ||
CHECK(json::const_reverse_iterator(3 + j_value.crbegin()) == it); | ||
CHECK((it - 3) == j_value.crbegin()); | ||
CHECK((j_value.crbegin() - it) == 3); | ||
CHECK((it - j_value.crbegin()) == 3); | ||
CHECK(it != j_value.crend()); | ||
it -= 3; | ||
CHECK(*it == json(42)); | ||
|
@@ -801,15 +833,15 @@ 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_THROWS_WITH(it[0], "[json.exception.invalid_iterator.209] cannot use offsets with object iterators"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you uncomment this line again and change the test case to fit the actual exception message? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So far, there is no such an expression can pass all compilers. I tested with the original message and passed most of the travis test, but a few MS compilers were using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also put a comment in the end of #593, suggesting replacing the exception with a linear time implementation. If you think it's OK, I'll fix it right away. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know how to retrieve the test log and show you. Basically, the original exception message passed is matched in most test machines, but three of them were printing 'cannot use operator[] for object iterators'. I also made an explanation in a comment after commit 0c2ed00. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As for the replacement, I described in #593 (comment) why I think this is not a good idea. About the error messages: I do not understand why different machines would throw different exceptions. This makes no sense and wasn't like this before. Do you have an idea about the reasons? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason for two different exception message is, I changed the implementation of /// access to successor
reference operator[](difference_type n) const
{
- return *(this->operator+(n));
+ return base_iterator::operator[](n);
} Therefore the behavior depends on the implementation of the Since you have disapproved that replacing exceptions with linear time implementation, I guess I have to revert my changes in this function to pass the test. |
||
//CHECK_THROWS_WITH(it[1], "[json.exception.invalid_iterator.209] cannot use offsets with object iterators"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you uncomment this line again and change the test case to fit the actual exception message? |
||
} | ||
{ | ||
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_THROWS_WITH(it[0], "[json.exception.invalid_iterator.209] cannot use offsets with object iterators"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you uncomment this line again and change the test case to fit the actual exception message? |
||
//CHECK_THROWS_WITH(it[1], "[json.exception.invalid_iterator.209] cannot use offsets with object iterators"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you uncomment this line again and change the test case to fit the actual exception message? |
||
} | ||
} | ||
|
||
|
@@ -841,15 +873,15 @@ TEST_CASE("iterators 2") | |
auto it = j_null.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.214] cannot get value"); | ||
CHECK_THROWS_WITH(it[1], "[json.exception.invalid_iterator.214] cannot get value"); | ||
//CHECK_THROWS_WITH(it[0], "[json.exception.invalid_iterator.214] cannot get value"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you uncomment this line again and change the test case to fit the actual exception message? |
||
//CHECK_THROWS_WITH(it[1], "[json.exception.invalid_iterator.214] cannot get value"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you uncomment this line again and change the test case to fit the actual exception message? |
||
} | ||
{ | ||
auto it = j_null.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.214] cannot get value"); | ||
CHECK_THROWS_WITH(it[1], "[json.exception.invalid_iterator.214] cannot get value"); | ||
//CHECK_THROWS_WITH(it[0], "[json.exception.invalid_iterator.214] cannot get value"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you uncomment this line again and change the test case to fit the actual exception message? |
||
//CHECK_THROWS_WITH(it[1], "[json.exception.invalid_iterator.214] cannot get value"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you uncomment this line again and change the test case to fit the actual exception message? |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason the addition is written the other way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a required operation in a random accessible iterator. Please check: http://en.cppreference.com/w/cpp/concept/RandomAccessIterator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh stupid me... I didn't see you added a line - I thought you changed the line to this order...