Debian Codegraph @

unittest/gunit/cost_estimate-t.cc

raw symbols SCIP
1 /* Copyright (c) 2011, 2026, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <gtest/gtest.h>
25
26 #include "sql/handler.h"
27
28 namespace cost_estimate_unittest {
29
30 TEST(CostEstimateTest, Basics) {
31 Cost_estimate ce1;
32
33 EXPECT_EQ(0, ce1.total_cost());
34 EXPECT_TRUE(ce1.is_zero());
35
36 const double initial_io_cost = 4.5;
37
38 ce1.add_io(initial_io_cost);
39 EXPECT_FALSE(ce1.is_zero());
40 EXPECT_DOUBLE_EQ(initial_io_cost, ce1.total_cost());
41
42 const double initial_cpu_cost = 3.3;
43 ce1.add_cpu(initial_cpu_cost);
44
45 EXPECT_DOUBLE_EQ(initial_cpu_cost, ce1.get_cpu_cost());
46 EXPECT_DOUBLE_EQ(initial_io_cost, ce1.get_io_cost());
47 EXPECT_DOUBLE_EQ(initial_io_cost + initial_cpu_cost, ce1.total_cost());
48
49 EXPECT_EQ(0, ce1.get_mem_cost());
50 EXPECT_EQ(0, ce1.get_import_cost());
51
52 const double initial_mem_cost = 7;
53 const double initial_import_cost = 11;
54 ce1.add_mem(initial_mem_cost);
55 ce1.add_import(initial_import_cost);
56
57 const double total_initial_cost =
58 initial_io_cost + initial_cpu_cost + initial_import_cost;
59 EXPECT_DOUBLE_EQ(total_initial_cost, ce1.total_cost());
60
61 const double added_io_cost = 1.5;
62 ce1.add_io(added_io_cost);
63 EXPECT_DOUBLE_EQ(initial_io_cost + added_io_cost, ce1.get_io_cost());
64 EXPECT_DOUBLE_EQ(total_initial_cost + added_io_cost, ce1.total_cost());
65
66 EXPECT_FALSE(ce1.is_zero());
67
68 ce1.reset();
69 EXPECT_TRUE(ce1.is_zero());
70 }
71
72 TEST(CostEstimateTest, Operators) {
73 Cost_estimate ce_io;
74
75 EXPECT_EQ(0, ce_io.total_cost());
76 EXPECT_TRUE(ce_io.is_zero());
77
78 const double initial_io_cost = 4.5;
79 ce_io.add_io(initial_io_cost);
80 EXPECT_DOUBLE_EQ(initial_io_cost, ce_io.total_cost());
81
82 Cost_estimate ce_cpu;
83 const double initial_cpu_cost = 3.3;
84 ce_cpu.add_cpu(initial_cpu_cost);
85 EXPECT_DOUBLE_EQ(initial_cpu_cost, ce_cpu.total_cost());
86 EXPECT_EQ(0, ce_cpu.get_io_cost());
87
88 // Copy CTOR
89 Cost_estimate ce_copy(ce_io);
90 const double added_io_cost = 1.5;
91 ce_io.add_io(added_io_cost); // should not add to ce_copy
92 EXPECT_DOUBLE_EQ(initial_io_cost + added_io_cost, ce_io.total_cost());
93 EXPECT_DOUBLE_EQ(initial_io_cost, ce_copy.total_cost());
94
95 // operator+=
96 ce_copy += ce_cpu;
97 EXPECT_DOUBLE_EQ(initial_io_cost + initial_cpu_cost, ce_copy.total_cost());
98
99 // operator+
100 Cost_estimate ce_copy2 = ce_io + ce_cpu;
101 const double copy2_totcost =
102 initial_io_cost + added_io_cost + initial_cpu_cost;
103 EXPECT_DOUBLE_EQ(copy2_totcost, ce_copy2.total_cost());
104
105 Cost_estimate ce_mem_import1;
106 const double import1_mem_cost = 3;
107 const double import1_import_cost = 5;
108 ce_mem_import1.add_mem(import1_mem_cost);
109 ce_mem_import1.add_import(import1_import_cost);
110
111 Cost_estimate ce_mem_import2;
112 const double import2_mem_cost = 11;
113 const double import2_import_cost = 13;
114 ce_mem_import2.add_mem(import2_mem_cost);
115 ce_mem_import2.add_import(import2_import_cost);
116
117 // operator+
118 Cost_estimate ce_mi_copy = ce_mem_import1 + ce_mem_import2;
119 EXPECT_DOUBLE_EQ(import1_import_cost + import2_import_cost,
120 ce_mi_copy.total_cost());
121 EXPECT_DOUBLE_EQ(import1_mem_cost + import2_mem_cost,
122 ce_mi_copy.get_mem_cost());
123 EXPECT_DOUBLE_EQ(import1_import_cost + import2_import_cost,
124 ce_mi_copy.get_import_cost());
125
126 // operator+=
127 ce_mi_copy += ce_mem_import1;
128 EXPECT_DOUBLE_EQ(2 * import1_import_cost + import2_import_cost,
129 ce_mi_copy.total_cost());
130 EXPECT_DOUBLE_EQ(2 * import1_mem_cost + import2_mem_cost,
131 ce_mi_copy.get_mem_cost());
132 EXPECT_DOUBLE_EQ(2 * import1_import_cost + import2_import_cost,
133 ce_mi_copy.get_import_cost());
134
135 // operator-
136 ce_mi_copy = ce_mem_import1 - ce_mem_import2;
137 EXPECT_DOUBLE_EQ(
138 ce_mi_copy.get_mem_cost(),
139 ce_mem_import1.get_mem_cost() - ce_mem_import2.get_mem_cost());
140 EXPECT_DOUBLE_EQ(
141 ce_mi_copy.get_import_cost(),
142 ce_mem_import1.get_import_cost() - ce_mem_import2.get_import_cost());
143
144 // copy assignment
145 Cost_estimate ce_copy3;
146 ce_copy3 = ce_copy2;
147 EXPECT_DOUBLE_EQ(copy2_totcost, ce_copy3.total_cost());
148 }
149
150 TEST(CostEstimateTest, MaxValue) {
151 Cost_estimate ce;
152 Cost_estimate ce2;
153
154 EXPECT_FALSE(ce.is_max_cost());
155
156 ce.set_max_cost();
157 EXPECT_TRUE(ce.is_max_cost());
158
159 EXPECT_TRUE(ce > ce2);
160 EXPECT_FALSE(ce < ce2);
161 EXPECT_FALSE(ce2 > ce);
162 EXPECT_TRUE(ce2 < ce);
163
164 ce2 = ce;
165 EXPECT_TRUE(ce2.is_max_cost());
166 EXPECT_FALSE(ce < ce2);
167 EXPECT_FALSE(ce2 > ce);
168 }
169
170 } // namespace cost_estimate_unittest