test/test_list.c

changeset 449
68ad5750ba6b
parent 446
668757098b73
child 453
bb144d08cd44
equal deleted inserted replaced
448:37c5d2fdb36b 449:68ad5750ba6b
78 c.prev = &b; 78 c.prev = &b;
79 c.next = &d; 79 c.next = &d;
80 d.prev = &c; 80 d.prev = &c;
81 d.next = NULL; 81 d.next = NULL;
82 82
83 CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&a, 0, loc_next, 0)); 83 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 0), &a)
84 CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&a, 0, loc_next, 1)); 84 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 1), &b)
85 CU_ASSERT_PTR_EQUAL(&c, cx_linked_list_at(&a, 0, loc_next, 2)); 85 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 2), &c)
86 CU_ASSERT_PTR_EQUAL(&d, cx_linked_list_at(&a, 0, loc_next, 3)); 86 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 3), &d)
87 CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4)); 87 CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4))
88 88
89 CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&b, 1, loc_prev, 0)); 89 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_prev, 0), &a)
90 CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&b, 1, loc_next, 1)); 90 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 1), &b)
91 CU_ASSERT_PTR_EQUAL(&c, cx_linked_list_at(&b, 1, loc_next, 2)); 91 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 2), &c)
92 CU_ASSERT_PTR_EQUAL(&d, cx_linked_list_at(&b, 1, loc_next, 3)); 92 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 3), &d)
93 CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4)); 93 CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4))
94 94
95 CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&d, 3, loc_prev, 0)); 95 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 0), &a)
96 CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&d, 3, loc_prev, 1)); 96 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 1), &b)
97 } 97 }
98 98
99 void test_linked_list_add(void) { 99 void test_linked_list_add(void) {
100 struct node { 100 struct node {
101 void *prev; 101 void *prev;
102 void *next; 102 void *next;
103 int value;
104 }; 103 };
105 104
106 struct node nodes[4]; 105 struct node nodes[4];
107 106
108 // test with begin, end / prev, next 107 // test with begin, end / prev, next
109 memset(nodes, 0, 4*sizeof(struct node)); 108 memset(nodes, 0, 4 * sizeof(struct node));
110 void *begin = NULL; 109 void *begin = NULL;
111 void *end = NULL; 110 void *end = NULL;
112 111
113 ptrdiff_t loc_prev = offsetof(struct node, prev); 112 ptrdiff_t loc_prev = offsetof(struct node, prev);
114 ptrdiff_t loc_next = offsetof(struct node, next); 113 ptrdiff_t loc_next = offsetof(struct node, next);
115 114
116 int ret; 115 int ret;
117 ret = cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]); 116 ret = cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]);
118 CU_ASSERT_EQUAL(ret, 0); 117 CU_ASSERT_EQUAL(ret, 0)
119 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); 118 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
120 CU_ASSERT_PTR_EQUAL(end, &nodes[0]); 119 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
121 CU_ASSERT_PTR_EQUAL(nodes[0].prev, NULL); 120 CU_ASSERT_PTR_EQUAL(nodes[0].prev, NULL)
122 CU_ASSERT_PTR_EQUAL(nodes[0].next, NULL); 121 CU_ASSERT_PTR_EQUAL(nodes[0].next, NULL)
123 122
124 ret = cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]); 123 ret = cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]);
125 CU_ASSERT_EQUAL(ret, 0); 124 CU_ASSERT_EQUAL(ret, 0)
126 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); 125 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
127 CU_ASSERT_PTR_EQUAL(end, &nodes[1]); 126 CU_ASSERT_PTR_EQUAL(end, &nodes[1])
128 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]); 127 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
129 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]); 128 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
130 129
131 // test with begin only / prev, next 130 // test with begin only / prev, next
132 memset(nodes, 0, 4*sizeof(struct node)); 131 memset(nodes, 0, 4 * sizeof(struct node));
133 begin = NULL; 132 begin = NULL;
134 end = NULL; 133 end = NULL;
135 134
136 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]); 135 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]);
137 CU_ASSERT_EQUAL(ret, 0); 136 CU_ASSERT_EQUAL(ret, 0)
138 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); 137 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
139 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]); 138 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]);
140 CU_ASSERT_EQUAL(ret, 0); 139 CU_ASSERT_EQUAL(ret, 0)
141 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); 140 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
142 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]); 141 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
143 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]); 142 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
144 143
145 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]); 144 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]);
146 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2]); 145 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2])
147 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1]); 146 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1])
148 147
149 // test with begin, end / next 148 // test with begin, end / next
150 memset(nodes, 0, 4*sizeof(struct node)); 149 memset(nodes, 0, 4 * sizeof(struct node));
151 begin = NULL; 150 begin = NULL;
152 end = NULL; 151 end = NULL;
153 152
154 ret = cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]); 153 ret = cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]);
155 CU_ASSERT_EQUAL(ret, 0); 154 CU_ASSERT_EQUAL(ret, 0)
156 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); 155 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
157 CU_ASSERT_PTR_EQUAL(end, &nodes[0]); 156 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
158 ret = cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]); 157 ret = cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]);
159 CU_ASSERT_PTR_EQUAL(end, &nodes[1]); 158 CU_ASSERT_EQUAL(ret, 0)
160 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]); 159 CU_ASSERT_PTR_EQUAL(end, &nodes[1])
161 CU_ASSERT_PTR_EQUAL(nodes[1].prev, NULL); 160 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
161 CU_ASSERT_PTR_NULL(nodes[1].prev)
162 } 162 }
163 163
164 int main() { 164 int main() {
165 CU_pSuite suite = NULL; 165 CU_pSuite suite = NULL;
166 166
167 if (CUE_SUCCESS != CU_initialize_registry()) { 167 if (CUE_SUCCESS != CU_initialize_registry()) {
168 return CU_get_error(); 168 return CU_get_error();
169 } 169 }
170 170
171 suite = CU_add_suite("linked list suite", NULL, NULL); 171 suite = CU_add_suite("linked list suite", NULL, NULL);
172 172
173 CU_add_test(suite, "linked list: create and destroy", test_linked_list_create); 173 CU_add_test(suite, "linked list: create and destroy", test_linked_list_create);
174 CU_add_test(suite, "linked list: get node at index", test_linked_list_at); 174 CU_add_test(suite, "linked list: get node at index", test_linked_list_at);
175 CU_add_test(suite, "linked list: add", test_linked_list_add); 175 CU_add_test(suite, "linked list: add", test_linked_list_add);
176 176
177 CU_basic_set_mode(UCX_CU_BRM); 177 CU_basic_set_mode(UCX_CU_BRM);

mercurial