X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fhtable%2Ftest%2Frun-type.c;h=f97e7270b2fa65afd83e75cc9fcf33220d06d357;hb=4ad5144790a12523f8a7c24c469a34907b6942a6;hp=02dac29e10012f9f83f8451af73bfd05b545f10a;hpb=23e4603462692031a29ea4b2a882f957de4f2922;p=ccan diff --git a/ccan/htable/test/run-type.c b/ccan/htable/test/run-type.c index 02dac29e..f97e7270 100644 --- a/ccan/htable/test/run-type.c +++ b/ccan/htable/test/run-type.c @@ -4,7 +4,8 @@ #include #include -#define NUM_VALS (1 << HTABLE_BASE_BITS) +#define NUM_BITS 7 +#define NUM_VALS (1 << NUM_BITS) struct obj { /* Makes sure we don't try to treat and obj as a key or vice versa */ @@ -23,7 +24,7 @@ static const unsigned int *objkey(const struct obj *obj) static size_t objhash(const unsigned int *key) { size_t h = *key / 2; - h |= -1UL << HTABLE_BASE_BITS; + h |= -1UL << NUM_BITS; return h; } @@ -32,7 +33,7 @@ static bool cmp(const struct obj *obj, const unsigned int *key) return obj->key == *key; } -HTABLE_DEFINE_TYPE(struct obj, objkey, objhash, cmp, obj); +HTABLE_DEFINE_TYPE(struct obj, objkey, objhash, cmp, htable_obj); static void add_vals(struct htable_obj *ht, struct obj val[], unsigned int num) @@ -109,7 +110,7 @@ static bool check_mask(struct htable *ht, const struct obj val[], unsigned num) int main(int argc, char *argv[]) { unsigned int i; - struct htable_obj *ht; + struct htable_obj ht; struct obj val[NUM_VALS]; unsigned int dne; void *p; @@ -120,57 +121,55 @@ int main(int argc, char *argv[]) val[i].key = i; dne = i; - ht = htable_obj_new(); - ok1(((struct htable *)ht)->max < (1 << ((struct htable *)ht)->bits)); - ok1(((struct htable *)ht)->bits == HTABLE_BASE_BITS); + htable_obj_init(&ht); + ok1(ht.raw.max == 0); + ok1(ht.raw.bits == 0); /* We cannot find an entry which doesn't exist. */ - ok1(!htable_obj_get(ht, &dne)); + ok1(!htable_obj_get(&ht, &dne)); - /* Fill it, it should increase in size (once). */ - add_vals(ht, val, NUM_VALS); - ok1(((struct htable *)ht)->bits == HTABLE_BASE_BITS + 1); - ok1(((struct htable *)ht)->max < (1 << ((struct htable *)ht)->bits)); + /* Fill it, it should increase in size. */ + add_vals(&ht, val, NUM_VALS); + ok1(ht.raw.bits == NUM_BITS + 1); + ok1(ht.raw.max < (1 << ht.raw.bits)); /* Mask should be set. */ - ok1(((struct htable *)ht)->common_mask != 0); - ok1(((struct htable *)ht)->common_mask != -1); - ok1(check_mask((struct htable *)ht, val, NUM_VALS)); + ok1(ht.raw.common_mask != 0); + ok1(ht.raw.common_mask != -1); + ok1(check_mask(&ht.raw, val, NUM_VALS)); /* Find all. */ - find_vals(ht, val, NUM_VALS); - ok1(!htable_obj_get(ht, &dne)); + find_vals(&ht, val, NUM_VALS); + ok1(!htable_obj_get(&ht, &dne)); /* Walk once, should get them all. */ i = 0; - for (p = htable_obj_first(ht,&iter); p; p = htable_obj_next(ht, &iter)) + for (p = htable_obj_first(&ht,&iter); p; p = htable_obj_next(&ht, &iter)) i++; ok1(i == NUM_VALS); /* Delete all. */ - del_vals(ht, val, NUM_VALS); - ok1(!htable_obj_get(ht, &val[0].key)); + del_vals(&ht, val, NUM_VALS); + ok1(!htable_obj_get(&ht, &val[0].key)); /* Worst case, a "pointer" which doesn't have any matching bits. */ - htable_add((struct htable *)ht, 0, - (void *)~(uintptr_t)&val[NUM_VALS-1]); - htable_obj_add(ht, &val[NUM_VALS-1]); - ok1(((struct htable *)ht)->common_mask == 0); - ok1(((struct htable *)ht)->common_bits == 0); + htable_add(&ht.raw, 0, (void *)~(uintptr_t)&val[NUM_VALS-1]); + htable_obj_add(&ht, &val[NUM_VALS-1]); + ok1(ht.raw.common_mask == 0); + ok1(ht.raw.common_bits == 0); /* Delete the bogus one before we trip over it. */ - htable_del((struct htable *)ht, 0, - (void *)~(uintptr_t)&val[NUM_VALS-1]); + htable_del(&ht.raw, 0, (void *)~(uintptr_t)&val[NUM_VALS-1]); /* Add the rest. */ - add_vals(ht, val, NUM_VALS-1); + add_vals(&ht, val, NUM_VALS-1); /* Check we can find them all. */ - find_vals(ht, val, NUM_VALS); - ok1(!htable_obj_get(ht, &dne)); + find_vals(&ht, val, NUM_VALS); + ok1(!htable_obj_get(&ht, &dne)); /* Delete them all by key. */ - del_vals_bykey(ht, val, NUM_VALS); - htable_obj_free(ht); + del_vals_bykey(&ht, val, NUM_VALS); + htable_obj_clear(&ht); return exit_status(); }