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