17 #include "my_config.h"
18 #include <gtest/gtest.h>
20 #include "filesort_utils.h"
26 namespace filesort_compare_unittest {
51 template <
class T,
int size>
52 int array_size(
const T (&)[
size])
57 inline int bytes_to_int(
const uchar *s)
61 return val ^ 0x80000000;
64 inline void int_to_bytes(uchar *s,
int val)
66 val= val ^ 0x80000000;
71 TEST(BufferAlignmentTest, IntsToBytesToInt)
74 memset(buf, 0,
sizeof(buf));
75 for (
int ix= 0; ix < 6; ++ix)
77 int test_data[]= { INT_MIN32, -42, -1, 0, 1, 42, INT_MAX32 };
78 for (
int iy= 0; iy < array_size(test_data); ++iy)
80 int val= test_data[iy];
81 int_to_bytes(buf+ix, val);
82 EXPECT_EQ(val, bytes_to_int(buf+ix));
92 static const int num_iterations= 1;
94 static const int num_records= 100 * 1000;
96 static const int keys_per_record= 4;
98 static const int record_size= keys_per_record *
sizeof(int);
102 static std::vector<int> test_data;
104 static void SetUpTestCase()
106 test_data.reserve(num_records * keys_per_record);
107 union {
int val; uchar buf[
sizeof(int)]; } sort_str;
109 for (
int ix= 0; ix < num_records * keys_per_record; ++ix)
111 int val= ix / (10 * keys_per_record);
112 if (ix % 10 == 0) val= -val;
113 int_to_bytes(sort_str.buf, val);
114 test_data.push_back(sort_str.val);
117 std::random_shuffle(test_data.begin(), test_data.end());
120 static void TearDownTestCase()
123 std::vector<int>().swap(test_data);
128 sort_keys=
new uchar* [num_records];
129 for (
int ix= 0; ix < num_records; ++ix)
131 static_cast<uchar*>(static_cast<void*>(&test_data[keys_per_record*ix]));
134 virtual void TearDown()
141 std::vector<int> FileSortCompareTest::test_data;
148 inline bool mem_compare_1(
const uchar *s1,
const uchar *s2,
size_t len)
152 return *--s1 < *--s2;
153 }
while (--len != 0);
157 inline bool mem_compare_2(
const uchar *s1,
const uchar *s2,
size_t len)
160 while (len-- > 0 && v == 0)
162 v= *(s1++) - *(s2++);
167 inline bool mem_compare_3(
const uchar *s1,
const uchar *s2,
size_t len)
169 while (--len && (s1[0] == s2[0]))
173 return s1[0] < s2[0];
177 #pragma intrinsic(memcmp)
184 public std::binary_function<const uchar*, const uchar*, bool>
188 bool operator()(
const uchar *s1,
const uchar *s2)
190 return memcmp(s1, s2, m_size) < 0;
197 public std::binary_function<const uchar*, const uchar*, bool>
201 bool operator()(
const uchar *s1,
const uchar *s2)
203 return mem_compare_1(s1, s2, m_size);
210 public std::binary_function<const uchar*, const uchar*, bool>
214 bool operator()(
const uchar *s1,
const uchar *s2)
216 return mem_compare_2(s1, s2, m_size);
223 public std::binary_function<const uchar*, const uchar*, bool>
227 bool operator()(
const uchar *s1,
const uchar *s2)
229 return mem_compare_3(s1, s2, m_size);
235 #define COMPARE(N) if (s1[N] != s2[N]) return s1[N] < s2[N]
238 public std::binary_function<const uchar*, const uchar*, bool>
242 bool operator()(
const uchar *s1,
const uchar *s2)
264 public std::binary_function<const uchar*, const uchar*, bool>
268 bool operator()(
const uchar *s1,
const uchar *s2)
270 int int1= bytes_to_int(s1);
271 int int2= bytes_to_int(s2);
273 return mem_compare_1(s1 + rest, s2 + rest, rest);
282 public std::binary_function<const uchar*, const uchar*, bool>
286 bool operator() (
const uchar *s1,
const uchar *s2)
288 int inta1= bytes_to_int(s1);
289 int intb1= bytes_to_int(s2);
290 if (keyno < 4 && inta1 == intb1)
293 return operator()(s1 +
sizeof(
int), s2 +
sizeof(
int));
295 return inta1 < intb1;
308 for (
int ix= 0; ix < num_iterations; ++ix)
310 std::vector<uchar*> keys(sort_keys, sort_keys + num_records);
314 TEST_F(FileSortCompareTest, RadixSort)
316 for (
int ix= 0; ix < num_iterations; ++ix)
318 std::vector<uchar*> keys(sort_keys, sort_keys + num_records);
319 std::pair<uchar**, ptrdiff_t> buffer=
320 std::get_temporary_buffer<uchar*>(num_records);
321 radixsort_for_str_ptr(&keys[0], num_records, record_size, buffer.first);
322 std::return_temporary_buffer(buffer.first);
326 TEST_F(FileSortCompareTest, MyQsort)
328 size_t size= record_size;
329 for (
int ix= 0; ix < num_iterations; ++ix)
331 std::vector<uchar*> keys(sort_keys, sort_keys + num_records);
332 my_qsort2((uchar*) &keys[0], num_records,
sizeof(uchar*),
333 get_ptr_compare(record_size), &size);
337 TEST_F(FileSortCompareTest, StdSortmemcmp)
339 for (
int ix= 0; ix < num_iterations; ++ix)
341 std::vector<uchar*> keys(sort_keys, sort_keys + num_records);
342 std::sort(keys.begin(), keys.end(), Mem_compare_memcmp(record_size));
346 TEST_F(FileSortCompareTest, StdStableSortmemcmp)
348 for (
int ix= 0; ix < num_iterations; ++ix)
350 std::vector<uchar*> keys(sort_keys, sort_keys + num_records);
351 std::stable_sort(keys.begin(), keys.end(),
352 Mem_compare_memcmp(record_size));
356 TEST_F(FileSortCompareTest, StdSortCompare1)
358 for (
int ix= 0; ix < num_iterations; ++ix)
360 std::vector<uchar*> keys(sort_keys, sort_keys + num_records);
361 std::sort(keys.begin(), keys.end(), Mem_compare_1(record_size));
365 TEST_F(FileSortCompareTest, StdStableSortCompare1)
367 for (
int ix= 0; ix < num_iterations; ++ix)
369 std::vector<uchar*> keys(sort_keys, sort_keys + num_records);
370 std::stable_sort(keys.begin(), keys.end(), Mem_compare_1(record_size));
374 TEST_F(FileSortCompareTest, StdSortCompare2)
376 for (
int ix= 0; ix < num_iterations; ++ix)
378 std::vector<uchar*> keys(sort_keys, sort_keys + num_records);
379 std::sort(keys.begin(), keys.end(), Mem_compare_2(record_size));
383 TEST_F(FileSortCompareTest, StdStableSortCompare2)
385 for (
int ix= 0; ix < num_iterations; ++ix)
387 std::vector<uchar*> keys(sort_keys, sort_keys + num_records);
388 std::stable_sort(keys.begin(), keys.end(), Mem_compare_2(record_size));
392 TEST_F(FileSortCompareTest, StdSortCompare3)
394 for (
int ix= 0; ix < num_iterations; ++ix)
396 std::vector<uchar*> keys(sort_keys, sort_keys + num_records);
397 std::sort(keys.begin(), keys.end(), Mem_compare_3(record_size));
401 TEST_F(FileSortCompareTest, StdStableSortCompare3)
403 for (
int ix= 0; ix < num_iterations; ++ix)
405 std::vector<uchar*> keys(sort_keys, sort_keys + num_records);
406 std::stable_sort(keys.begin(), keys.end(), Mem_compare_3(record_size));
410 TEST_F(FileSortCompareTest, StdSortCompare4)
412 for (
int ix= 0; ix < num_iterations; ++ix)
414 std::vector<uchar*> keys(sort_keys, sort_keys + num_records);
415 std::sort(keys.begin(), keys.end(), Mem_compare_0(record_size));
419 TEST_F(FileSortCompareTest, StdStableSortCompare4)
421 for (
int ix= 0; ix < num_iterations; ++ix)
423 std::vector<uchar*> keys(sort_keys, sort_keys + num_records);
424 std::stable_sort(keys.begin(), keys.end(), Mem_compare_0(record_size));
428 TEST_F(FileSortCompareTest, DISABLED_StdSortIntCompare)
430 for (
int ix= 0; ix < num_iterations; ++ix)
432 std::vector<uchar*> keys(sort_keys, sort_keys + num_records);
433 std::sort(keys.begin(), keys.end(), Mem_compare_int(record_size));
437 TEST_F(FileSortCompareTest, DISABLED_StdStableSortIntCompare)
439 for (
int ix= 0; ix < num_iterations; ++ix)
441 std::vector<uchar*> keys(sort_keys, sort_keys + num_records);
442 std::stable_sort(keys.begin(), keys.end(), Mem_compare_int(record_size));
446 TEST_F(FileSortCompareTest, DISABLED_StdSortIntIntIntInt)
448 for (
int ix= 0; ix < num_iterations; ++ix)
450 std::vector<uchar*> keys(sort_keys, sort_keys + num_records);
451 std::sort(keys.begin(), keys.end(),
452 Mem_compare_int_4(record_size));
456 TEST_F(FileSortCompareTest, DISABLED_StdStableSortIntIntIntInt)
458 for (
int ix= 0; ix < num_iterations; ++ix)
460 std::vector<uchar*> keys(sort_keys, sort_keys + num_records);
461 std::stable_sort(keys.begin(), keys.end(),
462 Mem_compare_int_4(record_size));