51 lines
1.6 KiB
C
51 lines
1.6 KiB
C
#include "util.h"
|
|
#include "unit.h"
|
|
|
|
#define TEST_LEN 32768
|
|
|
|
void check_arr(int check[], size_t check_len) {
|
|
int_dynarr_t dyn = dynarr_initi(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);
|
|
}
|
|
}
|
|
|
|
int tests_dynarr_remove(int argc, char **argv) {
|
|
int_dynarr_t randdel = dynarr_initi(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_destroy(randdel);
|
|
return 0;
|
|
}
|