File: cost_estimate-t.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (170 lines) | stat: -rw-r--r-- 5,611 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Copyright (c) 2011, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include <gtest/gtest.h>

#include "sql/handler.h"

namespace cost_estimate_unittest {

TEST(CostEstimateTest, Basics) {
  Cost_estimate ce1;

  EXPECT_EQ(0, ce1.total_cost());
  EXPECT_TRUE(ce1.is_zero());

  const double initial_io_cost = 4.5;

  ce1.add_io(initial_io_cost);
  EXPECT_FALSE(ce1.is_zero());
  EXPECT_DOUBLE_EQ(initial_io_cost, ce1.total_cost());

  const double initial_cpu_cost = 3.3;
  ce1.add_cpu(initial_cpu_cost);

  EXPECT_DOUBLE_EQ(initial_cpu_cost, ce1.get_cpu_cost());
  EXPECT_DOUBLE_EQ(initial_io_cost, ce1.get_io_cost());
  EXPECT_DOUBLE_EQ(initial_io_cost + initial_cpu_cost, ce1.total_cost());

  EXPECT_EQ(0, ce1.get_mem_cost());
  EXPECT_EQ(0, ce1.get_import_cost());

  const double initial_mem_cost = 7;
  const double initial_import_cost = 11;
  ce1.add_mem(initial_mem_cost);
  ce1.add_import(initial_import_cost);

  const double total_initial_cost =
      initial_io_cost + initial_cpu_cost + initial_import_cost;
  EXPECT_DOUBLE_EQ(total_initial_cost, ce1.total_cost());

  const double added_io_cost = 1.5;
  ce1.add_io(added_io_cost);
  EXPECT_DOUBLE_EQ(initial_io_cost + added_io_cost, ce1.get_io_cost());
  EXPECT_DOUBLE_EQ(total_initial_cost + added_io_cost, ce1.total_cost());

  EXPECT_FALSE(ce1.is_zero());

  ce1.reset();
  EXPECT_TRUE(ce1.is_zero());
}

TEST(CostEstimateTest, Operators) {
  Cost_estimate ce_io;

  EXPECT_EQ(0, ce_io.total_cost());
  EXPECT_TRUE(ce_io.is_zero());

  const double initial_io_cost = 4.5;
  ce_io.add_io(initial_io_cost);
  EXPECT_DOUBLE_EQ(initial_io_cost, ce_io.total_cost());

  Cost_estimate ce_cpu;
  const double initial_cpu_cost = 3.3;
  ce_cpu.add_cpu(initial_cpu_cost);
  EXPECT_DOUBLE_EQ(initial_cpu_cost, ce_cpu.total_cost());
  EXPECT_EQ(0, ce_cpu.get_io_cost());

  // Copy CTOR
  Cost_estimate ce_copy(ce_io);
  const double added_io_cost = 1.5;
  ce_io.add_io(added_io_cost);  // should not add to ce_copy
  EXPECT_DOUBLE_EQ(initial_io_cost + added_io_cost, ce_io.total_cost());
  EXPECT_DOUBLE_EQ(initial_io_cost, ce_copy.total_cost());

  // operator+=
  ce_copy += ce_cpu;
  EXPECT_DOUBLE_EQ(initial_io_cost + initial_cpu_cost, ce_copy.total_cost());

  // operator+
  Cost_estimate ce_copy2 = ce_io + ce_cpu;
  const double copy2_totcost =
      initial_io_cost + added_io_cost + initial_cpu_cost;
  EXPECT_DOUBLE_EQ(copy2_totcost, ce_copy2.total_cost());

  Cost_estimate ce_mem_import1;
  const double import1_mem_cost = 3;
  const double import1_import_cost = 5;
  ce_mem_import1.add_mem(import1_mem_cost);
  ce_mem_import1.add_import(import1_import_cost);

  Cost_estimate ce_mem_import2;
  const double import2_mem_cost = 11;
  const double import2_import_cost = 13;
  ce_mem_import2.add_mem(import2_mem_cost);
  ce_mem_import2.add_import(import2_import_cost);

  // operator+
  Cost_estimate ce_mi_copy = ce_mem_import1 + ce_mem_import2;
  EXPECT_DOUBLE_EQ(import1_import_cost + import2_import_cost,
                   ce_mi_copy.total_cost());
  EXPECT_DOUBLE_EQ(import1_mem_cost + import2_mem_cost,
                   ce_mi_copy.get_mem_cost());
  EXPECT_DOUBLE_EQ(import1_import_cost + import2_import_cost,
                   ce_mi_copy.get_import_cost());

  // operator+=
  ce_mi_copy += ce_mem_import1;
  EXPECT_DOUBLE_EQ(2 * import1_import_cost + import2_import_cost,
                   ce_mi_copy.total_cost());
  EXPECT_DOUBLE_EQ(2 * import1_mem_cost + import2_mem_cost,
                   ce_mi_copy.get_mem_cost());
  EXPECT_DOUBLE_EQ(2 * import1_import_cost + import2_import_cost,
                   ce_mi_copy.get_import_cost());

  // operator-
  ce_mi_copy = ce_mem_import1 - ce_mem_import2;
  EXPECT_DOUBLE_EQ(
      ce_mi_copy.get_mem_cost(),
      ce_mem_import1.get_mem_cost() - ce_mem_import2.get_mem_cost());
  EXPECT_DOUBLE_EQ(
      ce_mi_copy.get_import_cost(),
      ce_mem_import1.get_import_cost() - ce_mem_import2.get_import_cost());

  // copy assignment
  Cost_estimate ce_copy3;
  ce_copy3 = ce_copy2;
  EXPECT_DOUBLE_EQ(copy2_totcost, ce_copy3.total_cost());
}

TEST(CostEstimateTest, MaxValue) {
  Cost_estimate ce;
  Cost_estimate ce2;

  EXPECT_FALSE(ce.is_max_cost());

  ce.set_max_cost();
  EXPECT_TRUE(ce.is_max_cost());

  EXPECT_TRUE(ce > ce2);
  EXPECT_FALSE(ce < ce2);
  EXPECT_FALSE(ce2 > ce);
  EXPECT_TRUE(ce2 < ce);

  ce2 = ce;
  EXPECT_TRUE(ce2.is_max_cost());
  EXPECT_FALSE(ce < ce2);
  EXPECT_FALSE(ce2 > ce);
}

}  // namespace cost_estimate_unittest