Debian Codegraph @

unittest/gunit/bounds_checked_array-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 #include <algorithm>
26 #include <random>
27
28 #include "sql/sql_array.h"
29
30 namespace bounds_check_array_unittest {
31
32 typedef Bounds_checked_array<int> Int_array;
33
34 class BoundsCheckedArray : public ::testing::Test {
35 public:
36 BoundsCheckedArray() : some_integer(0) {}
37
38 void SetUp() override {
39 for (int ix = 0; ix < c_array_size; ++ix) c_array[ix] = ix;
40 }
41
42 static const int c_array_size = 5;
43 int c_array[c_array_size];
44
45 int some_integer;
46 Int_array int_array;
47 };
48
49 } // namespace bounds_check_array_unittest
50
51 /*
52 operator<<() is needed by the EXPECT macros.
53 It is a template argument, so static rather than in unnamed namespace.
54 */
55 static inline std::ostream &operator<<(
56 std::ostream &s, const bounds_check_array_unittest::Int_array &v) {
57 return s << "{" << v.array() << ", " << v.size() << "}";
58 }
59
60 namespace bounds_check_array_unittest {
61
62 TEST_F(BoundsCheckedArray, Empty) {
63 EXPECT_EQ(sizeof(int), int_array.element_size());
64 EXPECT_EQ(0U, int_array.size());
65 EXPECT_TRUE(int_array.is_null());
66 int *pi = nullptr;
67 EXPECT_EQ(pi, int_array.array());
68 }
69
70 #if !defined(NDEBUG)
71
72 // Google Test recommends DeathTest suffix for classes used in death tests.
73 typedef BoundsCheckedArray BoundsCheckedArrayDeathTest;
74
75 TEST_F(BoundsCheckedArrayDeathTest, BoundsCheckRead) {
76 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
77 int_array = Int_array(c_array, 2);
78 EXPECT_DEATH_IF_SUPPORTED(some_integer = int_array[5],
79 ".*Assertion .*n < m_size.*");
80 }
81
82 TEST_F(BoundsCheckedArrayDeathTest, BoundsCheckAssign) {
83 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
84 int_array = Int_array(c_array, 2);
85 EXPECT_DEATH_IF_SUPPORTED(int_array[5] = some_integer,
86 ".*Assertion .*n < m_size.*");
87 }
88
89 TEST_F(BoundsCheckedArrayDeathTest, BoundsCheckPopFront) {
90 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
91 int_array = Int_array(c_array, 1);
92 int_array.pop_front();
93 EXPECT_DEATH_IF_SUPPORTED(int_array.pop_front(),
94 ".*Assertion .*m_size > 0.*");
95 }
96
97 TEST_F(BoundsCheckedArrayDeathTest, BoundsCheckResize) {
98 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
99 int_array = Int_array(c_array, 1);
100 EXPECT_DEATH_IF_SUPPORTED(int_array.resize(2),
101 ".*Assertion .*new_size <= m_size.*");
102 }
103
104 TEST_F(BoundsCheckedArrayDeathTest, BoundsCheckResizeAssign) {
105 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
106 int_array = Int_array(c_array, 2);
107 int_array[1] = some_integer;
108 int_array.resize(1);
109 EXPECT_DEATH_IF_SUPPORTED(int_array[1] = some_integer,
110 ".*Assertion .*n < m_size.*");
111 }
112
113 #endif // !defined(NDEBUG)
114
115 TEST_F(BoundsCheckedArray, Indexing) {
116 int_array = Int_array(c_array, c_array_size);
117 EXPECT_EQ(0, int_array[0]);
118 int_array[0] = 42;
119 EXPECT_EQ(42, int_array[0]);
120 }
121
122 TEST_F(BoundsCheckedArray, Reset) {
123 int_array = Int_array(c_array, c_array_size);
124 EXPECT_EQ(c_array, int_array.array());
125 EXPECT_FALSE(int_array.is_null());
126 int_array.reset();
127 int *pi = nullptr;
128 EXPECT_EQ(pi, int_array.array());
129 EXPECT_TRUE(int_array.is_null());
130 }
131
132 TEST_F(BoundsCheckedArray, Resize) {
133 int_array = Int_array(c_array, c_array_size);
134 int_array.resize(c_array_size - 1);
135 EXPECT_EQ(c_array_size - 1, static_cast<int>(int_array.size()));
136
137 int count = 0;
138 while (int_array.size() > 0) {
139 EXPECT_EQ(count, int_array[0]);
140 count++;
141 int_array.pop_front();
142 }
143
144 EXPECT_EQ(count, c_array_size - 1);
145 }
146
147 TEST_F(BoundsCheckedArray, PopFront) {
148 int_array = Int_array(c_array, c_array_size);
149 for (int ix = 0; ix < c_array_size; ++ix) {
150 EXPECT_EQ(ix, int_array[0]);
151 int_array.pop_front();
152 }
153 }
154
155 TEST_F(BoundsCheckedArray, Equality) {
156 int_array = Int_array(c_array, c_array_size);
157 EXPECT_EQ(int_array, int_array);
158
159 Int_array int_array_copy(int_array);
160 EXPECT_EQ(int_array, int_array_copy)
161 << " original " << int_array << " copy " << int_array_copy;
162
163 int_array_copy.resize(c_array_size - 1);
164 EXPECT_NE(int_array, int_array_copy);
165
166 // We share the underlying array, so these should be equal.
167 Int_array int_array_two(c_array, c_array_size);
168 EXPECT_EQ(int_array, int_array_two);
169
170 int_array_two.pop_front();
171 EXPECT_NE(int_array, int_array_two);
172 }
173
174 TEST_F(BoundsCheckedArray, Sort) {
175 int_array = Int_array(c_array, c_array_size);
176 std::random_device rng;
177 std::mt19937 urng(rng());
178 std::shuffle(int_array.begin(), int_array.end(), urng);
179 std::sort(int_array.begin(), int_array.end());
180 Int_array::const_iterator it;
181 int ix;
182 for (ix = 0, it = int_array.begin(); it != int_array.end(); ++it, ++ix)
183 EXPECT_EQ(ix, *it);
184 }
185
186 } // namespace bounds_check_array_unittest