1 /* Copyright (c) 2014, 2021, 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 also distributed 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 included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 // First include (the generated) my_config.h, to get correct platform defines.
24 #include "my_config.h"
25 #include <gtest/gtest.h>
26 #include "my_sys.h"
27 
28 #include <algorithm>
29 #include <vector>
30 
31 namespace myqsort_vs_stdsort_unittest {
32 
cmp_double(void * cmp_arg,double * a,double * b)33 static int cmp_double(void *cmp_arg, double *a,double *b)
34 {
35   return *a < *b ? -1 : *a == *b ? 0 : 1;
36 }
37 
38 #if defined(GTEST_HAS_PARAM_TEST)
39 
40 #if !defined(NDEBUG)
41 // There is no point in benchmarking anything in debug mode.
42 const size_t num_iterations= 1ULL;
43 #else
44 // Set this so that each test case takes a few seconds.
45 // And set it back to a small value before pushing!!
46 // const size_t num_iterations= 2000000ULL;
47 const size_t num_iterations= 2ULL;
48 #endif
49 
50 class DoubleSortCompareTest : public ::testing::TestWithParam<int>
51 {
52 public:
SetUpTestCase()53   static void SetUpTestCase()
54   {
55     doubles_to_sort.reserve(1000);
56     for (int ix= 0; ix < 1000; ++ix)
57     {
58       doubles_to_sort.push_back(ix);
59     }
60     // Remove comment to get results for randomized data.
61     // std::random_shuffle(doubles_to_sort.begin(), doubles_to_sort.end());
62   }
63 
SetUp()64   virtual void SetUp()
65   {
66     num_elements= GetParam();
67   }
68 
69   int num_elements;
70   static std::vector<double> doubles_to_sort;
71 };
72 std::vector<double> DoubleSortCompareTest::doubles_to_sort;
73 
74 
75 int test_values[]= {10, 100, 1000};
76 
77 INSTANTIATE_TEST_CASE_P(Sort, DoubleSortCompareTest,
78                         ::testing::ValuesIn(test_values));
79 
TEST_P(DoubleSortCompareTest,StdSort)80 TEST_P(DoubleSortCompareTest, StdSort)
81 {
82   for (size_t ix= 0; ix < num_iterations; ++ix)
83   {
84     std::vector<double> data(doubles_to_sort.begin(),
85                              doubles_to_sort.begin() + num_elements);
86     std::sort(data.begin(), data.end());
87   }
88 }
89 
TEST_P(DoubleSortCompareTest,MyQSort)90 TEST_P(DoubleSortCompareTest, MyQSort)
91 {
92   for (size_t ix= 0; ix < num_iterations; ++ix)
93   {
94     std::vector<double> data(doubles_to_sort.begin(),
95                              doubles_to_sort.begin() + num_elements);
96     my_qsort2(&data[0], num_elements, sizeof(double),
97               reinterpret_cast<qsort2_cmp>(cmp_double), NULL);
98   }
99 }
100 
101 #endif  // GTEST_HAS_PARAM_TEST
102 
103 }
104