-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Conversation
The travis test exceeded 10 mins time limit, but it seems that the other tests also took around 10 mins. Shall we extend the time limit a little? As for the AppVeyor fail, I think it's a result of one previous PR before mine. I really don't know what the coveralls is about. Is there a test report I can read? |
The latest several commits are fixing the incorrect arithmetic operations in reverse iterator. The changes include:
|
The exception message checking has been commented out. Although most compilers implements the |
The other commit is adding some missing test cases for the |
@HenryRLee Could you please merge the current |
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.
Hi @HenryRLee, I have some question on the changes.
test/src/unit-iterators2.cpp
Outdated
} | ||
{ | ||
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 comment
The 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?
test/src/unit-iterators2.cpp
Outdated
@@ -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 comment
The 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?
test/src/unit-iterators2.cpp
Outdated
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"); | ||
//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 comment
The 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?
test/src/unit-iterators2.cpp
Outdated
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"); | ||
//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 comment
The 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?
test/src/unit-iterators2.cpp
Outdated
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"); | ||
//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 comment
The 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?
test/src/unit-iterators2.cpp
Outdated
} | ||
{ | ||
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 comment
The 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?
test/src/unit-iterators2.cpp
Outdated
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"); | ||
//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 comment
The 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?
test/src/unit-iterators2.cpp
Outdated
@@ -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 comment
The 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.
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 []
to implement the []
in the reverse iterator, leading to another version exception output.
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.
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 comment
The 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 comment
The 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 comment
The 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 operator []
in the reverse iterator to calling the operator []
from std::reverse_iterator
directly.
/// 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 operator[]
in std::reverse_iterator
, which varies by compiler. Most of the compiler implements it using operator-
, but a few others uses operator[]
.
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.
@@ -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); |
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...
Hi Niels, I think this PR is ready for review. This one is supposed to be bug fixing and adding missing operators. As for the improvement at the end of #593, I can create another PR some other time if that's needed. |
Thanks a lot. We shall continue the discussion in #593. |
Thank you as well. |
Changes include: