| 1 | /* Copyright (c) 2019, 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 <gtest/gtest.h> |
| 25 | |
| 26 | #include "sql/intrusive_list_iterator.h" |
| 27 | |
| 28 | namespace intrusive_list_iterator_unittest { |
| 29 | |
| 30 | struct IntrusivePointee; |
| 31 | |
| 32 | class IntrusiveListIteratorTest : public ::testing::Test { |
| 33 | void SetUp() override; |
| 34 | void TearDown() override; |
| 35 | |
| 36 | protected: |
| 37 | IntrusivePointee *first; |
| 38 | }; |
| 39 | |
| 40 | IntrusivePointee *GetNextInList(const IntrusivePointee *ip); |
| 41 | |
| 42 | struct IntrusivePointee { |
| 43 | int value; |
| 44 | IntrusivePointee *next; |
| 45 | IntrusivePointee *GetNext() const { return next; } |
| 46 | }; |
| 47 | |
| 48 | void IntrusiveListIteratorTest::SetUp() { |
| 49 | auto two = new IntrusivePointee{2, nullptr}; |
| 50 | auto one = new IntrusivePointee{1, two}; |
| 51 | first = one; |
| 52 | } |
| 53 | |
| 54 | void IntrusiveListIteratorTest::TearDown() { |
| 55 | IntrusiveListContainer<IntrusivePointee, &IntrusivePointee::next> container( |
| 56 | first); |
| 57 | auto it = container.begin(); |
| 58 | while (it != container.end()) delete *(it++); |
| 59 | } |
| 60 | |
| 61 | TEST_F(IntrusiveListIteratorTest, BarePointer) { |
| 62 | IntrusiveListIterator<IntrusivePointee, &IntrusivePointee::next> it(first); |
| 63 | EXPECT_EQ(1, (*it)->value); |
| 64 | } |
| 65 | |
| 66 | TEST_F(IntrusiveListIteratorTest, PreIncrement) { |
| 67 | IntrusiveListIterator<IntrusivePointee, &IntrusivePointee::next> it(first); |
| 68 | EXPECT_EQ(1, (*it)->value); |
| 69 | EXPECT_EQ(2, (*++it)->value); |
| 70 | } |
| 71 | |
| 72 | // Testing that we can instantiate it without a definition of 'next'. |
| 73 | using TestedIterator = NextFunctionIterator<IntrusivePointee, GetNextInList>; |
| 74 | |
| 75 | IntrusivePointee *GetNextInList(const IntrusivePointee *ip) { return ip->next; } |
| 76 | |
| 77 | TEST_F(IntrusiveListIteratorTest, IndirectionFunction) { |
| 78 | TestedIterator it(first); |
| 79 | EXPECT_EQ(1, (*it)->value); |
| 80 | } |
| 81 | |
| 82 | TEST_F(IntrusiveListIteratorTest, PostIncrement) { |
| 83 | TestedIterator it(first); |
| 84 | EXPECT_EQ(1, (*it++)->value); |
| 85 | EXPECT_EQ(2, (*it)->value); |
| 86 | } |
| 87 | |
| 88 | TEST_F(IntrusiveListIteratorTest, IteratorContainer) { |
| 89 | IteratorContainer<TestedIterator> container(first); |
| 90 | int answers[] = {1, 2}; |
| 91 | int i = 0; |
| 92 | for (auto element : container) EXPECT_EQ(answers[i++], element->value); |
| 93 | } |
| 94 | |
| 95 | TEST_F(IntrusiveListIteratorTest, NextFunctionContainer) { |
| 96 | NextFunctionContainer<IntrusivePointee, GetNextInList> container(first); |
| 97 | int answers[] = {1, 2}; |
| 98 | int i = 0; |
| 99 | for (auto element : container) EXPECT_EQ(answers[i++], element->value); |
| 100 | } |
| 101 | } // namespace intrusive_list_iterator_unittest |