MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_plist-t.cc
1 /* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
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 as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 // First include (the generated) my_config.h, to get correct platform defines.
17 #include "my_config.h"
18 #include <gtest/gtest.h>
19 
20 #include "sql_plist.h"
21 #include "test_utils.h"
22 
23 namespace sql_plist_unittest {
24 
25 // A simple helper function to insert values into a List.
26 template <class T, int size, class L>
27 void insert_values(T (&array)[size], L *list)
28 {
29  uint ix, elements= list->elements();
30  for (ix= 0; ix < size; ++ix)
31  list->push_back(&array[ix]);
32  EXPECT_EQ(ix + elements, list->elements());
33 }
34 
35 /*
36  The fixture for testing the MySQL List and List_iterator classes.
37  A fresh instance of this class will be created for each of the
38  TEST_F functions below.
39 */
40 class IPListTest : public ::testing::Test
41 {
42 protected:
43  IPListTest()
44  : m_int_list(), m_int_list_iter(m_int_list)
45  {
46  }
47 
48 public:
49  template<typename V>
51  {
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  struct I_P_ListTestValue<V> *next;
57  struct I_P_ListTestValue<V> **prev;
58  };
59 
60 protected:
61  template<typename V>
63  {
65  typedef I_P_List<Value,
69  > Type;
70  };
71 
74 
75 private:
76  // Declares (but does not define) copy constructor and assignment operator.
77  GTEST_DISALLOW_COPY_AND_ASSIGN_(IPListTest);
78 };
79 
80 
81 // Allow construction of test messages via the << operator.
82 template<typename T>
83 std::ostream &operator<< (std::ostream &s,
85 {
86  return s << v.value;
87 }
88 
89 
90 // Tests that we can construct and destruct lists.
91 TEST_F(IPListTest, ConstructAndDestruct)
92 {
93  EXPECT_TRUE(m_int_list.is_empty());
94  I_P_ListCountedPushBack<int>::Type *p_int_list;
95  p_int_list= new I_P_ListCountedPushBack<int>::Type;
96  EXPECT_TRUE(p_int_list->is_empty());
97  delete p_int_list;
98 }
99 
100 
101 // Tests basic operations push and remove.
102 TEST_F(IPListTest, BasicOperations)
103 {
104  I_P_ListTestValue<int> v1(1), v2(2), v3(3);
105  m_int_list.push_front(&v1);
106  m_int_list.insert_after(&v1, &v2);
107  m_int_list.push_back(&v3);
108  EXPECT_FALSE(m_int_list.is_empty());
109  EXPECT_EQ(3U, m_int_list.elements());
110 
111  EXPECT_EQ(&v1, m_int_list.front());
112  m_int_list.remove(&v1);
113  EXPECT_EQ(&v2, m_int_list.front());
114  m_int_list.remove(&v2);
115  EXPECT_EQ(&v3, m_int_list.front());
116  m_int_list.remove(&v3);
117  EXPECT_TRUE(m_int_list.is_empty()) << "The list should be empty now!";
118 }
119 
120 
121 // Tests that we can iterate over values.
122 TEST_F(IPListTest, Iterate)
123 {
124  I_P_ListTestValue<int> values[]= {3, 2, 1};
125  insert_values(values, &m_int_list);
126  m_int_list_iter.init(m_int_list);
127  for (int ix= 0; ix < array_size(values); ++ix)
128  {
129  EXPECT_EQ(values[ix], *m_int_list_iter++);
130  }
131  m_int_list_iter.init(m_int_list);
132  I_P_ListTestValue<int> *value;
133  int value_number= 0;
134  while ((value= m_int_list_iter++))
135  {
136  EXPECT_EQ(values[value_number++], value->value);
137  }
138 }
139 
140 } // namespace