Debian Codegraph @

unittest/gunit/binlogevents/managed_buffer-t.cc

raw symbols SCIP
1 /* Copyright (c) 2023, 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 #include "libbinlogevents/include/buffer/managed_buffer.h"
26
27 #include <gmock/gmock.h>
28 #include <gtest/gtest.h>
29 #include <functional> // std::function
30 #include <iterator> // std::distance
31
32 #include "libbinlogevents/include/math/math.h"
33 #include "libbinlogevents/include/string/concat.h"
34
35 using mysqlns::string::concat;
36
37 namespace mysqlns::buffer::managed_buffer::unittest {
38
39 using Grow_calculator_t = mysqlns::buffer::Grow_calculator;
40 using Grow_status_t = mysqlns::buffer::Grow_status;
41 using Size_t = mysqlns::buffer::Buffer_view<>::Size_t;
42 using Debug_function = const std::function<std::string(std::string)>;
43 using Difference_t = mysqlns::buffer::Rw_buffer<>::Difference_t;
44
45 // Return the current file and line as a string delimited and ended by
46 // colons.
47 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
48 #define FILELINE() concat(__FILE__, ":", __LINE__, ": ")
49
50 // Helper macros to make assertions output the debug info we need, and
51 // make the program stop with assertion.
52 static int n_assertions = 0;
53 static bool _shall_stop_after_assertion = false;
54 // NOLINTBEGIN(cppcoreguidelines-macro-usage)
55 #define ASSERTION_TAIL \
56 << debug_output(fileline) << (_shall_stop_after_assertion = true, ""), \
57 assert(!_shall_stop_after_assertion)
58 #define AEQ(v1, v2) \
59 do { \
60 ASSERT_EQ(v1, v2) ASSERTION_TAIL; \
61 ++n_assertions; \
62 } while (0)
63 #define ANE(v1, v2) \
64 do { \
65 ASSERT_NE(v1, v2) ASSERTION_TAIL; \
66 ++n_assertions; \
67 } while (0)
68 // NOLINTEND(cppcoreguidelines-macro-usage)
69
70 // Requirements:
71 //
72 // * Resize operations shall satisfy the following requirements:
73 //
74 // R1. When the existing or requested size is bigger than the max
75 // size, an error shall be returned. Otherwise, the resize shall
76 // succeed.
77 //
78 // R2. When the resize operation is successful, the read part shall
79 // remain unchanged and the write part shall increase as needed.
80 //
81 // R3. When the resize operation is successful, and new space is
82 // needed, one new buffer shall be allocated to accomodate the
83 // remaining requested size.
84 //
85 // R4. The buffer sequence shall remain self-consistent after the
86 // operation.
87 //
88 // * The operation of moving the position shall satisfy the following
89 // requirement:
90 //
91 // R5. It shall be possible to move the position from A to B, for any
92 // A, B between 0 and the size.
93 //
94 // * The reset operation shall satisfy the following requirements:
95 //
96 // R7. The reset operation shall set the read position to 0, leave the
97 // specified amount of buffers in the write part, and leave the
98 // specified extra capacity in the vector
99 //
100 // The exact execution of the resize operation and the operation of
101 // moving the position depends on the relative order of the following
102 // quantities:
103 //
104 // - size
105 // - position
106 // - extra container capacity
107 // - requested size
108 // - max size
109 // - requested new position
110 //
111 // The behavior may also depend on whether some of these are 0 or not,
112 // and whether some are equal or not.
113 //
114 // We test all relative orders among these variables, including
115 // boundary cases where some of them are equal, and including cases
116 // where the smallest variable is 0 as well as nonzero. Therefore
117 // we vary all these quantities between 0 and 4, inclusive.
118 //
119 // We expect the following outcome:
120 // - if size > max size, or requested size > max size,
121 // exceeds_max_size is returned
122 // - else if requested size <= size, size is unchanged
123 // - else, new size is requested size
124 template <class Char_t>
125 class Grow_tester {
126 public:
127 using Managed_buffer_t = buffer::Managed_buffer<Char_t>;
128 using Rw_buffer_t = buffer::Rw_buffer<Char_t>;
129
130 int m_scenario_count = 0;
131
132 // Execute "grow_test" in many configurations.
133 //
134 // The goal is to exercise as many potential corner cases as
135 // possible related to growing the buffer sequence or moving the
136 // position.
137 //
138 // In each scenario, we will setup the buffer in an "initial state",
139 // verify that it is as we expect, attempt to grow it, verify that
140 // it worked or didn't work according to expectation, attempt to
141 // move the position, verify that it worked, reset, verify that it
142 // worked.
143 //
144 // We vary the scenarios by using different initial states, by using
145 // different requestes for more capacity, by setting different
146 // maximum sizes in the Grow_calculator, and by moving the position
147 // to different places. These quantities can be zero or nonzero,
148 // and can coincide or be different.
149 void combinatorial_grow_test() {
150 std::vector<Size_t> sizes = {0, 1, 2, 10, 100, 1000, 10000};
151 for (Size_t size : sizes) {
152 for (Size_t position : sizes) {
153 if (position <= size) {
154 for (Size_t max_size : sizes) {
155 for (Size_t requested_size : sizes) {
156 for (Size_t requested_position : sizes) {
157 grow_test_helper<0>(size, position, max_size, requested_size,
158 requested_position);
159 grow_test_helper<1>(size, position, max_size, requested_size,
160 requested_position);
161 grow_test_helper<2>(size, position, max_size, requested_size,
162 requested_position);
163 grow_test_helper<10>(size, position, max_size, requested_size,
164 requested_position);
165 grow_test_helper<100>(size, position, max_size, requested_size,
166 requested_position);
167 grow_test_helper<1000>(size, position, max_size, requested_size,
168 requested_position);
169 grow_test_helper<10000>(size, position, max_size,
170 requested_size, requested_position);
171 }
172 }
173 }
174 }
175 }
176 }
177 {
178 // Corner cases: we exercise the code path where the destructor
179 // is used while the buffer is null and verify that there is no
180 // crash. Tests above already checked that the sizes are 0.
181 Managed_buffer_t mb0(0);
182 Managed_buffer_t mb1(1);
183 Preallocated_managed_buffer<Char_t, 0> pmb0;
184 Preallocated_managed_buffer<Char_t, 1> pmb1;
185 }
186
187 // Uncomment for debugging.
188 // std::cout << "scenarios: " << m_scenario_count
189 // << " assertions: " << n_assertions << "\n";
190 }
191
192 template <Size_t default_capacity>
193 void grow_test_helper(Size_t capacity, Size_t position, Size_t max_capacity,
194 Size_t requested_capacity, Size_t requested_position) {
195 // The 'if constexpr' is needed to silence some old/buggy
196 // compilers that would otherwise warn about pointless comparison
197 // when default_capacity is 0.
198 if constexpr (default_capacity != 0) {
199 if (default_capacity > capacity) return;
200 }
201 Size_t new_capacity = compute_new_size(capacity, default_capacity,
202 max_capacity, requested_capacity);
203 if (requested_position > new_capacity) return;
204
205 ++m_scenario_count;
206 Managed_buffer_t mb(default_capacity);
207 grow_test(mb, capacity, position, default_capacity, max_capacity,
208 requested_capacity, requested_position, 0);
209
210 ++m_scenario_count;
211 Preallocated_managed_buffer<Char_t, default_capacity> pmb;
212 grow_test(pmb, capacity, position, default_capacity, max_capacity,
213 requested_capacity, requested_position, default_capacity);
214 }
215
216 Size_t compute_new_size(Size_t capacity,
217 [[maybe_unused]] Size_t default_capacity,
218 Size_t max_capacity, Size_t requested_capacity) {
219 // In the first two cases, either the existing capacity or the
220 // requested capacity exceeds the maximum capacity configured in the
221 // Grow_calculator. Therefore it refuses to grow and leaves the buffer
222 // unchanged. In the third case, the request is for a smaller
223 // capacity than the existing capacity, so it succeeds and leaves the
224 // buffer unchanged.
225 if (capacity > max_capacity || requested_capacity > max_capacity ||
226 requested_capacity <= capacity)
227 return capacity;
228 return requested_capacity;
229 }
230
231 void grow_test(Managed_buffer_t &mb, Size_t capacity, Size_t position,
232 Size_t default_capacity, Size_t max_capacity,
233 Size_t requested_capacity, Size_t requested_position,
234 Size_t initial_capacity) {
235 // NOLINTBEGIN(cppcoreguidelines-macro-usage)
236 #define CHECK_SIZES(POSITION, CAPACITY) \
237 check_sizes(FILELINE(), debug_output, mb, POSITION, CAPACITY)
238 // NOLINTEND(cppcoreguidelines-macro-usage)
239
240 // This does the following:
241 //
242 // 1. Create empty buffer
243 // 2. Grow to the required total capacity
244 // 3. Set the position as required
245 // 4. Set Grow_calculator.max_size and grow to the requested capacity
246 // 5. Move position to the requested new position
247
248 Size_t new_capacity = compute_new_size(capacity, default_capacity,
249 max_capacity, requested_capacity);
250
251 // Prepare Grow_calculator with a high max size
252 {
253 Grow_calculator_t gc;
254 gc.set_block_size(1);
255 gc.set_grow_factor(1);
256 gc.set_grow_increment(1);
257 mb.set_grow_calculator(gc);
258 }
259
260 std::string fileline;
261 Debug_function debug_output = [&](const std::string &fileline_arg) {
262 // clang-format off
263 return concat(
264 fileline_arg,
265 "\nposition=", position,
266 ",\ncapacity=", capacity,
267 ",\nmax_capacity=", max_capacity,
268 ",\nrequested_capacity=", requested_capacity,
269 ",\nrequested_position=", requested_position,
270 ",\ndefault_capacity=", default_capacity,
271 ",\nnew_capacity=", new_capacity,
272 ",\ninitial_capacity=", initial_capacity);
273 // clang-format on
274 };
275 auto work = [&](bool use_increase_position_api) {
276 CHECK_SIZES(0, initial_capacity);
277 // Set the initial capacity.
278 {
279 auto gc = mb.get_grow_calculator();
280 gc.set_max_size(1000000);
281 mb.set_grow_calculator(gc);
282 auto status = mb.reserve_total_size(capacity);
283 AEQ(status, Grow_status_t::success);
284 CHECK_SIZES(0, capacity);
285 }
286 // Set the initial position.
287 mb.set_position(position);
288 CHECK_SIZES(position, capacity);
289 // Grow to the requested capacity.
290 {
291 auto gc = mb.get_grow_calculator();
292 gc.set_max_size(max_capacity);
293 mb.set_grow_calculator(gc);
294 auto status = mb.reserve_total_size(requested_capacity);
295 auto expected_status = Grow_status_t::success;
296 if (capacity > max_capacity || requested_capacity > max_capacity)
297 expected_status = Grow_status_t::exceeds_max_size;
298 AEQ(status, expected_status);
299 CHECK_SIZES(position, new_capacity);
300 }
301 // Move to the requested position. Use different methods to
302 // move the position, just to exercise all the API entries.
303 if (requested_position >= position && use_increase_position_api)
304 mb.increase_position(requested_position - position);
305 else
306 mb.move_position(Difference_t(requested_position) -
307 Difference_t(position));
308 CHECK_SIZES(requested_position, new_capacity);
309 // Reset.
310 mb.reset();
311 // If we have allocated a default buffer, the initial capacity is
312 // now the default capacity.
313 if (new_capacity > 0 && capacity <= default_capacity)
314 initial_capacity = default_capacity;
315 CHECK_SIZES(0, initial_capacity);
316 };
317
318 // Run twice, to verify that everything works the same way also
319 // after `reset`, and also to exercise different members that move
320 // the position.
321 work(false);
322 work(true);
323 }
324
325 // Check that the sizes of the parts, and the number of buffers per
326 // part, is as expected.
327 void check_sizes(const std::string &fileline, Debug_function &debug_prefix,
328 Managed_buffer_t &mb, Size_t position, Size_t capacity) {
329 auto w_size = capacity - position;
330
331 auto debug_output = [&](std::string fileline_arg) {
332 // clang-format off
333 return concat(debug_prefix(std::move(fileline_arg)),
334 ", position=", position,
335 ", capacity=", capacity,
336 ", w_size=", w_size);
337 // clang-format on
338 };
339
340 auto &r = mb.read_part();
341 auto &w = mb.write_part();
342
343 AEQ(r.size(), position);
344 AEQ(w.size(), w_size);
345 AEQ(mb.capacity(), capacity);
346
347 AEQ(std::distance(r.begin(), r.end()), position);
348 AEQ(std::distance(w.begin(), w.end()), w_size);
349 }
350 };
351
352 TEST(ManagedBufferTest, CombinatorialGrowTestChar) {
353 Grow_tester<char>().combinatorial_grow_test();
354 }
355
356 TEST(ManagedBufferTest, CombinatorialGrowTestUchar) {
357 Grow_tester<unsigned char>().combinatorial_grow_test();
358 }
359
360 } // namespace mysqlns::buffer::managed_buffer::unittest