Debian Codegraph @

unittest/gunit/xplugin/xcl/xpriority_list_t.cc

raw symbols SCIP
1 /*
2 * Copyright (c) 2017, 2026, Oracle and/or its affiliates.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2.0,
6 * as published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms,
10 * as designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License, version 2.0, for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include <gmock/gmock.h>
27 #include <gtest/gtest.h>
28 #include <initializer_list>
29 #include <memory>
30 #include <string>
31 #include <vector>
32
33 #include "plugin/x/client/xpriority_list.h"
34
35 namespace xcl {
36 namespace test {
37
38 using ::testing::ContainerEq;
39 using ::testing::Test;
40 using ::testing::Values;
41 using ::testing::WithParamInterface;
42
43 class String_with_prio {
44 public:
45 String_with_prio(const std::string &v, int prio) : m_v(v), m_prio(prio) {}
46
47 static bool compare(const String_with_prio &lhs,
48 const String_with_prio &rhs) {
49 return lhs.m_prio < rhs.m_prio;
50 }
51
52 bool operator==(const std::string &expected_value) const {
53 return expected_value == get_value();
54 }
55
56 std::string get_value() const { return m_v; }
57
58 std::string m_v;
59 int m_prio;
60 };
61
62 class Direction {
63 public:
64 Direction() : m_element("", 0) {}
65
66 Direction(const bool is_front, const std::string &value, const int prio)
67 : m_element(value, prio), m_is_front(is_front) {}
68
69 operator std::string() const { return m_element.get_value(); }
70
71 bool operator==(const std::string &expected_value) const {
72 return expected_value == m_element.get_value();
73 }
74
75 String_with_prio m_element;
76 bool m_is_front;
77 };
78
79 template <bool is_front>
80 class Front_back : public Direction {
81 public:
82 Front_back(const std::string &value, const int prio)
83 : Direction(is_front, value, prio) {}
84 };
85
86 using F = Front_back<true>;
87 using B = Front_back<false>;
88
89 class Xcl_xpriority_list_tests : public Test {
90 public:
91 using Test_list = Priority_list<String_with_prio>;
92
93 public:
94 void push(const Direction &d) {
95 if (d.m_is_front) {
96 m_sut->push_front(d.m_element);
97 } else {
98 m_sut->push_back(d.m_element);
99 }
100 }
101
102 void push_and_assert(const std::string &scope_name,
103 const std::vector<Direction> &elements) {
104 std::vector<std::string> expected_from_elements(elements.size());
105
106 std::copy(elements.begin(), elements.end(), expected_from_elements.begin());
107
108 push_and_assert(scope_name, expected_from_elements, elements);
109 }
110
111 void push_and_assert(const std::string &scope_name,
112 const std::vector<std::string> &expected_elements,
113 const std::vector<Direction> &elements) {
114 SCOPED_TRACE(scope_name.c_str());
115
116 m_sut->clear();
117
118 for (const auto &element : elements) {
119 push(element);
120 }
121
122 assert_plist(expected_elements);
123 }
124
125 void assert_plist(const std::vector<std::string> &expected_elements) {
126 auto plist_element = m_sut->begin();
127
128 for (const auto &expected_element : expected_elements) {
129 ASSERT_NE(m_sut->end(), plist_element);
130 ASSERT_EQ(expected_element, (*plist_element).get_value());
131
132 ++plist_element;
133 }
134
135 ASSERT_EQ(m_sut->end(), plist_element);
136 }
137
138 void assert_plist_size(const uint32_t expected_elements_count) {
139 const auto elements_count = std::distance(m_sut->begin(), m_sut->end());
140
141 ASSERT_EQ(expected_elements_count, elements_count);
142 }
143
144 void assert_plist_remove(const std::string &element) {
145 auto plist_iterator = std::find(m_sut->begin(), m_sut->end(), element);
146
147 ASSERT_NE(m_sut->end(), plist_iterator);
148
149 m_sut->erase(plist_iterator);
150 }
151
152 void assert_plist_remove_element_and_verify(
153 const std::string &scope_name, const std::string &element_to_remove,
154 const std::vector<std::string> &expected_elements_after_remove) {
155 SCOPED_TRACE(scope_name.c_str());
156
157 assert_plist_remove(element_to_remove);
158 assert_plist_size(
159 static_cast<uint32_t>(expected_elements_after_remove.size()));
160 assert_plist(expected_elements_after_remove);
161 }
162
163 // priority list
164 std::unique_ptr<Test_list> m_sut{new Test_list()};
165 };
166
167 TEST_F(Xcl_xpriority_list_tests, simple_operations) {
168 push_and_assert("Push_one_element", {"A"}, {F{"A", 1}});
169 assert_plist_size(1);
170
171 m_sut->erase(m_sut->begin());
172 assert_plist_size(0);
173
174 push_and_assert("Push_one_element", {"B"}, {F{"B", 2}});
175 assert_plist_size(1);
176
177 m_sut->erase(m_sut->begin());
178 assert_plist_size(0);
179 }
180
181 // All elements has different priorities.
182 TEST_F(Xcl_xpriority_list_tests, sequence_of_pushes_doesn_t_matter) {
183 const std::vector<std::string> expected_sequence{"a1", "b2", "c3", "d4",
184 "e5"};
185
186 push_and_assert("All_front", expected_sequence,
187 {F{"a1", 1}, F{"b2", 2}, F{"c3", 3}, F{"d4", 4}, F{"e5", 5}});
188
189 push_and_assert("All_back", expected_sequence,
190 {B{"a1", 1}, B{"b2", 2}, B{"c3", 3}, B{"d4", 4}, B{"e5", 5}});
191
192 push_and_assert("Mixed_front_back", expected_sequence,
193 {F{"e5", 5}, B{"a1", 1}, B{"d4", 4}, F{"b2", 2}, F{"c3", 3}});
194 }
195
196 TEST_F(Xcl_xpriority_list_tests, sequence_of_pushes_matters_on_same_priority) {
197 push_and_assert("All_front", {"e1", "d1", "c1", "b1", "a1"},
198 {F{"a1", 1}, F{"b1", 1}, F{"c1", 1}, F{"d1", 1}, F{"e1", 1}});
199
200 push_and_assert("All_back", {"a1", "b1", "c1", "d1", "e1"},
201 {B{"a1", 1}, B{"b1", 1}, B{"c1", 1}, B{"d1", 1}, B{"e1", 1}});
202
203 push_and_assert("Mixed_front_back", {"d1", "b1", "a1", "c1", "e1"},
204 {B{"a1", 1}, F{"b1", 1}, B{"c1", 1}, F{"d1", 1}, B{"e1", 1}});
205 }
206
207 TEST_F(Xcl_xpriority_list_tests,
208 sequence_of_pushes_matters_on_mixed_priorities) {
209 push_and_assert("All_front", {"b1", "a1", "b2", "a2", "a3"},
210 {F{"a1", 1}, F{"a2", 2}, F{"b2", 2}, F{"b1", 1}, F{"a3", 3}});
211
212 push_and_assert("All_back", {"a1", "b1", "a2", "b2", "a3"},
213 {B{"a1", 1}, B{"a2", 2}, B{"b2", 2}, B{"b1", 1}, B{"a3", 3}});
214
215 push_and_assert("Mixed_front_back",
216 {"c1", "a1", "b1", "b2", "a2", "c2", "b3", "a3"},
217 {B{"a2", 2}, F{"a1", 1}, F{"b2", 2}, B{"c2", 2}, B{"b1", 1},
218 B{"a3", 3}, F{"b3", 3}, F{"c1", 1}});
219 }
220
221 TEST_F(Xcl_xpriority_list_tests, remove_holds_elements_in_sequence) {
222 push_and_assert("Initialize_and_verify_the_plist",
223 {"c1", "a1", "b1", "b2", "a2", "c2", "b3", "a3"},
224 {B{"a2", 2}, F{"a1", 1}, F{"b2", 2}, B{"c2", 2}, B{"b1", 1},
225 B{"a3", 3}, F{"b3", 3}, F{"c1", 1}});
226
227 assert_plist_size(8);
228
229 assert_plist_remove_element_and_verify(
230 "remove_first", "b1", {"c1", "a1", "b2", "a2", "c2", "b3", "a3"});
231
232 assert_plist_remove_element_and_verify("remove_second", "b2",
233 {"c1", "a1", "a2", "c2", "b3", "a3"});
234
235 assert_plist_remove_element_and_verify("remove_third", "b3",
236 {"c1", "a1", "a2", "c2", "a3"});
237
238 assert_plist_remove_element_and_verify("remove_fourth", "c2",
239 {"c1", "a1", "a2", "a3"});
240
241 assert_plist_remove_element_and_verify("remove_fifth", "a3",
242 {
243 "c1",
244 "a1",
245 "a2",
246 });
247
248 assert_plist_remove_element_and_verify("remove_sixth", "a1", {"c1", "a2"});
249
250 assert_plist_remove_element_and_verify("remove_seventh", "a2",
251 {
252 "c1",
253 });
254
255 assert_plist_remove_element_and_verify("remove_eight", "c1", {});
256 }
257
258 } // namespace test
259 } // namespace xcl