Debian Codegraph @

unittest/gunit/sql_list-t.cc

raw symbols SCIP
1 /* Copyright (c) 2009, 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 /*
25 This is a simple example of how to use the google unit test framework.
26
27 For an introduction to the constructs used below, see:
28 http://code.google.com/p/googletest/wiki/GoogleTestPrimer
29 */
30
31 #include <gtest/gtest.h>
32 #include <stddef.h>
33
34 #include "my_inttypes.h"
35 #include "my_thread.h"
36 #include "sql/sql_error.h"
37 #include "sql/sql_list.h"
38 #include "sql/thr_malloc.h"
39 #include "sql_string.h"
40 #include "unittest/gunit/gunit_test_main.h"
41
42 namespace sql_list_unittest {
43
44 // A simple helper function to insert values into a List.
45 template <class T, int size>
46 void insert_values(T (&array)[size], List<T> *list) {
47 for (int ix = 0; ix < size; ++ix) {
48 EXPECT_FALSE(list->push_back(&array[ix]));
49 }
50 }
51
52 /*
53 The fixture for testing the MySQL List and List_iterator classes.
54 A fresh instance of this class will be created for each of the
55 TEST_F functions below.
56 The functions SetUp(), TearDown(), SetUpTestCase(), TearDownTestCase() are
57 inherited from ::testing::Test (google naming style differs from MySQL).
58 */
59 class SqlListTest : public ::testing::Test {
60 protected:
61 SqlListTest()
62 : m_mem_root_p(&m_mem_root), m_int_list(), m_int_list_iter(m_int_list) {}
63
64 void SetUp() override { THR_MALLOC = &m_mem_root_p; }
65
66 static void SetUpTestCase() {
67 current_thd = nullptr;
68 THR_MALLOC = nullptr;
69 }
70
71 MEM_ROOT m_mem_root{PSI_NOT_INSTRUMENTED, 1024};
72 MEM_ROOT *m_mem_root_p;
73 List<int> m_int_list;
74 List_iterator<int> m_int_list_iter;
75
76 private:
77 SqlListTest(SqlListTest const &) = delete;
78 SqlListTest &operator=(SqlListTest const &) = delete;
79 };
80
81 // Tests that we can construct and destruct lists.
82 TEST_F(SqlListTest, ConstructAndDestruct) {
83 EXPECT_TRUE(m_int_list.is_empty());
84 List<int> *p_int_list = new (*THR_MALLOC) List<int>;
85 EXPECT_TRUE(p_int_list->is_empty());
86 destroy(p_int_list);
87 }
88
89 // Tests basic operations push and pop.
90 TEST_F(SqlListTest, BasicOperations) {
91 int i1 = 1;
92 int i2 = 2;
93 EXPECT_FALSE(m_int_list.push_front(&i1));
94 EXPECT_FALSE(m_int_list.push_back(&i2));
95 EXPECT_FALSE(m_int_list.is_empty());
96 EXPECT_EQ(2U, m_int_list.elements);
97
98 EXPECT_EQ(&i1, m_int_list.head());
99 EXPECT_EQ(&i1, m_int_list.pop());
100 EXPECT_EQ(&i2, m_int_list.head());
101 EXPECT_EQ(&i2, m_int_list.pop());
102 EXPECT_TRUE(m_int_list.is_empty()) << "The list should be empty now!";
103 }
104
105 // Tests list copying.
106 TEST_F(SqlListTest, DeepCopy) {
107 int values[] = {11, 22, 33, 42, 5};
108 insert_values(values, &m_int_list);
109 MEM_ROOT mem_root(PSI_NOT_INSTRUMENTED, 4096);
110 List<int> list_copy(m_int_list, &mem_root);
111 EXPECT_EQ(list_copy.elements, m_int_list.elements);
112 while (!list_copy.is_empty()) {
113 EXPECT_EQ(*m_int_list.pop(), *list_copy.pop());
114 }
115 EXPECT_TRUE(m_int_list.is_empty());
116 mem_root.Clear();
117 }
118
119 // Tests that we can iterate over values.
120 TEST_F(SqlListTest, Iterate) {
121 int values[] = {3, 2, 1};
122 insert_values(values, &m_int_list);
123 for (size_t ix = 0; ix < array_elements(values); ++ix) {
124 EXPECT_EQ(values[ix], *m_int_list_iter++);
125 }
126 m_int_list_iter.init(m_int_list);
127 int *value;
128 int value_number = 0;
129 while ((value = m_int_list_iter++)) {
130 EXPECT_EQ(values[value_number++], *value);
131 }
132 }
133
134 // A simple helper class for testing intrusive lists.
135 class Linked_node : public ilink<Linked_node> {
136 public:
137 Linked_node(int val) : m_value(val) {}
138 int get_value() const { return m_value; }
139
140 private:
141 int m_value;
142 };
143 const Linked_node *const null_node = nullptr;
144
145 // An example of a test without any fixture.
146 TEST(SqlIlistTest, ConstructAndDestruct) {
147 I_List<Linked_node> i_list;
148 I_List_iterator<Linked_node> i_list_iter(i_list);
149 EXPECT_TRUE(i_list.is_empty());
150 EXPECT_EQ(null_node, i_list_iter++);
151 }
152
153 // Tests iteration over intrusive lists.
154 TEST(SqlIlistTest, PushBackAndIterate) {
155 I_List<Linked_node> i_list;
156 I_List_iterator<Linked_node> i_list_iter(i_list);
157 int values[] = {11, 22, 33, 42, 5};
158 EXPECT_EQ(null_node, i_list.head());
159 for (size_t ix = 0; ix < array_elements(values); ++ix) {
160 i_list.push_back(new Linked_node(values[ix]));
161 }
162
163 Linked_node *node;
164 size_t value_number = 0;
165 while ((node = i_list_iter++)) {
166 EXPECT_EQ(values[value_number++], node->get_value());
167 }
168 for (value_number = 0; (node = i_list.get()); ++value_number) {
169 EXPECT_EQ(values[value_number], node->get_value());
170 delete node;
171 }
172 EXPECT_EQ(array_elements(values), value_number);
173 }
174
175 // Another iteration test over intrusive lists.
176 TEST(SqlIlistTest, PushFrontAndIterate) {
177 I_List<Linked_node> i_list;
178 I_List_iterator<Linked_node> i_list_iter(i_list);
179 int values[] = {11, 22, 33, 42, 5};
180 for (size_t ix = 0; ix < array_elements(values); ++ix) {
181 i_list.push_front(new Linked_node(values[ix]));
182 }
183
184 Linked_node *node;
185 int value_number = array_elements(values) - 1;
186 while ((node = i_list_iter++)) {
187 EXPECT_EQ(values[value_number--], node->get_value());
188 }
189 while ((node = i_list.get())) delete node;
190 }
191
192 static int cmp_test(int *a, int *b) {
193 return (*(int *)a < *(int *)b) ? -1 : (*(int *)a > *(int *)b) ? 1 : 0;
194 }
195
196 // Tests list sorting.
197 TEST_F(SqlListTest, Sort) {
198 int values[] = {1, 9, 2, 7, 3, 6, 4, 5, 8};
199 insert_values(values, &m_int_list);
200 m_int_list.sort(cmp_test);
201 for (int i = 1; i < 10; i++) {
202 EXPECT_EQ(*m_int_list.pop(), i);
203 }
204 EXPECT_TRUE(m_int_list.is_empty());
205 // Test sorting of empty string.
206 m_int_list.sort(cmp_test);
207 // Check that nothing has changed.
208 EXPECT_TRUE(m_int_list.is_empty());
209 }
210
211 // Tests prepend on empty list followed by push_back, Bug#26813454
212 TEST_F(SqlListTest, PrependBug) {
213 int values1[] = {1, 2};
214 insert_values(values1, &m_int_list);
215 EXPECT_EQ(2U, m_int_list.elements);
216
217 List<int> ilist;
218 EXPECT_TRUE(ilist.is_empty());
219 ilist.prepend(&m_int_list);
220
221 int values2[] = {3, 4};
222 insert_values(values2, &ilist);
223 EXPECT_EQ(4U, ilist.elements);
224
225 for (int i = 1; i <= 4; i++) EXPECT_EQ(*ilist.pop(), i);
226 }
227
228 // Tests swap_elts
229 TEST_F(SqlListTest, Swap) {
230 int values[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
231 insert_values(values, &m_int_list);
232 EXPECT_EQ(m_int_list.swap_elts(1, 1), false);
233 // Expect no change
234 for (int i = 0; i < 10; i++) {
235 EXPECT_EQ(*m_int_list.pop(), i);
236 }
237
238 insert_values(values, &m_int_list);
239 EXPECT_EQ(m_int_list.swap_elts(9, 10), true /* error */);
240 // Expect no change: 10 out of bounds
241 for (int i = 0; i < 10; i++) {
242 EXPECT_EQ(*m_int_list.pop(), i);
243 }
244
245 insert_values(values, &m_int_list);
246 EXPECT_EQ(m_int_list.swap_elts(10, 9), true /* error */);
247 // Expect no change: 10 out of bounds
248 for (int i = 0; i < 10; i++) {
249 EXPECT_EQ(*m_int_list.pop(), i);
250 }
251
252 insert_values(values, &m_int_list);
253 EXPECT_EQ(m_int_list.swap_elts(10, 11), true /* error */);
254 // Expect no change: 10, 11 out of bounds
255 for (int i = 0; i < 10; i++) {
256 EXPECT_EQ(*m_int_list.pop(), i);
257 }
258
259 insert_values(values, &m_int_list);
260 EXPECT_EQ(m_int_list.swap_elts(0, 1), false);
261
262 for (int i = 0; i < 10; i++) {
263 EXPECT_EQ(*m_int_list.pop(), (i == 0 ? 1 : (i == 1 ? 0 : i)));
264 }
265
266 insert_values(values, &m_int_list);
267 EXPECT_EQ(m_int_list.swap_elts(0, 9), false);
268
269 for (int i = 0; i < 10; i++) {
270 EXPECT_EQ(*m_int_list.pop(), (i == 0 ? 9 : (i == 9 ? 0 : i)));
271 }
272
273 insert_values(values, &m_int_list);
274 EXPECT_EQ(m_int_list.swap_elts(9, 0), false);
275
276 for (int i = 0; i < 10; i++) {
277 EXPECT_EQ(*m_int_list.pop(), (i == 0 ? 9 : (i == 9 ? 0 : i)));
278 }
279 }
280
281 } // namespace sql_list_unittest