X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fhtable%2Ftest%2Frun-zero-hash-first-entry.c;fp=ccan%2Fhtable%2Ftest%2Frun-zero-hash-first-entry.c;h=fdd18569e7cc516f2d5b7fb5bbc25a7d0a35bacf;hp=0000000000000000000000000000000000000000;hb=0b93bd102aad6b61f1e569fb12aabc6352a1d7cd;hpb=a85a809bb17af6b6cf6fa31b300c6622f64ee700 diff --git a/ccan/htable/test/run-zero-hash-first-entry.c b/ccan/htable/test/run-zero-hash-first-entry.c new file mode 100644 index 00000000..fdd18569 --- /dev/null +++ b/ccan/htable/test/run-zero-hash-first-entry.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include + +struct data { + size_t key; +}; + +/* Hash is simply key itself. */ +static size_t hash(const void *e, void *unused) +{ + struct data *d = (struct data *)e; + + return d->key; +} + +static bool eq(const void *e, void *k) +{ + struct data *d = (struct data *)e; + size_t *key = (size_t *)k; + + return (d->key == *key); +} + +int main(void) +{ + struct htable table; + struct data *d0, *d1; + + plan_tests(6); + + d1 = malloc(sizeof(struct data)); + d1->key = 1; + d0 = malloc(sizeof(struct data)); + d0->key = 0; + + htable_init(&table, hash, NULL); + + htable_add(&table, d0->key, d0); + htable_add(&table, d1->key, d1); + + ok1(table.elems == 2); + ok1(htable_get(&table, 1, eq, &d1->key) == d1); + ok1(htable_get(&table, 0, eq, &d0->key) == d0); + htable_clear(&table); + + /* Now add in reverse order, should still be OK. */ + htable_add(&table, d1->key, d1); + htable_add(&table, d0->key, d0); + + ok1(table.elems == 2); + ok1(htable_get(&table, 1, eq, &d1->key) == d1); + ok1(htable_get(&table, 0, eq, &d0->key) == d0); + htable_clear(&table); + + free(d0); + free(d1); + return exit_status(); +} +