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 | Vector* vec; |
| 76 | |
| 77 | for(truth = false; truth == false || truth == true; truth++) |
| 78 | { |
| 79 | string = new_string(truth); |
| 80 | |
| 81 | string_push(string, 'a'); |
| 82 | string_push(string, 'b'); |
| 83 | string_push(string, 'c'); |
| 84 | string_push(string, 'd'); |
| 85 | assert(string->length == 4); |
| 86 | |
| 87 | cstring = to_cstring(string); |
| 88 | assert(strcmp(cstring, "abcd") == 0); |
| 89 | free(cstring); |
| 90 | |
| 91 | string_remove(string, 3); |
| 92 | cstring = to_cstring(string); |
| 93 | assert(strcmp(cstring, "abc") == 0); |
| 94 | free(cstring); |
| 95 | assert(string->length == 3); |
| 96 | |
| 97 | string_insert(string, 0,'a'); |
| 98 | cstring = to_cstring(string); |
| 99 | assert(strcmp(cstring, "aabc") == 0); |
| 100 | free(cstring); |
| 101 | string2 = from_cstring("xyz", truth); |
| 102 | |
| 103 | string_concat(string,string2); |
| 104 | cstring = to_cstring(string); |
| 105 | assert(strcmp(cstring, "aabcxyz") == 0); |
| 106 | free(cstring); |
| 107 | |
| 108 | delete_string(string2); |
| 109 | delete_string(string); |
| 110 | |
| 111 | string = from_cstring("abababcd", truth); |
| 112 | string2 = from_cstring("ababcd", truth); |
| 113 | assert(string_find_str(string, string2, &tmp)); |
| 114 | assert(tmp == 2); |
| 115 | delete_string(string2); |
| 116 | delete_string(string); |
| 117 | |
| 118 | string = from_cstring("abab", truth); |
| 119 | string2 = from_cstring("abc", truth); |
| 120 | assert(!string_find_str(string, string2, &tmp)); |
| 121 | delete_string(string2); |
| 122 | delete_string(string); |
| 123 | |
| 124 | string = from_cstring("abababcd", truth); |
| 125 | string2 = from_cstring("ab", truth); |
| 126 | assert(string_find_str(string, string2, &tmp)); |
| 127 | assert(tmp == 0); |
| 128 | delete_string(string2); |
| 129 | delete_string(string); |
| 130 | |
| 131 | string = from_cstring("ABC ABCDAB ABCDABCDABDE", truth); |
| 132 | string2 = from_cstring("ABCDABD", truth); |
| 133 | assert(string_find_str(string, string2, &tmp)); |
| 134 | assert(tmp == 15); |
| 135 | delete_string(string2); |
| 136 | delete_string(string); |
| 137 | |
| 138 | string = from_cstring("asdasdasdasdp", truth); |
| 139 | string2 = from_cstring("p", truth); |
| 140 | assert(string_find_str(string, string2, &tmp)); |
| 141 | assert(tmp == 12); |
| 142 | delete_string(string2); |
| 143 | delete_string(string); |
| 144 | |
| 145 | string = from_cstring("asdpsdasdasdpa", truth); |
| 146 | string2 = from_cstring("pa", truth); |
| 147 | assert(string_find_str(string, string2, &tmp)); |
| 148 | assert(tmp == 12); |
| 149 | delete_string(string2); |
| 150 | delete_string(string); |
| 151 | |
| 152 | string = from_cstring("abcd", truth); |
| 153 | assert(string->length == 4); |
| 154 | string_remove_range(string, 0, string->length); |
| 155 | assert(string->length == 0); |
| 156 | delete_string(string); |
| 157 | |
| 158 | string = from_cstring("bba", truth); |
| 159 | string_strip(string, 'a'); |
| 160 | assert(string->length == 2); |
| 161 | |
| 162 | cstring = to_cstring_del(string); |
| 163 | free(cstring); |
| 164 | |
| 165 | |
| 166 | string = from_cstring("asdyx782", truth); |
| 167 | string2 = from_cstring("asdyx782", truth); |
| 168 | assert(string_cmp(string, string2) == 0); |
| 169 | delete_string(string2); |
| 170 | delete_string(string); |
| 171 | } |
| 172 | |
| 173 | string = from_cstring("asdyx782", true); |
| 174 | string_push(string, 'a'); |
| 175 | string_remove(string, 2); |
| 176 | assert(strcmp(string->chars, "asyx782a") == 0); |
| 177 | string_remove_range(string, 1, 7); |
| 178 | assert(strcmp(string->chars, "a") == 0); |
| 179 | assert(string_pop(string) == 'a'); |
| 180 | string2 = from_cstring("asdasdas", false); |
| 181 | string_concat(string, string2); |
| 182 | assert(strcmp(string->chars, "asdasdas") == 0); |
| 183 | |
| 184 | delete_string(string2); |
| 185 | string2 = from_cstring("koasä_ #", true); |
| 186 | string_move(string, string2); |
| 187 | assert(strcmp(string->chars, "koasä_ #") == 0); |
| 188 | |
| 189 | delete_string(string); |
| 190 | |
| 191 | string = from_cstring_reuse("asdasdas1", strlen("asdasdas1"), false); |
| 192 | string2 = from_cstring_reuse("asdasdas", strlen("asdasdas"), false); |
| 193 | assert(string_cmp(string, string2) > 0); |
| 194 | free(string); |
| 195 | free(string2); |
| 196 | |
| 197 | string = from_cstring(" -hi1 hi2 hello3 ; hi4!-!hi5- ", false); |
| 198 | vec = string_split(string, " ;-", true); |
| 199 | assert(vec->length == 5); |
| 200 | assert(string_cmp_cstr(vector_at(vec,0), "hi1") == 0); |
| 201 | assert(string_cmp_cstr(vector_at(vec,1), "hi2") == 0); |
| 202 | assert(string_cmp_cstr(vector_at(vec,2), "hello3") == 0); |
| 203 | assert(string_cmp_cstr(vector_at(vec,3), "hi4!") == 0); |
| 204 | assert(string_cmp_cstr(vector_at(vec,4), "!hi5") == 0); |
| 205 | |
| 206 | delete_string(string); |
| 207 | delete_vector(vec, delete_string); |
| 208 | |
| 209 | } |
| 210 | |
| 211 | static void test_bytearray(void) |
| 212 | { |
| 213 | Bytearray* bt; |
| 214 | char* tmpchar; |
| 215 | |
| 216 | bt = new_bytearray(1); |
| 217 | bytearray_push(bt, "a"); |
| 218 | bytearray_push(bt, "b"); |
| 219 | bytearray_push(bt, "c"); |
| 220 | bytearray_push(bt, "d"); |
| 221 | bytearray_push(bt, "\0"); |
| 222 | |
| 223 | assert(strcmp((char*)bt->items, "abcd") == 0); |
| 224 | |
| 225 | tmpchar = bytearray_pop(bt, NULL); |
| 226 | assert(*tmpchar == '\0'); |
| 227 | free(tmpchar); |
| 228 | |
| 229 | tmpchar = bytearray_pop(bt, NULL); |
| 230 | assert(*tmpchar == 'd'); |
| 231 | free(tmpchar); |
| 232 | |
| 233 | tmpchar = bytearray_pop(bt, NULL); |
| 234 | assert(*tmpchar == 'c'); |
| 235 | free(tmpchar); |
| 236 | |
| 237 | tmpchar = malloc(1); |
| 238 | bytearray_pop(bt, tmpchar); |
| 239 | assert(*tmpchar == 'b'); |
| 240 | |
| 241 | bytearray_pop(bt, tmpchar); |
| 242 | assert(*tmpchar == 'a'); |
| 243 | free(tmpchar); |
| 244 | |
| 245 | bytearray_push(bt, "a"); |
| 246 | tmpchar = bytearray_pop(bt, NULL); |
| 247 | assert(*tmpchar == 'a'); |
| 248 | free(tmpchar); |
| 249 | |
| 250 | bytearray_push(bt, "a"); |
| 251 | bytearray_push(bt, "a"); |
| 252 | bytearray_push(bt, "a"); |
| 253 | assert(bt->length == 3); |
| 254 | bytearray_remove_range(bt, 0, bt->length, NULL); |
| 255 | assert(bt->length == 0); |
| 256 | |
| 257 | delete_bytearray(bt, NULL); |
| 258 | } |
| 259 | #if __STDC_VERSION__ >= 201112L |
| 260 | static void test_timespec(void) |
| 261 | { |
| 262 | struct timespec ts1 = {10, 10}; |
| 263 | struct timespec ts2 = {10, 10}; |
| 264 | struct timespec res; |
| 265 | |
| 266 | res = timespec_add(&ts1, &ts2); |
| 267 | assert(res.tv_sec == 20 && res.tv_nsec == 20); |
| 268 | |
| 269 | ts1.tv_sec = 10; |
| 270 | ts1.tv_nsec = 999999999; |
| 271 | ts2.tv_sec = 0; |
| 272 | ts2.tv_nsec = 1; |
| 273 | |
| 274 | res = timespec_add(&ts1, &ts2); |
| 275 | assert(res.tv_sec == 11 && res.tv_nsec == 0); |
| 276 | |
| 277 | ts1.tv_sec = 0; |
| 278 | ts1.tv_nsec = 1; |
| 279 | ts2.tv_sec = 10; |
| 280 | ts2.tv_nsec = 999999999; |
| 281 | |
| 282 | res = timespec_add(&ts1, &ts2); |
| 283 | assert(res.tv_sec == 11 && res.tv_nsec == 0); |
| 284 | |
| 285 | ts1.tv_sec = 0; |
| 286 | ts1.tv_nsec = 1; |
| 287 | ts2.tv_sec = 10; |
| 288 | ts2.tv_nsec = 999999999; |
| 289 | |
| 290 | res = timespec_diff(&ts2, &ts1); |
| 291 | assert(res.tv_sec == 10 && res.tv_nsec == 999999998); |
| 292 | |
| 293 | ts1.tv_sec = 0; |
| 294 | ts1.tv_nsec = 999999999; |
| 295 | ts2.tv_sec = 10; |
| 296 | ts2.tv_nsec = 1; |
| 297 | |
| 298 | res = timespec_diff(&ts2, &ts1); |
| 299 | assert(res.tv_sec == 9 && res.tv_nsec == 2); |
| 300 | |
| 301 | ts1.tv_sec = 0; |
| 302 | ts1.tv_nsec = 1; |
| 303 | ts2.tv_sec = 10; |
| 304 | ts2.tv_nsec = 999999999; |
| 305 | |
| 306 | res = timespec_diff(&ts2, &ts1); |
| 307 | assert(res.tv_sec == 10 && res.tv_nsec == 999999998); |
| 308 | |
| 309 | res = timespec_diff(&ts1, &ts2); |
| 310 | assert(res.tv_sec == 0 && res.tv_nsec == 0); |
| 311 | } |
| 312 | #endif |
| 313 | static void test_ll(void) |
| 314 | { |
| 315 | LinkedList* ll; |
| 316 | struct test my_structa, my_structb, *my_structptr; |
| 317 | int truth; |
| 318 | my_structa.a = 5; |
| 319 | my_structa.b = 5; |
| 320 | my_structb.a = 7; |
| 321 | my_structb.b = 7; |
| 322 | |
| 323 | for(truth = 0; truth<2; truth++) |
| 324 | { |
| 325 | ll = new_ll(truth,truth); |
| 326 | |
| 327 | ll_push(ll, &my_structa); |
| 328 | my_structptr = ll_pop(ll); |
| 329 | assert(my_structptr->a == 5 && my_structptr->b == 5); |
| 330 | |
| 331 | ll_push(ll, &my_structa); |
| 332 | ll_push(ll, &my_structa); |
| 333 | ll_push(ll, &my_structa); |
| 334 | ll_push(ll, &my_structb); |
| 335 | |
| 336 | my_structptr = ll_at(ll, ll->length-1); |
| 337 | assert(my_structptr->a == 7); |
| 338 | |
| 339 | ll_swap(ll, 0, ll->length-1); |
| 340 | |
| 341 | my_structptr = ll_at(ll, ll->length-1); |
| 342 | assert(my_structptr->a == 5); |
| 343 | |
| 344 | |
| 345 | ll_remove_range(ll, 0, ll->length-1, NULL); |
| 346 | assert(ll->length == 1); |
| 347 | |
| 348 | delete_ll(ll, NULL); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | static void test_bitfuncs(void) |
| 353 | { |
| 354 | byte tmp = 0; |
| 355 | uintmax_t tmp2 = UINTMAX_MAX; |
| 356 | |
| 357 | setbit(tmp, 0); |
| 358 | assert(tmp == 1); |
| 359 | setbitc(tmp, 1, byte); |
| 360 | assert(tmp == 3); |
| 361 | setbit(tmp, 2); |
| 362 | assert(tmp == 7); |
| 363 | |
| 364 | clearbit(tmp2, numbits(uintmax_t)-1); |
| 365 | assert(tmp2 == UINTMAX_MAX/2); |
| 366 | |
| 367 | assert(getbit(tmp, 0) == 1); |
| 368 | assert(getbit(tmp, 5) == 0); |
| 369 | } |
| 370 | |
| 371 | int main(int argc, char** argv) |
| 372 | { |
| 373 | /* memswap tests */ |
| 374 | int a = 5; |
| 375 | int b = 2; |
| 376 | Bytearray* bt; |
| 377 | void* tmp = malloc(sizeof(int)); |
| 378 | |
| 379 | memswap(&a, &b, sizeof(int)); |
| 380 | assert(a == 2 && b == 5); |
| 381 | #if __STDC_VERSION__ >= 199901L |
| 382 | memqswap_stack(&a, &b, sizeof(int)); |
| 383 | assert(a == 5 && b == 2); |
| 384 | memqswap(&a, &b, tmp, sizeof(int)); |
| 385 | assert(a == 2 && b == 5); |
| 386 | #else |
| 387 | memqswap(&a, &b, tmp, sizeof(int)); |
| 388 | assert(a == 5 && b == 2); |
| 389 | #endif |
| 390 | free(tmp); |
| 391 | /* memswap tests */ |
| 392 | |
| 393 | test_vector(); |
| 394 | test_string(); |
| 395 | test_bytearray(); |
| 396 | #if __STDC_VERSION__ >= 201112L |
| 397 | test_timespec(); |
| 398 | #endif |
| 399 | test_ll(); |
| 400 | test_bitfuncs(); |
| 401 | |
| 402 | /* misc tests */ |
| 403 | sleep_ms(250); |
| 404 | assert(cutil_strcasecmp("ASD", "aSd") == 0); |
| 405 | assert(cutil_strcasecmp("BSD", "asd") > 0); |
| 406 | assert(cutil_strcasecmp("ASD", "bsd") < 0); |
| 407 | assert(cutil_strncasecmp("ASDn", "asd", 3) == 0); |
| 408 | assert(cutil_strncasecmp("ASDn", "asd", 4) >0); |
| 409 | |
| 410 | |
| 411 | assert(ipow(2,32) == 4294967296); |
| 412 | assert(isqrt(123123) == 350); |
| 413 | bt = primesieve(100000000); |
| 414 | assert(*(uintmax_t*)bytearray_at(bt, 312312) == 4444697); |
| 415 | |
| 416 | delete_bytearray(bt, NULL); |
| 417 | puts(endianess_strings[check_endianess()]); |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | /* TODO: split up test files */ |
| 422 |