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 string = from_cstring_reuse("asdasdas1", strlen("asdasdas1"), false);
158 string2 = from_cstring_reuse("asdasdas", strlen("asdasdas"), false);
159 assert(string_cmp(string, string2) > 0);
160 free(string);
161 free(string2);
162
163}
164
165static void test_bytearray(void)
166{
167 Bytearray* bt;
168 char* tmpchar;
169
170 bt = new_bytearray(1);
171 bytearray_push(bt, "a");
172 bytearray_push(bt, "b");
173 bytearray_push(bt, "c");
174 bytearray_push(bt, "d");
175 bytearray_push(bt, "\0");
176
177 assert(strcmp((char*)bt->items, "abcd") == 0);
178
179 tmpchar = bytearray_pop(bt, NULL);
180 assert(*tmpchar == '\0');
181 free(tmpchar);
182
183 tmpchar = bytearray_pop(bt, NULL);
184 assert(*tmpchar == 'd');
185 free(tmpchar);
186
187 tmpchar = bytearray_pop(bt, NULL);
188 assert(*tmpchar == 'c');
189 free(tmpchar);
190
191 tmpchar = malloc(1);
192 bytearray_pop(bt, tmpchar);
193 assert(*tmpchar == 'b');
194
195 bytearray_pop(bt, tmpchar);
196 assert(*tmpchar == 'a');
197 free(tmpchar);
198
199 bytearray_push(bt, "a");
200 tmpchar = bytearray_pop(bt, NULL);
201 assert(*tmpchar == 'a');
202 free(tmpchar);
203
204 bytearray_push(bt, "a");
205 bytearray_push(bt, "a");
206 bytearray_push(bt, "a");
207 assert(bt->length == 3);
208 bytearray_remove_range(bt, 0, bt->length, NULL);
209 assert(bt->length == 0);
210
211 delete_bytearray(bt, NULL);
212}
213#if __STDC_VERSION__ >= 201112L
214static void test_timespec(void)
215{
216 struct timespec ts1 = {10, 10};
217 struct timespec ts2 = {10, 10};
218 struct timespec res;
219
220 res = timespec_add(&ts1, &ts2);
221 assert(res.tv_sec == 20 && res.tv_nsec == 20);
222
223 ts1.tv_sec = 10;
224 ts1.tv_nsec = 999999999;
225 ts2.tv_sec = 0;
226 ts2.tv_nsec = 1;
227
228 res = timespec_add(&ts1, &ts2);
229 assert(res.tv_sec == 11 && res.tv_nsec == 0);
230
231 ts1.tv_sec = 0;
232 ts1.tv_nsec = 1;
233 ts2.tv_sec = 10;
234 ts2.tv_nsec = 999999999;
235
236 res = timespec_add(&ts1, &ts2);
237 assert(res.tv_sec == 11 && res.tv_nsec == 0);
238
239 ts1.tv_sec = 0;
240 ts1.tv_nsec = 1;
241 ts2.tv_sec = 10;
242 ts2.tv_nsec = 999999999;
243
244 res = timespec_diff(&ts1, &ts2);
245 assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
246
247 ts1.tv_sec = 0;
248 ts1.tv_nsec = 999999999;
249 ts2.tv_sec = 10;
250 ts2.tv_nsec = 1;
251
252 res = timespec_diff(&ts1, &ts2);
253 assert(res.tv_sec == 9 && res.tv_nsec == 2);
254
255 ts1.tv_sec = 0;
256 ts1.tv_nsec = 1;
257 ts2.tv_sec = 10;
258 ts2.tv_nsec = 999999999;
259
260 res = timespec_diff(&ts1, &ts2);
261 assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
262}
263#endif
264static void test_ll(void)
265{
266 LinkedList* ll;
267 struct test my_structa, my_structb, *my_structptr;
268 bool truth;
269 my_structa.a = 5;
270 my_structa.b = 5;
271 my_structb.a = 7;
272 my_structb.b = 7;
273
274 for(truth = 0; truth<2; truth++)
275 {
276 ll = new_ll(truth,truth);
277
278 ll_push(ll, &my_structa);
279 my_structptr = ll_pop(ll);
280 assert(my_structptr->a == 5 && my_structptr->b == 5);
281
282 ll_push(ll, &my_structa);
283 ll_push(ll, &my_structa);
284 ll_push(ll, &my_structa);
285 ll_push(ll, &my_structb);
286
287 my_structptr = ll_at(ll, ll->length-1);
288 assert(my_structptr->a == 7);
289
290 ll_swap(ll, 0, ll->length-1);
291
292 my_structptr = ll_at(ll, ll->length-1);
293 assert(my_structptr->a == 5);
294
295
296 ll_remove_range(ll, 0, ll->length-1, NULL);
297 assert(ll->length == 1);
298
299 delete_ll(ll, NULL);
300 }
301}
302
303static void test_bitfuncs(void)
304{
305 byte tmp = 0;
306
307 setbit(byte, tmp, 0);
308 assert(tmp == 1);
309 setbit(byte, tmp, 1);
310 assert(tmp == 3);
311 setbit(byte, tmp, 2);
312 assert(tmp == 7);
313
314 clearbit(byte, tmp, 2);
315 assert(tmp == 3);
316
317 assert(getbit(byte, tmp, 0) == 1);
318 assert(getbit(byte, tmp, 5) == 0);
319}
320
321int main(int argc, char** argv)
322{
323 /* memswap tests */
324 int a = 5;
325 int b = 2;
326 Bytearray* bt;
327 void* tmp = malloc(sizeof(int));
328
329 memswap(&a, &b, sizeof(int));
330 assert(a == 2 && b == 5);
331 #if __STDC_VERSION__ >= 199901L
332 memqswap_stack(&a, &b, sizeof(int));
333 assert(a == 5 && b == 2);
334 memqswap(&a, &b, tmp, sizeof(int));
335 assert(a == 2 && b == 5);
336 #else
337 memqswap(&a, &b, tmp, sizeof(int));
338 assert(a == 5 && b == 2);
339 #endif
340 free(tmp);
341 /* memswap tests */
342
343 test_vector();
344 test_string();
345 test_bytearray();
346 #if __STDC_VERSION__ >= 201112L
347 test_timespec();
348 #endif
349 test_ll();
350 test_bitfuncs();
351
352 /* misc tests */
353 sleep_ms(1000);
354 assert(cutil_strcasecmp("ASD", "aSd") == 0);
355 assert(cutil_strcasecmp("BSD", "asd") > 0);
356 assert(cutil_strcasecmp("ASD", "bsd") < 0);
357 assert(cutil_strncasecmp("ASDn", "asd", 3) == 0);
358 assert(cutil_strncasecmp("ASDn", "asd", 4) >0);
359
360
361 assert(ipow(2,32) == 4294967296);
362 assert(isqrt(123123) == 350);
363 bt = primesieve(100000000);
364 assert(*(uintmax_t*)bytearray_at(bt, 312312) == 4444697);
365
366 delete_bytearray(bt, NULL);
367
368 return 0;
369}
370