From 934d18b64968da0c7db55a69ce48ccd6ff23498d Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Wed, 8 Jul 2020 02:41:10 +0300 Subject: [PATCH] Added a test for PaddedPODArray just in case --- src/Common/tests/gtest_pod_array.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Common/tests/gtest_pod_array.cpp b/src/Common/tests/gtest_pod_array.cpp index b77bc56b59a..988a3e649ba 100644 --- a/src/Common/tests/gtest_pod_array.cpp +++ b/src/Common/tests/gtest_pod_array.cpp @@ -44,3 +44,25 @@ TEST(Common, PODPushBackRawMany) EXPECT_EQ(15, chars.size()); EXPECT_EQ(std::string("first0123456789"), std::string(chars.data(), chars.size())); } + +TEST(Common, PODNoOverallocation) +{ + /// Check that PaddedPODArray allocates for smaller number of elements than the power of two due to padding. + /// NOTE: It's Ok to change these numbers if you will modify initial size or padding. + + PaddedPODArray chars; + std::vector capacities; + + size_t prev_capacity = 0; + for (size_t i = 0; i < 1000000; ++i) + { + chars.emplace_back(); + if (chars.capacity() != prev_capacity) + { + prev_capacity = chars.capacity(); + capacities.emplace_back(prev_capacity); + } + } + + EXPECT_EQ(capacities, (std::vector{4065, 8161, 16353, 32737, 65505, 131041, 262113, 524257, 1048545})); +}