test.c
| 1 | #include <cutils/cutils.h> |
| 2 | #include <stdio.h> |
| 3 | #include <assert.h> |
| 4 | |
| 5 | struct test |
| 6 | { |
| 7 | int a; |
| 8 | int b; |
| 9 | }; |
| 10 | |
| 11 | static 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 | |
| 18 | static 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 | |
| 68 | static void test_string(void) |
| 69 | { |
| 70 | String* string; |
| 71 | String* string2; |
| 72 | char* cstring; |
| 73 | size_t tmp; |
| 74 | int 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 | |
| 165 | static 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 |
| 214 | static 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(&ts2, &ts1); |
| 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(&ts2, &ts1); |
| 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(&ts2, &ts1); |
| 261 | assert(res.tv_sec == 10 && res.tv_nsec == 999999998); |
| 262 | |
| 263 | res = timespec_diff(&ts1, &ts2); |
| 264 | assert(res.tv_sec == 0 && res.tv_nsec == 0); |
| 265 | } |
| 266 | #endif |
| 267 | static void test_ll(void) |
| 268 | { |
| 269 | LinkedList* ll; |
| 270 | struct test my_structa, my_structb, *my_structptr; |
| 271 | int truth; |
| 272 | my_structa.a = 5; |
| 273 | my_structa.b = 5; |
| 274 | my_structb.a = 7; |
| 275 | my_structb.b = 7; |
| 276 | |
| 277 | for(truth = 0; truth<2; truth++) |
| 278 | { |
| 279 | ll = new_ll(truth,truth); |
| 280 | |
| 281 | ll_push(ll, &my_structa); |
| 282 | my_structptr = ll_pop(ll); |
| 283 | assert(my_structptr->a == 5 && my_structptr->b == 5); |
| 284 | |
| 285 | ll_push(ll, &my_structa); |
| 286 | ll_push(ll, &my_structa); |
| 287 | ll_push(ll, &my_structa); |
| 288 | ll_push(ll, &my_structb); |
| 289 | |
| 290 | my_structptr = ll_at(ll, ll->length-1); |
| 291 | assert(my_structptr->a == 7); |
| 292 | |
| 293 | ll_swap(ll, 0, ll->length-1); |
| 294 | |
| 295 | my_structptr = ll_at(ll, ll->length-1); |
| 296 | assert(my_structptr->a == 5); |
| 297 | |
| 298 | |
| 299 | ll_remove_range(ll, 0, ll->length-1, NULL); |
| 300 | assert(ll->length == 1); |
| 301 | |
| 302 | delete_ll(ll, NULL); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | static void test_bitfuncs(void) |
| 307 | { |
| 308 | byte tmp = 0; |
| 309 | uintmax_t tmp2 = UINTMAX_MAX; |
| 310 | |
| 311 | setbit(tmp, 0); |
| 312 | assert(tmp == 1); |
| 313 | setbitc(tmp, 1, byte); |
| 314 | assert(tmp == 3); |
| 315 | setbit(tmp, 2); |
| 316 | assert(tmp == 7); |
| 317 | |
| 318 | clearbit(tmp2, numbits(uintmax_t)-1); |
| 319 | assert(tmp2 == UINTMAX_MAX/2); |
| 320 | |
| 321 | assert(getbit(tmp, 0) == 1); |
| 322 | assert(getbit(tmp, 5) == 0); |
| 323 | } |
| 324 | |
| 325 | int main(int argc, char** argv) |
| 326 | { |
| 327 | /* memswap tests */ |
| 328 | int a = 5; |
| 329 | int b = 2; |
| 330 | Bytearray* bt; |
| 331 | void* tmp = malloc(sizeof(int)); |
| 332 | |
| 333 | memswap(&a, &b, sizeof(int)); |
| 334 | assert(a == 2 && b == 5); |
| 335 | #if __STDC_VERSION__ >= 199901L |
| 336 | memqswap_stack(&a, &b, sizeof(int)); |
| 337 | assert(a == 5 && b == 2); |
| 338 | memqswap(&a, &b, tmp, sizeof(int)); |
| 339 | assert(a == 2 && b == 5); |
| 340 | #else |
| 341 | memqswap(&a, &b, tmp, sizeof(int)); |
| 342 | assert(a == 5 && b == 2); |
| 343 | #endif |
| 344 | free(tmp); |
| 345 | /* memswap tests */ |
| 346 | |
| 347 | test_vector(); |
| 348 | test_string(); |
| 349 | test_bytearray(); |
| 350 | #if __STDC_VERSION__ >= 201112L |
| 351 | test_timespec(); |
| 352 | #endif |
| 353 | test_ll(); |
| 354 | test_bitfuncs(); |
| 355 | |
| 356 | /* misc tests */ |
| 357 | sleep_ms(1000); |
| 358 | assert(cutil_strcasecmp("ASD", "aSd") == 0); |
| 359 | assert(cutil_strcasecmp("BSD", "asd") > 0); |
| 360 | assert(cutil_strcasecmp("ASD", "bsd") < 0); |
| 361 | assert(cutil_strncasecmp("ASDn", "asd", 3) == 0); |
| 362 | assert(cutil_strncasecmp("ASDn", "asd", 4) >0); |
| 363 | |
| 364 | |
| 365 | assert(ipow(2,32) == 4294967296); |
| 366 | assert(isqrt(123123) == 350); |
| 367 | bt = primesieve(100000000); |
| 368 | assert(*(uintmax_t*)bytearray_at(bt, 312312) == 4444697); |
| 369 | |
| 370 | delete_bytearray(bt, NULL); |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 |