Skip to content

Fix bug in ValueIteratorBase::operator- #172

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

Merged
merged 3 commits into from
Feb 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ class JSON_API ValueIteratorBase {
bool operator!=(const SelfType& other) const { return !isEqual(other); }

difference_type operator-(const SelfType& other) const {
return computeDistance(other);
return other.computeDistance(*this);
}

/// Return either the index or the member name of the referenced value as a
Expand Down
2 changes: 1 addition & 1 deletion src/lib_json/json_valueiterator.inl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ ValueIteratorBase::difference_type
ValueIteratorBase::computeDistance(const SelfType& other) const {
#ifndef JSON_VALUE_USE_INTERNAL_MAP
#ifdef JSON_USE_CPPTL_SMALLMAP
return current_ - other.current_;
return other.current_ - current_;
#else
// Iterator for null value are initialized using the default
// constructor, which initialize current_ to the default
Expand Down
19 changes: 19 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,23 @@ JSONTEST_FIXTURE(CharReaderFailIfExtraTest, commentAfterBool) {
JSONTEST_ASSERT_EQUAL(true, root.asBool());
delete reader;
}

struct IteratorTest : JsonTest::TestCase {};

JSONTEST_FIXTURE(IteratorTest, distance) {
Json::Value json;
json["k1"] = "a";
json["k2"] = "b";
int dist;
std::string str;
for (Json::ValueIterator it = json.begin(); it != json.end(); ++it) {
dist = it - json.begin();
str = it->asString().c_str();
}
JSONTEST_ASSERT_EQUAL(1, dist);
JSONTEST_ASSERT_STRING_EQUAL("b", str);
}

int main(int argc, const char* argv[]) {
JsonTest::Runner runner;
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, checkNormalizeFloatingPointStr);
Expand Down Expand Up @@ -1905,6 +1922,8 @@ int main(int argc, const char* argv[]) {
JSONTEST_REGISTER_FIXTURE(runner, CharReaderFailIfExtraTest, commentAfterArray);
JSONTEST_REGISTER_FIXTURE(runner, CharReaderFailIfExtraTest, commentAfterBool);

JSONTEST_REGISTER_FIXTURE(runner, IteratorTest, distance);

JSONTEST_REGISTER_FIXTURE(runner, WriterTest, dropNullPlaceholders);
JSONTEST_REGISTER_FIXTURE(runner, StreamWriterTest, dropNullPlaceholders);

Expand Down