diff options
author | Rusty Russell <rusty@rustcorp.com.au> | 2011-12-05 16:42:47 +1030 |
---|---|---|
committer | Rusty Russell <rusty@rustcorp.com.au> | 2011-12-05 16:42:47 +1030 |
commit | 0ac7deefbf190e11d38cc47807e0f5f6cfb1775e (patch) | |
tree | eb45e5db11b9d051b117e63a2cd4f4d69e7e9c19 /lib/ccan/htable/test | |
parent | 19409ddaf2414dd1d8ef183c834b37c8767034b0 (diff) | |
download | samba-0ac7deefbf190e11d38cc47807e0f5f6cfb1775e.tar.gz samba-0ac7deefbf190e11d38cc47807e0f5f6cfb1775e.tar.bz2 samba-0ac7deefbf190e11d38cc47807e0f5f6cfb1775e.zip |
lib/ccan/htable: clean up interface, document htable_type better.
We change from htable_new()/htable_free() to htable_init/htable_clear.
We also change HTABLE_DEFINE_TYPE() to be the full name, without automatically
prepending htable_.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
(Imported from CCAN commit 0c3590dc33d644f73bb8587db454c491830aaf26)
Diffstat (limited to 'lib/ccan/htable/test')
-rw-r--r-- | lib/ccan/htable/test/run-size.c | 12 | ||||
-rw-r--r-- | lib/ccan/htable/test/run-type.c | 56 | ||||
-rw-r--r-- | lib/ccan/htable/test/run.c | 82 |
3 files changed, 73 insertions, 77 deletions
diff --git a/lib/ccan/htable/test/run-size.c b/lib/ccan/htable/test/run-size.c index 01e4bb41ac..c92401c3c6 100644 --- a/lib/ccan/htable/test/run-size.c +++ b/lib/ccan/htable/test/run-size.c @@ -16,7 +16,7 @@ static size_t hash(const void *elem, void *unused) int main(int argc, char *argv[]) { - struct htable *ht; + struct htable ht; uint64_t val[NUM_VALS]; unsigned int i; @@ -24,13 +24,13 @@ int main(int argc, char *argv[]) for (i = 0; i < NUM_VALS; i++) val[i] = i; - ht = htable_new(hash, NULL); + 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]); + ok1(ht.max >= i); + ok1(ht.max <= i * 2); + htable_add(&ht, hash(&val[i], NULL), &val[i]); } - htable_free(ht); + htable_clear(&ht); return exit_status(); } diff --git a/lib/ccan/htable/test/run-type.c b/lib/ccan/htable/test/run-type.c index aca9c59488..f97e7270b2 100644 --- a/lib/ccan/htable/test/run-type.c +++ b/lib/ccan/htable/test/run-type.c @@ -33,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) @@ -110,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; @@ -121,57 +121,55 @@ int main(int argc, char *argv[]) val[i].key = i; dne = i; - ht = htable_obj_new(); - ok1(((struct htable *)ht)->max == 0); - ok1(((struct htable *)ht)->bits == 0); + 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. */ - add_vals(ht, val, NUM_VALS); - ok1(((struct htable *)ht)->bits == NUM_BITS + 1); - ok1(((struct htable *)ht)->max < (1 << ((struct htable *)ht)->bits)); + 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(); } diff --git a/lib/ccan/htable/test/run.c b/lib/ccan/htable/test/run.c index 5e9d23ee30..1a9e2de4cb 100644 --- a/lib/ccan/htable/test/run.c +++ b/lib/ccan/htable/test/run.c @@ -99,7 +99,7 @@ int main(int argc, char *argv[]) { unsigned int i; uintptr_t perfect_bit; - struct htable *ht; + struct htable ht; uint64_t val[NUM_VALS]; uint64_t dne; void *p; @@ -110,81 +110,79 @@ int main(int argc, char *argv[]) val[i] = i; dne = i; - ht = htable_new(hash, NULL); - ok1(ht->max == 0); - ok1(ht->bits == 0); + htable_init(&ht, hash, NULL); + ok1(ht.max == 0); + ok1(ht.bits == 0); /* We cannot find an entry which doesn't exist. */ - ok1(!htable_get(ht, hash(&dne, NULL), objcmp, &dne)); + ok1(!htable_get(&ht, hash(&dne, NULL), objcmp, &dne)); /* This should increase it once. */ - add_vals(ht, val, 0, 1); - ok1(ht->bits == 1); - ok1(ht->max == 1); - ok1(ht->common_mask == -1); + add_vals(&ht, val, 0, 1); + ok1(ht.bits == 1); + ok1(ht.max == 1); + ok1(ht.common_mask == -1); /* Mask should be set. */ - ok1(check_mask(ht, val, 1)); + ok1(check_mask(&ht, val, 1)); /* This should increase it again. */ - add_vals(ht, val, 1, 1); - ok1(ht->bits == 2); - ok1(ht->max == 3); + add_vals(&ht, val, 1, 1); + ok1(ht.bits == 2); + ok1(ht.max == 3); /* Mask should be set. */ - ok1(ht->common_mask != 0); - ok1(ht->common_mask != -1); - ok1(check_mask(ht, val, 2)); + ok1(ht.common_mask != 0); + ok1(ht.common_mask != -1); + ok1(check_mask(&ht, val, 2)); /* Now do the rest. */ - add_vals(ht, val, 2, NUM_VALS - 2); + add_vals(&ht, val, 2, NUM_VALS - 2); /* Find all. */ - find_vals(ht, val, NUM_VALS); - ok1(!htable_get(ht, hash(&dne, NULL), objcmp, &dne)); + find_vals(&ht, val, NUM_VALS); + ok1(!htable_get(&ht, hash(&dne, NULL), objcmp, &dne)); /* Walk once, should get them all. */ i = 0; - for (p = htable_first(ht,&iter); p; p = htable_next(ht, &iter)) + for (p = htable_first(&ht,&iter); p; p = htable_next(&ht, &iter)) i++; ok1(i == NUM_VALS); /* Delete all. */ - del_vals(ht, val, NUM_VALS); - ok1(!htable_get(ht, hash(&val[0], NULL), objcmp, &val[0])); + del_vals(&ht, val, NUM_VALS); + ok1(!htable_get(&ht, hash(&val[0], NULL), objcmp, &val[0])); /* Worst case, a "pointer" which doesn't have any matching bits. */ - htable_add(ht, 0, (void *)~(uintptr_t)&val[NUM_VALS-1]); - htable_add(ht, hash(&val[NUM_VALS-1], NULL), &val[NUM_VALS-1]); - ok1(ht->common_mask == 0); - ok1(ht->common_bits == 0); + htable_add(&ht, 0, (void *)~(uintptr_t)&val[NUM_VALS-1]); + htable_add(&ht, hash(&val[NUM_VALS-1], NULL), &val[NUM_VALS-1]); + ok1(ht.common_mask == 0); + ok1(ht.common_bits == 0); /* Get rid of bogus pointer before we trip over it! */ - htable_del(ht, 0, (void *)~(uintptr_t)&val[NUM_VALS-1]); + htable_del(&ht, 0, (void *)~(uintptr_t)&val[NUM_VALS-1]); /* Add the rest. */ - add_vals(ht, val, 0, NUM_VALS-1); + add_vals(&ht, val, 0, NUM_VALS-1); /* Check we can find them all. */ - find_vals(ht, val, NUM_VALS); - ok1(!htable_get(ht, hash(&dne, NULL), objcmp, &dne)); - - htable_free(ht); + find_vals(&ht, val, NUM_VALS); + ok1(!htable_get(&ht, hash(&dne, NULL), objcmp, &dne)); /* Corner cases: wipe out the perfect bit using bogus pointer. */ - ht = htable_new(hash, NULL); - htable_add(ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1])); - ok1(ht->perfect_bit); - perfect_bit = ht->perfect_bit; - htable_add(ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1] + htable_clear(&ht); + htable_add(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1])); + ok1(ht.perfect_bit); + perfect_bit = ht.perfect_bit; + htable_add(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1] | perfect_bit)); - ok1(ht->perfect_bit == 0); - htable_del(ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1] | perfect_bit)); + ok1(ht.perfect_bit == 0); + htable_del(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1] | perfect_bit)); /* Enlarging should restore it... */ - add_vals(ht, val, 0, NUM_VALS-1); + add_vals(&ht, val, 0, NUM_VALS-1); - ok1(ht->perfect_bit != 0); - htable_free(ht); + ok1(ht.perfect_bit != 0); + htable_clear(&ht); return exit_status(); } |