From adc73d6d5f3354da5b95283279e06e615e848738 Mon Sep 17 00:00:00 2001 From: HenryLee Date: Mon, 29 May 2017 18:52:53 +1000 Subject: [PATCH] Fix the arithmetic mistakes in reverse iterator --- src/json.hpp | 6 +++--- test/src/unit-iterators2.cpp | 28 ++++++++++++++-------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 902d80f6..fb2740ab 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -8672,7 +8672,7 @@ class basic_json json_reverse_iterator operator+(difference_type i) const { auto result = *this; - result += i; + result -= i; return result; } @@ -8680,14 +8680,14 @@ class basic_json json_reverse_iterator operator-(difference_type i) const { auto result = *this; - result -= i; + result += i; return result; } /// return difference difference_type operator-(const json_reverse_iterator& other) const { - return this->base() - other.base(); + return other.base() - this->base(); } /// access to successor diff --git a/test/src/unit-iterators2.cpp b/test/src/unit-iterators2.cpp index d9f43e2f..3b767211 100644 --- a/test/src/unit-iterators2.cpp +++ b/test/src/unit-iterators2.cpp @@ -747,7 +747,7 @@ TEST_CASE("iterators 2") it += 3; CHECK((j_array.rbegin() + 3) == 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)); @@ -757,7 +757,7 @@ TEST_CASE("iterators 2") it += 3; CHECK((j_array.crbegin() + 3) == 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)); @@ -769,9 +769,9 @@ TEST_CASE("iterators 2") { auto it = j_null.rbegin(); it += 3; - CHECK((j_null.rbegin() + 3) == it); - CHECK((it - 3) == j_null.rbegin()); - CHECK((j_null.rbegin() - it) == 3); + CHECK((j_null.rbegin() - 3) == it); + CHECK((it + 3) == j_null.rbegin()); + CHECK((it - j_null.rbegin()) == 3); CHECK(it != j_null.rend()); it -= 3; CHECK(it == j_null.rend()); @@ -779,9 +779,9 @@ TEST_CASE("iterators 2") { auto it = j_null.crbegin(); it += 3; - CHECK((j_null.crbegin() + 3) == it); - CHECK((it - 3) == j_null.crbegin()); - CHECK((j_null.crbegin() - it) == 3); + CHECK((j_null.crbegin() - 3) == it); + CHECK((it + 3) == j_null.crbegin()); + CHECK((it - j_null.crbegin()) == 3); CHECK(it != j_null.crend()); it -= 3; CHECK(it == j_null.crend()); @@ -793,9 +793,9 @@ TEST_CASE("iterators 2") { auto it = j_value.rbegin(); it += 3; - CHECK((j_value.rbegin() + 3) == it); - CHECK((it - 3) == j_value.rbegin()); - CHECK((j_value.rbegin() - it) == 3); + CHECK((j_value.rbegin() - 3) == it); + CHECK((it + 3) == j_value.rbegin()); + CHECK((it - j_value.rbegin()) == 3); CHECK(it != j_value.rend()); it -= 3; CHECK(*it == json(42)); @@ -803,9 +803,9 @@ TEST_CASE("iterators 2") { auto it = j_value.crbegin(); it += 3; - CHECK((j_value.crbegin() + 3) == it); - CHECK((it - 3) == j_value.crbegin()); - CHECK((j_value.crbegin() - it) == 3); + CHECK((j_value.crbegin() - 3) == it); + CHECK((it + 3) == j_value.crbegin()); + CHECK((it - j_value.crbegin()) == 3); CHECK(it != j_value.crend()); it -= 3; CHECK(*it == json(42));