From 9b0ba5026a5ace733d845a907631faf2a2d732ac Mon Sep 17 00:00:00 2001 From: lilinchao Date: Fri, 21 Jun 2019 15:44:08 +0800 Subject: [PATCH 01/14] add a new method to insert a new value in an array at specific index. --- include/json/value.h | 3 ++- src/lib_json/json_value.cpp | 21 ++++++++++++++++++++- src/test_lib_json/main.cpp | 14 +++++++++++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/include/json/value.h b/include/json/value.h index 5f8d21444..2d2df50df 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -463,8 +463,9 @@ Json::Value obj_value(Json::objectValue); // {} /// /// Equivalent to jsonvalue[jsonvalue.size()] = value; Value& append(const Value& value); + /// \brief Insert value in array at specific index + bool insert(ArrayIndex index, const Value& newValue); Value& append(Value&& value); - /// Access an object value by name, create a null member if it does not exist. /// \note Because of our implementation, keys are limited to 2^30 -1 chars. /// Exceeding that will cause an exception. diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index b046c1939..f9c7941be 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -1170,7 +1170,26 @@ Value const& Value::operator[](CppTL::ConstString const& key) const { #endif Value& Value::append(const Value& value) { return (*this)[size()] = value; } - +/// \brief Insert value in array at specific index +bool Value::insert(ArrayIndex index, const Value& newValue){ + if(type() != arrayValue){ + return false; + } + if(!isValidIndex()){ + return false; + } + ArrayIndex oldsize = size(); + resize(oldsize+1); + ArrayIndex length = size(); + if(length != oldsize +1){ + return false; + } + for(ArrayIndex i = length ; i> index ; i--){ + (*this)[i] = (*this)[i-1]; + } + (*this)[index]= newValue; + return true; +} Value& Value::append(Value&& value) { return (*this)[size()] = std::move(value); } diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 280f7eac6..60e04492d 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -307,7 +307,18 @@ JSONTEST_FIXTURE(ValueTest, arrayIssue252) { } // JSONTEST_ASSERT_EQUAL(5, root["array"].size()); } - +JSONTEST_FIXTURE(ValueTest, arrayIssue691) { + Json::Value array2_; + array2_.append(1); + array2_.append(2); + array2_.append(3); + array2_.append(5); + ////use insert method + array2_.insert(3,4); + JSONTEST_ASSERT_EQUAL(Json::Value(3),array2_[2]); + JSONTEST_ASSERT_EQUAL(Json::Value(4),array2_[3]); + JSONTEST_ASSERT_EQUAL(Json::Value(5),array2_[4]); +} JSONTEST_FIXTURE(ValueTest, null) { JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type()); @@ -2535,6 +2546,7 @@ int main(int argc, const char* argv[]) { JSONTEST_REGISTER_FIXTURE(runner, ValueTest, objects); JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrays); JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrayIssue252); + JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrayIssue691); JSONTEST_REGISTER_FIXTURE(runner, ValueTest, null); JSONTEST_REGISTER_FIXTURE(runner, ValueTest, strings); JSONTEST_REGISTER_FIXTURE(runner, ValueTest, bools); From 4b9a293e04605cafc0a4a3ef27f07cfed9ff88dd Mon Sep 17 00:00:00 2001 From: lilinchao Date: Tue, 25 Jun 2019 10:28:36 +0800 Subject: [PATCH 02/14] run clang-format. --- include/json/value.h | 4 ++++ src/lib_json/json_value.cpp | 34 ++++++++++++++++++++++++++-------- src/test_lib_json/main.cpp | 23 +++++++++++++---------- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/include/json/value.h b/include/json/value.h index 2d2df50df..e13284534 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -465,7 +465,11 @@ Json::Value obj_value(Json::objectValue); // {} Value& append(const Value& value); /// \brief Insert value in array at specific index bool insert(ArrayIndex index, const Value& newValue); +#if JSON_HAS_RVALUE_REFERENCES Value& append(Value&& value); + bool insert(ArrayIndex index, const Value&& newValue); +#endif + /// Access an object value by name, create a null member if it does not exist. /// \note Because of our implementation, keys are limited to 2^30 -1 chars. /// Exceeding that will cause an exception. diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index f9c7941be..cd5e92c02 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -1171,29 +1171,47 @@ Value const& Value::operator[](CppTL::ConstString const& key) const { Value& Value::append(const Value& value) { return (*this)[size()] = value; } /// \brief Insert value in array at specific index -bool Value::insert(ArrayIndex index, const Value& newValue){ - if(type() != arrayValue){ +bool Value::insert(ArrayIndex index, const Value& newValue) { + if(type() != arrayValue) { return false; } - if(!isValidIndex()){ + if(!isValidIndex(index)) { return false; } ArrayIndex oldsize = size(); resize(oldsize+1); ArrayIndex length = size(); - if(length != oldsize +1){ + if(length != oldsize +1) { return false; } - for(ArrayIndex i = length ; i> index ; i--){ + for(ArrayIndex i = length ; i> index ; i--) { (*this)[i] = (*this)[i-1]; } (*this)[index]= newValue; return true; } -Value& Value::append(Value&& value) { - return (*this)[size()] = std::move(value); +#if JSON_HAS_RVALUE_REFERENCES +Value& Value::append(Value&& value) { return (*this)[size()] = std::move(value); } +bool Value::insert(ArrayIndex index, const Value&& newValue) { + if(type() != arrayValue) { + return false; + } + if(!isValidIndex(index)) { + return false; + } + ArrayIndex oldsize = size(); + resize(oldsize+1); + ArrayIndex length = size(); + if(length != oldsize +1) { + return false; + } + for(ArrayIndex i = length ; i> index ; i--) { + (*this)[i] = (*this)[i-1]; + } + (*this)[index]= std::move(newValue); + return true; } - +#endif Value Value::get(char const* begin, char const* end, Value const& defaultValue) const { diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 60e04492d..79c542c7e 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -308,16 +308,19 @@ JSONTEST_FIXTURE(ValueTest, arrayIssue252) { // JSONTEST_ASSERT_EQUAL(5, root["array"].size()); } JSONTEST_FIXTURE(ValueTest, arrayIssue691) { - Json::Value array2_; - array2_.append(1); - array2_.append(2); - array2_.append(3); - array2_.append(5); - ////use insert method - array2_.insert(3,4); - JSONTEST_ASSERT_EQUAL(Json::Value(3),array2_[2]); - JSONTEST_ASSERT_EQUAL(Json::Value(4),array2_[3]); - JSONTEST_ASSERT_EQUAL(Json::Value(5),array2_[4]); + Json::Value array2; + array2.append(10); // index 0 + array2.append(20); // index 1 + array2.append(30); // index 2 + array2.append(50); // index 3 + + JSONTEST_ASSERT_EQUAL(Json::Value(50),array2[3]); + + array2.insert(3,40); // index 3 + // After updating, index 3 should be changed from 50 to 40, and 50 moved to + // index 4. + JSONTEST_ASSERT_EQUAL(Json::Value(40),array2[3]); + JSONTEST_ASSERT_EQUAL(Json::Value(50),array2[4]); } JSONTEST_FIXTURE(ValueTest, null) { JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type()); From 8a1d8693034ed44271db707c55e14fc517a8c5b9 Mon Sep 17 00:00:00 2001 From: lilinchao Date: Mon, 1 Jul 2019 20:06:55 +0800 Subject: [PATCH 03/14] update --- src/lib_json/json_value.cpp | 8 ++----- src/test_lib_json/main.cpp | 43 ++++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index cd5e92c02..b583eb96e 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -1181,9 +1181,7 @@ bool Value::insert(ArrayIndex index, const Value& newValue) { ArrayIndex oldsize = size(); resize(oldsize+1); ArrayIndex length = size(); - if(length != oldsize +1) { - return false; - } + for(ArrayIndex i = length ; i> index ; i--) { (*this)[i] = (*this)[i-1]; } @@ -1202,9 +1200,7 @@ bool Value::insert(ArrayIndex index, const Value&& newValue) { ArrayIndex oldsize = size(); resize(oldsize+1); ArrayIndex length = size(); - if(length != oldsize +1) { - return false; - } + for(ArrayIndex i = length ; i> index ; i--) { (*this)[i] = (*this)[i-1]; } diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 79c542c7e..b0dc0a324 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -307,20 +307,33 @@ JSONTEST_FIXTURE(ValueTest, arrayIssue252) { } // JSONTEST_ASSERT_EQUAL(5, root["array"].size()); } -JSONTEST_FIXTURE(ValueTest, arrayIssue691) { - Json::Value array2; - array2.append(10); // index 0 - array2.append(20); // index 1 - array2.append(30); // index 2 - array2.append(50); // index 3 - - JSONTEST_ASSERT_EQUAL(Json::Value(50),array2[3]); - - array2.insert(3,40); // index 3 - // After updating, index 3 should be changed from 50 to 40, and 50 moved to - // index 4. - JSONTEST_ASSERT_EQUAL(Json::Value(40),array2[3]); - JSONTEST_ASSERT_EQUAL(Json::Value(50),array2[4]); +JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) { + Json::Value array; + JSONCPP_STRING str = "index5"; + array.append("index0"); + array.append("index1"); + array.append("index2"); + array.append("index3"); + + JSONTEST_ASSERT_EQUAL(Json::Value("index0"),array[0]); + JSONTEST_ASSERT_EQUAL(Json::Value("index1"),array[1]); + JSONTEST_ASSERT_EQUAL(Json::Value("index2"),array[2]); + JSONTEST_ASSERT_EQUAL(Json::Value("index3"),array[3]); + + array.insert(3,"index4"); //rvalue + JSONTEST_ASSERT_EQUAL(Json::Value("index0"),array[0]); + JSONTEST_ASSERT_EQUAL(Json::Value("index1"),array[1]); + JSONTEST_ASSERT_EQUAL(Json::Value("index2"),array[2]); + JSONTEST_ASSERT_EQUAL(Json::Value("index4"),array[3]); + JSONTEST_ASSERT_EQUAL(Json::Value("index3"),array[4]); + + array.insert(4,str); //lvalue + JSONTEST_ASSERT_EQUAL(Json::Value("index0"),array[0]); + JSONTEST_ASSERT_EQUAL(Json::Value("index1"),array[1]); + JSONTEST_ASSERT_EQUAL(Json::Value("index2"),array[2]); + JSONTEST_ASSERT_EQUAL(Json::Value("index4"),array[3]); + JSONTEST_ASSERT_EQUAL(Json::Value("index5"),array[4]); + JSONTEST_ASSERT_EQUAL(Json::Value("index3"),array[5]); } JSONTEST_FIXTURE(ValueTest, null) { JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type()); @@ -2549,7 +2562,7 @@ int main(int argc, const char* argv[]) { JSONTEST_REGISTER_FIXTURE(runner, ValueTest, objects); JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrays); JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrayIssue252); - JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrayIssue691); + JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrayInsertAtRandomIndex); JSONTEST_REGISTER_FIXTURE(runner, ValueTest, null); JSONTEST_REGISTER_FIXTURE(runner, ValueTest, strings); JSONTEST_REGISTER_FIXTURE(runner, ValueTest, bools); From 288028d150fd3dff2def848f983481d1ef994e2a Mon Sep 17 00:00:00 2001 From: lilinchao Date: Mon, 1 Jul 2019 20:06:55 +0800 Subject: [PATCH 04/14] update --- src/lib_json/json_value.cpp | 39 ++++++++++++------------ src/test_lib_json/jsontest.cpp | 4 +-- src/test_lib_json/main.cpp | 54 ++++++++++++++++++++++------------ 3 files changed, 55 insertions(+), 42 deletions(-) diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index cd5e92c02..393fd8c79 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -1172,43 +1172,41 @@ Value const& Value::operator[](CppTL::ConstString const& key) const { Value& Value::append(const Value& value) { return (*this)[size()] = value; } /// \brief Insert value in array at specific index bool Value::insert(ArrayIndex index, const Value& newValue) { - if(type() != arrayValue) { + if (type() != arrayValue) { return false; } - if(!isValidIndex(index)) { + if (!isValidIndex(index)) { return false; } ArrayIndex oldsize = size(); - resize(oldsize+1); + resize(oldsize + 1); ArrayIndex length = size(); - if(length != oldsize +1) { - return false; - } - for(ArrayIndex i = length ; i> index ; i--) { - (*this)[i] = (*this)[i-1]; + + for (ArrayIndex i = length; i > index; i--) { + (*this)[i] = (*this)[i - 1]; } - (*this)[index]= newValue; + (*this)[index] = newValue; return true; } #if JSON_HAS_RVALUE_REFERENCES -Value& Value::append(Value&& value) { return (*this)[size()] = std::move(value); } +Value& Value::append(Value&& value) { + return (*this)[size()] = std::move(value); +} bool Value::insert(ArrayIndex index, const Value&& newValue) { - if(type() != arrayValue) { + if (type() != arrayValue) { return false; } - if(!isValidIndex(index)) { + if (!isValidIndex(index)) { return false; } ArrayIndex oldsize = size(); - resize(oldsize+1); + resize(oldsize + 1); ArrayIndex length = size(); - if(length != oldsize +1) { - return false; - } - for(ArrayIndex i = length ; i> index ; i--) { - (*this)[i] = (*this)[i-1]; + + for (ArrayIndex i = length; i > index; i--) { + (*this)[i] = (*this)[i - 1]; } - (*this)[index]= std::move(newValue); + (*this)[index] = std::move(newValue); return true; } #endif @@ -1473,8 +1471,7 @@ bool Value::isObject() const { return type() == objectValue; } Value::Comments::Comments(const Comments& that) : ptr_{cloneUnique(that.ptr_)} {} -Value::Comments::Comments(Comments&& that) - : ptr_{std::move(that.ptr_)} {} +Value::Comments::Comments(Comments&& that) : ptr_{std::move(that.ptr_)} {} Value::Comments& Value::Comments::operator=(const Comments& that) { ptr_ = cloneUnique(that.ptr_); diff --git a/src/test_lib_json/jsontest.cpp b/src/test_lib_json/jsontest.cpp index c0b5296d9..4c59d07b5 100644 --- a/src/test_lib_json/jsontest.cpp +++ b/src/test_lib_json/jsontest.cpp @@ -378,8 +378,8 @@ void Runner::preventDialogOnCrash() { _CrtSetReportHook(&msvcrtSilentReportHook); #endif // if defined(_MSC_VER) - // @todo investigate this handler (for buffer overflow) - // _set_security_error_handler +// @todo investigate this handler (for buffer overflow) +// _set_security_error_handler #if defined(_WIN32) // Prevents the system from popping a dialog for debugging if the diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 79c542c7e..cd00265a9 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -211,14 +211,17 @@ JSONTEST_FIXTURE(ValueTest, objects) { JSONTEST_ASSERT_EQUAL(Json::Value(1234), *foundId); const char unknownIdKey[] = "unknown id"; - const Json::Value* foundUnknownId = object1_.find(unknownIdKey, unknownIdKey + strlen(unknownIdKey)); + const Json::Value* foundUnknownId = + object1_.find(unknownIdKey, unknownIdKey + strlen(unknownIdKey)); JSONTEST_ASSERT_EQUAL(nullptr, foundUnknownId); // Access through demand() const char yetAnotherIdKey[] = "yet another id"; - const Json::Value* foundYetAnotherId = object1_.find(yetAnotherIdKey, yetAnotherIdKey + strlen(yetAnotherIdKey)); + const Json::Value* foundYetAnotherId = + object1_.find(yetAnotherIdKey, yetAnotherIdKey + strlen(yetAnotherIdKey)); JSONTEST_ASSERT_EQUAL(nullptr, foundYetAnotherId); - Json::Value* demandedYetAnotherId = object1_.demand(yetAnotherIdKey, yetAnotherIdKey + strlen(yetAnotherIdKey)); + Json::Value* demandedYetAnotherId = object1_.demand( + yetAnotherIdKey, yetAnotherIdKey + strlen(yetAnotherIdKey)); JSONTEST_ASSERT(demandedYetAnotherId != nullptr); *demandedYetAnotherId = "baz"; @@ -307,20 +310,33 @@ JSONTEST_FIXTURE(ValueTest, arrayIssue252) { } // JSONTEST_ASSERT_EQUAL(5, root["array"].size()); } -JSONTEST_FIXTURE(ValueTest, arrayIssue691) { - Json::Value array2; - array2.append(10); // index 0 - array2.append(20); // index 1 - array2.append(30); // index 2 - array2.append(50); // index 3 - - JSONTEST_ASSERT_EQUAL(Json::Value(50),array2[3]); - - array2.insert(3,40); // index 3 - // After updating, index 3 should be changed from 50 to 40, and 50 moved to - // index 4. - JSONTEST_ASSERT_EQUAL(Json::Value(40),array2[3]); - JSONTEST_ASSERT_EQUAL(Json::Value(50),array2[4]); +JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) { + Json::Value array; + JSONCPP_STRING str = "index5"; + array.append("index0"); + array.append("index1"); + array.append("index2"); + array.append("index3"); + + JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); + JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); + JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); + JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[3]); + + array.insert(3, "index4"); // rvalue + JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); + JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); + JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); + JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]); + JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[4]); + + array.insert(4, str); // lvalue + JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); + JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); + JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); + JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]); + JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array[4]); + JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[5]); } JSONTEST_FIXTURE(ValueTest, null) { JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type()); @@ -2509,7 +2525,7 @@ JSONTEST_FIXTURE(IteratorTest, const) { Json::Value const v; JSONTEST_ASSERT_THROWS( Json::Value::iterator it(v.begin()) // Compile, but throw. - ); + ); Json::Value value; @@ -2549,7 +2565,7 @@ int main(int argc, const char* argv[]) { JSONTEST_REGISTER_FIXTURE(runner, ValueTest, objects); JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrays); JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrayIssue252); - JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrayIssue691); + JSONTEST_REGISTER_FIXTURE(runner, ValueTest, arrayInsertAtRandomIndex); JSONTEST_REGISTER_FIXTURE(runner, ValueTest, null); JSONTEST_REGISTER_FIXTURE(runner, ValueTest, strings); JSONTEST_REGISTER_FIXTURE(runner, ValueTest, bools); From 7095282a692068b40eb456f1e47118811333c9e2 Mon Sep 17 00:00:00 2001 From: lilinchao Date: Mon, 1 Jul 2019 21:43:41 +0800 Subject: [PATCH 05/14] update with clang-format --- src/lib_json/json_value.cpp | 10 ---------- src/test_lib_json/main.cpp | 27 --------------------------- 2 files changed, 37 deletions(-) diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index f969f000f..dd7bfd898 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -1182,13 +1182,8 @@ bool Value::insert(ArrayIndex index, const Value& newValue) { resize(oldsize + 1); ArrayIndex length = size(); -<<<<<<< HEAD - for (ArrayIndex i = length; i > index; i--) { - (*this)[i] = (*this)[i - 1]; -======= for(ArrayIndex i = length ; i> index ; i--) { (*this)[i] = (*this)[i-1]; ->>>>>>> 8a1d8693034ed44271db707c55e14fc517a8c5b9 } (*this)[index] = newValue; return true; @@ -1208,13 +1203,8 @@ bool Value::insert(ArrayIndex index, const Value&& newValue) { resize(oldsize + 1); ArrayIndex length = size(); -<<<<<<< HEAD - for (ArrayIndex i = length; i > index; i--) { - (*this)[i] = (*this)[i - 1]; -======= for(ArrayIndex i = length ; i> index ; i--) { (*this)[i] = (*this)[i-1]; ->>>>>>> 8a1d8693034ed44271db707c55e14fc517a8c5b9 } (*this)[index] = std::move(newValue); return true; diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index d02d86847..59b828e32 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -313,32 +313,6 @@ JSONTEST_FIXTURE(ValueTest, arrayIssue252) { JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) { Json::Value array; JSONCPP_STRING str = "index5"; -<<<<<<< HEAD - array.append("index0"); - array.append("index1"); - array.append("index2"); - array.append("index3"); - - JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); - JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); - JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); - JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[3]); - - array.insert(3, "index4"); // rvalue - JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); - JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); - JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); - JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]); - JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[4]); - - array.insert(4, str); // lvalue - JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); - JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); - JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); - JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]); - JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array[4]); - JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[5]); -======= array.append("index0"); array.append("index1"); array.append("index2"); @@ -363,7 +337,6 @@ JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) { JSONTEST_ASSERT_EQUAL(Json::Value("index4"),array[3]); JSONTEST_ASSERT_EQUAL(Json::Value("index5"),array[4]); JSONTEST_ASSERT_EQUAL(Json::Value("index3"),array[5]); ->>>>>>> 8a1d8693034ed44271db707c55e14fc517a8c5b9 } JSONTEST_FIXTURE(ValueTest, null) { JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type()); From 589b60151997eab810792265e5cc9a4783a64059 Mon Sep 17 00:00:00 2001 From: lilinchao Date: Wed, 3 Jul 2019 17:40:49 +0800 Subject: [PATCH 06/14] update --- include/json/value.h | 2 +- src/lib_json/json_value.cpp | 12 ++++++---- src/test_lib_json/main.cpp | 48 ++++++++++++++++++------------------- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/include/json/value.h b/include/json/value.h index e13284534..c0825ff98 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -467,7 +467,7 @@ Json::Value obj_value(Json::objectValue); // {} bool insert(ArrayIndex index, const Value& newValue); #if JSON_HAS_RVALUE_REFERENCES Value& append(Value&& value); - bool insert(ArrayIndex index, const Value&& newValue); + bool insert(ArrayIndex index, Value&& newValue); #endif /// Access an object value by name, create a null member if it does not exist. diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index dd7bfd898..947e9568e 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -1182,8 +1182,9 @@ bool Value::insert(ArrayIndex index, const Value& newValue) { resize(oldsize + 1); ArrayIndex length = size(); - for(ArrayIndex i = length ; i> index ; i--) { - (*this)[i] = (*this)[i-1]; + for (ArrayIndex i = length; i > index; i--) { + CZString key(i); + (*value_.map_)[key] = (*this)[i - 1]; } (*this)[index] = newValue; return true; @@ -1192,7 +1193,7 @@ bool Value::insert(ArrayIndex index, const Value& newValue) { Value& Value::append(Value&& value) { return (*this)[size()] = std::move(value); } -bool Value::insert(ArrayIndex index, const Value&& newValue) { +bool Value::insert(ArrayIndex index, Value&& newValue) { if (type() != arrayValue) { return false; } @@ -1203,8 +1204,9 @@ bool Value::insert(ArrayIndex index, const Value&& newValue) { resize(oldsize + 1); ArrayIndex length = size(); - for(ArrayIndex i = length ; i> index ; i--) { - (*this)[i] = (*this)[i-1]; + for (ArrayIndex i = length; i > index; i--) { + CZString key(i); + (*value_.map_)[key] = std::move((*this)[i - 1]); } (*this)[index] = std::move(newValue); return true; diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 59b828e32..cd00265a9 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -313,30 +313,30 @@ JSONTEST_FIXTURE(ValueTest, arrayIssue252) { JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) { Json::Value array; JSONCPP_STRING str = "index5"; - array.append("index0"); - array.append("index1"); - array.append("index2"); - array.append("index3"); - - JSONTEST_ASSERT_EQUAL(Json::Value("index0"),array[0]); - JSONTEST_ASSERT_EQUAL(Json::Value("index1"),array[1]); - JSONTEST_ASSERT_EQUAL(Json::Value("index2"),array[2]); - JSONTEST_ASSERT_EQUAL(Json::Value("index3"),array[3]); - - array.insert(3,"index4"); //rvalue - JSONTEST_ASSERT_EQUAL(Json::Value("index0"),array[0]); - JSONTEST_ASSERT_EQUAL(Json::Value("index1"),array[1]); - JSONTEST_ASSERT_EQUAL(Json::Value("index2"),array[2]); - JSONTEST_ASSERT_EQUAL(Json::Value("index4"),array[3]); - JSONTEST_ASSERT_EQUAL(Json::Value("index3"),array[4]); - - array.insert(4,str); //lvalue - JSONTEST_ASSERT_EQUAL(Json::Value("index0"),array[0]); - JSONTEST_ASSERT_EQUAL(Json::Value("index1"),array[1]); - JSONTEST_ASSERT_EQUAL(Json::Value("index2"),array[2]); - JSONTEST_ASSERT_EQUAL(Json::Value("index4"),array[3]); - JSONTEST_ASSERT_EQUAL(Json::Value("index5"),array[4]); - JSONTEST_ASSERT_EQUAL(Json::Value("index3"),array[5]); + array.append("index0"); + array.append("index1"); + array.append("index2"); + array.append("index3"); + + JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); + JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); + JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); + JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[3]); + + array.insert(3, "index4"); // rvalue + JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); + JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); + JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); + JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]); + JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[4]); + + array.insert(4, str); // lvalue + JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); + JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); + JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); + JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]); + JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array[4]); + JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[5]); } JSONTEST_FIXTURE(ValueTest, null) { JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type()); From d5848ed9cc94abe36ae59041cf251dd646f697de Mon Sep 17 00:00:00 2001 From: lilinchao Date: Tue, 9 Jul 2019 17:34:13 +0800 Subject: [PATCH 07/14] modify insert method --- include/json/value.h | 2 +- src/lib_json/json_value.cpp | 47 ++++++++++++++++++------------------- src/test_lib_json/main.cpp | 19 +++++++++------ 3 files changed, 36 insertions(+), 32 deletions(-) diff --git a/include/json/value.h b/include/json/value.h index c0825ff98..e13284534 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -467,7 +467,7 @@ Json::Value obj_value(Json::objectValue); // {} bool insert(ArrayIndex index, const Value& newValue); #if JSON_HAS_RVALUE_REFERENCES Value& append(Value&& value); - bool insert(ArrayIndex index, Value&& newValue); + bool insert(ArrayIndex index, const Value&& newValue); #endif /// Access an object value by name, create a null member if it does not exist. diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index 947e9568e..493027125 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -1168,48 +1168,47 @@ Value const& Value::operator[](CppTL::ConstString const& key) const { return *found; } #endif - Value& Value::append(const Value& value) { return (*this)[size()] = value; } /// \brief Insert value in array at specific index bool Value::insert(ArrayIndex index, const Value& newValue) { if (type() != arrayValue) { return false; } - if (!isValidIndex(index)) { - return false; - } ArrayIndex oldsize = size(); - resize(oldsize + 1); - ArrayIndex length = size(); - - for (ArrayIndex i = length; i > index; i--) { - CZString key(i); - (*value_.map_)[key] = (*this)[i - 1]; + if (index > oldsize) { + (*this)[oldsize] = newValue; + return true; + } else { + (*this)[oldsize] = null; + ArrayIndex length = size(); + for (ArrayIndex i = length - 1; i > index; i--) { + (*this)[i] = (*this)[i - 1]; + } + (*this)[index] = newValue; + return true; } - (*this)[index] = newValue; - return true; } #if JSON_HAS_RVALUE_REFERENCES Value& Value::append(Value&& value) { return (*this)[size()] = std::move(value); } -bool Value::insert(ArrayIndex index, Value&& newValue) { +bool Value::insert(ArrayIndex index, const Value&& newValue) { if (type() != arrayValue) { return false; } - if (!isValidIndex(index)) { - return false; - } ArrayIndex oldsize = size(); - resize(oldsize + 1); - ArrayIndex length = size(); - - for (ArrayIndex i = length; i > index; i--) { - CZString key(i); - (*value_.map_)[key] = std::move((*this)[i - 1]); + if (index > oldsize) { + append(newValue); + return true; + } else { + (*this)[oldsize] = null; + ArrayIndex length = size(); + for (ArrayIndex i = length - 1; i > index; i--) { + (*this)[i] = std::move((*this)[i - 1]); + } + (*this)[index] = std::move(newValue); + return true; } - (*this)[index] = std::move(newValue); - return true; } #endif Value Value::get(char const* begin, diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index cd00265a9..43bad338e 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -312,31 +312,36 @@ JSONTEST_FIXTURE(ValueTest, arrayIssue252) { } JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) { Json::Value array; - JSONCPP_STRING str = "index5"; + JSONCPP_STRING str0 = "index2"; + JSONCPP_STRING str1 = "index4"; array.append("index0"); array.append("index1"); - array.append("index2"); - array.append("index3"); + array.append(str0); + JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); + JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); + JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); + array.insert(3, "index3"); // rvalue JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[3]); - array.insert(3, "index4"); // rvalue + array.insert(3, str1); // lvalue JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]); JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[4]); - array.insert(4, str); // lvalue + array.insert(9, "index6"); + // beyond size(). it should be allowed to insert into its tail. JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]); - JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array[4]); - JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[5]); + JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[4]); + JSONTEST_ASSERT_EQUAL(Json::Value("index6"), array[5]); } JSONTEST_FIXTURE(ValueTest, null) { JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type()); From e0cd860fa256e4e200ff029203779c7e0c3d771f Mon Sep 17 00:00:00 2001 From: chenguoping Date: Wed, 4 Sep 2019 14:07:25 +0800 Subject: [PATCH 08/14] update --- include/json/value.h | 6 ++---- src/lib_json/json_value.cpp | 32 +++++++------------------------- src/test_lib_json/main.cpp | 26 +++++++++++++------------- 3 files changed, 22 insertions(+), 42 deletions(-) diff --git a/include/json/value.h b/include/json/value.h index e13284534..fc36793a9 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -463,12 +463,10 @@ Json::Value obj_value(Json::objectValue); // {} /// /// Equivalent to jsonvalue[jsonvalue.size()] = value; Value& append(const Value& value); + Value& append(Value&& value); /// \brief Insert value in array at specific index bool insert(ArrayIndex index, const Value& newValue); -#if JSON_HAS_RVALUE_REFERENCES - Value& append(Value&& value); - bool insert(ArrayIndex index, const Value&& newValue); -#endif + bool insert(ArrayIndex index, Value&& newValue); /// Access an object value by name, create a null member if it does not exist. /// \note Because of our implementation, keys are limited to 2^30 -1 chars. diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index 493027125..b4b78190c 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -1169,48 +1169,30 @@ Value const& Value::operator[](CppTL::ConstString const& key) const { } #endif Value& Value::append(const Value& value) { return (*this)[size()] = value; } -/// \brief Insert value in array at specific index -bool Value::insert(ArrayIndex index, const Value& newValue) { - if (type() != arrayValue) { - return false; - } - ArrayIndex oldsize = size(); - if (index > oldsize) { - (*this)[oldsize] = newValue; - return true; - } else { - (*this)[oldsize] = null; - ArrayIndex length = size(); - for (ArrayIndex i = length - 1; i > index; i--) { - (*this)[i] = (*this)[i - 1]; - } - (*this)[index] = newValue; - return true; - } -} -#if JSON_HAS_RVALUE_REFERENCES Value& Value::append(Value&& value) { return (*this)[size()] = std::move(value); } -bool Value::insert(ArrayIndex index, const Value&& newValue) { +/// \brief Insert value in array at specific index +bool Value::insert(ArrayIndex index, const Value& newValue) { + return insert(index,Value(newValue)); +} +bool Value::insert(ArrayIndex index, Value&& newValue) { if (type() != arrayValue) { return false; } ArrayIndex oldsize = size(); if (index > oldsize) { - append(newValue); + append(std::move(newValue)); return true; } else { - (*this)[oldsize] = null; ArrayIndex length = size(); - for (ArrayIndex i = length - 1; i > index; i--) { + for (ArrayIndex i = length; i > index; i--) { (*this)[i] = std::move((*this)[i - 1]); } (*this)[index] = std::move(newValue); return true; } } -#endif Value Value::get(char const* begin, char const* end, Value const& defaultValue) const { diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 43bad338e..5a6d91451 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -321,26 +321,26 @@ JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) { JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); - array.insert(3, "index3"); // rvalue - JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); - JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); - JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); - JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[3]); + array.insert(0, "index3"); // rvalue + JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]); + JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]); + JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[2]); + JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[3]); array.insert(3, str1); // lvalue - JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); - JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); - JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); + JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]); + JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]); + JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[2]); JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]); - JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[4]); + JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]); array.insert(9, "index6"); // beyond size(). it should be allowed to insert into its tail. - JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); - JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); - JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); + JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]); + JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]); + JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[2]); JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]); - JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[4]); + JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]); JSONTEST_ASSERT_EQUAL(Json::Value("index6"), array[5]); } JSONTEST_FIXTURE(ValueTest, null) { From 14ac99dba51f55d98da6aa3e5cc86590381707f5 Mon Sep 17 00:00:00 2001 From: chenguoping Date: Wed, 4 Sep 2019 14:50:41 +0800 Subject: [PATCH 09/14] update --- src/lib_json/json_value.cpp | 5 ++--- src/test_lib_json/main.cpp | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index b4b78190c..684859116 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -1180,12 +1180,11 @@ bool Value::insert(ArrayIndex index, Value&& newValue) { if (type() != arrayValue) { return false; } - ArrayIndex oldsize = size(); - if (index > oldsize) { + ArrayIndex length = size(); + if (index > length) { append(std::move(newValue)); return true; } else { - ArrayIndex length = size(); for (ArrayIndex i = length; i > index; i--) { (*this)[i] = std::move((*this)[i - 1]); } diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 5a6d91451..fdd91fa51 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -2530,7 +2530,7 @@ JSONTEST_FIXTURE(IteratorTest, const) { Json::Value const v; JSONTEST_ASSERT_THROWS( Json::Value::iterator it(v.begin()) // Compile, but throw. - ); + ); Json::Value value; From 228f4fc2ff8f268b763f5f51e05bcf3c77b0873b Mon Sep 17 00:00:00 2001 From: chenguoping Date: Thu, 5 Sep 2019 20:49:50 +0800 Subject: [PATCH 10/14] run ninja -v -C build-shared clang-format --- src/lib_json/json_value.cpp | 13 +++++--- src/test_lib_json/main.cpp | 66 ++++++++++++++++++++++++++++--------- 2 files changed, 59 insertions(+), 20 deletions(-) diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index 684859116..0ecd3a622 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -1169,17 +1169,22 @@ Value const& Value::operator[](CppTL::ConstString const& key) const { } #endif Value& Value::append(const Value& value) { return (*this)[size()] = value; } + Value& Value::append(Value&& value) { return (*this)[size()] = std::move(value); } + /// \brief Insert value in array at specific index bool Value::insert(ArrayIndex index, const Value& newValue) { - return insert(index,Value(newValue)); + JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue, + "in Json::Value::insert(ArrayIndex index, const Value& " + "newValue): requires arrayValue"); + return insert(index, Value(newValue)); } bool Value::insert(ArrayIndex index, Value&& newValue) { - if (type() != arrayValue) { - return false; - } + JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue, + "in Json::Value::insert(ArrayIndex index, Value&& " + "newValue): requires arrayValue"); ArrayIndex length = size(); if (index > length) { append(std::move(newValue)); diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index fdd91fa51..aef2eae65 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -310,38 +310,72 @@ JSONTEST_FIXTURE(ValueTest, arrayIssue252) { } // JSONTEST_ASSERT_EQUAL(5, root["array"].size()); } + JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) { Json::Value array; - JSONCPP_STRING str0 = "index2"; - JSONCPP_STRING str1 = "index4"; - array.append("index0"); + Json::Value str0("index2"); + Json::Value str1("index3"); + array.append("index0"); // append rvalue array.append("index1"); - array.append(str0); - JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); + array.append(str0); // append lvalue + + std::vector vec; // storage value address for checking + for (int i = 0; i < 3; i++) { + vec.push_back(&array[i]); + } + JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); // check append JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]); JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); - array.insert(0, "index3"); // rvalue + // insert lvalue at the head + array.insert(0, str1); JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]); JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]); JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[2]); JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[3]); - - array.insert(3, str1); // lvalue + // checking address + for (int i = 0; i < 3; i++) { + JSONTEST_ASSERT_EQUAL(vec[i], &array[i]); + } + vec.push_back(&array[3]); + // insert rvalue at middle + array.insert(2, "index4"); JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]); JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]); - JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[2]); - JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]); + JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]); + JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[3]); JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]); - - array.insert(9, "index6"); - // beyond size(). it should be allowed to insert into its tail. + // checking address + for (int i = 0; i < 4; i++) { + JSONTEST_ASSERT_EQUAL(vec[i], &array[i]); + } + vec.push_back(&array[4]); + // insert rvalue at the tail + array.insert(5, "index5"); JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]); JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]); - JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[2]); - JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]); + JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]); + JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[3]); JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]); - JSONTEST_ASSERT_EQUAL(Json::Value("index6"), array[5]); + JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array[5]); + // checking address + for (int i = 0; i < 5; i++) { + JSONTEST_ASSERT_EQUAL(vec[i], &array[i]); + } + vec.push_back(&array[5]); + // beyond max array size, it should be allowed to insert into its tail + array.insert(10, "index10"); + JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]); + JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]); + JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]); + JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[3]); + JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]); + JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array[5]); + JSONTEST_ASSERT_EQUAL(Json::Value("index10"), array[6]); + // checking address + for (int i = 0; i < 6; i++) { + JSONTEST_ASSERT_EQUAL(vec[i], &array[i]); + } } JSONTEST_FIXTURE(ValueTest, null) { JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type()); From 25258ab492eec889a6819e6b8fc41774a4691e83 Mon Sep 17 00:00:00 2001 From: chenguoping Date: Thu, 26 Sep 2019 15:44:07 +0800 Subject: [PATCH 11/14] update: index > length, return false; --- src/lib_json/json_value.cpp | 3 +-- src/test_lib_json/main.cpp | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index 1853be36c..72eba1bde 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -1179,8 +1179,7 @@ bool Value::insert(ArrayIndex index, Value&& newValue) { "newValue): requires arrayValue"); ArrayIndex length = size(); if (index > length) { - append(std::move(newValue)); - return true; + return false; } else { for (ArrayIndex i = length; i > index; i--) { (*this)[i] = std::move((*this)[i - 1]); diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 35851dce3..b25d640ac 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -324,8 +324,8 @@ JSONTEST_FIXTURE(ValueTest, arrayIssue252) { JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) { Json::Value array; - Json::Value str0("index2"); - Json::Value str1("index3"); + const Json::Value str0("index2"); + const Json::Value str1("index3"); array.append("index0"); // append rvalue array.append("index1"); array.append(str0); // append lvalue From 4765282e908586ceb7866ad1aa159346af9bc3df Mon Sep 17 00:00:00 2001 From: chenguoping Date: Thu, 26 Sep 2019 16:00:54 +0800 Subject: [PATCH 12/14] update testcase --- src/test_lib_json/main.cpp | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index b25d640ac..e3d4fb28f 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -339,7 +339,7 @@ JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) { JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]); // insert lvalue at the head - array.insert(0, str1); + JSONTEST_ASSERT(array.insert(0, str1)); JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]); JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]); JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[2]); @@ -350,7 +350,7 @@ JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) { } vec.push_back(&array[3]); // insert rvalue at middle - array.insert(2, "index4"); + JSONTEST_ASSERT(array.insert(2, "index4")); JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]); JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]); JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]); @@ -362,7 +362,7 @@ JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) { } vec.push_back(&array[4]); // insert rvalue at the tail - array.insert(5, "index5"); + JSONTEST_ASSERT(array.insert(5, "index5")); JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]); JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]); JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]); @@ -374,19 +374,8 @@ JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) { JSONTEST_ASSERT_EQUAL(vec[i], &array[i]); } vec.push_back(&array[5]); - // beyond max array size, it should be allowed to insert into its tail - array.insert(10, "index10"); - JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]); - JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]); - JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]); - JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[3]); - JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]); - JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array[5]); - JSONTEST_ASSERT_EQUAL(Json::Value("index10"), array[6]); - // checking address - for (int i = 0; i < 6; i++) { - JSONTEST_ASSERT_EQUAL(vec[i], &array[i]); - } + // beyond max array size, it should not be allowed to insert into its tail + JSONTEST_ASSERT(!array.insert(10, "index10")); } JSONTEST_FIXTURE(ValueTest, null) { JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type()); From e2fdd7c97ac89df4d77c01bb9299e2b873c4d2bd Mon Sep 17 00:00:00 2001 From: chenguoping Date: Sat, 12 Oct 2019 10:01:30 +0800 Subject: [PATCH 13/14] update code --- include/json/value.h | 3 +-- src/test_lib_json/main.cpp | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/json/value.h b/include/json/value.h index 8e20c0cdf..aafc78938 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -465,8 +465,7 @@ class JSON_API Value { Value& append(const Value& value); Value& append(Value&& value); /// \brief Insert value in array at specific index - bool insert(ArrayIndex index, const Value& newValue); - bool insert(ArrayIndex index, Value&& newValue); + bool insert(ArrayIndex index, Value newValue); /// Access an object value by name, create a null member if it does not exist. /// \note Because of our implementation, keys are limited to 2^30 -1 chars. diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index dfb7da35d..5f3f9efdc 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -362,7 +362,8 @@ JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) { } vec.push_back(&array[4]); // insert rvalue at the tail - JSONTEST_ASSERT(array.insert(5, "index5")); + Json::Value index5("index5"); + JSONTEST_ASSERT(array.insert(5, std::move(index5))); JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]); JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]); JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]); From d83613e823b522aef160577450bb906411515774 Mon Sep 17 00:00:00 2001 From: chenguoping Date: Sat, 12 Oct 2019 10:10:11 +0800 Subject: [PATCH 14/14] fix clang-format --- src/test_lib_json/main.cpp | 134 ++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 70 deletions(-) diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 5f3f9efdc..513f2b558 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -2110,16 +2110,16 @@ JSONTEST_FIXTURE(StyledWriterTest, multiLineArray) { { // Array member has more than 20 print effect rendering lines const Json::String expected("[\n " - "0,\n 1,\n 2,\n " - "3,\n 4,\n 5,\n " - "6,\n 7,\n 8,\n " - "9,\n 10,\n 11,\n " - "12,\n 13,\n 14,\n " - "15,\n 16,\n 17,\n " - "18,\n 19,\n 20\n]\n"); + "0,\n 1,\n 2,\n " + "3,\n 4,\n 5,\n " + "6,\n 7,\n 8,\n " + "9,\n 10,\n 11,\n " + "12,\n 13,\n 14,\n " + "15,\n 16,\n 17,\n " + "18,\n 19,\n 20\n]\n"); Json::Value root; for (int i = 0; i < 21; i++) - root[i] = i; + root[i] = i; const Json::String result = writer.write(root); JSONTEST_ASSERT_STRING_EQUAL(expected, result); } @@ -2128,7 +2128,7 @@ JSONTEST_FIXTURE(StyledWriterTest, multiLineArray) { const Json::String expected("[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]\n"); Json::Value root; for (int i = 0; i < 10; i++) - root[i] = i; + root[i] = i; const Json::String result = writer.write(root); JSONTEST_ASSERT_STRING_EQUAL(expected, result); } @@ -2139,8 +2139,7 @@ JSONTEST_FIXTURE(StyledWriterTest, writeValueWithComment) { { const Json::String expected("\n//commentBeforeValue\n\"hello\"\n"); Json::Value root = "hello"; - root.setComment(Json::String("//commentBeforeValue"), - Json::commentBefore); + root.setComment(Json::String("//commentBeforeValue"), Json::commentBefore); const Json::String result = writer.write(root); JSONTEST_ASSERT_STRING_EQUAL(expected, result); } @@ -2155,8 +2154,7 @@ JSONTEST_FIXTURE(StyledWriterTest, writeValueWithComment) { { const Json::String expected("\"hello\"\n//commentAfter\n\n"); Json::Value root = "hello"; - root.setComment(Json::String("//commentAfter"), - Json::commentAfter); + root.setComment(Json::String("//commentAfter"), Json::commentAfter); const Json::String result = writer.write(root); JSONTEST_ASSERT_STRING_EQUAL(expected, result); } @@ -2233,44 +2231,42 @@ JSONTEST_FIXTURE(StyledStreamWriterTest, multiLineArray) { Json::StyledStreamWriter writer; { // Array member has more than 20 print effect rendering lines - const Json::String expected( - "[" - "\n\t0," - "\n\t1," - "\n\t2," - "\n\t3," - "\n\t4," - "\n\t5," - "\n\t6," - "\n\t7," - "\n\t8," - "\n\t9," - "\n\t10," - "\n\t11," - "\n\t12," - "\n\t13," - "\n\t14," - "\n\t15," - "\n\t16," - "\n\t17," - "\n\t18," - "\n\t19," - "\n\t20\n]\n"); + const Json::String expected("[\n\t0," + "\n\t1," + "\n\t2," + "\n\t3," + "\n\t4," + "\n\t5," + "\n\t6," + "\n\t7," + "\n\t8," + "\n\t9," + "\n\t10," + "\n\t11," + "\n\t12," + "\n\t13," + "\n\t14," + "\n\t15," + "\n\t16," + "\n\t17," + "\n\t18," + "\n\t19," + "\n\t20\n]\n"); Json::StyledStreamWriter writer; Json::Value root; for (int i = 0; i < 21; i++) - root[i] = i; + root[i] = i; Json::OStringStream sout; writer.write(sout, root); const Json::String result = sout.str(); JSONTEST_ASSERT_STRING_EQUAL(expected, result); } - { + { // Array members do not exceed 21 print effects to render a single line const Json::String expected("[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]\n"); Json::Value root; for (int i = 0; i < 10; i++) - root[i] = i; + root[i] = i; Json::OStringStream sout; writer.write(sout, root); const Json::String result = sout.str(); @@ -2284,8 +2280,7 @@ JSONTEST_FIXTURE(StyledStreamWriterTest, writeValueWithComment) { const Json::String expected("//commentBeforeValue\n\"hello\"\n"); Json::Value root = "hello"; Json::OStringStream sout; - root.setComment(Json::String("//commentBeforeValue"), - Json::commentBefore); + root.setComment(Json::String("//commentBeforeValue"), Json::commentBefore); writer.write(sout, root); const Json::String result = sout.str(); JSONTEST_ASSERT_STRING_EQUAL(expected, result); @@ -2304,8 +2299,7 @@ JSONTEST_FIXTURE(StyledStreamWriterTest, writeValueWithComment) { const Json::String expected("\"hello\"\n//commentAfter\n"); Json::Value root = "hello"; Json::OStringStream sout; - root.setComment(Json::String("//commentAfter"), - Json::commentAfter); + root.setComment(Json::String("//commentAfter"), Json::commentAfter); writer.write(sout, root); const Json::String result = sout.str(); JSONTEST_ASSERT_STRING_EQUAL(expected, result); @@ -2382,42 +2376,41 @@ JSONTEST_FIXTURE(StreamWriterTest, multiLineArray) { wb.settings_["commentStyle"] = "None"; { // When wb.settings_["commentStyle"] = "None", the effect of - // printing multiple lines will be displayed when there are + // printing multiple lines will be displayed when there are // more than 20 array members. - const Json::String expected( - "[\n\t0," - "\n\t1," - "\n\t2," - "\n\t3," - "\n\t4," - "\n\t5," - "\n\t6," - "\n\t7," - "\n\t8," - "\n\t9," - "\n\t10," - "\n\t11," - "\n\t12," - "\n\t13," - "\n\t14," - "\n\t15," - "\n\t16," - "\n\t17," - "\n\t18," - "\n\t19," - "\n\t20\n]"); + const Json::String expected("[\n\t0," + "\n\t1," + "\n\t2," + "\n\t3," + "\n\t4," + "\n\t5," + "\n\t6," + "\n\t7," + "\n\t8," + "\n\t9," + "\n\t10," + "\n\t11," + "\n\t12," + "\n\t13," + "\n\t14," + "\n\t15," + "\n\t16," + "\n\t17," + "\n\t18," + "\n\t19," + "\n\t20\n]"); Json::Value root; for (int i = 0; i < 21; i++) - root[i] = i; + root[i] = i; const Json::String result = Json::writeString(wb, root); JSONTEST_ASSERT_STRING_EQUAL(expected, result); } { - //Array members do not exceed 21 print effects to render a single line + // Array members do not exceed 21 print effects to render a single line const Json::String expected("[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]"); Json::Value root; for (int i = 0; i < 10; i++) - root[i] = i; + root[i] = i; const Json::String result = Json::writeString(wb, root); JSONTEST_ASSERT_STRING_EQUAL(expected, result); } @@ -3226,7 +3219,8 @@ int main(int argc, const char* argv[]) { JSONTEST_REGISTER_FIXTURE(runner, StyledStreamWriterTest, writeArrays); JSONTEST_REGISTER_FIXTURE(runner, StyledStreamWriterTest, writeNestedObjects); JSONTEST_REGISTER_FIXTURE(runner, StyledStreamWriterTest, multiLineArray); - JSONTEST_REGISTER_FIXTURE(runner, StyledStreamWriterTest, writeValueWithComment); + JSONTEST_REGISTER_FIXTURE(runner, StyledStreamWriterTest, + writeValueWithComment); JSONTEST_REGISTER_FIXTURE(runner, StreamWriterTest, writeNumericValue); JSONTEST_REGISTER_FIXTURE(runner, StreamWriterTest, writeArrays); JSONTEST_REGISTER_FIXTURE(runner, StreamWriterTest, writeNestedObjects);