Debian Codegraph @

unittest/gunit/dynarray-t.cc

raw symbols SCIP
1 /* Copyright (c) 2011, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <gtest/gtest.h>
25 #include <sys/types.h>
26 #include <algorithm>
27 #include <functional>
28 #include <random>
29 #include <vector>
30
31 #include "my_inttypes.h"
32 #include "my_macros.h"
33 #include "my_table_map.h"
34 #include "sql/current_thd.h"
35 #include "sql/mem_root_array.h"
36 #include "sql/mysqld.h" // THR_MALLOC
37 #include "sql/sql_optimizer.h" // Key_use_array
38
39 /**
40 WL#5774 Decrease number of malloc's for normal DML queries.
41 One of the malloc's was due to DYNAMIC_ARRAY keyuse;
42 We replace the DYNAMIC_ARRAY with a std::vector-like class Mem_root_array.
43
44 Below are unit tests for comparing performance, and for testing
45 functionality of Mem_root_array.
46 */
47
48 namespace dynarray_unittest {
49
50 // We generate some random data at startup, for testing of sorting.
51 void generate_test_data(Key_use *keys, Table_ref *tables, int n) {
52 int ix;
53 for (ix = 0; ix < n; ++ix) {
54 tables[ix].set_tableno(ix % 3);
55 keys[ix] = Key_use(&tables[ix],
56 nullptr, // Item *val
57 0, // table_map used_tables
58 ix % 4, // uint key
59 ix % 2, // uint keypart
60 0, // uint optimize
61 0, // keypart_map
62 0, // ha_rows ref_table_rows
63 true, // bool null_rejecting
64 nullptr, // bool *cond_guard
65 0 // uint sj_pred_no
66 );
67 }
68 std::random_device rng;
69 std::mt19937 urng(rng());
70 std::shuffle(&keys[0], &keys[n], urng);
71 }
72
73 constexpr int num_elements = 200;
74
75 /*
76 This class is for unit testing of Mem_root_array.
77 */
78 class MemRootTest : public ::testing::Test {
79 protected:
80 MemRootTest() : m_mem_root_p(&m_mem_root), m_array_std(m_mem_root_p) {}
81
82 void SetUp() override {
83 THR_MALLOC = &m_mem_root_p;
84
85 m_array_std.reserve(num_elements);
86 destroy_counter = 0;
87 }
88
89 void TearDown() override { m_mem_root.Clear(); }
90
91 static void SetUpTestCase() {
92 generate_test_data(test_data, table_list, num_elements);
93 THR_MALLOC = nullptr;
94 }
95
96 static void TearDownTestCase() { THR_MALLOC = nullptr; }
97
98 MEM_ROOT m_mem_root{PSI_NOT_INSTRUMENTED, 1024};
99 MEM_ROOT *m_mem_root_p;
100 Key_use_array m_array_std;
101
102 public:
103 static size_t destroy_counter;
104
105 private:
106 static Key_use test_data[num_elements];
107 static Table_ref table_list[num_elements];
108
109 MemRootTest(MemRootTest const &) = delete;
110 MemRootTest &operator=(MemRootTest const &) = delete;
111 };
112
113 size_t MemRootTest::destroy_counter;
114 Key_use MemRootTest::test_data[num_elements];
115 Table_ref MemRootTest::table_list[num_elements];
116
117 // Test that Mem_root_array re-expanding works.
118 TEST_F(MemRootTest, Reserve) {
119 Mem_root_array<uint> intarr(m_mem_root_p);
120 intarr.reserve(2);
121 const uint num_pushes = 20;
122 for (uint ix = 0; ix < num_pushes; ++ix) {
123 EXPECT_EQ(ix, intarr.size());
124 EXPECT_FALSE(intarr.push_back(ix));
125 EXPECT_EQ(ix, intarr.at(ix));
126 }
127 for (uint ix = 0; ix < num_pushes; ++ix) {
128 EXPECT_EQ(ix, intarr.at(ix));
129 }
130 EXPECT_EQ(sizeof(uint), intarr.element_size());
131 EXPECT_EQ(num_pushes, intarr.size());
132 EXPECT_LE(num_pushes, intarr.capacity());
133 }
134
135 class DestroyCounter {
136 public:
137 DestroyCounter() : p_counter(&MemRootTest::destroy_counter) {}
138 DestroyCounter(const DestroyCounter &rhs) = default;
139 explicit DestroyCounter(size_t *p) : p_counter(p) {}
140 DestroyCounter &operator=(const DestroyCounter &) = default;
141 ~DestroyCounter() { (*p_counter) += 1; }
142
143 private:
144 size_t *p_counter;
145 };
146
147 // Test chop() and clear() and that destructors are executed.
148 TEST_F(MemRootTest, ChopAndClear) {
149 Mem_root_array<DestroyCounter> array(m_mem_root_p);
150 const size_t nn = 4;
151 array.reserve(nn);
152 size_t counter = 0;
153 DestroyCounter foo(&counter);
154 for (size_t ix = 0; ix < array.capacity(); ++ix) array.push_back(foo);
155
156 EXPECT_EQ(0U, counter);
157 array.chop(nn / 2);
158 EXPECT_EQ(nn / 2, counter);
159 EXPECT_EQ(nn / 2, array.size());
160
161 array.clear();
162 EXPECT_EQ(nn, counter);
163 }
164
165 // Test that elements are destroyed if push_back() needs to call reserve().
166 TEST_F(MemRootTest, ReserveDestroy) {
167 Mem_root_array<DestroyCounter> array(m_mem_root_p);
168 const size_t nn = 4;
169 array.reserve(nn / 2);
170 size_t counter = 0;
171 DestroyCounter foo(&counter);
172 for (size_t ix = 0; ix < nn; ++ix) array.push_back(foo);
173
174 EXPECT_EQ(nn / 2, counter);
175 EXPECT_EQ(nn, array.size());
176
177 counter = 0;
178 array.clear();
179 EXPECT_EQ(nn, counter);
180 }
181
182 TEST_F(MemRootTest, ResizeSame) {
183 Mem_root_array<DestroyCounter> array(m_mem_root_p);
184 array.reserve(100);
185 size_t counter = 0;
186 DestroyCounter foo(&counter);
187 for (int ix = 0; ix < 10; ++ix) array.push_back(foo);
188 EXPECT_EQ(10U, array.size());
189 array.resize(10U);
190 EXPECT_EQ(10U, array.size());
191 array.clear();
192 EXPECT_EQ(10U, counter);
193 }
194
195 TEST_F(MemRootTest, ResizeGrow) {
196 Mem_root_array<DestroyCounter> array(m_mem_root_p);
197 array.reserve(100);
198 size_t counter = 0;
199 DestroyCounter foo(&counter);
200 array.resize(10, foo);
201 EXPECT_EQ(0U, counter);
202 array.clear();
203 EXPECT_EQ(0U, MemRootTest::destroy_counter);
204 EXPECT_EQ(10U, counter);
205 }
206
207 TEST_F(MemRootTest, ResizeShrink) {
208 size_t counter = 0;
209 Mem_root_array<DestroyCounter> array(m_mem_root_p);
210 array.reserve(100);
211 DestroyCounter foo(&counter);
212 array.resize(10, foo);
213 EXPECT_EQ(0U, counter);
214 array.resize(5);
215 EXPECT_EQ(5U, counter);
216 }
217
218 TEST_F(MemRootTest, Erase) {
219 using A = Mem_root_array<DestroyCounter>;
220 size_t counter = 0;
221 DestroyCounter foo(&counter);
222 A array(m_mem_root_p);
223 array.resize(10, foo);
224 EXPECT_EQ(10U, array.size());
225 EXPECT_EQ(0U, counter);
226
227 A::iterator it = array.erase(array.cbegin() + 2, array.cbegin() + 4);
228 EXPECT_EQ(8U, array.size());
229 EXPECT_EQ(array.begin() + 2, it);
230 EXPECT_EQ(2U, counter);
231
232 it = array.erase(array.cend(), array.cend());
233 EXPECT_EQ(8U, array.size());
234 EXPECT_EQ(array.cend(), it);
235 EXPECT_EQ(2U, counter);
236
237 it = array.erase(array.cbegin(), array.cbegin());
238 EXPECT_EQ(8U, array.size());
239 EXPECT_EQ(array.cbegin(), it);
240 EXPECT_EQ(2U, counter);
241
242 it = array.erase(array.cbegin(), array.cend());
243 EXPECT_EQ(0U, array.size());
244 EXPECT_EQ(array.cbegin(), it);
245 EXPECT_EQ(array.cend(), it);
246 EXPECT_EQ(10U, counter);
247 }
248
249 TEST_F(MemRootTest, Erase2) {
250 using A = Mem_root_array<DestroyCounter>;
251 size_t counter = 0;
252 DestroyCounter foo(&counter);
253 A array(m_mem_root_p);
254 array.resize(10, foo);
255 EXPECT_EQ(10U, array.size());
256 EXPECT_EQ(0U, counter);
257
258 A::iterator it = array.erase(5);
259 EXPECT_EQ(9U, array.size());
260 EXPECT_EQ(std::next(array.cbegin(), 5), it);
261 EXPECT_EQ(1U, counter);
262
263 it = array.erase(static_cast<size_t>(0));
264 EXPECT_EQ(8U, array.size());
265 EXPECT_EQ(array.cbegin(), it);
266 EXPECT_EQ(2U, counter);
267
268 it = array.erase(7);
269 EXPECT_EQ(7U, array.size());
270 EXPECT_EQ(array.cend(), it);
271 EXPECT_EQ(3U, counter);
272 }
273
274 TEST_F(MemRootTest, Insert) {
275 using A = Mem_root_array<int>;
276 A array(m_mem_root_p);
277 A::iterator it = array.insert(array.cbegin(), 1);
278 EXPECT_EQ(array.cbegin(), it);
279 it = array.insert(array.cbegin(), 2);
280 EXPECT_EQ(array.cbegin(), it);
281 it = array.insert(array.cbegin() + 1, 3);
282 EXPECT_EQ(array.cbegin() + 1, it);
283 it = array.insert(array.cend(), 4);
284 EXPECT_EQ(array.cend() - 1, it);
285 EXPECT_EQ(4U, array.size());
286 EXPECT_EQ(2, array[0]);
287 EXPECT_EQ(3, array[1]);
288 EXPECT_EQ(1, array[2]);
289 EXPECT_EQ(4, array[3]);
290 }
291
292 } // namespace dynarray_unittest