24 #include "my_config.h"
25 #include <gtest/gtest.h>
29 #include "thr_malloc.h"
30 #include "sql_string.h"
31 #include "sql_error.h"
32 #include <my_pthread.h>
33 #include "test_utils.h"
35 namespace sql_list_unittest {
38 template <
class T,
int size>
41 for (
int ix= 0; ix <
size; ++ix)
43 EXPECT_FALSE(list->push_back(&array[ix]));
58 : m_mem_root_p(&m_mem_root), m_int_list(), m_int_list_iter(m_int_list)
64 init_sql_alloc(&m_mem_root, 1024, 0);
65 ASSERT_EQ(0, my_pthread_setspecific_ptr(THR_MALLOC, &m_mem_root_p));
67 ASSERT_EQ(root, m_mem_root_p);
70 virtual void TearDown()
72 free_root(&m_mem_root, MYF(0));
75 static void SetUpTestCase()
77 ASSERT_EQ(0, pthread_key_create(&THR_THD, NULL));
78 ASSERT_EQ(0, pthread_key_create(&THR_MALLOC, NULL));
81 static void TearDownTestCase()
83 pthread_key_delete(THR_THD);
84 pthread_key_delete(THR_MALLOC);
101 EXPECT_TRUE(m_int_list.is_empty());
103 EXPECT_TRUE(p_int_list->is_empty());
109 TEST_F(SqlListTest, BasicOperations)
113 EXPECT_FALSE(m_int_list.push_front(&i1));
114 EXPECT_FALSE(m_int_list.push_back(&i2));
115 EXPECT_FALSE(m_int_list.is_empty());
116 EXPECT_EQ(2
U, m_int_list.elements);
118 EXPECT_EQ(&i1, m_int_list.head());
119 EXPECT_EQ(&i1, m_int_list.pop());
120 EXPECT_EQ(&i2, m_int_list.head());
121 EXPECT_EQ(&i2, m_int_list.pop());
122 EXPECT_TRUE(m_int_list.is_empty()) <<
"The list should be empty now!";
127 TEST_F(SqlListTest, DeepCopy)
129 int values[] = {11, 22, 33, 42, 5};
130 insert_values(values, &m_int_list);
132 init_alloc_root(&mem_root, 4096, 4096);
133 List<int> list_copy(m_int_list, &mem_root);
134 EXPECT_EQ(list_copy.elements, m_int_list.elements);
135 while (!list_copy.is_empty())
137 EXPECT_EQ(*m_int_list.pop(), *list_copy.pop());
139 EXPECT_TRUE(m_int_list.is_empty());
140 free_root(&mem_root, MYF(0));
145 TEST_F(SqlListTest, Iterate)
147 int values[] = {3, 2, 1};
148 insert_values(values, &m_int_list);
149 for (
int ix= 0; ix < array_size(values); ++ix)
151 EXPECT_EQ(values[ix], *m_int_list_iter++);
153 m_int_list_iter.init(m_int_list);
156 while ((value= m_int_list_iter++))
158 EXPECT_EQ(values[value_number++], *value);
168 int get_value()
const {
return m_value; }
176 TEST(SqlIlistTest, ConstructAndDestruct)
180 EXPECT_TRUE(i_list.is_empty());
181 EXPECT_EQ(null_node, i_list_iter++);
186 TEST(SqlIlistTest, PushBackAndIterate)
190 int values[] = {11, 22, 33, 42, 5};
191 EXPECT_EQ(null_node, i_list.head());
192 for (
int ix= 0; ix < array_size(values); ++ix)
194 i_list.
push_back(
new Linked_node(values[ix]));
199 while ((node= i_list_iter++))
201 EXPECT_EQ(values[value_number++], node->get_value());
203 for (value_number= 0; (node= i_list.get()); ++value_number)
205 EXPECT_EQ(values[value_number], node->get_value());
208 EXPECT_EQ(array_size(values), value_number);
212 TEST(SqlIlistTest, PushFrontAndIterate)
216 int values[] = {11, 22, 33, 42, 5};
217 for (
int ix= 0; ix < array_size(values); ++ix)
219 i_list.
push_front(
new Linked_node(values[ix]));
223 int value_number= array_size(values) - 1;
224 while ((node= i_list_iter++))
226 EXPECT_EQ(values[value_number--], node->get_value());
228 while ((node= i_list.get()))
232 static int cmp_test(
void *a,
void *b,
void *c)
234 EXPECT_EQ(c, (
void *)0xFEE1BEEF);
235 return (*(
int*)a < *(
int*)b) ? -1 : (*(
int*)a > *(
int*)b) ? 1 : 0;
239 TEST_F(SqlListTest, Sort)
241 int values[] = {1, 9, 2, 7, 3, 6, 4, 5, 8};
242 insert_values(values, &m_int_list);
243 m_int_list.sort(cmp_test, (
void*)0xFEE1BEEF);
244 for (
int i= 1;
i < 10 ;
i++)
246 EXPECT_EQ(*m_int_list.pop(),
i);
248 EXPECT_TRUE(m_int_list.is_empty());
250 m_int_list.sort(cmp_test, (
void*)0xFEE1BEEF);
252 EXPECT_TRUE(m_int_list.is_empty());