1 #include <gtest/gtest.h>
2
3 #include "AllocationTestHarness.h"
4
5 extern "C" {
6 #include "osi/include/list.h"
7 #include "osi/include/osi.h"
8 }
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)
75 list_append(list, &x[i]);
76
77 EXPECT_EQ(list_front(list), &x[0]);
78
79 list_free(list);
80 }
81
TEST_F(ListTest,test_list_back)82 TEST_F(ListTest, test_list_back) {
83 int x[] = { 1, 2, 3, 4, 5 };
84 list_t *list = list_new(NULL);
85
86 for (size_t i = 0; i < ARRAY_SIZE(x); ++i)
87 list_append(list, &x[i]);
88
89 EXPECT_EQ(list_back(list), &x[ARRAY_SIZE(x) - 1]);
90
91 list_free(list);
92 }
93
TEST_F(ListTest,test_list_clear)94 TEST_F(ListTest, test_list_clear) {
95 int x[] = { 1, 2, 3, 4, 5 };
96 list_t *list = list_new(NULL);
97
98 for (size_t i = 0; i < ARRAY_SIZE(x); ++i)
99 list_append(list, &x[i]);
100
101 list_clear(list);
102 EXPECT_TRUE(list_is_empty(list));
103 EXPECT_EQ(list_length(list), 0U);
104
105 list_free(list);
106 }
107
TEST_F(ListTest,test_list_append_multiple)108 TEST_F(ListTest, test_list_append_multiple) {
109 int x[] = { 1, 2, 3, 4, 5 };
110 list_t *list = list_new(NULL);
111
112 for (size_t i = 0; i < ARRAY_SIZE(x); ++i)
113 list_append(list, &x[i]);
114
115 int i = 0;
116 for (const list_node_t *node = list_begin(list); node != list_end(list); node = list_next(node), ++i)
117 EXPECT_EQ(list_node(node), &x[i]);
118
119 list_free(list);
120 }
121
TEST_F(ListTest,test_list_prepend_multiple)122 TEST_F(ListTest, test_list_prepend_multiple) {
123 int x[] = { 1, 2, 3, 4, 5 };
124 list_t *list = list_new(NULL);
125
126 for (size_t i = 0; i < ARRAY_SIZE(x); ++i)
127 list_prepend(list, &x[i]);
128
129 int i = ARRAY_SIZE(x) - 1;
130 for (const list_node_t *node = list_begin(list); node != list_end(list); node = list_next(node), --i)
131 EXPECT_EQ(list_node(node), &x[i]);
132
133 list_free(list);
134 }
135
TEST_F(ListTest,test_list_begin_empty_list)136 TEST_F(ListTest, test_list_begin_empty_list) {
137 list_t *list = list_new(NULL);
138 EXPECT_EQ(list_begin(list), list_end(list));
139 list_free(list);
140 }
141
TEST_F(ListTest,test_list_next)142 TEST_F(ListTest, test_list_next) {
143 list_t *list = list_new(NULL);
144 list_append(list, &list);
145 EXPECT_NE(list_begin(list), list_end(list));
146 EXPECT_EQ(list_next(list_begin(list)), list_end(list));
147 list_free(list);
148 }
149
list_callback_sum(void * data,void * context)150 static bool list_callback_sum(void *data, void *context) {
151 assert(data);
152 assert(context);
153 int *sum = (int *)context;
154 int *value = (int *)data;
155 *sum += *value;
156 return true;
157 }
158
list_callback_find_int(void * data,void * context)159 static bool list_callback_find_int(void *data, void *context) {
160 assert(data);
161 assert(context);
162 return (*(int *)data != *(int *)context);
163 }
164
TEST_F(ListTest,test_list_foreach_full)165 TEST_F(ListTest, test_list_foreach_full) {
166 list_t *list = list_new(NULL);
167
168 // Fill in test data
169 int x[] = { 1, 2, 3, 4, 5 };
170 for (size_t i = 0; i < ARRAY_SIZE(x); ++i)
171 list_append(list, &x[i]);
172 EXPECT_EQ(list_length(list), (size_t)5);
173
174 // Test complete iteration
175 int sum = 0;
176 list_node_t *rc = list_foreach(list, list_callback_sum, &sum);
177 EXPECT_EQ(sum, 15);
178 EXPECT_TRUE(rc == NULL);
179
180 list_free(list);
181 }
182
TEST_F(ListTest,test_list_foreach_partial)183 TEST_F(ListTest, test_list_foreach_partial) {
184 list_t *list = list_new(NULL);
185
186 // Fill in test data
187 int x[] = { 1, 2, 3, 4, 5 };
188 for (size_t i = 0; i < ARRAY_SIZE(x); ++i)
189 list_append(list, &x[i]);
190 EXPECT_EQ(list_length(list), (size_t)5);
191
192 // Test partial iteration
193 int find = 4;
194 list_node_t *rc = list_foreach(list, list_callback_find_int, &find);
195 EXPECT_TRUE(rc != NULL);
196 int *rc_val = (int *)list_node(rc);
197 EXPECT_TRUE(*rc_val == 4);
198
199 find = 1;
200 rc = list_foreach(list, list_callback_find_int, &find);
201 EXPECT_TRUE(rc != NULL);
202 rc_val = (int *)list_node(rc);
203 EXPECT_TRUE(*rc_val == 1);
204
205 find = 5;
206 rc = list_foreach(list, list_callback_find_int, &find);
207 EXPECT_TRUE(rc != NULL);
208 rc_val = (int *)list_node(rc);
209 EXPECT_TRUE(*rc_val == 5);
210
211 find = 0;
212 rc = list_foreach(list, list_callback_find_int, &find);
213 EXPECT_TRUE(rc == NULL);
214
215 list_free(list);
216 }
217