From: Rusty Russell Date: Mon, 26 Mar 2018 10:38:34 +0000 (+1030) Subject: intmap: add exhaustive testcases for intmap_after X-Git-Url: http://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=a241664cddd9c1a9a66064171f871ae52598b82b;hp=4d9c7e1c2f899930a0bef5f05391615875ff668c intmap: add exhaustive testcases for intmap_after We can't do the full range, but we can for a handful of bits (8). Signed-off-by: Rusty Russell --- diff --git a/ccan/intmap/test/run-after-exhaustive.c b/ccan/intmap/test/run-after-exhaustive.c new file mode 100644 index 00000000..1655ddb5 --- /dev/null +++ b/ccan/intmap/test/run-after-exhaustive.c @@ -0,0 +1,41 @@ +#include +#include +#include +#include + +#define ELEMENTS 8 + +int main(void) +{ + UINTMAP(void *) umap; + + plan_tests((1 << ELEMENTS) * ELEMENTS); + + /* Run through every combination of elements */ + for (int i = 0; i < (1 << ELEMENTS); i++) { + /* Set up map */ + uintmap_init(&umap); + for (int j = 0; j < ELEMENTS; j++) { + if ((1 << j) & i) + uintmap_add(&umap, j, &umap); + } + + /* Try each uintmap_after value */ + for (int j = 0; j < ELEMENTS; j++) { + intmap_index_t idx = j, next; + + if ((i >> (j + 1)) == 0) + next = 0; + else + next = j + 1 + bitops_ls32(i >> (j + 1)); + + if (!uintmap_after(&umap, &idx)) + idx = 0; + ok1(idx == next); + } + uintmap_clear(&umap); + } + + /* This exits depending on whether all tests passed */ + return exit_status(); +}