17 #include "my_config.h"
18 #include <gtest/gtest.h>
22 #include "bounded_queue.h"
23 #include "filesort_utils.h"
26 namespace bounded_queue_unittest {
28 const int num_elements= 14;
31 template <
class T,
int size>
32 int array_size(
const T (&)[
size])
51 snprintf(text, array_size(text),
"%4d", i);
66 Test_key() : element(NULL), key(-1) {}
78 EXPECT_EQ(*cmp_arg,
sizeof(
int));
94 void test_keymaker(
Sort_param *sp, Test_key *key, Test_element *element)
96 key->element= element;
97 key->key= element->val;
104 template<
int sz,
typename Key_type>
109 for (
int ix= 0; ix <= sz; ++ix)
110 key_ptrs[ix]= &key_data[ix];
113 Key_type *key_ptrs[sz+1];
114 Key_type key_data[sz+1];
128 for (ix=0; ix < array_size(m_test_data); ++ix)
130 std::random_shuffle(&m_test_data[0], &m_test_data[array_size(m_test_data)]);
133 void insert_test_data()
135 for (
int ix= 0; ix < array_size(m_test_data); ++ix)
136 m_queue.
push(&m_test_data[ix]);
155 #if !defined(DBUG_OFF)
161 ::testing::FLAGS_gtest_death_test_style =
"threadsafe";
163 EXPECT_DEATH_IF_SUPPORTED(m_queue.push(&foo),
164 ".*Assertion .*is_initialized.*");
170 TEST_F(BoundedQueueDeathTest, DieIfPoppingEmptyQueue)
172 EXPECT_EQ(0, m_queue.init(0,
true, test_key_compare,
174 &test_keymaker, NULL, m_keys.key_ptrs));
175 ::testing::FLAGS_gtest_death_test_style =
"threadsafe";
176 EXPECT_DEATH_IF_SUPPORTED(m_queue.pop(),
177 ".*Assertion .*elements > 0.*");
179 #endif // !defined(DBUG_OFF)
185 TEST_F(BoundedQueueTest, ConstructAndDestruct)
187 EXPECT_EQ(0, m_queue.init(num_elements/2,
true,
190 &test_keymaker, NULL, m_keys.key_ptrs));
197 TEST_F(BoundedQueueTest, TooManyElements)
199 EXPECT_EQ(1, m_queue.init(UINT_MAX,
true,
202 &test_keymaker, NULL, m_keys.key_ptrs));
203 EXPECT_EQ(1, m_queue.init(UINT_MAX - 1,
true,
206 &test_keymaker, NULL, m_keys.key_ptrs));
213 TEST_F(BoundedQueueTest, ZeroSizeQueue)
215 EXPECT_EQ(0, m_queue.init(0,
true, test_key_compare,
217 &test_keymaker, NULL, m_keys.key_ptrs));
219 EXPECT_EQ(1
U, m_queue.num_elements());
226 TEST_F(BoundedQueueTest, PushAndPopKeepLargest)
228 EXPECT_EQ(0, m_queue.init(num_elements/2,
false, test_key_compare,
230 &test_keymaker, NULL, m_keys.key_ptrs));
233 const int max_key_val= array_size(m_test_data) - 1;
234 while (m_queue.num_elements() > 0)
236 Test_key **top= m_queue.pop();
237 int expected_key_val= max_key_val - m_queue.num_elements();
238 int key_val= (*top)->key;
239 EXPECT_EQ(expected_key_val, key_val);
240 Test_element *element= (*top)->element;
241 EXPECT_EQ(expected_key_val, element->val);
250 TEST_F(BoundedQueueTest, PushAndPopKeepSmallest)
252 EXPECT_EQ(0, m_queue.init(num_elements/2,
true, test_key_compare,
254 &test_keymaker, NULL, m_keys.key_ptrs));
257 while (m_queue.num_elements() > 0)
259 Test_key **top= m_queue.pop();
260 int expected_key_val= m_queue.num_elements();
261 int key_val= (*top)->key;
262 EXPECT_EQ(expected_key_val, key_val);
263 Test_element *element= (*top)->element;
264 EXPECT_EQ(expected_key_val, element->val);
272 TEST_F(BoundedQueueTest, InsertAndSort)
274 EXPECT_EQ(0, m_queue.init(num_elements/2,
true, test_key_compare,
276 &test_keymaker, NULL, m_keys.key_ptrs));
278 uchar *base= (uchar*) &m_keys.key_ptrs[0];
279 size_t size=
sizeof(Test_key);
281 for (
int ii= 0; ii < array_size(m_keys.key_data); ++ii)
282 m_keys.key_data[ii].element= NULL;
284 my_string_ptr_sort(base, array_size(m_keys.key_ptrs),
size);
285 for (
int ii= 0; ii < num_elements/2; ++ii)
287 Test_key *sorted_key= m_keys.key_ptrs[ii];
288 EXPECT_EQ(ii, sorted_key->key);
296 TEST(CostEstimationTest, MergeManyBuff)
298 ha_rows num_rows= 512;
300 ulong row_lenght= 100;
301 double prev_cost= 0.0;
302 while (num_rows <= MAX_FILE_SIZE/4)
305 get_merge_many_buffs_cost_fast(num_rows, num_keys, row_lenght);
306 EXPECT_LT(0.0, merge_cost);
307 EXPECT_LT(prev_cost, merge_cost);
309 prev_cost= merge_cost;
317 int int_ptr_compare(
size_t *cmp_arg,
int **a,
int **b)
319 EXPECT_EQ(*cmp_arg,
sizeof(
int));
337 memcpy(to, from,
sizeof(
int));
346 const int num_rows= 10000;
347 const int row_limit= 100;
348 const int num_iterations= 10;
358 enum { limit= row_limit };
369 enum { limit= num_rows };
374 void insert_and_sort()
377 for (
int it= 0; it < num_iterations; ++it)
379 Container *keys=
new Container;
382 EXPECT_EQ(0, queue.
init(
limit,
true, int_ptr_compare,
383 sizeof(
int), &int_keymaker, NULL, keys->key_ptrs));
384 for (
int ix= 0; ix < num_rows; ++ix)
389 my_string_ptr_sort((uchar*) &keys->key_ptrs[0],
399 TEST_F(PerfTestSmall, InsertAndSort)
401 insert_and_sort<limit>();
408 TEST_F(PerfTestLarge, InsertAndSort)
410 insert_and_sort<limit>();
417 TEST_F(PerfTestLarge, WithoutQueue)
419 for (
int it= 0; it < num_iterations; ++it)
421 Container *keys=
new Container;
423 for (
int ix= 0; ix <
limit; ++ix)
426 keys->key_data[ix]= data;
428 my_string_ptr_sort((uchar*) &keys->key_ptrs[0], limit,
sizeof(
int));
437 TEST_F(PerfTestLarge, NoSorting)
439 for (
int it= 0; it < num_iterations; ++it)
441 Container *keys=
new Container;
443 for (
int ix= 0; ix <
limit; ++ix)
446 keys->key_data[ix]= data;