X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fhtable%2Ftest%2Frun-copy.c;fp=ccan%2Fhtable%2Ftest%2Frun-copy.c;h=02d5bbe46856daef1689c784152bee6c218ea806;hb=f359fde1b7c3ca60faebd4df710bf30a68784e27;hp=0000000000000000000000000000000000000000;hpb=7b56762984ce66f38728b542ca347bcb6fe3e981;p=ccan diff --git a/ccan/htable/test/run-copy.c b/ccan/htable/test/run-copy.c new file mode 100644 index 00000000..02d5bbe4 --- /dev/null +++ b/ccan/htable/test/run-copy.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include + +#define NUM_VALS 512 + +static size_t hash(const void *elem, void *unused) +{ + size_t h = *(uint64_t *)elem / 2; + return h; +} + +static bool cmp(const void *candidate, void *ptr) +{ + return *(const uint64_t *)candidate == *(const uint64_t *)ptr; +} + +int main(int argc, char *argv[]) +{ + struct htable ht, ht2; + uint64_t val[NUM_VALS], i; + + plan_tests((NUM_VALS) * 3); + for (i = 0; i < NUM_VALS; i++) + val[i] = i; + + htable_init(&ht, hash, NULL); + for (i = 0; i < NUM_VALS; i++) { + ok1(ht.max >= i); + ok1(ht.max <= i * 2); + htable_add(&ht, hash(&val[i], NULL), &val[i]); + } + + htable_copy(&ht2, &ht); + htable_clear(&ht); + + for (i = 0; i < NUM_VALS; i++) + ok1(htable_get(&ht2, hash(&i, NULL), cmp, &i) == &val[i]); + htable_clear(&ht2); + + return exit_status(); +}