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 bool truth;
75
76 for(truth = false; truth == false || truth == true; truth++)
77 {
78 string = new_string(truth);
79
80 string_push(string, 'a');
81 string_push(string, 'b');
82 string_push(string, 'c');
83 string_push(string, 'd');
84 assert(string->length == 4);
85
86 cstring = to_cstring(string);
87 assert(strcmp(cstring, "abcd") == 0);
88 free(cstring);
89
90 string_remove(string, 3);
91 cstring = to_cstring(string);
92 assert(strcmp(cstring, "abc") == 0);
93 free(cstring);
94 assert(string->length == 3);
95
96 string_insert(string, 0,'a');
97 cstring = to_cstring(string);
98 assert(strcmp(cstring, "aabc") == 0);
99 free(cstring);
100 string2 = from_cstring("xyz", truth);
101
102 string_concat(string,string2);
103 cstring = to_cstring(string);
104 assert(strcmp(cstring, "aabcxyz") == 0);
105 free(cstring);
106
107 delete_string(string2);
108 delete_string(string);
109
110 string = from_cstring("abcd", truth);
111 string2 = from_cstring("cd", truth);
112 assert(string_find_str(string, string2, &tmp));
113 assert(tmp == 2);
114
115 delete_string(string2);
116 delete_string(string);
117
118 string = from_cstring("abcd", truth);
119 assert(string->length == 4);
120 string_remove_range(string, 0, string->length);
121 assert(string->length == 0);
122 delete_string(string);
123
124 string = from_cstring("bba", truth);
125 string_strip(string, 'a');
126 assert(string->length == 2);
127
128 cstring = to_cstring_del(string);
129 free(cstring);
130
131
132 string = from_cstring("asdyx782", truth);
133 string2 = from_cstring("asdyx782", truth);
134 assert(string_cmp(string, string2) == 0);
135 delete_string(string2);
136 delete_string(string);
137 }
138
139 string = from_cstring("asdyx782", true);
140 string_push(string, 'a');
141 string_remove(string, 2);
142 assert(strcmp(string->chars, "asyx782a") == 0);
143 string_remove_range(string, 1, 7);
144 assert(strcmp(string->chars, "a") == 0);
145 assert(string_pop(string) == 'a');
146 string2 = from_cstring("asdasdas", false);
147 string_concat(string, string2);
148 assert(strcmp(string->chars, "asdasdas") == 0);
149
150 delete_string(string2);
151 string2 = from_cstring("koasä_ #", true);
152 string_move(string, string2);
153 assert(strcmp(string->chars, "koasä_ #") == 0);
154
155 delete_string(string);
156}
157
158static void test_bytearray(void)
159{
160 Bytearray* bt;
161 char* tmpchar;
162
163 bt = new_bytearray(1);
164 bytearray_push(bt, "a");
165 bytearray_push(bt, "b");
166 bytearray_push(bt, "c");
167 bytearray_push(bt, "d");
168 bytearray_push(bt, "\0");
169
170 assert(strcmp((char*)bt->items, "abcd") == 0);
171
172 tmpchar = bytearray_pop(bt, NULL);
173 assert(*tmpchar == '\0');
174 free(tmpchar);
175
176 tmpchar = bytearray_pop(bt, NULL);
177 assert(*tmpchar == 'd');
178 free(tmpchar);
179
180 tmpchar = bytearray_pop(bt, NULL);
181 assert(*tmpchar == 'c');
182 free(tmpchar);
183
184 tmpchar = malloc(1);
185 bytearray_pop(bt, tmpchar);
186 assert(*tmpchar == 'b');
187
188 bytearray_pop(bt, tmpchar);
189 assert(*tmpchar == 'a');
190 free(tmpchar);
191
192 bytearray_push(bt, "a");
193 tmpchar = bytearray_pop(bt, NULL);
194 assert(*tmpchar == 'a');
195 free(tmpchar);
196
197 bytearray_push(bt, "a");
198 bytearray_push(bt, "a");
199 bytearray_push(bt, "a");
200 assert(bt->length == 3);
201 bytearray_remove_range(bt, 0, bt->length, NULL);
202 assert(bt->length == 0);
203
204 delete_bytearray(bt, NULL);
205}
206#if __STDC_VERSION__ >= 201112L
207static void test_timespec(void)
208{
209 struct timespec ts1 = {10, 10};
210 struct timespec ts2 = {10, 10};
211 struct timespec res;
212
213 res = timespec_add(&ts1, &ts2);
214 assert(res.tv_sec == 20 && res.tv_nsec == 20);
215
216 ts1.tv_sec = 10;
217 ts1.tv_nsec = 999999999;
218 ts2.tv_sec = 0;
219 ts2.tv_nsec = 1;
220
221 res = timespec_add(&ts1, &ts2);
222 assert(res.tv_sec == 11 && res.tv_nsec == 0);
223
224 ts1.tv_sec = 0;
225 ts1.tv_nsec = 1;
226 ts2.tv_sec = 10;
227 ts2.tv_nsec = 999999999;
228
229 res = timespec_add(&ts1, &ts2);
230 assert(res.tv_sec == 11 && res.tv_nsec == 0);
231
232 ts1.tv_sec = 0;
233 ts1.tv_nsec = 1;
234 ts2.tv_sec = 10;
235 ts2.tv_nsec = 999999999;
236
237 res = timespec_diff(&ts1, &ts2);
238 assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
239
240 ts1.tv_sec = 0;
241 ts1.tv_nsec = 999999999;
242 ts2.tv_sec = 10;
243 ts2.tv_nsec = 1;
244
245 res = timespec_diff(&ts1, &ts2);
246 assert(res.tv_sec == 9 && res.tv_nsec == 2);
247
248 ts1.tv_sec = 0;
249 ts1.tv_nsec = 1;
250 ts2.tv_sec = 10;
251 ts2.tv_nsec = 999999999;
252
253 res = timespec_diff(&ts1, &ts2);
254 assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
255}
256#endif
257static void test_ll(void)
258{
259 LinkedList* ll;
260 struct test my_structa, my_structb, *my_structptr;
261 int truth;
262 my_structa.a = 5;
263 my_structa.b = 5;
264 my_structb.a = 7;
265 my_structb.b = 7;
266
267 for(truth = 0; truth<2; truth++)
268 {
269 ll = new_ll(truth,truth);
270
271 ll_push(ll, &my_structa);
272 my_structptr = ll_pop(ll);
273 assert(my_structptr->a == 5 && my_structptr->b == 5);
274
275 ll_push(ll, &my_structa);
276 ll_push(ll, &my_structa);
277 ll_push(ll, &my_structa);
278 ll_push(ll, &my_structb);
279
280 my_structptr = ll_at(ll, ll->length-1);
281 assert(my_structptr->a == 7);
282
283 ll_swap(ll, 0, ll->length-1);
284
285 my_structptr = ll_at(ll, ll->length-1);
286 assert(my_structptr->a == 5);
287
288
289 ll_remove_range(ll, 0, ll->length-1, NULL);
290 assert(ll->length == 1);
291
292 delete_ll(ll, NULL);
293 }
294}
295
296static void test_bitfuncs(void)
297{
298 byte tmp = 0;
299
300 setbit(byte, tmp, 0);
301 assert(tmp == 1);
302 setbit(byte, tmp, 1);
303 assert(tmp == 3);
304 setbit(byte, tmp, 2);
305 assert(tmp == 7);
306
307 clearbit(byte, tmp, 2);
308 assert(tmp == 3);
309
310 assert(getbit(byte, tmp, 0) == 1);
311 assert(getbit(byte, tmp, 5) == 0);
312}
313
314int main(int argc, char** argv)
315{
316 /* memswap tests */
317 int a = 5;
318 int b = 2;
319 void* tmp = malloc(sizeof(int));
320
321 memswap(&a, &b, sizeof(int));
322 assert(a == 2 && b == 5);
323 #if __STDC_VERSION__ >= 199901L
324 memqswap_stack(&a, &b, sizeof(int));
325 assert(a == 5 && b == 2);
326 memqswap(&a, &b, tmp, sizeof(int));
327 assert(a == 2 && b == 5);
328 #else
329 memqswap(&a, &b, tmp, sizeof(int));
330 assert(a == 5 && b == 2);
331 #endif
332 free(tmp);
333 /* memswap tests */
334
335 test_vector();
336 test_string();
337 test_bytearray();
338 #if __STDC_VERSION__ >= 201112L
339 test_timespec();
340 #endif
341 test_ll();
342 test_bitfuncs();
343
344 /* misc tests */
345 sleep_ms(1000);
346 assert(cutil_strcasecmp("ASD", "aSd") == 0);
347 assert(cutil_strcasecmp("BSD", "asd") > 0);
348 assert(cutil_strcasecmp("ASD", "bsd") < 0);
349 assert(cutil_strncasecmp("ASDn", "asd", 3) == 0);
350 assert(cutil_strncasecmp("ASDn", "asd", 4) >0);
351 return 0;
352}
353