]> git.ozlabs.org Git - ccan/blob - ccan/invbloom/test/run-singletoncb.c
invbloom: singleton callback for when destroying a singleton, too.
[ccan] / ccan / invbloom / test / run-singletoncb.c
1 #include <ccan/invbloom/invbloom.h>
2 /* Include the C files directly. */
3 #include <ccan/invbloom/invbloom.c>
4 #include <ccan/tap/tap.h>
5
6 static void singleton_cb(struct invbloom *ib, size_t n, bool before,
7                          unsigned *count)
8 {
9         ok1(ib->count[n] == 1 || ib->count[n] == -1);
10         count[before]++;
11 }       
12
13 int main(void)
14 {
15         struct invbloom *ib;
16         const tal_t *ctx = tal(NULL, char);
17         int val;
18         unsigned singleton_count[2];
19
20         /* This is how many tests you plan to run */
21         plan_tests(16 + 6 + NUM_HASHES * 3);
22
23         /* Single entry ib table keeps it simple. */
24         ib = invbloom_new(ctx, int, 1, 100);
25         invbloom_singleton_cb(ib, singleton_cb, singleton_count);
26
27         val = 0;
28         singleton_count[false] = singleton_count[true] = 0;
29         invbloom_insert(ib, &val);
30         ok1(ib->count[0] == NUM_HASHES);
31         ok1(singleton_count[true] == 1);
32         ok1(singleton_count[false] == 1);
33         ok1(!invbloom_empty(ib));
34
35         /* First delete takes it via singleton. */
36         invbloom_delete(ib, &val);
37         ok1(singleton_count[true] == 2);
38         ok1(singleton_count[false] == 2);
39         ok1(invbloom_empty(ib));
40
41         /* Second delete creates negative singleton. */
42         invbloom_delete(ib, &val);
43         ok1(singleton_count[true] == 3);
44         ok1(singleton_count[false] == 3);
45
46         /* Now a larger table: this seed set so entries don't clash */
47         ib = invbloom_new(ctx, int, 1024, 0);
48         singleton_count[false] = singleton_count[true] = 0;
49         invbloom_singleton_cb(ib, singleton_cb, singleton_count);
50
51         val = 0;
52         invbloom_insert(ib, &val);
53         ok1(singleton_count[true] == 0);
54         ok1(singleton_count[false] == NUM_HASHES);
55
56         /* First delete removes singletons. */
57         invbloom_delete(ib, &val);
58         ok1(singleton_count[true] == NUM_HASHES);
59         ok1(singleton_count[false] == NUM_HASHES);
60         ok1(invbloom_empty(ib));
61
62         /* Second delete creates negative singletons. */
63         invbloom_delete(ib, &val);
64         ok1(singleton_count[true] == NUM_HASHES);
65         ok1(singleton_count[false] == NUM_HASHES * 2);
66
67         tal_free(ctx);
68
69         /* This exits depending on whether all tests passed */
70         return exit_status();
71 }