]> git.ozlabs.org Git - ccan/blob - ccan/invbloom/test/run-singletoncb.c
invbloom: reduce hash count to 3.
[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,
7                          unsigned *count)
8 {
9         ok1(ib->count[n] == 1 || ib->count[n] == -1);
10         (*count)++;
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;
19
20         /* This is how many tests you plan to run */
21         plan_tests(10 + 3 + NUM_HASHES * 2);
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 = 0;
29         invbloom_insert(ib, &val);
30         ok1(ib->count[0] == NUM_HASHES);
31         ok1(singleton_count == 1);
32         ok1(!invbloom_empty(ib));
33
34         /* First delete takes it via singleton. */
35         invbloom_delete(ib, &val);
36         ok1(singleton_count == 2);
37         ok1(invbloom_empty(ib));
38
39         /* Second delete creates negative singleton. */
40         invbloom_delete(ib, &val);
41         ok1(singleton_count == 3);
42
43         /* Now a larger table: this seed set so entries don't clash */
44         ib = invbloom_new(ctx, int, 1024, 0);
45         singleton_count = 0;
46         invbloom_singleton_cb(ib, singleton_cb, &singleton_count);
47
48         val = 0;
49         invbloom_insert(ib, &val);
50         ok1(singleton_count == NUM_HASHES);
51
52         /* First delete does nothing. */
53         invbloom_delete(ib, &val);
54         ok1(singleton_count == NUM_HASHES);
55         ok1(invbloom_empty(ib));
56
57         /* Second delete creates negative singletons. */
58         invbloom_delete(ib, &val);
59         ok1(singleton_count == NUM_HASHES * 2);
60
61         tal_free(ctx);
62
63         /* This exits depending on whether all tests passed */
64         return exit_status();
65 }