1 #include <gtest/gtest.h>
2
3 #include <base/logging.h>
4
5 #include "AllocationTestHarness.h"
6
7 #include "osi/include/list.h"
8 #include "osi/include/osi.h"
9
10 class ListTest : public AllocationTestHarness {};
11
TEST_F(ListTest,test_new_free_simple)12 TEST_F(ListTest, test_new_free_simple) {
13 list_t* list = list_new(NULL);
14 ASSERT_TRUE(list != NULL);
15 list_free(list);
16 }
17
TEST_F(ListTest,test_free_null)18 TEST_F(ListTest, test_free_null) {
19 // In this test we just verify that list_free is callable with NULL.
20 list_free(NULL);
21 }
22
TEST_F(ListTest,test_empty_list_is_empty)23 TEST_F(ListTest, test_empty_list_is_empty) {
24 list_t* list = list_new(NULL);
25 EXPECT_TRUE(list_is_empty(list));
26 list_free(list);
27 }
28
TEST_F(ListTest,test_empty_list_has_no_length)29 TEST_F(ListTest, test_empty_list_has_no_length) {
30 list_t* list = list_new(NULL);
31 EXPECT_EQ(list_length(list), 0U);
32 list_free(list);
33 }
34
TEST_F(ListTest,test_simple_list_prepend)35 TEST_F(ListTest, test_simple_list_prepend) {
36 list_t* list = list_new(NULL);
37 EXPECT_TRUE(list_prepend(list, &list));
38 EXPECT_FALSE(list_is_empty(list));
39 EXPECT_EQ(list_length(list), 1U);
40 list_free(list);
41 }
42
TEST_F(ListTest,test_simple_list_append)43 TEST_F(ListTest, test_simple_list_append) {
44 list_t* list = list_new(NULL);
45 EXPECT_TRUE(list_append(list, &list));
46 EXPECT_FALSE(list_is_empty(list));
47 EXPECT_EQ(list_length(list), 1U);
48 list_free(list);
49 }
50
TEST_F(ListTest,test_list_remove_found)51 TEST_F(ListTest, test_list_remove_found) {
52 list_t* list = list_new(NULL);
53 list_append(list, &list);
54 EXPECT_TRUE(list_remove(list, &list));
55 EXPECT_TRUE(list_is_empty(list));
56 EXPECT_EQ(list_length(list), 0U);
57 list_free(list);
58 }
59
TEST_F(ListTest,test_list_remove_not_found)60 TEST_F(ListTest, test_list_remove_not_found) {
61 int x;
62 list_t* list = list_new(NULL);
63 list_append(list, &list);
64 EXPECT_FALSE(list_remove(list, &x));
65 EXPECT_FALSE(list_is_empty(list));
66 EXPECT_EQ(list_length(list), 1U);
67 list_free(list);
68 }
69
TEST_F(ListTest,test_list_front)70 TEST_F(ListTest, test_list_front) {
71 int x[] = {1, 2, 3, 4, 5};
72 list_t* list = list_new(NULL);
73
74 for (size_t i = 0; i < ARRAY_SIZE(x); ++i) list_append(list, &x[i]);
75
76 EXPECT_EQ(list_front(list), &x[0]);
77
78 list_free(list);
79 }
80
TEST_F(ListTest,test_list_back)81 TEST_F(ListTest, test_list_back) {
82 int x[] = {1, 2, 3, 4, 5};
83 list_t* list = list_new(NULL);
84
85 for (size_t i = 0; i < ARRAY_SIZE(x); ++i) list_append(list, &x[i]);
86
87 EXPECT_EQ(list_back(list), &x[ARRAY_SIZE(x) - 1]);
88
89 list_free(list);
90 }
91
TEST_F(ListTest,test_list_clear)92 TEST_F(ListTest, test_list_clear) {
93 int x[] = {1, 2, 3, 4, 5};
94 list_t* list = list_new(NULL);
95
96 for (size_t i = 0; i < ARRAY_SIZE(x); ++i) list_append(list, &x[i]);
97
98 list_clear(list);
99 EXPECT_TRUE(list_is_empty(list));
100 EXPECT_EQ(list_length(list), 0U);
101
102 list_free(list);
103 }
104
TEST_F(ListTest,test_list_append_multiple)105 TEST_F(ListTest, test_list_append_multiple) {
106 int x[] = {1, 2, 3, 4, 5};
107 list_t* list = list_new(NULL);
108
109 for (size_t i = 0; i < ARRAY_SIZE(x); ++i) list_append(list, &x[i]);
110
111 int i = 0;
112 for (const list_node_t *node = list_begin(list); node != list_end(list);
113 node = list_next(node), ++i)
114 EXPECT_EQ(list_node(node), &x[i]);
115
116 list_free(list);
117 }
118
TEST_F(ListTest,test_list_prepend_multiple)119 TEST_F(ListTest, test_list_prepend_multiple) {
120 int x[] = {1, 2, 3, 4, 5};
121 list_t* list = list_new(NULL);
122
123 for (size_t i = 0; i < ARRAY_SIZE(x); ++i) list_prepend(list, &x[i]);
124
125 int i = ARRAY_SIZE(x) - 1;
126 for (const list_node_t *node = list_begin(list); node != list_end(list);
127 node = list_next(node), --i)
128 EXPECT_EQ(list_node(node), &x[i]);
129
130 list_free(list);
131 }
132
TEST_F(ListTest,test_list_begin_empty_list)133 TEST_F(ListTest, test_list_begin_empty_list) {
134 list_t* list = list_new(NULL);
135 EXPECT_EQ(list_begin(list), list_end(list));
136 list_free(list);
137 }
138
TEST_F(ListTest,test_list_next)139 TEST_F(ListTest, test_list_next) {
140 list_t* list = list_new(NULL);
141 list_append(list, &list);
142 EXPECT_NE(list_begin(list), list_end(list));
143 EXPECT_EQ(list_next(list_begin(list)), list_end(list));
144 list_free(list);
145 }
146
list_callback_sum(void * data,void * context)147 static bool list_callback_sum(void* data, void* context) {
148 CHECK(data);
149 CHECK(context);
150 int* sum = (int*)context;
151 int* value = (int*)data;
152 *sum += *value;
153 return true;
154 }
155
list_callback_find_int(void * data,void * context)156 static bool list_callback_find_int(void* data, void* context) {
157 CHECK(data);
158 CHECK(context);
159 return (*(int*)data != *(int*)context);
160 }
161
TEST_F(ListTest,test_list_foreach_full)162 TEST_F(ListTest, test_list_foreach_full) {
163 list_t* list = list_new(NULL);
164
165 // Fill in test data
166 int x[] = {1, 2, 3, 4, 5};
167 for (size_t i = 0; i < ARRAY_SIZE(x); ++i) list_append(list, &x[i]);
168 EXPECT_EQ(list_length(list), (size_t)5);
169
170 // Test complete iteration
171 int sum = 0;
172 list_node_t* rc = list_foreach(list, list_callback_sum, &sum);
173 EXPECT_EQ(sum, 15);
174 EXPECT_TRUE(rc == NULL);
175
176 list_free(list);
177 }
178
TEST_F(ListTest,test_list_foreach_partial)179 TEST_F(ListTest, test_list_foreach_partial) {
180 list_t* list = list_new(NULL);
181
182 // Fill in test data
183 int x[] = {1, 2, 3, 4, 5};
184 for (size_t i = 0; i < ARRAY_SIZE(x); ++i) list_append(list, &x[i]);
185 EXPECT_EQ(list_length(list), (size_t)5);
186
187 // Test partial iteration
188 int find = 4;
189 list_node_t* rc = list_foreach(list, list_callback_find_int, &find);
190 EXPECT_TRUE(rc != NULL);
191 int* rc_val = (int*)list_node(rc);
192 EXPECT_TRUE(*rc_val == 4);
193
194 find = 1;
195 rc = list_foreach(list, list_callback_find_int, &find);
196 EXPECT_TRUE(rc != NULL);
197 rc_val = (int*)list_node(rc);
198 EXPECT_TRUE(*rc_val == 1);
199
200 find = 5;
201 rc = list_foreach(list, list_callback_find_int, &find);
202 EXPECT_TRUE(rc != NULL);
203 rc_val = (int*)list_node(rc);
204 EXPECT_TRUE(*rc_val == 5);
205
206 find = 0;
207 rc = list_foreach(list, list_callback_find_int, &find);
208 EXPECT_TRUE(rc == NULL);
209
210 list_free(list);
211 }
212