test/list_tests.c

changeset 33
9c219a62070d
parent 30
23bb65cbf7a4
child 35
fdabd1240b69
equal deleted inserted replaced
32:c7af4ec56e19 33:9c219a62070d
2 * tests of list implementation 2 * tests of list implementation
3 */ 3 */
4 4
5 #include "list_tests.h" 5 #include "list_tests.h"
6 6
7 UCX_TEST_BEGIN(test_ucx_list_append) { 7 UCX_TEST_IMPLEMENT(test_ucx_list_append) {
8 UcxList *list = ucx_list_append(NULL, "Hello"); 8 UcxList *list = ucx_list_append(NULL, "Hello");
9 9 UCX_TEST_BEGIN
10 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") 10 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
11 11
12 list = ucx_list_append(list, " World!"); 12 list = ucx_list_append(list, " World!");
13 13
14 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") 14 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
15 UCX_TEST_ASSERT(list->next->next == NULL, "failed") 15 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
16 16
17 UCX_TEST_END
17 ucx_list_free(list); 18 ucx_list_free(list);
18
19 UCX_TEST_END
20 } 19 }
21 20
22 UCX_TEST_BEGIN(test_ucx_list_prepend) { 21 UCX_TEST_IMPLEMENT(test_ucx_list_prepend) {
23 UcxList *list = ucx_list_prepend(NULL, " World!"); 22 UcxList *list = ucx_list_prepend(NULL, " World!");
23 UCX_TEST_BEGIN
24 list = ucx_list_prepend(list, "Hello"); 24 list = ucx_list_prepend(list, "Hello");
25 25
26 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") 26 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
27 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") 27 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
28 UCX_TEST_ASSERT(list->next->next == NULL, "failed") 28 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
29 29
30 UCX_TEST_END
30 ucx_list_free(list); 31 ucx_list_free(list);
31
32 UCX_TEST_END
33 } 32 }
34 33
35 UCX_TEST_BEGIN(test_ucx_list_equals) { 34 UCX_TEST_IMPLEMENT(test_ucx_list_equals) {
36 UcxList *list = ucx_list_append(NULL, "Hello"); 35 UcxList *list = ucx_list_append(NULL, "Hello");
37 list = ucx_list_append(list, " World!"); 36 list = ucx_list_append(list, " World!");
38 UcxList *list2 = ucx_list_prepend(NULL, " World!"); 37 UcxList *list2 = ucx_list_prepend(NULL, " World!");
39 list2 = ucx_list_prepend(list2, "Hello"); 38 list2 = ucx_list_prepend(list2, "Hello");
40 UcxList *list3 = ucx_list_prepend(NULL, " Welt!"); 39 UcxList *list3 = ucx_list_prepend(NULL, " Welt!");
41 list3 = ucx_list_prepend(list3, "Hallo"); 40 list3 = ucx_list_prepend(list3, "Hallo");
42 41
42 UCX_TEST_BEGIN
43 UCX_TEST_ASSERT(ucx_list_equals(list, list2, cmp_string, NULL), "failed") 43 UCX_TEST_ASSERT(ucx_list_equals(list, list2, cmp_string, NULL), "failed")
44 UCX_TEST_ASSERT(!ucx_list_equals(list, list3, cmp_string, NULL), "failed") 44 UCX_TEST_ASSERT(!ucx_list_equals(list, list3, cmp_string, NULL), "failed")
45 UCX_TEST_END
45 46
46 ucx_list_free(list3); 47 ucx_list_free(list3);
47 ucx_list_free(list2); 48 ucx_list_free(list2);
48 ucx_list_free(list); 49 ucx_list_free(list);
49
50 UCX_TEST_END
51 } 50 }
52 51
53 UCX_TEST_BEGIN(test_ucx_list_concat) { 52 UCX_TEST_IMPLEMENT(test_ucx_list_concat) {
54 UcxList *list = ucx_list_append(NULL, "Hello"); 53 UcxList *list = ucx_list_append(NULL, "Hello");
55 UcxList *list2 = ucx_list_prepend(NULL, " World!"); 54 UcxList *list2 = ucx_list_prepend(NULL, " World!");
56 55
57 list = ucx_list_concat(list, list2); 56 list = ucx_list_concat(list, list2);
57 UCX_TEST_BEGIN
58 58
59 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") 59 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
60 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") 60 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
61 UCX_TEST_ASSERT(list->next->next == NULL, "failed") 61 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
62 62
63 UCX_TEST_END
64 if (list->next == NULL) {
65 ucx_list_free(list2);
66 }
63 ucx_list_free(list); 67 ucx_list_free(list);
64
65 UCX_TEST_END
66 } 68 }
67 69
68 UCX_TEST_BEGIN(test_ucx_list_size) { 70 UCX_TEST_IMPLEMENT(test_ucx_list_size) {
69 UcxList *list = ucx_list_append(NULL, "This "); 71 UcxList *list = ucx_list_append(NULL, "This ");
72 UCX_TEST_BEGIN
70 list = ucx_list_append(list, "list "); 73 list = ucx_list_append(list, "list ");
71 list = ucx_list_append(list, "has "); 74 list = ucx_list_append(list, "has ");
72 list = ucx_list_append(list, "size "); 75 list = ucx_list_append(list, "size ");
73 list = ucx_list_append(list, "5!"); 76 list = ucx_list_append(list, "5!");
74 77
75 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed"); 78 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
76 79
80 UCX_TEST_END
77 ucx_list_free(list); 81 ucx_list_free(list);
78
79 UCX_TEST_END
80 } 82 }
81 83
82 UCX_TEST_BEGIN(test_ucx_list_last) { 84 UCX_TEST_IMPLEMENT(test_ucx_list_last) {
83 UcxList *list = ucx_list_append(NULL, "Find "); 85 UcxList *list = ucx_list_append(NULL, "Find ");
86 UCX_TEST_BEGIN
84 list = ucx_list_append(list, "the "); 87 list = ucx_list_append(list, "the ");
85 list = ucx_list_append(list, "last!"); 88 list = ucx_list_append(list, "last!");
86 89
87 char* last = (char*) (ucx_list_last(list)->data); 90 char* last = (char*) (ucx_list_last(list)->data);
88 91
89 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed"); 92 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
90 93
94 UCX_TEST_END
91 ucx_list_free(list); 95 ucx_list_free(list);
92 96
93 UCX_TEST_END
94 } 97 }
95 98
96 UCX_TEST_BEGIN(test_ucx_list_get) { 99 UCX_TEST_IMPLEMENT(test_ucx_list_get) {
97 UcxList *list = ucx_list_append(NULL, "Find "); 100 UcxList *list = ucx_list_append(NULL, "Find ");
101 UCX_TEST_BEGIN
98 list = ucx_list_append(list, "the "); 102 list = ucx_list_append(list, "the ");
99 list = ucx_list_append(list, "mid!"); 103 list = ucx_list_append(list, "mid!");
100 104
101 char* mid = (char*) (ucx_list_get(list, 1)->data); 105 char* mid = (char*) (ucx_list_get(list, 1)->data);
102 106
103 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed"); 107 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
104 108
109 UCX_TEST_END
105 ucx_list_free(list); 110 ucx_list_free(list);
106
107 UCX_TEST_END
108 } 111 }
109 112
110 UCX_TEST_BEGIN(test_ucx_list_remove) { 113 UCX_TEST_IMPLEMENT(test_ucx_list_remove) {
111 UcxList *list = ucx_list_append(NULL, "Hello"); 114 UcxList *list = ucx_list_append(NULL, "Hello");
115 UCX_TEST_BEGIN
112 list = ucx_list_append(list, " fucking"); 116 list = ucx_list_append(list, " fucking");
113 list = ucx_list_append(list, " World!"); 117 list = ucx_list_append(list, " World!");
114 118
115 list = ucx_list_remove(list, ucx_list_get(list, 1)); 119 list = ucx_list_remove(list, ucx_list_get(list, 1));
116 120
117 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") 121 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
118 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") 122 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
119 UCX_TEST_ASSERT(list->next->next == NULL, "failed") 123 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
124 UCX_TEST_END
120 125
121 ucx_list_free(list); 126 ucx_list_free(list);
122
123 UCX_TEST_END
124 } 127 }
125 128
126 UCX_TEST_BEGIN(test_ucx_list_clone) { 129 UCX_TEST_IMPLEMENT(test_ucx_list_clone) {
127 130
128 char *hello = (char*)malloc(6); 131 char *hello = (char*)malloc(6);
129 char *world = (char*)malloc(8); 132 char *world = (char*)malloc(8);
130 133
131 memcpy(hello, "Hello", 6); 134 memcpy(hello, "Hello", 6);
133 136
134 UcxList *list = ucx_list_append(NULL, hello); 137 UcxList *list = ucx_list_append(NULL, hello);
135 list = ucx_list_append(list, world); 138 list = ucx_list_append(list, world);
136 139
137 UcxList *copy = ucx_list_clone(list, copy_string, NULL); 140 UcxList *copy = ucx_list_clone(list, copy_string, NULL);
141 UCX_TEST_BEGIN
138 142
139 UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, NULL), "failed") 143 UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, NULL), "failed")
140 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy") 144 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy")
141 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy") 145 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy")
142 146
147 UCX_TEST_END
143 free(copy->next->data); 148 free(copy->next->data);
144 free(copy->data); 149 free(copy->data);
145 150
146 free(world); 151 free(world);
147 free(hello); 152 free(hello);
148 ucx_list_free(list); 153 ucx_list_free(list);
149 ucx_list_free(copy); 154 ucx_list_free(copy);
150
151 UCX_TEST_END
152 } 155 }

mercurial