]> git.ozlabs.org Git - ccan/blobdiff - ccan/agar/test/api-adjacency.c
agar: Re-entrant Abstract Graph Algorithms
[ccan] / ccan / agar / test / api-adjacency.c
diff --git a/ccan/agar/test/api-adjacency.c b/ccan/agar/test/api-adjacency.c
new file mode 100644 (file)
index 0000000..c17ead5
--- /dev/null
@@ -0,0 +1,86 @@
+#include "config.h"
+
+#include <stddef.h>
+#include <assert.h>
+
+#include <ccan/agar/agar.h>
+#include <ccan/ptrint/ptrint.h>
+
+#include <ccan/tap/tap.h>
+
+#include "simple-graphr.h"
+
+static void test_adjacency(const char *name,
+                          const struct agar_graph *gr,
+                          const struct adjacency_listr *atr)
+{
+       int i;
+
+       for (i = 0; atr[i].from != 0; i++) {
+               const void *e;
+               struct agar_edge_info eir;
+               int j = 0;
+               int err;
+               ptrint_t *from = int2ptr(atr[i].from);
+
+               agar_for_each_edge_info(e, eir, err, gr, from) {
+                       const void *cmpto;
+
+                       assert(j < MAX_EDGES);
+                       cmpto = int2ptr(atr[i].to[j]);
+                       ok(cmpto == eir.to,
+                          "%s: %p #%d -> #%ld (expected #%d -> #%d)", name, e,
+                          atr[i].from, ptr2int(eir.to),
+                          atr[i].from, atr[i].to[j]);
+
+                       j++;
+               }
+               if (atr[i].to[j] < 0) {
+                       ok(err == atr[i].to[j], "%s: %p #%d -> ERROR %d",
+                          name, e, atr[i].from, atr[i].to[j]);
+                       continue; /* Move onto next node on errors */
+               }
+               assert(j < MAX_EDGES);
+               ok(atr[i].to[j] == 0,
+                  "%s: %p #%d -> --- (expected #%d -> #%d)", name, e,
+                  atr[i].from, atr[i].from, atr[i].to[j]);
+       }
+}
+
+int main(void)
+{
+       struct trivial_graphr tgr;
+       struct parallel_graphr pgr;
+       struct full_graphr fgr;
+       struct chain_graphr cgr;
+       struct grid_graphr ggr1, ggr2;
+       struct error_graphr egr;
+
+       plan_tests(1 + 5 + 30 + 22 + 21 + 33 + 6);
+
+       trivial_graphr_init(&tgr);
+       test_adjacency("trivial", &tgr.gr, trivial_adjacencyr);
+
+       parallel_graphr_init(&pgr, 3);
+       test_adjacency("parallel nlinks 3", &pgr.gr,
+                      parallel_adjacencyr_nlinks3);
+
+       full_graphr_init(&fgr, 5);
+       test_adjacency("full 5", &fgr.gr, full_adjacencyr_5);
+
+       chain_graphr_init(&cgr, 8);
+       test_adjacency("chain 8", &cgr.fgr.gr, chain_adjacencyr_8);
+
+       grid_graphr_init(&ggr1, 3, 3, true, true, false, false);
+       test_adjacency("grid 3x3 right-down", &ggr1.gr,
+                      grid_adjacencyr_3x3_rightdown);
+
+       grid_graphr_init(&ggr2, 3, 3, true, true, true, true);
+       test_adjacency("grid 3x3 all", &ggr2.gr,
+                      grid_adjacencyr_3x3_all);
+
+       error_graphr_init(&egr);
+       test_adjacency("error graph", &egr.gr, error_adjacencyr);
+
+       return exit_status();
+}