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 tmp = string_find_str(string, string2);
111 assert(*tmp == 2);
112 free(tmp);
113
114 delete_string(string2);
115 delete_string(string);
116}
117
118static void test_bytearray(void)
119{
120 Bytearray* bt;
121 char* tmpchar;
122
123 bt = new_bytearray(1);
124 bytearray_push(bt, "a");
125 bytearray_push(bt, "b");
126 bytearray_push(bt, "c");
127 bytearray_push(bt, "d");
128 bytearray_push(bt, "\0");
129
130 assert(strcmp(bt->items, "abcd") == 0);
131
132 tmpchar = bytearray_pop(bt, NULL);
133 assert(*tmpchar == '\0');
134 free(tmpchar);
135
136 tmpchar = bytearray_pop(bt, NULL);
137 assert(*tmpchar == 'd');
138 free(tmpchar);
139
140 tmpchar = bytearray_pop(bt, NULL);
141 assert(*tmpchar == 'c');
142 free(tmpchar);
143
144 tmpchar = malloc(1);
145 bytearray_pop(bt, tmpchar);
146 assert(*tmpchar == 'b');
147
148 bytearray_pop(bt, tmpchar);
149 assert(*tmpchar == 'a');
150 free(tmpchar);
151
152 tmpchar = bytearray_pop(bt, NULL);
153 assert(tmpchar == NULL);
154
155 bytearray_push(bt, "a");
156 tmpchar = bytearray_pop(bt, NULL);
157 assert(*tmpchar == 'a');
158 free(tmpchar);
159
160 delete_bytearray(bt, NULL);
161}
162#if __STDC_VERSION__ >= 201112L
163static void test_timespec(void)
164{
165 struct timespec ts1 = {10, 10};
166 struct timespec ts2 = {10, 10};
167 struct timespec res;
168
169 res = timespec_add(&ts1, &ts2);
170 assert(res.tv_sec == 20 && res.tv_nsec == 20);
171
172 ts1.tv_sec = 10;
173 ts1.tv_nsec = 999999999;
174 ts2.tv_sec = 0;
175 ts2.tv_nsec = 1;
176
177 res = timespec_add(&ts1, &ts2);
178 assert(res.tv_sec == 11 && res.tv_nsec == 0);
179
180 ts1.tv_sec = 0;
181 ts1.tv_nsec = 1;
182 ts2.tv_sec = 10;
183 ts2.tv_nsec = 999999999;
184
185 res = timespec_add(&ts1, &ts2);
186 assert(res.tv_sec == 11 && res.tv_nsec == 0);
187
188 ts1.tv_sec = 0;
189 ts1.tv_nsec = 1;
190 ts2.tv_sec = 10;
191 ts2.tv_nsec = 999999999;
192
193 res = timespec_diff(&ts1, &ts2);
194 assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
195
196 ts1.tv_sec = 0;
197 ts1.tv_nsec = 999999999;
198 ts2.tv_sec = 10;
199 ts2.tv_nsec = 1;
200
201 res = timespec_diff(&ts1, &ts2);
202 assert(res.tv_sec == 9 && res.tv_nsec == 2);
203
204 ts1.tv_sec = 0;
205 ts1.tv_nsec = 1;
206 ts2.tv_sec = 10;
207 ts2.tv_nsec = 999999999;
208
209 res = timespec_diff(&ts1, &ts2);
210 assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
211}
212#endif
213static void test_ll(void)
214{
215 LinkedList* ll;
216 struct test my_structa, my_structb, *my_structptr;
217 int truth;
218 my_structa.a = 5;
219 my_structa.b = 5;
220 my_structb.a = 7;
221 my_structb.b = 7;
222
223 for(truth = 0; truth<2; truth++)
224 {
225 ll = new_ll(truth,truth);
226
227 ll_push(ll, &my_structa);
228 my_structptr = ll_pop(ll);
229 assert(my_structptr->a == 5 && my_structptr->b == 5);
230
231 ll_push(ll, &my_structa);
232 ll_push(ll, &my_structa);
233 ll_push(ll, &my_structa);
234 ll_push(ll, &my_structb);
235
236 my_structptr = ll_at(ll, ll->length-1);
237 assert(my_structptr->a == 7);
238
239 ll_swap(ll, 0, ll->length-1);
240
241 my_structptr = ll_at(ll, ll->length-1);
242 assert(my_structptr->a == 5);
243
244
245 ll_remove_range(ll, 0, ll->length-1, NULL);
246
247 delete_ll(ll, NULL);
248 }
249
250}
251
252int main(int argc, char** argv)
253{
254 int a = 5;
255 int b = 2;
256 memswap(&a, &b, sizeof(int));
257 assert(a == 2 && b == 5);
258
259 test_vector();
260 test_string();
261 test_bytearray();
262 #if __STDC_VERSION__ >= 201112L
263 test_timespec();
264 #endif
265 test_ll();
266
267 sleep_ms(1000);
268 return 0;
269}
270