| 1 | /* |
| 2 | * Copyright (c) 2021, 2026, Oracle and/or its affiliates. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2.0, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is designed to work with certain software (including |
| 9 | * but not limited to OpenSSL) that is licensed under separate terms, |
| 10 | * as designated in a particular file or component or in included license |
| 11 | * documentation. The authors of MySQL hereby grant you an additional |
| 12 | * permission to link the program and your derivative works with the |
| 13 | * separately licensed software that they have either included with |
| 14 | * the program or referenced in the documentation. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License, version 2.0, for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | |
| 28 | #include <algorithm> |
| 29 | #include <iterator> |
| 30 | |
| 31 | #include "plugin/x/client/xcyclic_buffer.h" |
| 32 | |
| 33 | namespace xcl { |
| 34 | |
| 35 | class Action { |
| 36 | public: |
| 37 | virtual ~Action() = default; |
| 38 | |
| 39 | virtual bool execute(Cyclic_buffer *buffer, |
| 40 | std::vector<uint8_t> *all_elements_container) = 0; |
| 41 | |
| 42 | std::vector<uint8_t> create_vector(const uint64_t number_of_elements) { |
| 43 | return std::vector<uint8_t>(number_of_elements); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | class Action_insert_linear : public Action { |
| 48 | public: |
| 49 | Action_insert_linear(const uint64_t elements_to_insert, |
| 50 | const uint64_t start_value, const int32_t step) |
| 51 | : m_elements_to_insert(elements_to_insert), |
| 52 | m_start_value(start_value), |
| 53 | m_step(step), |
| 54 | m_value(start_value) {} |
| 55 | |
| 56 | bool execute(Cyclic_buffer *buffer, |
| 57 | std::vector<uint8_t> *all_elements_container) override { |
| 58 | auto result = create_vector(m_elements_to_insert); |
| 59 | |
| 60 | if (buffer->space_left() < m_elements_to_insert) { |
| 61 | ADD_FAILURE() << "Cyclic buffer, doesn't have enough free space to put " |
| 62 | "data, expected: " |
| 63 | << m_elements_to_insert |
| 64 | << ", actual:" << buffer->space_left(); |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | for (auto &element : result) { |
| 69 | element = (m_value = m_value + m_step); |
| 70 | all_elements_container->push_back(element); |
| 71 | } |
| 72 | |
| 73 | buffer->put(result.data(), result.size()); |
| 74 | |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | uint64_t m_elements_to_insert; |
| 79 | uint64_t m_start_value; |
| 80 | uint32_t m_step; |
| 81 | uint64_t m_value; |
| 82 | }; |
| 83 | |
| 84 | class Action_verify : public Action { |
| 85 | public: |
| 86 | Action_verify(const uint64_t number_of_elements) |
| 87 | : m_number_of_elements(number_of_elements) {} |
| 88 | |
| 89 | bool execute(Cyclic_buffer *buffer, |
| 90 | std::vector<uint8_t> *all_elements_container) override { |
| 91 | std::vector<uint8_t> result; |
| 92 | std::vector<uint8_t> matcher; |
| 93 | |
| 94 | if (buffer->space_used() < m_number_of_elements) { |
| 95 | ADD_FAILURE() |
| 96 | << "Cyclic buffer, doesn't have enough data to retrieve, expected: " |
| 97 | << m_number_of_elements << ", actual:" << buffer->space_used(); |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | result.resize(m_number_of_elements); |
| 102 | |
| 103 | buffer->get(result.data(), result.size()); |
| 104 | |
| 105 | std::copy_n(all_elements_container->begin(), m_number_of_elements, |
| 106 | std::back_inserter(matcher)); |
| 107 | |
| 108 | all_elements_container->erase( |
| 109 | all_elements_container->begin(), |
| 110 | all_elements_container->begin() + m_number_of_elements); |
| 111 | |
| 112 | if (result.size() != matcher.size()) { |
| 113 | ADD_FAILURE() |
| 114 | << "Verify fails because array from cyclic buffer has size: " |
| 115 | << result.size() |
| 116 | << ", and it differs from verifying array:" << matcher.size(); |
| 117 | |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | for (uint64_t i = 0; i < result.size(); ++i, ++m_element) { |
| 122 | if (result[i] != matcher[i]) { |
| 123 | ADD_FAILURE() << "Element at global position: " << m_element |
| 124 | << ", local position: " << i |
| 125 | << ", differs from matcher array (" << (int)result[i] |
| 126 | << " vs " << (int)matcher[i] << ")."; |
| 127 | return false; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | uint64_t m_number_of_elements; |
| 135 | static uint64_t m_element; |
| 136 | }; |
| 137 | |
| 138 | uint64_t Action_verify::m_element = 0; |
| 139 | |
| 140 | class Action_create_cyclic_buffer : public Action { |
| 141 | public: |
| 142 | Action_create_cyclic_buffer(const uint64_t number_of_elements) |
| 143 | : m_number_of_elements(number_of_elements) {} |
| 144 | |
| 145 | bool execute(Cyclic_buffer *buffer, std::vector<uint8_t> *) override { |
| 146 | buffer->change_size(m_number_of_elements); |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | uint64_t m_number_of_elements; |
| 151 | }; |
| 152 | |
| 153 | using Vector_of_actions = std::vector<std::shared_ptr<Action>>; |
| 154 | using Vector_of_bytes = std::vector<uint8_t>; |
| 155 | |
| 156 | class Cyclic_buffer_test_suite |
| 157 | : public testing::TestWithParam<Vector_of_actions> { |
| 158 | public: |
| 159 | void SetUp() override { Action_verify::m_element = 0; } |
| 160 | |
| 161 | bool execute_action_base_test(Cyclic_buffer *buffer) { |
| 162 | Vector_of_bytes matcher; |
| 163 | const auto param = GetParam(); |
| 164 | int index = 0; |
| 165 | |
| 166 | for (auto action : param) { |
| 167 | if (!action->execute(buffer, &matcher)) { |
| 168 | ADD_FAILURE() << "Failed at action " << index; |
| 169 | return false; |
| 170 | } |
| 171 | ++index; |
| 172 | } |
| 173 | |
| 174 | return true; |
| 175 | } |
| 176 | }; |
| 177 | |
| 178 | class Creator { |
| 179 | public: |
| 180 | using Action_ptr = std::shared_ptr<Action>; |
| 181 | |
| 182 | static Action_ptr new_cyclic_buffer(const uint64_t numer_of_elements) { |
| 183 | return Action_ptr(new Action_create_cyclic_buffer(numer_of_elements)); |
| 184 | } |
| 185 | |
| 186 | static Action_ptr insert_linear(const uint64_t numer_of_elements, |
| 187 | const uint64_t start_element, |
| 188 | const uint32_t step) { |
| 189 | return Action_ptr( |
| 190 | new Action_insert_linear(numer_of_elements, start_element, step)); |
| 191 | } |
| 192 | |
| 193 | static Action_ptr retrive_and_verify(const uint64_t number_of_element) { |
| 194 | return Action_ptr(new Action_verify(number_of_element)); |
| 195 | } |
| 196 | }; |
| 197 | |
| 198 | TEST_P(Cyclic_buffer_test_suite, last_insert_id_not_initialized) { |
| 199 | Cyclic_buffer buffer(0); |
| 200 | |
| 201 | EXPECT_NO_FATAL_FAILURE(execute_action_base_test(&buffer)); |
| 202 | } |
| 203 | |
| 204 | INSTANTIATE_TEST_SUITE_P( |
| 205 | FillsAndRetrives, Cyclic_buffer_test_suite, |
| 206 | testing::Values( |
| 207 | // Single fill and single read, without rolling buffer. |
| 208 | Vector_of_actions({ |
| 209 | Creator::new_cyclic_buffer(1000), |
| 210 | Creator::insert_linear(1000, 0, 1), |
| 211 | Creator::retrive_and_verify(1000), |
| 212 | }), |
| 213 | // Two fills, second at roll boundry |
| 214 | Vector_of_actions({ |
| 215 | Creator::new_cyclic_buffer(10), |
| 216 | Creator::insert_linear(9, 0, 1), |
| 217 | Creator::retrive_and_verify(1), |
| 218 | Creator::insert_linear(1, 200, -1), |
| 219 | Creator::retrive_and_verify(9), |
| 220 | }), |
| 221 | // Multiple fills and single retrive, without rolling buffer. |
| 222 | Vector_of_actions({Creator::new_cyclic_buffer(1000), |
| 223 | Creator::insert_linear(200, 0, 1), |
| 224 | Creator::insert_linear(200, 200, -1), |
| 225 | Creator::insert_linear(100, 0, 2), |
| 226 | Creator::insert_linear(100, 200, -2), |
| 227 | Creator::insert_linear(200, 200, -1), |
| 228 | Creator::insert_linear(200, 0, 1), |
| 229 | Creator::retrive_and_verify(1000)}), |
| 230 | // Multiple fills and matching retrives, without rolling buffer. |
| 231 | Vector_of_actions({ |
| 232 | Creator::new_cyclic_buffer(1000), |
| 233 | Creator::insert_linear(200, 0, 1), |
| 234 | Creator::retrive_and_verify(200), |
| 235 | Creator::insert_linear(200, 200, -1), |
| 236 | Creator::retrive_and_verify(200), |
| 237 | Creator::insert_linear(100, 0, 2), |
| 238 | Creator::retrive_and_verify(100), |
| 239 | Creator::insert_linear(100, 200, -2), |
| 240 | Creator::retrive_and_verify(100), |
| 241 | Creator::insert_linear(200, 200, -1), |
| 242 | Creator::retrive_and_verify(200), |
| 243 | Creator::insert_linear(200, 0, 1), |
| 244 | Creator::retrive_and_verify(200), |
| 245 | }), |
| 246 | // Multiple fills and multiple retrives, without rolling buffer. |
| 247 | Vector_of_actions({ |
| 248 | Creator::new_cyclic_buffer(1000), |
| 249 | Creator::insert_linear(200, 0, 1), |
| 250 | Creator::retrive_and_verify(100), |
| 251 | Creator::insert_linear(200, 200, -1), |
| 252 | Creator::retrive_and_verify(100), |
| 253 | Creator::insert_linear(100, 0, 2), |
| 254 | Creator::retrive_and_verify(50), |
| 255 | Creator::insert_linear(100, 200, -2), |
| 256 | Creator::retrive_and_verify(50), |
| 257 | Creator::insert_linear(200, 200, -1), |
| 258 | Creator::retrive_and_verify(100), |
| 259 | Creator::insert_linear(200, 0, 1), |
| 260 | Creator::retrive_and_verify(100), |
| 261 | Creator::retrive_and_verify(500), |
| 262 | }), |
| 263 | // Multiple fills and multiple retrives, with rolling buffer. |
| 264 | Vector_of_actions({ |
| 265 | Creator::new_cyclic_buffer(1000), |
| 266 | |
| 267 | Creator::insert_linear(200, 0, 1), |
| 268 | Creator::retrive_and_verify(100), |
| 269 | Creator::insert_linear(200, 200, -1), |
| 270 | Creator::retrive_and_verify(100), |
| 271 | Creator::insert_linear(100, 0, 2), |
| 272 | Creator::retrive_and_verify(50), |
| 273 | Creator::insert_linear(100, 200, -2), |
| 274 | Creator::retrive_and_verify(50), |
| 275 | Creator::insert_linear(200, 200, -1), |
| 276 | Creator::retrive_and_verify(100), |
| 277 | Creator::insert_linear(200, 0, 1), |
| 278 | Creator::retrive_and_verify(100), |
| 279 | Creator::insert_linear(200, 0, 1), // First roll |
| 280 | Creator::retrive_and_verify(100), |
| 281 | Creator::insert_linear(200, 200, -1), |
| 282 | Creator::retrive_and_verify(100), |
| 283 | Creator::insert_linear(100, 0, 2), |
| 284 | Creator::retrive_and_verify(50), |
| 285 | Creator::insert_linear(100, 200, -2), |
| 286 | Creator::retrive_and_verify(50), |
| 287 | Creator::insert_linear(200, 200, -1), |
| 288 | Creator::retrive_and_verify(100), |
| 289 | Creator::retrive_and_verify(900), |
| 290 | |
| 291 | }), |
| 292 | // Make one roll, and verify the data |
| 293 | Vector_of_actions({ |
| 294 | Creator::new_cyclic_buffer(1000), |
| 295 | Creator::insert_linear(1000, 0, 1), |
| 296 | Creator::retrive_and_verify(400), |
| 297 | Creator::insert_linear(400, 200, -10), // roll buffer |
| 298 | Creator::retrive_and_verify(1000), |
| 299 | }), |
| 300 | // Make two rolls, and verify the data |
| 301 | Vector_of_actions({ |
| 302 | Creator::new_cyclic_buffer(1000), |
| 303 | Creator::insert_linear(1000, 0, 1), |
| 304 | Creator::retrive_and_verify(400), |
| 305 | Creator::insert_linear(400, 200, -10), // first roll |
| 306 | Creator::retrive_and_verify(600), |
| 307 | Creator::insert_linear(600, 0, 20), // second roll |
| 308 | Creator::retrive_and_verify(1000), |
| 309 | }))); |
| 310 | |
| 311 | } // namespace xcl |