MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
join_tab_sort-t.cc
1 /* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
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 as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
15 
16 // First include (the generated) my_config.h, to get correct platform defines.
17 #include "my_config.h"
18 #include <gtest/gtest.h>
19 
20 #include "test_utils.h"
21 
22 #include "sql_select.h"
23 #include "merge_sort.h"
24 
25 #include <vector>
26 
27 namespace join_tab_sort_unittest {
28 
31 
32 class JTSortTest : public ::testing::Test
33 {
34 protected:
35  virtual void SetUp() { initializer.SetUp(); }
36  virtual void TearDown() { initializer.TearDown(); }
37 
38  Server_initializer initializer;
39 };
40 
41 
42 class MOCK_JOIN_TAB : public JOIN_TAB
43 {
44 public:
45  MOCK_JOIN_TAB(uint recs, uint table_no) : JOIN_TAB()
46  {
47  found_records= recs;
48  m_table.map= 1ULL << table_no;
49  this->table= &m_table;
50  }
51 
52  TABLE m_table;
53 };
54 
55 std::ostream &operator<<(std::ostream &s, const MOCK_JOIN_TAB &jt)
56 {
57  return s << "{"
58  << jt.found_records << ", "
59  << jt.m_table.map
60  << "}";
61 }
62 
63 
64 TEST_F(JTSortTest, SimpleSortTest)
65 {
66  MOCK_JOIN_TAB jt1(UINT_MAX, 0);
67  MOCK_JOIN_TAB jt2(2, 0);
68  MOCK_JOIN_TAB jt3(1, 0);
69  MOCK_JOIN_TAB jt4(10, 0);
70  MOCK_JOIN_TAB jt5(5, 0);
71 
72  MOCK_JOIN_TAB *arr[5];
73  arr[0]= &jt1;
74  arr[1]= &jt2;
75  arr[2]= &jt3;
76  arr[3]= &jt4;
77  arr[4]= &jt5;
78 
80 
81  EXPECT_EQ(1U, arr[0]->found_records);
82  EXPECT_EQ(2U, arr[1]->found_records);
83  EXPECT_EQ(5U, arr[2]->found_records);
84  EXPECT_EQ(10U, arr[3]->found_records);
85  EXPECT_EQ(UINT_MAX, arr[4]->found_records);
86 
87 }
88 
89 
90 TEST_F(JTSortTest, SortFoundRecordsTest)
91 {
92  const int num_tables= 50;
93  MOCK_JOIN_TAB *arr[num_tables];
94 
95  for (int i= 0; i < num_tables; i++)
96  arr[i]= new MOCK_JOIN_TAB(i, 0);
97 
98  // MERGE SORT
99  std::random_shuffle(arr, arr + 50);
100  merge_sort(arr, arr + num_tables, Join_tab_compare_default());
101  for (int i= 1; i < num_tables; i++)
102  EXPECT_TRUE(arr[i]->found_records > arr[i-1]->found_records);
103 
104  // INSERT SORT
105  std::random_shuffle(arr, arr + 50);
106  insert_sort(arr, arr + num_tables, Join_tab_compare_default());
107  for (int i= 1; i < num_tables; i++)
108  EXPECT_TRUE(arr[i]->found_records > arr[i-1]->found_records);
109 
110  for (int i= 0; i < num_tables; i++)
111  {
112  delete arr[i];
113  }
114 }
115 
116 
117 TEST_F(JTSortTest, SortDependsTest)
118 {
119  const int num_tables= 50;
120  MOCK_JOIN_TAB *arr[num_tables];
121 
122  /*
123  dependency has higher precedence than found_records, so the tables
124  shall be ordered with decreasing number of records in this test
125  */
126  for (int i= 0; i < num_tables; i++)
127  {
128  arr[i]= new MOCK_JOIN_TAB(i, i);
129  for (int j= i+1; j < num_tables; j++)
130  arr[i]->dependent|= 1ULL << j;
131  }
132 
133  // MERGE SORT
134  std::random_shuffle(arr, arr + num_tables);
135  merge_sort(arr, arr + num_tables, Join_tab_compare_default());
136  for (int i= 1; i < num_tables; i++)
137  EXPECT_TRUE(arr[i]->found_records < arr[i-1]->found_records)
138  << "i: " << *(arr[i]) << " "
139  << "i-1: " << *(arr[i-1]);
140 
141  // INSERT SORT
142  std::random_shuffle(arr, arr + num_tables);
143  insert_sort(arr, arr + num_tables, Join_tab_compare_default());
144  for (int i= 1; i < num_tables; i++)
145  EXPECT_TRUE(arr[i]->found_records < arr[i-1]->found_records);
146 
147  for (int i= 0; i < num_tables; i++)
148  {
149  delete arr[i];
150  }
151 }
152 
153 
154 TEST_F(JTSortTest, SortKeyDependsTest)
155 {
156  const int num_tables= 50;
157  MOCK_JOIN_TAB *arr[num_tables];
158 
159  /*
160  key_dependency has higher precedence than found_records, so the
161  tables shall be ordered with decreasing number of records in this
162  test
163  */
164  for (int i= 0; i < num_tables; i++)
165  {
166  arr[i]= new MOCK_JOIN_TAB(i, i);
167  for (int j= i+1; j < num_tables; j++)
168  arr[i]->key_dependent|= 1ULL << j;
169  }
170 
171  // MERGE SORT
172  std::random_shuffle(arr, arr + num_tables);
173  merge_sort(arr, arr + num_tables, Join_tab_compare_default());
174  for (int i= 1; i < num_tables; i++)
175  EXPECT_TRUE(arr[i]->found_records < arr[i-1]->found_records);
176 
177  // INSERT SORT
178  std::random_shuffle(arr, arr + num_tables);
179  insert_sort(arr, arr + num_tables, Join_tab_compare_default());
180  for (int i= 1; i < num_tables; i++)
181  EXPECT_TRUE(arr[i]->found_records < arr[i-1]->found_records);
182 
183  for (int i= 0; i < num_tables; i++)
184  delete arr[i];
185 }
186 
187 /*
188  Above, sorting for JOIN_TABs were tested. Below we check that the
189  sorting works for ints types as well.
190 */
191 
193  public std::binary_function<const int*, const int*, bool>
194 {
195 public:
196  bool operator()(const int *i1, const int *i2) const
197  {
198  return *i1 < *i2;
199  }
200 };
201 
202 
203 TEST_F(JTSortTest, SortIntTest)
204 {
205  const uint ints_to_sort= 1000;
206 
207  std::vector<int> arr;
208  std::vector<int*> arr_ptr;
209 
210  arr.reserve(ints_to_sort);
211  arr_ptr.reserve(ints_to_sort);
212 
213  for (uint i= 0; i < ints_to_sort; i++)
214  {
215  arr.push_back(i);
216  arr_ptr.push_back(&arr[i]);
217  }
218 
219  EXPECT_TRUE(arr.size() == ints_to_sort);
220  EXPECT_TRUE(arr_ptr.size() == ints_to_sort);
221 
222  // MERGE SORT
223  std::random_shuffle(&arr_ptr.front(), &arr_ptr.back() + 1);
224  merge_sort(&arr_ptr.front(), &arr_ptr.back() + 1, Int_compare_ptr());
225  for (uint i= 0; i < arr_ptr.size(); i++)
226  EXPECT_TRUE(*arr_ptr[i] == (int)i);
227 
228  // INSERT SORT
229  std::random_shuffle(&arr_ptr.front(), &arr_ptr.back() + 1);
230  insert_sort(&arr_ptr.front(), &arr_ptr.back() + 1, Int_compare_ptr());
231  for (uint i= 0; i < arr_ptr.size(); i++)
232  EXPECT_TRUE(*arr_ptr[i] == (int)i);
233 }
234 
235 
236 TEST_F(JTSortTest, SortInt2Test)
237 {
238  const uint ints_to_sort= 1000;
239 
240  std::vector<int> arr;
241  std::vector<int*> arr_ptr;
242 
243  arr.reserve(ints_to_sort);
244  arr_ptr.reserve(ints_to_sort);
245 
246  for (uint i= 0; i < (ints_to_sort - 2); i++)
247  {
248  arr.push_back((i % 2) ? i : (i * -1));
249  arr_ptr.push_back(&arr[i]);
250  }
251 
252  arr.push_back(INT_MAX32);
253  arr_ptr.push_back(&arr.back());
254 
255  arr.push_back(INT_MIN32);
256  arr_ptr.push_back(&arr.back());
257 
258  EXPECT_TRUE(arr.size() == ints_to_sort);
259  EXPECT_TRUE(arr_ptr.size() == ints_to_sort);
260 
261  // MERGE SORT
262  std::random_shuffle(&arr_ptr.front(), &arr_ptr.back() + 1);
263  merge_sort(&arr_ptr.front(), &arr_ptr.back() + 1, Int_compare_ptr());
264  for (uint i= 1; i < arr_ptr.size(); i++)
265  EXPECT_TRUE(*arr_ptr[i-1] < *arr_ptr[i]);
266 
267  // INSERT SORT
268  std::random_shuffle(&arr_ptr.front(), &arr_ptr.back() + 1);
269  insert_sort(&arr_ptr.front(), &arr_ptr.back() + 1, Int_compare_ptr());
270  for (uint i= 1; i < arr_ptr.size(); i++)
271  EXPECT_TRUE(*arr_ptr[i-1] < *arr_ptr[i]);
272 }
273 
274 }