2 * tests of dlist implementation |
2 * tests of dlist implementation |
3 */ |
3 */ |
4 |
4 |
5 #include "dlist_tests.h" |
5 #include "dlist_tests.h" |
6 |
6 |
7 UCX_TEST_BEGIN(test_ucx_dlist_append) { |
7 UCX_TEST_IMPLEMENT(test_ucx_dlist_append) { |
8 UcxDlist *list = ucx_dlist_append(NULL, "Hello"); |
8 UcxDlist *list = ucx_dlist_append(NULL, "Hello"); |
|
9 UCX_TEST_BEGIN |
9 |
10 |
10 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") |
11 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") |
11 |
12 |
12 list = ucx_dlist_append(list, " World!"); |
13 list = ucx_dlist_append(list, " World!"); |
13 |
14 |
14 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") |
15 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") |
15 UCX_TEST_ASSERT(list->next->next == NULL, "failed") |
16 UCX_TEST_ASSERT(list->next->next == NULL, "failed") |
|
17 UCX_TEST_END |
16 |
18 |
17 ucx_dlist_free(list); |
19 ucx_dlist_free(list); |
18 |
|
19 UCX_TEST_END |
|
20 } |
20 } |
21 |
21 |
22 UCX_TEST_BEGIN(test_ucx_dlist_prepend) { |
22 UCX_TEST_IMPLEMENT(test_ucx_dlist_prepend) { |
23 UcxDlist *list = ucx_dlist_prepend(NULL, " World!"); |
23 UcxDlist *list = ucx_dlist_prepend(NULL, " World!"); |
|
24 UCX_TEST_BEGIN |
|
25 |
24 list = ucx_dlist_prepend(list, "Hello"); |
26 list = ucx_dlist_prepend(list, "Hello"); |
25 |
27 |
26 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") |
28 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") |
27 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") |
29 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") |
28 UCX_TEST_ASSERT(list->next->next == NULL, "failed") |
30 UCX_TEST_ASSERT(list->next->next == NULL, "failed") |
29 |
31 |
|
32 UCX_TEST_END |
30 ucx_dlist_free(list); |
33 ucx_dlist_free(list); |
31 |
|
32 UCX_TEST_END |
|
33 } |
34 } |
34 |
35 |
35 UCX_TEST_BEGIN(test_ucx_dlist_equals) { |
36 UCX_TEST_IMPLEMENT(test_ucx_dlist_equals) { |
36 UcxDlist *list = ucx_dlist_append(NULL, "Hello"); |
37 UcxDlist *list = ucx_dlist_append(NULL, "Hello"); |
37 list = ucx_dlist_append(list, " World!"); |
38 list = ucx_dlist_append(list, " World!"); |
38 UcxDlist *list2 = ucx_dlist_prepend(NULL, " World!"); |
39 UcxDlist *list2 = ucx_dlist_prepend(NULL, " World!"); |
39 list2 = ucx_dlist_prepend(list2, "Hello"); |
40 list2 = ucx_dlist_prepend(list2, "Hello"); |
40 UcxDlist *list3 = ucx_dlist_prepend(NULL, " Welt!"); |
41 UcxDlist *list3 = ucx_dlist_prepend(NULL, " Welt!"); |
41 list3 = ucx_dlist_prepend(list3, "Hallo"); |
42 list3 = ucx_dlist_prepend(list3, "Hallo"); |
|
43 UCX_TEST_BEGIN |
42 |
44 |
43 UCX_TEST_ASSERT(ucx_dlist_equals(list, list2, cmp_string, NULL), "failed") |
45 UCX_TEST_ASSERT(ucx_dlist_equals(list, list2, cmp_string, NULL), "failed") |
44 UCX_TEST_ASSERT(!ucx_dlist_equals(list, list3, cmp_string, NULL), "failed") |
46 UCX_TEST_ASSERT(!ucx_dlist_equals(list, list3, cmp_string, NULL), "failed") |
45 |
47 |
|
48 UCX_TEST_END |
46 ucx_dlist_free(list3); |
49 ucx_dlist_free(list3); |
47 ucx_dlist_free(list2); |
50 ucx_dlist_free(list2); |
48 ucx_dlist_free(list); |
51 ucx_dlist_free(list); |
49 |
|
50 UCX_TEST_END |
|
51 } |
52 } |
52 |
53 |
53 UCX_TEST_BEGIN(test_ucx_dlist_concat) { |
54 UCX_TEST_IMPLEMENT(test_ucx_dlist_concat) { |
54 UcxDlist *list = ucx_dlist_append(NULL, "Hello"); |
55 UcxDlist *list = ucx_dlist_append(NULL, "Hello"); |
55 UcxDlist *list2 = ucx_dlist_prepend(NULL, " World!"); |
56 UcxDlist *list2 = ucx_dlist_prepend(NULL, " World!"); |
|
57 UCX_TEST_BEGIN |
56 |
58 |
57 list = ucx_dlist_concat(list, list2); |
59 list = ucx_dlist_concat(list, list2); |
58 |
60 |
59 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") |
61 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") |
60 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") |
62 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") |
61 UCX_TEST_ASSERT(list->next->next == NULL, "failed") |
63 UCX_TEST_ASSERT(list->next->next == NULL, "failed") |
62 |
64 |
|
65 UCX_TEST_END |
63 ucx_dlist_free(list); |
66 ucx_dlist_free(list); |
64 |
|
65 UCX_TEST_END |
|
66 } |
67 } |
67 |
68 |
68 UCX_TEST_BEGIN(test_ucx_dlist_size) { |
69 UCX_TEST_IMPLEMENT(test_ucx_dlist_size) { |
69 UcxDlist *list = ucx_dlist_append(NULL, "This "); |
70 UcxDlist *list = ucx_dlist_append(NULL, "This "); |
|
71 UCX_TEST_BEGIN |
70 list = ucx_dlist_append(list, "list "); |
72 list = ucx_dlist_append(list, "list "); |
71 list = ucx_dlist_append(list, "has "); |
73 list = ucx_dlist_append(list, "has "); |
72 list = ucx_dlist_append(list, "size "); |
74 list = ucx_dlist_append(list, "size "); |
73 list = ucx_dlist_append(list, "5!"); |
75 list = ucx_dlist_append(list, "5!"); |
74 |
76 |
75 UCX_TEST_ASSERT(ucx_dlist_size(list) == 5, "failed"); |
77 UCX_TEST_ASSERT(ucx_dlist_size(list) == 5, "failed"); |
76 |
78 |
|
79 UCX_TEST_END |
77 ucx_dlist_free(list); |
80 ucx_dlist_free(list); |
78 |
|
79 UCX_TEST_END |
|
80 } |
81 } |
81 |
82 |
82 UCX_TEST_BEGIN(test_ucx_dlist_first) { |
83 UCX_TEST_IMPLEMENT(test_ucx_dlist_first) { |
83 UcxDlist *list = ucx_dlist_append(NULL, "Find "); |
84 UcxDlist *list = ucx_dlist_append(NULL, "Find "); |
|
85 UCX_TEST_BEGIN |
84 list = ucx_dlist_append(list, "the "); |
86 list = ucx_dlist_append(list, "the "); |
85 list = ucx_dlist_append(list, "first!"); |
87 list = ucx_dlist_append(list, "first!"); |
86 |
88 |
87 char* first = (char*) (ucx_dlist_first(list)->data); |
89 char* first = (char*) (ucx_dlist_first(list)->data); |
88 |
90 |
89 UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed"); |
91 UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed"); |
90 |
92 |
|
93 UCX_TEST_END |
91 ucx_dlist_free(list); |
94 ucx_dlist_free(list); |
92 |
|
93 UCX_TEST_END |
|
94 } |
95 } |
95 |
96 |
96 UCX_TEST_BEGIN(test_ucx_dlist_last) { |
97 UCX_TEST_IMPLEMENT(test_ucx_dlist_last) { |
97 UcxDlist *list = ucx_dlist_append(NULL, "Find "); |
98 UcxDlist *list = ucx_dlist_append(NULL, "Find "); |
|
99 UCX_TEST_BEGIN |
98 list = ucx_dlist_append(list, "the "); |
100 list = ucx_dlist_append(list, "the "); |
99 list = ucx_dlist_append(list, "last!"); |
101 list = ucx_dlist_append(list, "last!"); |
100 |
102 |
101 char* last = (char*) (ucx_dlist_last(list)->data); |
103 char* last = (char*) (ucx_dlist_last(list)->data); |
102 |
104 |
103 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed"); |
105 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed"); |
104 |
106 |
|
107 UCX_TEST_END |
105 ucx_dlist_free(list); |
108 ucx_dlist_free(list); |
106 |
|
107 UCX_TEST_END |
|
108 } |
109 } |
109 |
110 |
110 UCX_TEST_BEGIN(test_ucx_dlist_get) { |
111 UCX_TEST_IMPLEMENT(test_ucx_dlist_get) { |
111 UcxDlist *list = ucx_dlist_append(NULL, "Find "); |
112 UcxDlist *list = ucx_dlist_append(NULL, "Find "); |
|
113 UCX_TEST_BEGIN |
112 list = ucx_dlist_append(list, "the "); |
114 list = ucx_dlist_append(list, "the "); |
113 list = ucx_dlist_append(list, "mid!"); |
115 list = ucx_dlist_append(list, "mid!"); |
114 |
116 |
115 char* mid = (char*) (ucx_dlist_get(list, 1)->data); |
117 char* mid = (char*) (ucx_dlist_get(list, 1)->data); |
116 |
118 |
117 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed"); |
119 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed"); |
118 |
120 |
|
121 UCX_TEST_END |
119 ucx_dlist_free(list); |
122 ucx_dlist_free(list); |
120 |
|
121 UCX_TEST_END |
|
122 } |
123 } |
123 |
124 |
124 UCX_TEST_BEGIN(test_ucx_dlist_remove) { |
125 UCX_TEST_IMPLEMENT(test_ucx_dlist_remove) { |
125 UcxDlist *list = ucx_dlist_append(NULL, "Hello"); |
126 UcxDlist *list = ucx_dlist_append(NULL, "Hello"); |
|
127 UCX_TEST_BEGIN |
126 list = ucx_dlist_append(list, " fucking"); |
128 list = ucx_dlist_append(list, " fucking"); |
127 list = ucx_dlist_append(list, " World!"); |
129 list = ucx_dlist_append(list, " World!"); |
128 |
130 |
129 list = ucx_dlist_remove(list, ucx_dlist_get(list, 1)); |
131 list = ucx_dlist_remove(list, ucx_dlist_get(list, 1)); |
130 |
132 |
131 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") |
133 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") |
132 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") |
134 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") |
133 UCX_TEST_ASSERT(list->next->next == NULL, "failed") |
135 UCX_TEST_ASSERT(list->next->next == NULL, "failed") |
134 |
136 |
|
137 UCX_TEST_END |
135 ucx_dlist_free(list); |
138 ucx_dlist_free(list); |
136 |
|
137 UCX_TEST_END |
|
138 } |
139 } |
139 |
140 |
140 UCX_TEST_BEGIN(test_ucx_dlist_clone) { |
141 UCX_TEST_IMPLEMENT(test_ucx_dlist_clone) { |
141 |
142 |
142 char *hello = (char*)malloc(6); |
143 char *hello = (char*)malloc(6); |
143 char *world = (char*)malloc(8); |
144 char *world = (char*)malloc(8); |
144 |
145 |
145 memcpy(hello, "Hello", 6); |
146 memcpy(hello, "Hello", 6); |