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 test1(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 test2(void)
69{
70 String* string;
71 String* string2;
72 char* cstring;
73
74 string = new_string();
75
76 string_push(string, 'a');
77 string_push(string, 'b');
78 string_push(string, 'c');
79 string_push(string, 'd');
80 assert(string->length == 4);
81
82 cstring = to_cstring(string);
83 assert(strcmp(cstring, "abcd") == 0);
84 free(cstring);
85
86 string_remove(string, 3);
87 cstring = to_cstring(string);
88 assert(strcmp(cstring, "abc") == 0);
89 free(cstring);
90 assert(string->length == 3);
91
92 string_insert(string, 0,'a');
93 cstring = to_cstring(string);
94 assert(strcmp(cstring, "aabc") == 0);
95 free(cstring);
96
97 string2 = from_cstring("xyz");
98
99 string_concat(string,string2);
100 cstring = to_cstring(string);
101 assert(strcmp(cstring, "aabcxyz") == 0);
102 free(cstring);
103
104 delete_string(string2);
105 delete_string(string);
106}
107
108static void test3(void)
109{
110 Bytearray* bt;
111 void* localarray;
112 struct test my_struct;
113 struct test* my_structptr;
114 char* tmpchar;
115
116 /*test pushing and popping with default constructor and simple values*/
117 bt = new_bytearray(1);
118 bytearray_push(bt, "a");
119 bytearray_push(bt, "b");
120 bytearray_push(bt, "c");
121 bytearray_push(bt, "d");
122 bytearray_push(bt, "\0");
123
124 assert(strcmp(bt->items, "abcd") == 0);
125
126 tmpchar = bytearray_pop(bt);
127 assert(*tmpchar == '\0');
128 free(tmpchar);
129
130 tmpchar = bytearray_pop(bt);
131 assert(*tmpchar == 'd');
132 free(tmpchar);
133
134 tmpchar = bytearray_pop(bt);
135 assert(*tmpchar == 'c');
136 free(tmpchar);
137
138 tmpchar = bytearray_pop(bt);
139 assert(*tmpchar == 'b');
140 free(tmpchar);
141
142 tmpchar = bytearray_pop(bt);
143 assert(*tmpchar == 'a');
144 free(tmpchar);
145
146 tmpchar = bytearray_pop(bt);
147 assert(tmpchar == NULL);
148 free(tmpchar);
149
150 bytearray_push(bt, "a");
151 tmpchar = bytearray_pop(bt);
152 assert(*tmpchar == 'a');
153 free(tmpchar);
154
155 delete_bytearray(bt);
156
157 /*test pushing and popping with self allocated array and fixed size*/
158 localarray = malloc(3*sizeof(struct test));
159 my_struct.a = 0;
160 my_struct.b = 0;
161
162 bt = new_bytearray_ext(3, sizeof(struct test), localarray, NULL, BT_FREE_ARRAY | BT_FIXED | BT_FREE_STRUCT);
163 bytearray_push(bt, &my_struct);
164 my_struct.a = 1;
165 my_struct.b = 1;
166 bytearray_push(bt, &my_struct);
167
168 my_struct.a = 2;
169 my_struct.b = 2;
170 bytearray_push(bt, &my_struct);
171
172 my_struct.a = 3;
173 my_struct.b = 3;
174 bytearray_push(bt, &my_struct);
175
176 my_structptr = bytearray_pop(bt);
177
178 my_struct.a = 2;
179 my_struct.b = 2;
180 assert(my_structptr->a == my_struct.a);
181 assert(my_structptr->b == my_struct.b);
182 free(my_structptr);
183
184
185
186 delete_bytearray(bt);
187
188}
189
190static void test4(void)
191{
192 struct timespec ts1 = {10, 10};
193 struct timespec ts2 = {10, 10};
194 struct timespec res;
195
196 res = timespec_add(&ts1, &ts2);
197 assert(res.tv_sec == 20 && res.tv_nsec == 20);
198
199 ts1.tv_sec = 10;
200 ts1.tv_nsec = 999999999;
201 ts2.tv_sec = 0;
202 ts2.tv_nsec = 1;
203
204 res = timespec_add(&ts1, &ts2);
205 assert(res.tv_sec == 11 && res.tv_nsec == 0);
206
207 ts1.tv_sec = 0;
208 ts1.tv_nsec = 1;
209 ts2.tv_sec = 10;
210 ts2.tv_nsec = 999999999;
211
212 res = timespec_add(&ts1, &ts2);
213 assert(res.tv_sec == 11 && res.tv_nsec == 0);
214
215 ts1.tv_sec = 0;
216 ts1.tv_nsec = 1;
217 ts2.tv_sec = 10;
218 ts2.tv_nsec = 999999999;
219
220 res = timespec_diff(&ts1, &ts2);
221 assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
222
223 ts1.tv_sec = 0;
224 ts1.tv_nsec = 999999999;
225 ts2.tv_sec = 10;
226 ts2.tv_nsec = 1;
227
228 res = timespec_diff(&ts1, &ts2);
229 assert(res.tv_sec == 9 && res.tv_nsec == 2);
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_diff(&ts1, &ts2);
237 assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
238}
239
240int main(int argc, char** argv)
241{
242 test1();
243 test2();
244 test3();
245 test4();
246 sleep_ms(1000);
247 return 0;
248}
249