From ea177061e8624611be915d44484b8487b07640b2 Mon Sep 17 00:00:00 2001 From: Dmitrii Kovalkov Date: Fri, 15 Nov 2019 17:36:52 +0300 Subject: [PATCH 1/2] Fix PODArray.insert --- dbms/src/Common/PODArray.h | 4 +-- dbms/src/Common/tests/gtest_pod_array.cpp | 34 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 dbms/src/Common/tests/gtest_pod_array.cpp diff --git a/dbms/src/Common/PODArray.h b/dbms/src/Common/PODArray.h index def8f675c25..b875e174243 100644 --- a/dbms/src/Common/PODArray.h +++ b/dbms/src/Common/PODArray.h @@ -430,11 +430,11 @@ public: template void insert(iterator it, It1 from_begin, It2 from_end) { - insertPrepare(from_begin, from_end); - size_t bytes_to_copy = this->byte_size(from_end - from_begin); size_t bytes_to_move = (end() - it) * sizeof(T); + insertPrepare(from_begin, from_end); + if (unlikely(bytes_to_move)) memcpy(this->c_end + bytes_to_copy - bytes_to_move, this->c_end - bytes_to_move, bytes_to_move); diff --git a/dbms/src/Common/tests/gtest_pod_array.cpp b/dbms/src/Common/tests/gtest_pod_array.cpp new file mode 100644 index 00000000000..a0e407fcb7b --- /dev/null +++ b/dbms/src/Common/tests/gtest_pod_array.cpp @@ -0,0 +1,34 @@ +#include + +#include + +using namespace DB; + +TEST(Common, PODArray_Insert) +{ + std::string str = "test_string_abacaba"; + PODArray chars; + chars.insert(chars.end(), str.begin(), str.end()); + EXPECT_EQ(str, std::string(chars.data(), chars.size())); + + std::string insert_in_the_middle = "insert_in_the_middle"; + auto pos = str.size() / 2; + str.insert(str.begin() + pos, insert_in_the_middle.begin(), insert_in_the_middle.end()); + chars.insert(chars.begin() + pos, insert_in_the_middle.begin(), insert_in_the_middle.end()); + EXPECT_EQ(str, std::string(chars.data(), chars.size())); + + std::string insert_with_resize; + insert_with_resize.reserve(chars.capacity() * 2); + char curChar = 'a'; + while (insert_with_resize.size() < insert_with_resize.capacity()) + { + insert_with_resize += curChar; + if (curChar == 'z') + curChar = 'a'; + else + ++curChar; + } + str.insert(str.begin(), insert_with_resize.begin(), insert_with_resize.end()); + chars.insert(chars.begin(), insert_with_resize.begin(), insert_with_resize.end()); + EXPECT_EQ(str, std::string(chars.data(), chars.size())); +} From 87c6a63b65a29f21be28c63447fd709d1d6ac6d0 Mon Sep 17 00:00:00 2001 From: alexey-milovidov Date: Fri, 15 Nov 2019 18:00:10 +0300 Subject: [PATCH 2/2] Update gtest_pod_array.cpp --- dbms/src/Common/tests/gtest_pod_array.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dbms/src/Common/tests/gtest_pod_array.cpp b/dbms/src/Common/tests/gtest_pod_array.cpp index a0e407fcb7b..7962bf39f07 100644 --- a/dbms/src/Common/tests/gtest_pod_array.cpp +++ b/dbms/src/Common/tests/gtest_pod_array.cpp @@ -19,14 +19,14 @@ TEST(Common, PODArray_Insert) std::string insert_with_resize; insert_with_resize.reserve(chars.capacity() * 2); - char curChar = 'a'; + char cur_char = 'a'; while (insert_with_resize.size() < insert_with_resize.capacity()) { - insert_with_resize += curChar; - if (curChar == 'z') - curChar = 'a'; + insert_with_resize += cur_char; + if (cur_char == 'z') + cur_char = 'a'; else - ++curChar; + ++cur_char; } str.insert(str.begin(), insert_with_resize.begin(), insert_with_resize.end()); chars.insert(chars.begin(), insert_with_resize.begin(), insert_with_resize.end());