test.c
Raw
1#include <cutils/cutils.h>
2#include <stdio.h>
3#include <assert.h>
4
5struct test
6{
7 int a;
8 int b;
9};
10
11static int cmp_str(const void* str1, const void* str2)
12{
13 const struct test* str1_s = str1;
14 const struct test* str2_s = str2;
15 return !((str1_s->a == str2_s->a) && (str1_s->b == str2_s->b));
16}
17
18static void test_vector(void)
19{
20 size_t* find;
21 Vector* vector;
22 struct test* my_struct;
23
24 my_struct = malloc(sizeof(*my_struct));
25 vector = new_vector();
26
27 my_struct->a = 5;
28 my_struct->b = 5;
29 vector_push(vector, my_struct);
30
31 my_struct = malloc(sizeof(*my_struct));
32 my_struct->a = 6;
33 my_struct->b = 6;
34 vector_push(vector, my_struct);
35
36 my_struct = malloc(sizeof(*my_struct));
37 my_struct->a = 7;
38 my_struct->b = 7;
39 vector_push(vector, my_struct);
40 assert(vector->length == 3);
41
42 vector_remove(vector, 0,free);
43 assert(vector->length == 2);
44
45 my_struct = malloc(sizeof(*my_struct));
46 my_struct->a = 8;
47 my_struct->b = 8;
48 vector_insert(vector, 2, my_struct);
49 assert(vector->length == 3);
50
51 my_struct = malloc(sizeof(*my_struct));
52 my_struct->a = 8;
53 my_struct->b = 8;
54
55 find = vector_find(vector, my_struct, cmp_str);
56
57 assert(*find == 2);
58 free(find);
59
60 free(my_struct);
61 my_struct = vector_pop(vector);
62 assert(my_struct->a == 8 && my_struct->b == 8);
63 free(my_struct);
64
65 delete_vector(vector, free);
66}
67
68static void test_string(void)
69{
70 String* string;
71 String* string2;
72 char* cstring;
73 size_t tmp;
74
75 string = new_string();
76
77 string_push(string, 'a');
78 string_push(string, 'b');
79 string_push(string, 'c');
80 string_push(string, 'd');
81 assert(string->length == 4);
82
83 cstring = to_cstring(string);
84 assert(strcmp(cstring, "abcd") == 0);
85 free(cstring);
86
87 string_remove(string, 3);
88 cstring = to_cstring(string);
89 assert(strcmp(cstring, "abc") == 0);
90 free(cstring);
91 assert(string->length == 3);
92
93 string_insert(string, 0,'a');
94 cstring = to_cstring(string);
95 assert(strcmp(cstring, "aabc") == 0);
96 free(cstring);
97
98 string2 = from_cstring("xyz");
99
100 string_concat(string,string2);
101 cstring = to_cstring(string);
102 assert(strcmp(cstring, "aabcxyz") == 0);
103 free(cstring);
104
105 delete_string(string2);
106 delete_string(string);
107
108 string = from_cstring("abcd");
109 string2 = from_cstring("cd");
110 string_find_str(string, string2, &tmp);
111 assert(tmp == 2);
112
113 delete_string(string2);
114 delete_string(string);
115
116 string = from_cstring("abcd");
117 assert(string->length == 4);
118 string_remove_range(string, 0, string->length);
119 assert(string->length == 0);
120 delete_string(string);
121
122 string = from_cstring("bba");
123 string_strip(string, 'a');
124 assert(string->length == 2);
125
126 cstring = to_cstring_del(string);
127 free(cstring);
128}
129
130static void test_bytearray(void)
131{
132 Bytearray* bt;
133 char* tmpchar;
134
135 bt = new_bytearray(1);
136 bytearray_push(bt, "a");
137 bytearray_push(bt, "b");
138 bytearray_push(bt, "c");
139 bytearray_push(bt, "d");
140 bytearray_push(bt, "\0");
141
142 assert(strcmp((char*)bt->items, "abcd") == 0);
143
144 tmpchar = bytearray_pop(bt, NULL);
145 assert(*tmpchar == '\0');
146 free(tmpchar);
147
148 tmpchar = bytearray_pop(bt, NULL);
149 assert(*tmpchar == 'd');
150 free(tmpchar);
151
152 tmpchar = bytearray_pop(bt, NULL);
153 assert(*tmpchar == 'c');
154 free(tmpchar);
155
156 tmpchar = malloc(1);
157 bytearray_pop(bt, tmpchar);
158 assert(*tmpchar == 'b');
159
160 bytearray_pop(bt, tmpchar);
161 assert(*tmpchar == 'a');
162 free(tmpchar);
163
164 bytearray_push(bt, "a");
165 tmpchar = bytearray_pop(bt, NULL);
166 assert(*tmpchar == 'a');
167 free(tmpchar);
168
169 bytearray_push(bt, "a");
170 bytearray_push(bt, "a");
171 bytearray_push(bt, "a");
172 assert(bt->length == 3);
173 bytearray_remove_range(bt, 0, bt->length, NULL);
174 assert(bt->length == 0);
175
176 delete_bytearray(bt, NULL);
177}
178#if __STDC_VERSION__ >= 201112L
179static void test_timespec(void)
180{
181 struct timespec ts1 = {10, 10};
182 struct timespec ts2 = {10, 10};
183 struct timespec res;
184
185 res = timespec_add(&ts1, &ts2);
186 assert(res.tv_sec == 20 && res.tv_nsec == 20);
187
188 ts1.tv_sec = 10;
189 ts1.tv_nsec = 999999999;
190 ts2.tv_sec = 0;
191 ts2.tv_nsec = 1;
192
193 res = timespec_add(&ts1, &ts2);
194 assert(res.tv_sec == 11 && res.tv_nsec == 0);
195
196 ts1.tv_sec = 0;
197 ts1.tv_nsec = 1;
198 ts2.tv_sec = 10;
199 ts2.tv_nsec = 999999999;
200
201 res = timespec_add(&ts1, &ts2);
202 assert(res.tv_sec == 11 && res.tv_nsec == 0);
203
204 ts1.tv_sec = 0;
205 ts1.tv_nsec = 1;
206 ts2.tv_sec = 10;
207 ts2.tv_nsec = 999999999;
208
209 res = timespec_diff(&ts1, &ts2);
210 assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
211
212 ts1.tv_sec = 0;
213 ts1.tv_nsec = 999999999;
214 ts2.tv_sec = 10;
215 ts2.tv_nsec = 1;
216
217 res = timespec_diff(&ts1, &ts2);
218 assert(res.tv_sec == 9 && res.tv_nsec == 2);
219
220 ts1.tv_sec = 0;
221 ts1.tv_nsec = 1;
222 ts2.tv_sec = 10;
223 ts2.tv_nsec = 999999999;
224
225 res = timespec_diff(&ts1, &ts2);
226 assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
227}
228#endif
229static void test_ll(void)
230{
231 LinkedList* ll;
232 struct test my_structa, my_structb, *my_structptr;
233 int truth;
234 my_structa.a = 5;
235 my_structa.b = 5;
236 my_structb.a = 7;
237 my_structb.b = 7;
238
239 for(truth = 0; truth<2; truth++)
240 {
241 ll = new_ll(truth,truth);
242
243 ll_push(ll, &my_structa);
244 my_structptr = ll_pop(ll);
245 assert(my_structptr->a == 5 && my_structptr->b == 5);
246
247 ll_push(ll, &my_structa);
248 ll_push(ll, &my_structa);
249 ll_push(ll, &my_structa);
250 ll_push(ll, &my_structb);
251
252 my_structptr = ll_at(ll, ll->length-1);
253 assert(my_structptr->a == 7);
254
255 ll_swap(ll, 0, ll->length-1);
256
257 my_structptr = ll_at(ll, ll->length-1);
258 assert(my_structptr->a == 5);
259
260
261 ll_remove_range(ll, 0, ll->length-1, NULL);
262 assert(ll->length == 1);
263
264 delete_ll(ll, NULL);
265 }
266}
267
268static void test_bitfuncs(void)
269{
270 byte tmp = 0;
271
272 setbit(byte, tmp, 0);
273 assert(tmp == 1);
274 setbit(byte, tmp, 1);
275 assert(tmp == 3);
276 setbit(byte, tmp, 2);
277 assert(tmp == 7);
278
279 clearbit(byte, tmp, 2);
280 assert(tmp == 3);
281
282 assert(getbit(byte, tmp, 0) == 1);
283 assert(getbit(byte, tmp, 5) == 0);
284}
285
286int main(int argc, char** argv)
287{
288 /* memswap tests */
289 int a = 5;
290 int b = 2;
291 void* tmp = malloc(sizeof(int));
292
293 memswap(&a, &b, sizeof(int));
294 assert(a == 2 && b == 5);
295 #if __STDC_VERSION__ >= 199901L
296 memqswap_stack(&a, &b, sizeof(int));
297 assert(a == 5 && b == 2);
298 memqswap(&a, &b, tmp, sizeof(int));
299 assert(a == 2 && b == 5);
300 #else
301 memqswap(&a, &b, tmp, sizeof(int));
302 assert(a == 5 && b == 2);
303 #endif
304 free(tmp);
305 /* memswap tests */
306
307 test_vector();
308 test_string();
309 test_bytearray();
310 #if __STDC_VERSION__ >= 201112L
311 test_timespec();
312 #endif
313 test_ll();
314 test_bitfuncs();
315
316 /* misc tests */
317 sleep_ms(1000);
318 assert(cutil_strcasecmp("ASD", "aSd") == 0);
319 assert(cutil_strcasecmp("BSD", "asd") > 0);
320 assert(cutil_strcasecmp("ASD", "bsd") < 0);
321 assert(cutil_strncasecmp("ASDn", "asd", 3) == 0);
322 assert(cutil_strncasecmp("ASDn", "asd", 4) >0);
323 return 0;
324}
325