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 char* tmpchar;
112
113 bt = new_bytearray(1);
114 bytearray_push(bt, "a");
115 bytearray_push(bt, "b");
116 bytearray_push(bt, "c");
117 bytearray_push(bt, "d");
118 bytearray_push(bt, "\0");
119
120 assert(strcmp(bt->items, "abcd") == 0);
121
122 tmpchar = bytearray_pop(bt, NULL);
123 assert(*tmpchar == '\0');
124 free(tmpchar);
125
126 tmpchar = bytearray_pop(bt, NULL);
127 assert(*tmpchar == 'd');
128 free(tmpchar);
129
130 tmpchar = bytearray_pop(bt, NULL);
131 assert(*tmpchar == 'c');
132 free(tmpchar);
133
134 tmpchar = malloc(1);
135 bytearray_pop(bt, tmpchar);
136 assert(*tmpchar == 'b');
137
138 bytearray_pop(bt, tmpchar);
139 assert(*tmpchar == 'a');
140 free(tmpchar);
141
142 tmpchar = bytearray_pop(bt, NULL);
143 assert(tmpchar == NULL);
144
145 bytearray_push(bt, "a");
146 tmpchar = bytearray_pop(bt, NULL);
147 assert(*tmpchar == 'a');
148 free(tmpchar);
149
150 delete_bytearray(bt, NULL);
151}
152#if __STDC_VERSION__ >= 201112L
153static void test4(void)
154{
155 struct timespec ts1 = {10, 10};
156 struct timespec ts2 = {10, 10};
157 struct timespec res;
158
159 res = timespec_add(&ts1, &ts2);
160 assert(res.tv_sec == 20 && res.tv_nsec == 20);
161
162 ts1.tv_sec = 10;
163 ts1.tv_nsec = 999999999;
164 ts2.tv_sec = 0;
165 ts2.tv_nsec = 1;
166
167 res = timespec_add(&ts1, &ts2);
168 assert(res.tv_sec == 11 && res.tv_nsec == 0);
169
170 ts1.tv_sec = 0;
171 ts1.tv_nsec = 1;
172 ts2.tv_sec = 10;
173 ts2.tv_nsec = 999999999;
174
175 res = timespec_add(&ts1, &ts2);
176 assert(res.tv_sec == 11 && res.tv_nsec == 0);
177
178 ts1.tv_sec = 0;
179 ts1.tv_nsec = 1;
180 ts2.tv_sec = 10;
181 ts2.tv_nsec = 999999999;
182
183 res = timespec_diff(&ts1, &ts2);
184 assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
185
186 ts1.tv_sec = 0;
187 ts1.tv_nsec = 999999999;
188 ts2.tv_sec = 10;
189 ts2.tv_nsec = 1;
190
191 res = timespec_diff(&ts1, &ts2);
192 assert(res.tv_sec == 9 && res.tv_nsec == 2);
193
194 ts1.tv_sec = 0;
195 ts1.tv_nsec = 1;
196 ts2.tv_sec = 10;
197 ts2.tv_nsec = 999999999;
198
199 res = timespec_diff(&ts1, &ts2);
200 assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
201}
202#endif
203
204int main(int argc, char** argv)
205{
206 test1();
207 test2();
208 test3();
209 #if __STDC_VERSION__ >= 201112L
210 test4();
211 #endif
212 sleep_ms(1000);
213 return 0;
214}
215