Debian Codegraph @

unittest/gunit/binlogevents/rw_buffer_sequence-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/rw_buffer_sequence.h"
26
27 #include <gmock/gmock.h>
28 #include <gtest/gtest.h>
29
30 #include "libbinlogevents/include/string/concat.h"
31
32 using mysqlns::string::concat;
33
34 namespace mysqlns::buffer::rw_buffer_sequence::unittest {
35
36 template <class Rw_buffer_sequence_tp>
37 class Reposition_tester {
38 public:
39 using Rw_buffer_sequence_t = Rw_buffer_sequence_tp;
40 using Char_t = typename Rw_buffer_sequence_t::Char_t;
41 using Difference_t = typename Rw_buffer_sequence_t::Difference_t;
42 using Size_t = typename Rw_buffer_sequence_t::Size_t;
43 using Container_t = typename Rw_buffer_sequence_t::Container_t;
44
45 /// Verify that the buffer sequence is as expected
46 ///
47 /// @param debug_string Extra debug info to print in case of failure.
48 ///
49 /// @param bs Rw_buffer_sequence to check
50 ///
51 /// @param contents Expected contents of all buffers
52 ///
53 /// @param underlying The underlying container
54 ///
55 /// @param read_size The expected size of the read part
56 ///
57 /// @param write_size The expected size of the write part
58 void expect_rw_buffer_sequence(
59 const std::string &debug_string, Rw_buffer_sequence_t &bs,
60 const std::string &contents, Container_t &underlying,
61 std::vector<std::pair<Char_t *, Size_t>> &expected, Size_t read_size,
62 Size_t write_size) {
63 EXPECT_EQ(bs.capacity(), read_size + write_size) << debug_string;
64 EXPECT_EQ(bs.read_part().size(), read_size) << debug_string;
65 EXPECT_EQ(bs.read_part().begin(), underlying.begin()) << debug_string;
66 EXPECT_EQ(bs.write_part().size(), write_size) << debug_string;
67 EXPECT_EQ(bs.write_part().end(), underlying.end()) << debug_string;
68 Size_t i = 0;
69 auto it = bs.read_part().begin();
70 for (; it != bs.write_part().end(); ++i, ++it) {
71 auto [base, size] = expected[i];
72 EXPECT_EQ(it->size(), size) << debug_string;
73 EXPECT_EQ(it->begin(), base) << debug_string;
74 EXPECT_EQ(it->end(), base + size) << debug_string;
75 }
76
77 EXPECT_EQ(bs.read_part().str(), contents.substr(0, read_size))
78 << debug_string;
79 }
80
81 void test() {
82 std::string str1(30, 'a');
83 std::string str2(30, 'b');
84 std::string str3(30, 'c');
85 std::string contents = concat(str1, str2, str3);
86 Char_t *buf1 = reinterpret_cast<Char_t *>(str1.data());
87 Char_t *buf2 = reinterpret_cast<Char_t *>(str2.data());
88 Char_t *buf3 = reinterpret_cast<Char_t *>(str3.data());
89 Container_t underlying;
90 underlying.emplace_back();
91 underlying.emplace_back(buf1, 30);
92 underlying.emplace_back(buf2, 30);
93 underlying.emplace_back(buf3, 30);
94 Rw_buffer_sequence_t bs(underlying.begin(), underlying.end());
95 std::vector<std::vector<std::pair<Char_t *, Size_t>>> expected = {
96 {{nullptr, 0}, {buf1, 30}, {buf2, 30}, {buf3, 30}},
97 {{buf1, 10}, {buf1 + 10, 20}, {buf2, 30}, {buf3, 30}},
98 {{buf1, 20}, {buf1 + 20, 10}, {buf2, 30}, {buf3, 30}},
99 {{buf1, 30}, {nullptr, 0}, {buf2, 30}, {buf3, 30}},
100 {{buf1, 30}, {buf2, 10}, {buf2 + 10, 20}, {buf3, 30}},
101 {{buf1, 30}, {buf2, 20}, {buf2 + 20, 10}, {buf3, 30}},
102 {{buf1, 30}, {buf2, 30}, {nullptr, 0}, {buf3, 30}},
103 {{buf1, 30}, {buf2, 30}, {buf3, 10}, {buf3 + 10, 20}},
104 {{buf1, 30}, {buf2, 30}, {buf3, 20}, {buf3 + 20, 10}},
105 {{buf1, 30}, {buf2, 30}, {buf3, 30}, {nullptr, 0}},
106 };
107
108 for (Size_t from = 0; from <= 9; ++from) {
109 for (Size_t to = 0; to <= 9; ++to) {
110 // Can only use a macro since we need __FILE__ and __LINE__.
111 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
112 #define DEBUG_STRING() \
113 concat(__FILE__, ":", __LINE__, " from=", from, " to=", to, \
114 " bs=", bs.debug_string())
115
116 bs.set_position(from * 10);
117 expect_rw_buffer_sequence(DEBUG_STRING(), bs, contents, underlying,
118 expected[from], from * 10, 90 - from * 10);
119 bs.set_position(to * 10);
120 expect_rw_buffer_sequence(DEBUG_STRING(), bs, contents, underlying,
121 expected[to], to * 10, 90 - to * 10);
122
123 bs.set_position(from * 10);
124 expect_rw_buffer_sequence(DEBUG_STRING(), bs, contents, underlying,
125 expected[from], from * 10, 90 - from * 10);
126 bs.move_position((Difference_t(to) - Difference_t(from)) * 10);
127 expect_rw_buffer_sequence(DEBUG_STRING(), bs, contents, underlying,
128 expected[to], to * 10, 90 - to * 10);
129 #undef DEBUG_STRING
130 }
131 }
132 }
133 };
134
135 TEST(RwBufferSequenceTest, CombinatorialRepositionTestCharVector) {
136 Reposition_tester<Rw_buffer_sequence<char, std::vector>>().test();
137 }
138
139 TEST(RwBufferSequenceTest, CombinatorialRepositionTestUcharVector) {
140 Reposition_tester<Rw_buffer_sequence<unsigned char, std::vector>>().test();
141 }
142
143 TEST(RwBufferSequenceTest, CombinatorialRepositionTestCharList) {
144 Reposition_tester<Rw_buffer_sequence<char, std::list>>().test();
145 }
146
147 TEST(RwBufferSequenceTest, CombinatorialRepositionTestUcharList) {
148 Reposition_tester<Rw_buffer_sequence<unsigned char, std::list>>().test();
149 }
150
151 } // namespace mysqlns::buffer::rw_buffer_sequence::unittest