1 /* Copyright (c) 2011, 2017, 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, 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
26 #include <gtest/gtest.h>
27
28 #include "sql/handler.h"
29
30 namespace cost_estimate_unittest {
31
TEST(CostEstimateTest,Basics)32 TEST(CostEstimateTest, Basics) {
33 Cost_estimate ce1;
34
35 EXPECT_EQ(0, ce1.total_cost());
36 EXPECT_TRUE(ce1.is_zero());
37
38 const double initial_io_cost = 4.5;
39
40 ce1.add_io(initial_io_cost);
41 EXPECT_FALSE(ce1.is_zero());
42 EXPECT_DOUBLE_EQ(initial_io_cost, ce1.total_cost());
43
44 const double initial_cpu_cost = 3.3;
45 ce1.add_cpu(initial_cpu_cost);
46
47 EXPECT_DOUBLE_EQ(initial_cpu_cost, ce1.get_cpu_cost());
48 EXPECT_DOUBLE_EQ(initial_io_cost, ce1.get_io_cost());
49 EXPECT_DOUBLE_EQ(initial_io_cost + initial_cpu_cost, ce1.total_cost());
50
51 EXPECT_EQ(0, ce1.get_mem_cost());
52 EXPECT_EQ(0, ce1.get_import_cost());
53
54 const double initial_mem_cost = 7;
55 const double initial_import_cost = 11;
56 ce1.add_mem(initial_mem_cost);
57 ce1.add_import(initial_import_cost);
58
59 const double total_initial_cost =
60 initial_io_cost + initial_cpu_cost + initial_import_cost;
61 EXPECT_DOUBLE_EQ(total_initial_cost, ce1.total_cost());
62
63 const double added_io_cost = 1.5;
64 ce1.add_io(added_io_cost);
65 EXPECT_DOUBLE_EQ(initial_io_cost + added_io_cost, ce1.get_io_cost());
66 EXPECT_DOUBLE_EQ(total_initial_cost + added_io_cost, ce1.total_cost());
67
68 EXPECT_FALSE(ce1.is_zero());
69
70 ce1.reset();
71 EXPECT_TRUE(ce1.is_zero());
72 }
73
TEST(CostEstimateTest,Operators)74 TEST(CostEstimateTest, Operators) {
75 Cost_estimate ce_io;
76
77 EXPECT_EQ(0, ce_io.total_cost());
78 EXPECT_TRUE(ce_io.is_zero());
79
80 const double initial_io_cost = 4.5;
81 ce_io.add_io(initial_io_cost);
82 EXPECT_DOUBLE_EQ(initial_io_cost, ce_io.total_cost());
83
84 Cost_estimate ce_cpu;
85 const double initial_cpu_cost = 3.3;
86 ce_cpu.add_cpu(initial_cpu_cost);
87 EXPECT_DOUBLE_EQ(initial_cpu_cost, ce_cpu.total_cost());
88 EXPECT_EQ(0, ce_cpu.get_io_cost());
89
90 // Copy CTOR
91 Cost_estimate ce_copy(ce_io);
92 const double added_io_cost = 1.5;
93 ce_io.add_io(added_io_cost); // should not add to ce_copy
94 EXPECT_DOUBLE_EQ(initial_io_cost + added_io_cost, ce_io.total_cost());
95 EXPECT_DOUBLE_EQ(initial_io_cost, ce_copy.total_cost());
96
97 // operator+=
98 ce_copy += ce_cpu;
99 EXPECT_DOUBLE_EQ(initial_io_cost + initial_cpu_cost, ce_copy.total_cost());
100
101 // operator+
102 Cost_estimate ce_copy2 = ce_io + ce_cpu;
103 const double copy2_totcost =
104 initial_io_cost + added_io_cost + initial_cpu_cost;
105 EXPECT_DOUBLE_EQ(copy2_totcost, ce_copy2.total_cost());
106
107 Cost_estimate ce_mem_import1;
108 const double import1_mem_cost = 3;
109 const double import1_import_cost = 5;
110 ce_mem_import1.add_mem(import1_mem_cost);
111 ce_mem_import1.add_import(import1_import_cost);
112
113 Cost_estimate ce_mem_import2;
114 const double import2_mem_cost = 11;
115 const double import2_import_cost = 13;
116 ce_mem_import2.add_mem(import2_mem_cost);
117 ce_mem_import2.add_import(import2_import_cost);
118
119 // operator+
120 Cost_estimate ce_mi_copy = ce_mem_import1 + ce_mem_import2;
121 EXPECT_DOUBLE_EQ(import1_import_cost + import2_import_cost,
122 ce_mi_copy.total_cost());
123 EXPECT_DOUBLE_EQ(import1_mem_cost + import2_mem_cost,
124 ce_mi_copy.get_mem_cost());
125 EXPECT_DOUBLE_EQ(import1_import_cost + import2_import_cost,
126 ce_mi_copy.get_import_cost());
127
128 // operator+=
129 ce_mi_copy += ce_mem_import1;
130 EXPECT_DOUBLE_EQ(2 * import1_import_cost + import2_import_cost,
131 ce_mi_copy.total_cost());
132 EXPECT_DOUBLE_EQ(2 * import1_mem_cost + import2_mem_cost,
133 ce_mi_copy.get_mem_cost());
134 EXPECT_DOUBLE_EQ(2 * import1_import_cost + import2_import_cost,
135 ce_mi_copy.get_import_cost());
136
137 // operator-
138 ce_mi_copy = ce_mem_import1 - ce_mem_import2;
139 EXPECT_DOUBLE_EQ(
140 ce_mi_copy.get_mem_cost(),
141 ce_mem_import1.get_mem_cost() - ce_mem_import2.get_mem_cost());
142 EXPECT_DOUBLE_EQ(
143 ce_mi_copy.get_import_cost(),
144 ce_mem_import1.get_import_cost() - ce_mem_import2.get_import_cost());
145
146 // copy assignment
147 Cost_estimate ce_copy3;
148 ce_copy3 = ce_copy2;
149 EXPECT_DOUBLE_EQ(copy2_totcost, ce_copy3.total_cost());
150 }
151
TEST(CostEstimateTest,MaxValue)152 TEST(CostEstimateTest, MaxValue) {
153 Cost_estimate ce;
154 Cost_estimate ce2;
155
156 EXPECT_FALSE(ce.is_max_cost());
157
158 ce.set_max_cost();
159 EXPECT_TRUE(ce.is_max_cost());
160
161 EXPECT_TRUE(ce > ce2);
162 EXPECT_FALSE(ce < ce2);
163 EXPECT_FALSE(ce2 > ce);
164 EXPECT_TRUE(ce2 < ce);
165
166 ce2 = ce;
167 EXPECT_TRUE(ce2.is_max_cost());
168 EXPECT_FALSE(ce < ce2);
169 EXPECT_FALSE(ce2 > ce);
170 }
171
172 } // namespace cost_estimate_unittest
173