]> git.ozlabs.org Git - ccan/blob - ccan/htable/test/run-extra.c
htable: commit extra tests I had lying around.
[ccan] / ccan / htable / test / run-extra.c
1 #include "../htable.c"
2
3 static size_t hash(const void *ptr, void *priv UNNEEDED)
4 {
5         /* We're hashing pointers; no need to get too fancy. */
6         return ((size_t)ptr / sizeof(ptr)) ^ ((size_t)ptr % sizeof(ptr));
7 }
8
9 /* 24042: Waiting on 0x5570a500c3f8 (11742786623615)
10 24042: Waiting on 0x5570a500c430 (11742786623622)
11 24042: Searching for 0x5570a500c3f8 (11742786623615) in 2 elems
12 24042: Searching for 0x5570a500c3f8 (11742786623615) in 2 elems
13 */
14 static struct htable waittable = HTABLE_INITIALIZER(waittable, hash, NULL);
15
16 int main(void)
17 {
18         const void *p1 = (void *)0x5570a500c3f8ULL;
19         const void *p2 = (void *)0x5570a500c430ULL;
20         size_t h;
21         struct htable_iter i;
22         void *p;
23         bool found;
24
25         printf("hash %p == %zu\n", p1, hash(p1, NULL));
26         printf("hash %p == %zu\n", p2, hash(p2, NULL));
27         htable_add(&waittable, hash(p1, NULL), p1);
28         htable_add(&waittable, hash(p2, NULL), p2);
29
30         found = false;
31         h = hash(p1, NULL);
32         for (p = htable_firstval(&waittable, &i, h);
33              p;
34              p = htable_nextval(&waittable, &i, h)) {
35                 if (p == p1)
36                         found = true;
37         }
38         assert(found);
39
40         found = false;
41         h = hash(p2, NULL);
42         for (p = htable_firstval(&waittable, &i, h);
43              p;
44              p = htable_nextval(&waittable, &i, h)) {
45                 if (p == p2)
46                         found = true;
47         }
48         assert(found);
49         
50         return found ? 0 : 1;
51 }