| 1 | /* Copyright (c) 2016, 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 "my_config.h" |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | #include <memory> |
| 28 | |
| 29 | #include "sql/record_buffer.h" |
| 30 | |
| 31 | namespace record_buffer_unittest { |
| 32 | |
| 33 | /** |
| 34 | Struct that holds a (records, record_size) pair that tells the size |
| 35 | of the Record_buffer to use in a test case. |
| 36 | */ |
| 37 | struct Buffer_param { |
| 38 | const ha_rows m_records; |
| 39 | const size_t m_record_size; |
| 40 | }; |
| 41 | |
| 42 | /// Run the test case with the following test parameters. |
| 43 | static const Buffer_param PARAMS[] = {{0, 0}, {1, 1}, {2, 2}, |
| 44 | {0, 100}, {100, 0}, {10, 10}, |
| 45 | {10, 100}, {100, 10}, {1024, 1024}}; |
| 46 | |
| 47 | class RecordBufferTestP : public ::testing::TestWithParam<Buffer_param> {}; |
| 48 | |
| 49 | TEST_P(RecordBufferTestP, BasicTest) { |
| 50 | const auto param = GetParam(); |
| 51 | const auto bufsize = |
| 52 | Record_buffer::buffer_size(param.m_records, param.m_record_size); |
| 53 | std::unique_ptr<uchar[]> ptr(new uchar[bufsize]); |
| 54 | |
| 55 | Record_buffer buf(param.m_records, param.m_record_size, ptr.get()); |
| 56 | EXPECT_EQ(param.m_records, buf.max_records()); |
| 57 | EXPECT_EQ(param.m_record_size, buf.record_size()); |
| 58 | EXPECT_EQ(0U, buf.records()); |
| 59 | EXPECT_FALSE(buf.is_out_of_range()); |
| 60 | |
| 61 | for (size_t i = 0; i < param.m_records; ++i) { |
| 62 | /* |
| 63 | Add a record and verify that the number of records has grown, |
| 64 | whereas the maximum size and the record size stay the same. |
| 65 | */ |
| 66 | const auto rec = buf.add_record(); |
| 67 | EXPECT_EQ(i + 1, buf.records()); |
| 68 | EXPECT_NE(nullptr, rec); |
| 69 | EXPECT_EQ(rec, buf.record(i)); |
| 70 | EXPECT_EQ(param.m_records, buf.max_records()); |
| 71 | EXPECT_EQ(param.m_record_size, buf.record_size()); |
| 72 | |
| 73 | /* |
| 74 | Remove the last added record, and see that the record count |
| 75 | decreases. And add it back again. |
| 76 | */ |
| 77 | buf.remove_last(); |
| 78 | EXPECT_EQ(i, buf.records()); |
| 79 | EXPECT_EQ(rec, buf.add_record()); |
| 80 | EXPECT_EQ(i + 1, buf.records()); |
| 81 | } |
| 82 | |
| 83 | // Test setting the out-of-range flag. |
| 84 | EXPECT_FALSE(buf.is_out_of_range()); |
| 85 | buf.set_out_of_range(true); |
| 86 | EXPECT_TRUE(buf.is_out_of_range()); |
| 87 | |
| 88 | /* |
| 89 | reset() should clear the out-of-range flag and the record count, |
| 90 | but the maximum size and the record size should stay the same. |
| 91 | */ |
| 92 | buf.reset(); |
| 93 | EXPECT_FALSE(buf.is_out_of_range()); |
| 94 | EXPECT_EQ(0U, buf.records()); |
| 95 | EXPECT_EQ(param.m_records, buf.max_records()); |
| 96 | EXPECT_EQ(param.m_record_size, buf.record_size()); |
| 97 | } |
| 98 | |
| 99 | INSTANTIATE_TEST_SUITE_P(Test, RecordBufferTestP, ::testing::ValuesIn(PARAMS)); |
| 100 | |
| 101 | TEST(RecordBufferTest, Clear) { |
| 102 | constexpr ha_rows rows = 10; |
| 103 | constexpr size_t row_size = 10; |
| 104 | uchar ch[Record_buffer::buffer_size(rows, row_size)]; |
| 105 | Record_buffer buf(rows, row_size, ch); |
| 106 | buf.add_record(); |
| 107 | buf.add_record(); |
| 108 | buf.set_out_of_range(true); |
| 109 | EXPECT_TRUE(buf.is_out_of_range()); |
| 110 | EXPECT_EQ(2U, buf.records()); |
| 111 | /* |
| 112 | Record_buffer::clear() should remove all records, but it should |
| 113 | keep the out-of-range flag. |
| 114 | */ |
| 115 | buf.clear(); |
| 116 | EXPECT_TRUE(buf.is_out_of_range()); |
| 117 | EXPECT_EQ(0U, buf.records()); |
| 118 | } |
| 119 | |
| 120 | TEST(RecordBufferTest, Reset) { |
| 121 | constexpr ha_rows rows = 10; |
| 122 | constexpr size_t row_size = 10; |
| 123 | uchar ch[Record_buffer::buffer_size(rows, row_size)]; |
| 124 | Record_buffer buf(rows, row_size, ch); |
| 125 | buf.add_record(); |
| 126 | buf.add_record(); |
| 127 | buf.set_out_of_range(true); |
| 128 | EXPECT_TRUE(buf.is_out_of_range()); |
| 129 | EXPECT_EQ(2U, buf.records()); |
| 130 | /* |
| 131 | Record_buffer::reset() should remove all records and clear the |
| 132 | out-of-range flag. |
| 133 | */ |
| 134 | buf.reset(); |
| 135 | EXPECT_FALSE(buf.is_out_of_range()); |
| 136 | EXPECT_EQ(0U, buf.records()); |
| 137 | } |
| 138 | |
| 139 | } // namespace record_buffer_unittest |