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 <stddef.h>
27 
28 #include "test_utils.h"
29 #include "fake_costmodel.h"
30 #include "sql_class.h"
31 #include "uniques.h"
32 
33 namespace unique_unittest {
34 
35 using my_testing::Server_initializer;
36 
37 class UniqueCostTest : public ::testing::Test
38 {
39 protected:
SetUp()40   virtual void SetUp() { initializer.SetUp(); }
TearDown()41   virtual void TearDown() { initializer.TearDown(); }
42 
thd()43   THD *thd() { return initializer.thd(); }
44 
45   Server_initializer initializer;
46 };
47 
48 // This is an excerpt of code from get_best_disjunct_quick()
TEST_F(UniqueCostTest,GetUseCost)49 TEST_F(UniqueCostTest, GetUseCost)
50 {
51   const ulong num_keys= 328238;
52   const ulong key_size= 96;
53 
54   // Set up the optimizer cost model
55   Fake_Cost_model_table cost_model_table;
56 
57   size_t unique_calc_buff_size=
58     Unique::get_cost_calc_buff_size(num_keys, key_size, MIN_SORT_MEMORY);
59   void *rawmem= alloc_root(thd()->mem_root,
60                            unique_calc_buff_size * sizeof(uint));
61   Bounds_checked_array<uint> cost_buff=
62     Bounds_checked_array<uint>(static_cast<uint*>(rawmem), unique_calc_buff_size);
63   const double dup_removal_cost=
64     Unique::get_use_cost(cost_buff, num_keys, key_size, MIN_SORT_MEMORY,
65                          &cost_model_table);
66   EXPECT_GT(dup_removal_cost, 0.0);
67 }
68 
69 }
70