1 /* Include the C files directly. */
2 #include <ccan/htable/htable.h>
3 #include <ccan/htable/htable.c>
4 #include <ccan/tap/tap.h>
8 struct htable_with_counters {
10 size_t num_alloc, num_free;
13 static void *test_alloc(struct htable *ht, size_t len)
15 ((struct htable_with_counters *)ht)->num_alloc++;
16 return calloc(len, 1);
20 static void test_free(struct htable *ht, void *p)
23 ((struct htable_with_counters *)ht)->num_free++;
28 static size_t hash(const void *elem, void *unused UNNEEDED)
30 return *(size_t *)elem;
35 struct htable_with_counters htc;
36 size_t val[] = { 0, 1 };
38 htc.num_alloc = htc.num_free = 0;
41 htable_set_allocator(test_alloc, test_free);
42 htable_init(&htc.ht, hash, NULL);
43 htable_add(&htc.ht, hash(&val[0], NULL), &val[0]);
44 ok1(htc.num_alloc == 1);
45 ok1(htc.num_free == 0);
46 /* Adding another increments, then frees old */
47 htable_add(&htc.ht, hash(&val[1], NULL), &val[1]);
48 ok1(htc.num_alloc == 2);
49 ok1(htc.num_free == 1);
50 htable_clear(&htc.ht);
51 ok1(htc.num_alloc == 2);
52 ok1(htc.num_free == 2);
54 /* Should restore defaults */
55 htable_set_allocator(NULL, NULL);
56 ok1(htable_alloc == htable_default_alloc);
57 ok1(htable_free == htable_default_free);
59 htable_init(&htc.ht, hash, NULL);
60 htable_add(&htc.ht, hash(&val[0], NULL), &val[0]);
61 ok1(htc.num_alloc == 2);
62 ok1(htc.num_free == 2);
63 htable_add(&htc.ht, hash(&val[1], NULL), &val[1]);
64 ok1(htc.num_alloc == 2);
65 ok1(htc.num_free == 2);
66 htable_clear(&htc.ht);
68 /* This exits depending on whether all tests passed */