| 1 | /* Copyright (c) 2011, 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 | #include <sys/types.h> |
| 26 | |
| 27 | #include "sql/sql_plist.h" |
| 28 | #include "template_utils.h" |
| 29 | |
| 30 | namespace sql_plist_unittest { |
| 31 | |
| 32 | // A simple helper function to insert values into a List. |
| 33 | template <class T, int size, class L> |
| 34 | void insert_values(T (&array)[size], L *list) { |
| 35 | uint ix, elements = list->elements(); |
| 36 | for (ix = 0; ix < size; ++ix) list->push_back(&array[ix]); |
| 37 | EXPECT_EQ(ix + elements, list->elements()); |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | The fixture for testing the MySQL List and List_iterator classes. |
| 42 | A fresh instance of this class will be created for each of the |
| 43 | TEST_F functions below. |
| 44 | */ |
| 45 | class IPListTest : public ::testing::Test { |
| 46 | protected: |
| 47 | IPListTest() : m_int_list(), m_int_list_iter(m_int_list) {} |
| 48 | |
| 49 | public: |
| 50 | template <typename V> |
| 51 | struct I_P_ListTestValue { |
| 52 | V value; |
| 53 | I_P_ListTestValue(V val) : value(val) {} |
| 54 | bool operator==(const I_P_ListTestValue<V> &obj) const { |
| 55 | return value == obj.value; |
| 56 | } |
| 57 | struct I_P_ListTestValue<V> *next; |
| 58 | struct I_P_ListTestValue<V> **prev; |
| 59 | }; |
| 60 | |
| 61 | protected: |
| 62 | template <typename V> |
| 63 | struct I_P_ListCountedPushBack { |
| 64 | typedef I_P_ListTestValue<V> Value; |
| 65 | typedef I_P_List<Value, I_P_List_adapter<Value, &Value::next, &Value::prev>, |
| 66 | I_P_List_counter, I_P_List_fast_push_back<Value>> |
| 67 | Type; |
| 68 | }; |
| 69 | |
| 70 | I_P_ListCountedPushBack<int>::Type m_int_list; |
| 71 | I_P_ListCountedPushBack<int>::Type::Iterator m_int_list_iter; |
| 72 | |
| 73 | private: |
| 74 | IPListTest(IPListTest const &) = delete; |
| 75 | IPListTest &operator=(IPListTest const &) = delete; |
| 76 | }; |
| 77 | |
| 78 | // Allow construction of test messages via the << operator. |
| 79 | template <typename T> |
| 80 | std::ostream &operator<<(std::ostream &s, |
| 81 | const IPListTest::I_P_ListTestValue<T> &v) { |
| 82 | return s << v.value; |
| 83 | } |
| 84 | |
| 85 | // Tests that we can construct and destruct lists. |
| 86 | TEST_F(IPListTest, ConstructAndDestruct) { |
| 87 | EXPECT_TRUE(m_int_list.is_empty()); |
| 88 | I_P_ListCountedPushBack<int>::Type *p_int_list; |
| 89 | p_int_list = new I_P_ListCountedPushBack<int>::Type; |
| 90 | EXPECT_TRUE(p_int_list->is_empty()); |
| 91 | delete p_int_list; |
| 92 | } |
| 93 | |
| 94 | // Tests basic operations push and remove. |
| 95 | TEST_F(IPListTest, BasicOperations) { |
| 96 | I_P_ListTestValue<int> v1(1), v2(2), v3(3); |
| 97 | m_int_list.push_front(&v1); |
| 98 | m_int_list.insert_after(&v1, &v2); |
| 99 | m_int_list.push_back(&v3); |
| 100 | EXPECT_FALSE(m_int_list.is_empty()); |
| 101 | EXPECT_EQ(3U, m_int_list.elements()); |
| 102 | |
| 103 | EXPECT_EQ(&v1, m_int_list.front()); |
| 104 | m_int_list.remove(&v1); |
| 105 | EXPECT_EQ(&v2, m_int_list.front()); |
| 106 | m_int_list.remove(&v2); |
| 107 | EXPECT_EQ(&v3, m_int_list.front()); |
| 108 | m_int_list.remove(&v3); |
| 109 | EXPECT_TRUE(m_int_list.is_empty()) << "The list should be empty now!"; |
| 110 | } |
| 111 | |
| 112 | // Tests that we can iterate over values. |
| 113 | TEST_F(IPListTest, Iterate) { |
| 114 | I_P_ListTestValue<int> values[] = {3, 2, 1}; |
| 115 | insert_values(values, &m_int_list); |
| 116 | m_int_list_iter.init(m_int_list); |
| 117 | for (size_t ix = 0; ix < array_elements(values); ++ix) { |
| 118 | EXPECT_EQ(values[ix], *m_int_list_iter++); |
| 119 | } |
| 120 | m_int_list_iter.init(m_int_list); |
| 121 | I_P_ListTestValue<int> *value; |
| 122 | int value_number = 0; |
| 123 | while ((value = m_int_list_iter++)) { |
| 124 | EXPECT_EQ(values[value_number++], value->value); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | } // namespace sql_plist_unittest |