Debian Codegraph @

unittest/gunit/inplace_vector-t.cc

raw symbols SCIP
1 /* Copyright (c) 2014, 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 #include <algorithm>
26
27 #include "sql/inplace_vector.h"
28
29 namespace inplace_vector_unittest {
30
31 class InplaceVectorTest : public ::testing::Test {
32 public:
33 InplaceVectorTest() : int_10(PSI_NOT_INSTRUMENTED) {}
34
35 protected:
36 Inplace_vector<int, 5> int_10;
37 int some_integer;
38 };
39
40 TEST_F(InplaceVectorTest, Empty) {
41 EXPECT_TRUE(int_10.empty());
42 EXPECT_EQ(0U, int_10.size());
43 }
44
45 #if !defined(NDEBUG)
46 // Google Test recommends DeathTest suffix for classes used in death tests.
47 typedef InplaceVectorTest InplaceVectorDeathTest;
48
49 TEST_F(InplaceVectorDeathTest, OutOfBoundsRead) {
50 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
51 EXPECT_DEATH_IF_SUPPORTED(some_integer = int_10[5],
52 ".*Assertion .*i < size.*");
53 }
54
55 TEST_F(InplaceVectorDeathTest, OutOfBoundsWrite) {
56 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
57 EXPECT_DEATH_IF_SUPPORTED(int_10[5] = some_integer,
58 ".*Assertion .*i < size.*");
59 }
60
61 TEST_F(InplaceVectorDeathTest, EmptyBackRead) {
62 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
63 EXPECT_DEATH_IF_SUPPORTED(some_integer = int_10.back(),
64 ".*Assertion .*size.*0.*");
65 }
66 TEST_F(InplaceVectorDeathTest, EmptyBackWrite) {
67 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
68 EXPECT_DEATH_IF_SUPPORTED(int_10.back() = 42, ".*Assertion .*size.*0.*");
69 }
70
71 #endif // NDEBUG
72
73 TEST_F(InplaceVectorTest, Insert5) {
74 for (int ix = 0; ix < 5; ++ix) int_10.push_back(ix);
75 for (int ix = 0; ix < 5; ++ix) EXPECT_EQ(ix, int_10[ix]);
76 for (int ix = 0; ix < 5; ++ix) int_10[ix] = ix;
77 EXPECT_EQ(5U, int_10.size());
78 EXPECT_EQ(5U, int_10.capacity());
79 }
80
81 TEST_F(InplaceVectorTest, Insert15) {
82 for (int ix = 0; ix < 15; ++ix) int_10.push_back(ix);
83 for (int ix = 0; ix < 15; ++ix) EXPECT_EQ(ix, int_10[ix]);
84 for (int ix = 0; ix < 15; ++ix) int_10[ix] = ix;
85 EXPECT_EQ(15U, int_10.size());
86 EXPECT_EQ(15U, int_10.capacity());
87 int_10.push_back(16);
88 EXPECT_EQ(20U, int_10.capacity());
89 }
90
91 TEST_F(InplaceVectorTest, Back) {
92 for (int ix = 0; ix <= 15; ++ix) int_10.push_back(ix);
93 EXPECT_EQ(15, int_10.back());
94 int_10.back() = 42;
95 EXPECT_EQ(42, int_10.back());
96 }
97
98 TEST_F(InplaceVectorTest, ResizeSame) {
99 for (int ix = 0; ix <= 15; ++ix) int_10.push_back(ix);
100 EXPECT_EQ(16U, int_10.size());
101 int_10.resize(16U);
102 EXPECT_EQ(16U, int_10.size());
103 }
104
105 TEST_F(InplaceVectorTest, ResizeGrow) {
106 int_10.push_back(1);
107 int_10.resize(20);
108 EXPECT_EQ(1, int_10[0]);
109 EXPECT_EQ(0, int_10[1]);
110 EXPECT_EQ(20U, int_10.size());
111 EXPECT_EQ(int_10.capacity(), 20U);
112 }
113
114 TEST_F(InplaceVectorTest, ResizeGrowVal) {
115 int_10.resize(20, 42);
116 EXPECT_EQ(42, int_10[0]);
117 EXPECT_EQ(42, int_10[19]);
118 EXPECT_EQ(20U, int_10.size());
119 EXPECT_EQ(int_10.capacity(), 20U);
120 }
121
122 TEST_F(InplaceVectorTest, ResizeShrink) {
123 for (int ix = 0; ix <= 15; ++ix) int_10.push_back(ix);
124 EXPECT_EQ(16U, int_10.size());
125 EXPECT_EQ(int_10.capacity(), 20U);
126 int_10.resize(10);
127 EXPECT_EQ(10U, int_10.size());
128 EXPECT_EQ(int_10.capacity(), 15U);
129
130 int_10.resize(3);
131 EXPECT_EQ(3U, int_10.size());
132 EXPECT_EQ(int_10.capacity(), 5U);
133 }
134
135 /*
136 A simple class for testing that object copying and destruction is done
137 properly when we have to expand the array a few times.
138 */
139 class IntWrap {
140 public:
141 IntWrap() { m_int = new int(0); }
142 explicit IntWrap(int arg) { m_int = new int(arg); }
143 IntWrap(const IntWrap &other) { m_int = new int(other.getval()); }
144 ~IntWrap() { delete m_int; }
145 int getval() const { return *m_int; }
146
147 private:
148 int *m_int;
149 };
150
151 /*
152 To verify that there are no leaks, do:
153 valgrind ./inplace_vector-t --gtest_filter="-*DeathTest*"
154 */
155 TEST_F(InplaceVectorTest, NoMemLeaksPushing) {
156 Inplace_vector<IntWrap, 5> array(PSI_NOT_INSTRUMENTED);
157 for (int ix = 0; ix < 42; ++ix) array.push_back(IntWrap(ix));
158 for (int ix = 0; ix < 42; ++ix) EXPECT_EQ(ix, array[ix].getval());
159 }
160
161 TEST_F(InplaceVectorTest, NoMemLeaksClearing) {
162 Inplace_vector<IntWrap, 5> array(PSI_NOT_INSTRUMENTED);
163 for (int ix = 0; ix < 42; ++ix) array.push_back(IntWrap(ix));
164 array.clear();
165 EXPECT_EQ(0U, array.size());
166 EXPECT_EQ(0U, array.capacity());
167
168 array.push_back(IntWrap(1));
169 EXPECT_EQ(1U, array.size());
170 EXPECT_EQ(5U, array.capacity());
171 }
172
173 TEST_F(InplaceVectorTest, NoMemLeaksResizing) {
174 Inplace_vector<IntWrap, 5> array(PSI_NOT_INSTRUMENTED);
175 for (int ix = 0; ix < 42; ++ix) array.push_back(IntWrap(ix));
176 array.resize(0);
177 EXPECT_EQ(0U, array.size());
178 EXPECT_EQ(5U, array.capacity());
179
180 array.push_back(IntWrap(1));
181 EXPECT_EQ(1U, array.size());
182 EXPECT_EQ(5U, array.capacity());
183 }
184
185 /*
186 A vector consists of a list of arrays of objects. Test that all
187 elements of all arrays are destroyed when the vector is
188 destroyed. If run in valgrind, these tests will report memory leaks
189 if some objects aren't destroyed.
190 */
191 class InplaceVectorTestP : public ::testing::TestWithParam<size_t> {
192 protected:
193 InplaceVectorTestP() : array(PSI_NOT_INSTRUMENTED) {}
194 void SetUp() override { n_elems = GetParam(); }
195 size_t n_elems;
196 Inplace_vector<IntWrap, 5> array;
197 };
198
199 size_t test_values[] = {5, 10, 15, 20};
200
201 INSTANTIATE_TEST_SUITE_P(NoMemLeaks, InplaceVectorTestP,
202 ::testing::ValuesIn(test_values));
203
204 TEST_P(InplaceVectorTestP, DestroyingFullArrays) {
205 for (size_t ix = 0; ix < n_elems; ++ix) array.push_back(IntWrap(ix));
206 EXPECT_EQ(n_elems, array.size());
207 EXPECT_EQ(n_elems, array.capacity());
208 }
209
210 TEST_P(InplaceVectorTestP, DestroyingAlmostFullArrays) {
211 for (size_t ix = 0; ix < n_elems - 1; ++ix) array.push_back(IntWrap(ix));
212 EXPECT_EQ(n_elems - 1, array.size());
213 EXPECT_EQ(n_elems, array.capacity());
214 }
215
216 TEST_P(InplaceVectorTestP, DestroyingAlmostEmptyArrays) {
217 for (size_t ix = 0; ix < n_elems - 5 + 1; ++ix) array.push_back(IntWrap(ix));
218 EXPECT_EQ(n_elems - 5 + 1, array.size());
219 EXPECT_EQ(n_elems, array.capacity());
220 }
221
222 /*
223 A simple class to verify that Inplace_vector also works for
224 classes which have their own operator new/delete.
225 */
226 class TestAlloc {
227 public:
228 explicit TestAlloc(int val) : m_int(val) {}
229
230 int getval() const { return m_int; }
231
232 private:
233 int m_int;
234
235 static void *operator new(size_t) { throw std::bad_alloc(); }
236 };
237
238 /*
239 There is no THD and no mem-root available for the execution of this test.
240 This shows that the memory management of Inplace_vector works OK for
241 classes with their own new/delete.
242 */
243 TEST_F(InplaceVectorTest, CustomNewDelete) {
244 Inplace_vector<TestAlloc, 5> array(PSI_NOT_INSTRUMENTED);
245 for (int ix = 0; ix < 42; ++ix) array.push_back(TestAlloc(ix));
246 for (int ix = 0; ix < 42; ++ix) EXPECT_EQ(ix, array[ix].getval());
247
248 EXPECT_EQ(array.size(), 42U);
249 EXPECT_EQ(array.capacity(), 45U);
250 }
251
252 } // namespace inplace_vector_unittest