#include #include "util.h" #include "unit.h" #define TEST_LEN 32768 void get_test(void) { int_dynarr_t arr = DYNARR_INIT(int_dynarr_t); for (int i = 0; i < 100; i++) DYNARR_PUSH(arr, 0); chi_assert("get(5) == arr.data + 5", DYNARR_GET(arr, 5) == arr.data + 5); } void extensions_test(void) { int *cmp = xmalloc(TEST_LEN * sizeof(int)); for (size_t i = 0; i < TEST_LEN; i++) cmp[i] = rand(); int_dynarr_t extend = DYNARR_INIT(int_dynarr_t); DYNARR_EXTEND_FIXED(extend, cmp, TEST_LEN); chi_assert("extend.data != cmp", memcmp(extend.data, cmp, TEST_LEN * sizeof(int)) == 0); chi_assert("extend.len != TEST_LEN", extend.len == TEST_LEN); DYNARR_DEINIT(extend); int_dynarr_t push = DYNARR_INIT(int_dynarr_t); for (size_t i = 0; i < TEST_LEN; i++) DYNARR_PUSH(push, cmp[i]); chi_assert("push.data != cmp", memcmp(push.data, cmp, TEST_LEN * sizeof(int)) == 0); chi_assert("push.len != TEST_LEN", push.len == TEST_LEN); DYNARR_DEINIT(push); int_dynarr_t both = DYNARR_INIT(int_dynarr_t); DYNARR_EXTEND_FIXED(both, cmp, TEST_LEN / 2); for (size_t i = TEST_LEN / 2; i < TEST_LEN; i++) DYNARR_PUSH(both, cmp[i]); chi_assert("both.data != cmp", memcmp(both.data, cmp, TEST_LEN * sizeof(int)) == 0); chi_assert("both.len != TEST_LEN", both.len == TEST_LEN); DYNARR_DEINIT(both); xfree(cmp); } void insert_test(void) { size_t_dynarr_t increm = DYNARR_INIT(size_t_dynarr_t); for (size_t i = 0; i < TEST_LEN; i += 2) DYNARR_PUSH(increm, i); for (size_t i = 1; i < TEST_LEN; i += 2) DYNARR_INSERT(increm, i, i); chi_assert("arr.len == TEST_LEN", increm.len == TEST_LEN); for (size_t i = 0; i < TEST_LEN; i++) chi_assert("arr[i] == i", *DYNARR_GET(increm, i) == i); DYNARR_DEINIT(increm); int_dynarr_t randins = DYNARR_INIT(int_dynarr_t); long long parity = 0, check = 0; for (size_t i = 0; i < TEST_LEN; i++) { int gen = rand() % 10; parity += gen; DYNARR_INSERT(randins, (rand() % (randins.len + 1)), gen); } chi_assert("arr.len == TEST_LEN", randins.len == TEST_LEN); for (size_t i = 0; i < TEST_LEN; i++) check += *DYNARR_GET(randins, i); chi_assert("parity == check", parity == check); DYNARR_DEINIT(randins); } void check_arr(int check[], size_t check_len) { int_dynarr_t dyn = DYNARR_INIT(int_dynarr_t); DYNARR_EXTEND_FIXED(dyn, check, check_len); chi_assert("dyn.len == check_len", dyn.len == check_len); chi_assert("dyn.data == check", memcmp(dyn.data, check, check_len * sizeof(int)) == 0); for (size_t i = 0; i < dyn.len; i++) { size_t ind = rand() % check_len; DYNARR_REMOVE(dyn, ind); memmove(check + ind, check + ind + 1, (check_len - ind - 1) * sizeof(int)); check_len -= 1; chi_assert("dyn.len == check_len (modified)", dyn.len == check_len); chi_assert("dyn.data == check (modified)", memcmp(dyn.data, check, check_len * sizeof(int)) == 0); } } void remove_test(void) { int_dynarr_t randdel = DYNARR_INIT(int_dynarr_t); long long parity = 0; for (size_t i = 0; i < TEST_LEN; i++) { int gen = rand() % 10; parity += gen; DYNARR_PUSH(randdel, gen); } long long check = parity; for (size_t i = 0; i < TEST_LEN; i++) { size_t ind = rand() % randdel.len; check -= *DYNARR_GET(randdel, ind); DYNARR_REMOVE(randdel, ind); chi_assert("randdel.len == TEST_LEN - i - 1", randdel.len == TEST_LEN - i - 1); } chi_assert("check == 0", check == 0); int *c = xmalloc(TEST_LEN * sizeof(int)); for (int i = 0; i < TEST_LEN; i++) c[i] = rand(); check_arr(c, TEST_LEN); xfree(c); DYNARR_DEINIT(randdel); } int main(int argc, char **argv) { if (argc != 2) return 1; if (strcmp(argv[1], "push") == 0) { push_test(); } else if (strcmp(argv[1], "get") == 0) { get_test(); } else if (strcmp(argv[1], "extensions") == 0) { extensions_test(); } else if (strcmp(argv[1], "insert") == 0) { insert_test(); } else if (strcmp(argv[1], "remove") == 0) { remove_test(); } }