| 1 | /* Copyright (c) 2022, 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 "sql/transaction_info.h" |
| 26 | |
| 27 | #include <gmock/gmock.h> |
| 28 | #include <gtest/gtest.h> |
| 29 | |
| 30 | namespace ha_info_iterator_unittests { |
| 31 | |
| 32 | class Ha_info_iterator_test : public ::testing::Test { |
| 33 | protected: |
| 34 | Ha_info_iterator_test() = default; |
| 35 | virtual void SetUp() {} |
| 36 | virtual void TearDown() {} |
| 37 | }; |
| 38 | |
| 39 | TEST_F(Ha_info_iterator_test, Iterator_test) { |
| 40 | Ha_trx_info ha_trx_info[3]; |
| 41 | Transaction_ctx::THD_TRANS thd_trans; |
| 42 | |
| 43 | // `register_ha` inserts the objects at the list head so, while iterating |
| 44 | // they will be shown in reverse order to the insertion |
| 45 | thd_trans.m_ha_list = &ha_trx_info[0]; |
| 46 | ha_trx_info[1].register_ha(&thd_trans, nullptr); |
| 47 | ha_trx_info[2].register_ha(&thd_trans, nullptr); |
| 48 | |
| 49 | Ha_trx_info_list ha_list1{&ha_trx_info[2]}; |
| 50 | size_t idx{2}; |
| 51 | for (auto &ha : ha_list1) { |
| 52 | EXPECT_EQ(&ha, &ha_trx_info[idx]); |
| 53 | --idx; |
| 54 | } |
| 55 | |
| 56 | // it1 now points to ha_trx_info[2] |
| 57 | auto it1 = ha_list1.begin(); |
| 58 | // it2 now points to ha_trx_info[2] and it1 now points to ha_trx_info[1] |
| 59 | auto it2 = it1++; |
| 60 | // it0 now points to ha_trx_info[2] |
| 61 | auto it0 = ha_list1.begin(); |
| 62 | // it0 now points to ha_trx_info[1] |
| 63 | ++it0; |
| 64 | // it0 now points to ha_trx_info[0] |
| 65 | ++it0; |
| 66 | EXPECT_TRUE(it2 == ha_trx_info[2]); |
| 67 | EXPECT_TRUE(it1 == ha_trx_info[1]); |
| 68 | EXPECT_TRUE(it0 == ha_trx_info[0]); |
| 69 | EXPECT_TRUE(it0 == ha_trx_info); |
| 70 | |
| 71 | EXPECT_TRUE(it1 != ha_trx_info); |
| 72 | EXPECT_TRUE(it2 != ha_trx_info[1]); |
| 73 | |
| 74 | it1 = it2; |
| 75 | EXPECT_TRUE(it1 == it2); |
| 76 | EXPECT_TRUE(it1 != it0); |
| 77 | |
| 78 | Ha_trx_info_list ha_list2; |
| 79 | ha_list2 = ha_list1; |
| 80 | Ha_trx_info_list ha_list3 = ha_list2; |
| 81 | Ha_trx_info_list const ha_list4 = std::move(ha_list2); |
| 82 | // after std::move, ha_list2 is empty |
| 83 | EXPECT_TRUE(ha_list2.begin() == ha_list2.end()); |
| 84 | // both ha_list3 and ha_list4 where assigned from ha_list2 so, they must |
| 85 | // evaluate to equal |
| 86 | EXPECT_TRUE(ha_list3 == ha_list4); |
| 87 | // ha_list2 is empty so it must evaluate to not equal to ha_list3 |
| 88 | EXPECT_TRUE(ha_list2 != ha_list3); |
| 89 | |
| 90 | // testing `operator*` |
| 91 | EXPECT_TRUE(&(*ha_list3) == &ha_trx_info[2]); |
| 92 | EXPECT_TRUE(&(*ha_list4) == &ha_trx_info[2]); |
| 93 | EXPECT_TRUE(&(*ha_list3) != &ha_trx_info[1]); |
| 94 | EXPECT_TRUE(&(*ha_list4) != &ha_trx_info[1]); |
| 95 | |
| 96 | // testing `operator==(Ha_trx_info)` |
| 97 | EXPECT_TRUE(ha_list3 == &ha_trx_info[2]); |
| 98 | EXPECT_TRUE(ha_list3 != &ha_trx_info[1]); |
| 99 | |
| 100 | // testing `operator==(std::nullptr_t)` |
| 101 | EXPECT_TRUE(ha_list2 == nullptr); |
| 102 | EXPECT_TRUE(ha_list3 != nullptr); |
| 103 | |
| 104 | // testing `operator->` |
| 105 | EXPECT_TRUE(!ha_list3->is_started()); |
| 106 | EXPECT_TRUE(!ha_list4->is_started()); |
| 107 | } |
| 108 | |
| 109 | } // namespace ha_info_iterator_unittests |