17 #include "my_config.h"
18 #include <gtest/gtest.h>
25 #include "mem_root_array.h"
54 ((a.
optimize & KEY_OPTIMIZE_REF_OR_NULL) <
55 (b.
optimize & KEY_OPTIMIZE_REF_OR_NULL));
63 static inline bool operator==(
const Key_use &lhs,
const Key_use &rhs)
72 (lhs.
optimize & KEY_OPTIMIZE_REF_OR_NULL)
74 (rhs.
optimize & KEY_OPTIMIZE_REF_OR_NULL);
78 static inline std::ostream &operator<<(std::ostream &s,
const Key_use &v)
81 << v.
table->tablenr <<
", "
91 namespace dynarray_unittest {
101 return (
int) (a->
table->tablenr - b->
table->tablenr);
103 return (
int) (a->
key - b->
key);
111 return (
int) ((a->
optimize & KEY_OPTIMIZE_REF_OR_NULL) -
112 (b->
optimize & KEY_OPTIMIZE_REF_OR_NULL));
120 for (ix= 0; ix <
n; ++ix)
122 tables[ix].tablenr= ix % 3;
137 std::random_shuffle(&keys[0], &keys[n]);
142 const int num_elements= 200;
143 const int num_iterations= 1000;
156 static void SetUpTestCase()
158 generate_test_data(test_data, table_list, num_elements);
163 my_init_dynamic_array(&m_keyuse_dyn,
sizeof(
Key_use), num_elements, 64);
164 m_keyuse_vec.reserve(num_elements);
167 virtual void TearDown()
169 delete_dynamic(&m_keyuse_dyn);
172 void insert_and_sort_dynamic()
174 reset_dynamic(&m_keyuse_dyn);
175 for (
int ix= 0; ix < num_elements; ++ix)
177 insert_dynamic(&m_keyuse_dyn, &test_data[ix]);
179 my_qsort(m_keyuse_dyn.buffer, m_keyuse_dyn.elements,
sizeof(
Key_use),
180 reinterpret_cast<qsort_cmp>(sort_keyuse));
183 void insert_and_sort_vector()
185 m_keyuse_vec.clear();
186 for (
int ix= 0; ix < num_elements; ++ix)
188 m_keyuse_vec.push_back(test_data[ix]);
190 std::sort(m_keyuse_vec.begin(), m_keyuse_vec.end(), std::less<Key_use>());
194 std::vector<Key_use> m_keyuse_vec;
196 static Key_use test_data[num_elements];
197 static TABLE table_list[num_elements];
202 Key_use DynArrayTest::test_data[num_elements];
203 TABLE DynArrayTest::table_list[num_elements];
209 for (
int ix= 0; ix < num_iterations; ++ix)
210 insert_and_sort_dynamic();
215 TEST_F(DynArrayTest,
Vector)
217 for (
int ix= 0; ix < num_iterations; ++ix)
218 insert_and_sort_vector();
229 : m_mem_root_p(&m_mem_root),
230 m_array_mysys(m_mem_root_p),
231 m_array_std(m_mem_root_p)
236 init_sql_alloc(&m_mem_root, 1024, 0);
237 ASSERT_EQ(0, my_pthread_setspecific_ptr(THR_MALLOC, &m_mem_root_p));
239 ASSERT_EQ(root, m_mem_root_p);
241 m_array_mysys.reserve(num_elements);
242 m_array_std.reserve(num_elements);
245 virtual void TearDown()
247 free_root(&m_mem_root, MYF(0));
250 static void SetUpTestCase()
252 generate_test_data(test_data, table_list, num_elements);
253 ASSERT_EQ(0, pthread_key_create(&THR_THD, NULL));
254 ASSERT_EQ(0, pthread_key_create(&THR_MALLOC, NULL));
257 static void TearDownTestCase()
259 pthread_key_delete(THR_THD);
260 pthread_key_delete(THR_MALLOC);
263 void insert_and_sort_mysys()
265 m_array_mysys.clear();
266 for (
int ix= 0; ix < num_elements; ++ix)
268 m_array_mysys.push_back(test_data[ix]);
270 my_qsort(m_array_mysys.begin(), m_array_mysys.size(),
271 m_array_mysys.element_size(),
272 reinterpret_cast<qsort_cmp
>(sort_keyuse));
275 void insert_and_sort_std()
278 for (
int ix= 0; ix < num_elements; ++ix)
280 m_array_std.push_back(test_data[ix]);
282 std::sort(m_array_std.begin(), m_array_std.end(), std::less<Key_use>());
290 static Key_use test_data[num_elements];
291 static TABLE table_list[num_elements];
296 Key_use MemRootTest::test_data[num_elements];
297 TABLE MemRootTest::table_list[num_elements];
303 for (
int ix= 0; ix < num_iterations; ++ix)
304 insert_and_sort_mysys();
309 TEST_F(MemRootTest, KeyUseStd)
311 for (
int ix= 0; ix < num_iterations; ++ix)
312 insert_and_sort_std();
317 TEST_F(MemRootTest, KeyUseCompare)
319 insert_and_sort_mysys();
320 insert_and_sort_std();
321 for (
int ix= 0; ix < num_elements; ++ix)
323 Key_use k1= m_array_mysys.at(ix);
324 Key_use k2= m_array_std.at(ix);
331 TEST_F(MemRootTest, Reserve)
335 const uint num_pushes= 20;
336 for (uint ix=0; ix < num_pushes; ++ix)
338 EXPECT_EQ(ix, intarr.size());
339 EXPECT_FALSE(intarr.push_back(ix));
340 EXPECT_EQ(ix, intarr.at(ix));
342 for (uint ix=0; ix < num_pushes; ++ix)
344 EXPECT_EQ(ix, intarr.at(ix));
346 EXPECT_EQ(
sizeof(uint), intarr.element_size());
347 EXPECT_EQ(num_pushes, intarr.size());
348 EXPECT_LE(num_pushes, intarr.capacity());
371 for (
size_t ix= 0; ix < array.capacity(); ++ix)
372 array.push_back(foo);
374 EXPECT_EQ(0
U, counter);
376 EXPECT_EQ(nn / 2, counter);
377 EXPECT_EQ(nn / 2, array.size());
380 EXPECT_EQ(nn, counter);
385 TEST_F(MemRootTest, ReserveDestroy)
389 array.reserve(nn / 2);
391 DestroyCounter foo(&counter);
392 for (
size_t ix= 0; ix < nn; ++ix)
393 array.push_back(foo);
395 EXPECT_EQ(nn / 2, counter);
396 EXPECT_EQ(nn, array.size());
400 EXPECT_EQ(nn, counter);