test/test_list.c

changeset 442
310019ddfe4e
parent 438
cd3069757010
child 443
d6d8712e15bc
equal deleted inserted replaced
440:003aa0a78e1e 442:310019ddfe4e
98 98
99 CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&d, 3, loc_prev, 0)); 99 CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&d, 3, loc_prev, 0));
100 CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&d, 3, loc_prev, 1)); 100 CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&d, 3, loc_prev, 1));
101 } 101 }
102 102
103 void test_cx_linked_list_add(void) {
104 struct node {
105 void *prev;
106 void *next;
107 int value;
108 };
109
110 struct node nodes[4];
111
112 // test with begin, end / prev, next
113 memset(nodes, 0, 4*sizeof(struct node));
114 void *begin = NULL;
115 void *end = NULL;
116
117 ptrdiff_t loc_prev = offsetof(struct node, prev);
118 ptrdiff_t loc_next = offsetof(struct node, next);
119
120 int ret;
121 ret = cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]);
122 CU_ASSERT_EQUAL(ret, 0);
123 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]);
124 CU_ASSERT_PTR_EQUAL(end, &nodes[0]);
125 CU_ASSERT_PTR_EQUAL(nodes[0].prev, NULL);
126 CU_ASSERT_PTR_EQUAL(nodes[0].next, NULL);
127
128 ret = cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]);
129 CU_ASSERT_EQUAL(ret, 0);
130 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]);
131 CU_ASSERT_PTR_EQUAL(end, &nodes[1]);
132 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]);
133 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]);
134
135 // test with begin only / prev, next
136 memset(nodes, 0, 4*sizeof(struct node));
137 begin = NULL;
138 end = NULL;
139
140 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]);
141 CU_ASSERT_EQUAL(ret, 0);
142 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]);
143 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]);
144 CU_ASSERT_EQUAL(ret, 0);
145 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]);
146 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]);
147 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]);
148
149 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]);
150 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2]);
151 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1]);
152
153 // test with begin, end / next
154 memset(nodes, 0, 4*sizeof(struct node));
155 begin = NULL;
156 end = NULL;
157
158 ret = cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]);
159 CU_ASSERT_EQUAL(ret, 0);
160 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]);
161 CU_ASSERT_PTR_EQUAL(end, &nodes[0]);
162 ret = cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]);
163 CU_ASSERT_PTR_EQUAL(end, &nodes[1]);
164 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]);
165 CU_ASSERT_PTR_EQUAL(nodes[1].prev, NULL);
166 }
167
103 int main() { 168 int main() {
104 CU_pSuite suite = NULL; 169 CU_pSuite suite = NULL;
105 170
106 if (CUE_SUCCESS != CU_initialize_registry()) { 171 if (CUE_SUCCESS != CU_initialize_registry()) {
107 return CU_get_error(); 172 return CU_get_error();
108 } 173 }
109 174
110 suite = CU_add_suite("linked list suite", NULL, NULL); 175 suite = CU_add_suite("linked list suite", NULL, NULL);
113 return CU_get_error(); 178 return CU_get_error();
114 } 179 }
115 180
116 if ( 181 if (
117 !CU_add_test(suite, "linked list: create and destroy", test_linked_list_create) || 182 !CU_add_test(suite, "linked list: create and destroy", test_linked_list_create) ||
118 !CU_add_test(suite, "linked list: get node at index", test_linked_list_at) 183 !CU_add_test(suite, "linked list: get node at index", test_linked_list_at) ||
184 !CU_add_test(suite, "linked list: add", test_cx_linked_list_add)
119 ) { 185 ) {
120 CU_cleanup_registry(); 186 CU_cleanup_registry();
121 return CU_get_error(); 187 return CU_get_error();
122 } 188 }
123 189

mercurial