mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-29 11:02:08 +00:00
Merge pull request #7791 from DimasKovas/fix_insert_in_pod_array
Fix PODArray.insert
This commit is contained in:
commit
06b9466de0
@ -430,11 +430,11 @@ public:
|
||||
template <typename It1, typename It2>
|
||||
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);
|
||||
|
||||
|
34
dbms/src/Common/tests/gtest_pod_array.cpp
Normal file
34
dbms/src/Common/tests/gtest_pod_array.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <Common/PODArray.h>
|
||||
|
||||
using namespace DB;
|
||||
|
||||
TEST(Common, PODArray_Insert)
|
||||
{
|
||||
std::string str = "test_string_abacaba";
|
||||
PODArray<char> 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 cur_char = 'a';
|
||||
while (insert_with_resize.size() < insert_with_resize.capacity())
|
||||
{
|
||||
insert_with_resize += cur_char;
|
||||
if (cur_char == 'z')
|
||||
cur_char = 'a';
|
||||
else
|
||||
++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());
|
||||
EXPECT_EQ(str, std::string(chars.data(), chars.size()));
|
||||
}
|
Loading…
Reference in New Issue
Block a user