1 /* Copyright (c) 2011, 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 Street, 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
27 #include "handler.h"
28
29 namespace cost_estimate_unittest {
30
TEST(CostEstimateTest,Basics)31 TEST(CostEstimateTest, Basics)
32 {
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 }
74
TEST(CostEstimateTest,Operators)75 TEST(CostEstimateTest, Operators)
76 {
77 Cost_estimate ce_io;
78
79 EXPECT_EQ(0, ce_io.total_cost());
80 EXPECT_TRUE(ce_io.is_zero());
81
82 const double initial_io_cost= 4.5;
83 ce_io.add_io(initial_io_cost);
84 EXPECT_DOUBLE_EQ(initial_io_cost, ce_io.total_cost());
85
86 Cost_estimate ce_cpu;
87 const double initial_cpu_cost= 3.3;
88 ce_cpu.add_cpu(initial_cpu_cost);
89 EXPECT_DOUBLE_EQ(initial_cpu_cost, ce_cpu.total_cost());
90 EXPECT_EQ(0, ce_cpu.get_io_cost());
91
92 // Copy CTOR
93 Cost_estimate ce_copy(ce_io);
94 const double added_io_cost= 1.5;
95 ce_io.add_io(added_io_cost); // should not add to ce_copy
96 EXPECT_DOUBLE_EQ(initial_io_cost + added_io_cost, ce_io.total_cost());
97 EXPECT_DOUBLE_EQ(initial_io_cost, ce_copy.total_cost());
98
99 // operator+=
100 ce_copy+= ce_cpu;
101 EXPECT_DOUBLE_EQ(initial_io_cost + initial_cpu_cost, ce_copy.total_cost());
102
103 // operator+
104 Cost_estimate ce_copy2= ce_io + ce_cpu;
105 const double copy2_totcost=
106 initial_io_cost + added_io_cost + initial_cpu_cost;
107 EXPECT_DOUBLE_EQ(copy2_totcost, ce_copy2.total_cost());
108
109 Cost_estimate ce_mem_import1;
110 const double import1_mem_cost= 3;
111 const double import1_import_cost= 5;
112 ce_mem_import1.add_mem(import1_mem_cost);
113 ce_mem_import1.add_import(import1_import_cost);
114
115 Cost_estimate ce_mem_import2;
116 const double import2_mem_cost= 11;
117 const double import2_import_cost= 13;
118 ce_mem_import2.add_mem(import2_mem_cost);
119 ce_mem_import2.add_import(import2_import_cost);
120
121 // operator+
122 Cost_estimate ce_mi_copy= ce_mem_import1 + ce_mem_import2;
123 EXPECT_DOUBLE_EQ(import1_import_cost + import2_import_cost,
124 ce_mi_copy.total_cost());
125 EXPECT_DOUBLE_EQ(import1_mem_cost + import2_mem_cost,
126 ce_mi_copy.get_mem_cost());
127 EXPECT_DOUBLE_EQ(import1_import_cost + import2_import_cost,
128 ce_mi_copy.get_import_cost());
129
130 // operator+=
131 ce_mi_copy+= ce_mem_import1;
132 EXPECT_DOUBLE_EQ(2*import1_import_cost + import2_import_cost,
133 ce_mi_copy.total_cost());
134 EXPECT_DOUBLE_EQ(2*import1_mem_cost + import2_mem_cost,
135 ce_mi_copy.get_mem_cost());
136 EXPECT_DOUBLE_EQ(2*import1_import_cost + import2_import_cost,
137 ce_mi_copy.get_import_cost());
138
139 // operator-
140 ce_mi_copy= ce_mem_import1 - ce_mem_import2;
141 EXPECT_DOUBLE_EQ(ce_mi_copy.get_mem_cost(),
142 ce_mem_import1.get_mem_cost() -
143 ce_mem_import2.get_mem_cost());
144 EXPECT_DOUBLE_EQ(ce_mi_copy.get_import_cost(),
145 ce_mem_import1.get_import_cost() -
146 ce_mem_import2.get_import_cost());
147
148 // copy assignment
149 Cost_estimate ce_copy3;
150 ce_copy3= ce_copy2;
151 EXPECT_DOUBLE_EQ(copy2_totcost, ce_copy3.total_cost());
152 }
153
TEST(CostEstimateTest,MaxValue)154 TEST(CostEstimateTest, MaxValue)
155 {
156 Cost_estimate ce;
157 Cost_estimate ce2;
158
159 EXPECT_FALSE(ce.is_max_cost());
160
161 ce.set_max_cost();
162 EXPECT_TRUE(ce.is_max_cost());
163
164 EXPECT_TRUE(ce > ce2);
165 EXPECT_FALSE(ce < ce2);
166 EXPECT_FALSE(ce2 > ce);
167 EXPECT_TRUE(ce2 < ce);
168
169 ce2= ce;
170 EXPECT_TRUE(ce2.is_max_cost());
171 EXPECT_FALSE(ce < ce2);
172 EXPECT_FALSE(ce2 > ce);
173 }
174
175
176 } //namespace
177