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 assert(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
130 string = from_cstring("asdyx782");
131 string2 = from_cstring("asdyx782");
132 assert(string_cmp(string, string2) == 0);
133 delete_string(string2);
134 delete_string(string);
135}
136
137static void test_bytearray(void)
138{
139 Bytearray* bt;
140 char* tmpchar;
141
142 bt = new_bytearray(1);
143 bytearray_push(bt, "a");
144 bytearray_push(bt, "b");
145 bytearray_push(bt, "c");
146 bytearray_push(bt, "d");
147 bytearray_push(bt, "\0");
148
149 assert(strcmp((char*)bt->items, "abcd") == 0);
150
151 tmpchar = bytearray_pop(bt, NULL);
152 assert(*tmpchar == '\0');
153 free(tmpchar);
154
155 tmpchar = bytearray_pop(bt, NULL);
156 assert(*tmpchar == 'd');
157 free(tmpchar);
158
159 tmpchar = bytearray_pop(bt, NULL);
160 assert(*tmpchar == 'c');
161 free(tmpchar);
162
163 tmpchar = malloc(1);
164 bytearray_pop(bt, tmpchar);
165 assert(*tmpchar == 'b');
166
167 bytearray_pop(bt, tmpchar);
168 assert(*tmpchar == 'a');
169 free(tmpchar);
170
171 bytearray_push(bt, "a");
172 tmpchar = bytearray_pop(bt, NULL);
173 assert(*tmpchar == 'a');
174 free(tmpchar);
175
176 bytearray_push(bt, "a");
177 bytearray_push(bt, "a");
178 bytearray_push(bt, "a");
179 assert(bt->length == 3);
180 bytearray_remove_range(bt, 0, bt->length, NULL);
181 assert(bt->length == 0);
182
183 delete_bytearray(bt, NULL);
184}
185#if __STDC_VERSION__ >= 201112L
186static void test_timespec(void)
187{
188 struct timespec ts1 = {10, 10};
189 struct timespec ts2 = {10, 10};
190 struct timespec res;
191
192 res = timespec_add(&ts1, &ts2);
193 assert(res.tv_sec == 20 && res.tv_nsec == 20);
194
195 ts1.tv_sec = 10;
196 ts1.tv_nsec = 999999999;
197 ts2.tv_sec = 0;
198 ts2.tv_nsec = 1;
199
200 res = timespec_add(&ts1, &ts2);
201 assert(res.tv_sec == 11 && res.tv_nsec == 0);
202
203 ts1.tv_sec = 0;
204 ts1.tv_nsec = 1;
205 ts2.tv_sec = 10;
206 ts2.tv_nsec = 999999999;
207
208 res = timespec_add(&ts1, &ts2);
209 assert(res.tv_sec == 11 && res.tv_nsec == 0);
210
211 ts1.tv_sec = 0;
212 ts1.tv_nsec = 1;
213 ts2.tv_sec = 10;
214 ts2.tv_nsec = 999999999;
215
216 res = timespec_diff(&ts1, &ts2);
217 assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
218
219 ts1.tv_sec = 0;
220 ts1.tv_nsec = 999999999;
221 ts2.tv_sec = 10;
222 ts2.tv_nsec = 1;
223
224 res = timespec_diff(&ts1, &ts2);
225 assert(res.tv_sec == 9 && res.tv_nsec == 2);
226
227 ts1.tv_sec = 0;
228 ts1.tv_nsec = 1;
229 ts2.tv_sec = 10;
230 ts2.tv_nsec = 999999999;
231
232 res = timespec_diff(&ts1, &ts2);
233 assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
234}
235#endif
236static void test_ll(void)
237{
238 LinkedList* ll;
239 struct test my_structa, my_structb, *my_structptr;
240 int truth;
241 my_structa.a = 5;
242 my_structa.b = 5;
243 my_structb.a = 7;
244 my_structb.b = 7;
245
246 for(truth = 0; truth<2; truth++)
247 {
248 ll = new_ll(truth,truth);
249
250 ll_push(ll, &my_structa);
251 my_structptr = ll_pop(ll);
252 assert(my_structptr->a == 5 && my_structptr->b == 5);
253
254 ll_push(ll, &my_structa);
255 ll_push(ll, &my_structa);
256 ll_push(ll, &my_structa);
257 ll_push(ll, &my_structb);
258
259 my_structptr = ll_at(ll, ll->length-1);
260 assert(my_structptr->a == 7);
261
262 ll_swap(ll, 0, ll->length-1);
263
264 my_structptr = ll_at(ll, ll->length-1);
265 assert(my_structptr->a == 5);
266
267
268 ll_remove_range(ll, 0, ll->length-1, NULL);
269 assert(ll->length == 1);
270
271 delete_ll(ll, NULL);
272 }
273}
274
275static void test_bitfuncs(void)
276{
277 byte tmp = 0;
278
279 setbit(byte, tmp, 0);
280 assert(tmp == 1);
281 setbit(byte, tmp, 1);
282 assert(tmp == 3);
283 setbit(byte, tmp, 2);
284 assert(tmp == 7);
285
286 clearbit(byte, tmp, 2);
287 assert(tmp == 3);
288
289 assert(getbit(byte, tmp, 0) == 1);
290 assert(getbit(byte, tmp, 5) == 0);
291}
292
293int main(int argc, char** argv)
294{
295 /* memswap tests */
296 int a = 5;
297 int b = 2;
298 void* tmp = malloc(sizeof(int));
299
300 memswap(&a, &b, sizeof(int));
301 assert(a == 2 && b == 5);
302 #if __STDC_VERSION__ >= 199901L
303 memqswap_stack(&a, &b, sizeof(int));
304 assert(a == 5 && b == 2);
305 memqswap(&a, &b, tmp, sizeof(int));
306 assert(a == 2 && b == 5);
307 #else
308 memqswap(&a, &b, tmp, sizeof(int));
309 assert(a == 5 && b == 2);
310 #endif
311 free(tmp);
312 /* memswap tests */
313
314 test_vector();
315 test_string();
316 test_bytearray();
317 #if __STDC_VERSION__ >= 201112L
318 test_timespec();
319 #endif
320 test_ll();
321 test_bitfuncs();
322
323 /* misc tests */
324 sleep_ms(1000);
325 assert(cutil_strcasecmp("ASD", "aSd") == 0);
326 assert(cutil_strcasecmp("BSD", "asd") > 0);
327 assert(cutil_strcasecmp("ASD", "bsd") < 0);
328 assert(cutil_strncasecmp("ASDn", "asd", 3) == 0);
329 assert(cutil_strncasecmp("ASDn", "asd", 4) >0);
330 return 0;
331}
332