From 8ba98759625ca528a73a8d37c5cdad34e7d21bc3 Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Sun, 15 Feb 2015 02:38:20 -0600 Subject: [PATCH 1/3] IteratorTest --- src/test_lib_json/main.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index bc8b9ae..dfa7c69 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -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); @@ -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); From 4c5832a0be947baf786e9a7185634bace3ac8382 Mon Sep 17 00:00:00 2001 From: Kevin Grant Date: Sat, 14 Feb 2015 20:53:35 -0800 Subject: [PATCH 2/3] Fix bug in ValueIteratorBase::operator- --- include/json/value.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/json/value.h b/include/json/value.h index 0c507a3..62a323b 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -946,7 +946,7 @@ public: 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 From bd55164089bdb0088c789c565cb99f575b1db0fa Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Sun, 15 Feb 2015 02:31:17 -0600 Subject: [PATCH 3/3] reverse sense for CPPTL too --- src/lib_json/json_valueiterator.inl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib_json/json_valueiterator.inl b/src/lib_json/json_valueiterator.inl index a9f7df6..b52c146 100644 --- a/src/lib_json/json_valueiterator.inl +++ b/src/lib_json/json_valueiterator.inl @@ -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