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