X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Faga%2Ftest%2Fapi-adjacency.c;fp=ccan%2Faga%2Ftest%2Fapi-adjacency.c;h=26cb52b342075c8f023fda87fc9c0a627db334ae;hb=2192bdd98afbb1ddb489c863edd191877051de8b;hp=0000000000000000000000000000000000000000;hpb=3b7409ea08a7d1643bc7de31ece63e20b89f319b;p=ccan diff --git a/ccan/aga/test/api-adjacency.c b/ccan/aga/test/api-adjacency.c new file mode 100644 index 00000000..26cb52b3 --- /dev/null +++ b/ccan/aga/test/api-adjacency.c @@ -0,0 +1,93 @@ +#include "config.h" + +#include +#include + +#include + +#include + +#include "simple-graph.h" + +static void test_adjacency(const char *name, + const struct simple_graph *sg, + const struct adjacency_list *at) +{ + int i; + + for (i = 0; at[i].from != 0; i++) { + const void *e; + struct aga_edge_info ei; + int j = 0; + const struct aga_node *from; + int err; + + assert(i < MAX_NODES); + + from = &sg->nodes[at[i].from]; + + aga_for_each_edge_info(e, ei, err, &sg->g, from) { + const struct aga_node *cmpto; + + assert(j < MAX_EDGES); + cmpto = &sg->nodes[at[i].to[j]]; + ok(cmpto == ei.to, + "%s: %p #%d -> #%ld (expected #%d -> #%d)", name, e, + at[i].from, (ei.to - sg->nodes), + at[i].from, at[i].to[j]); + + j++; + } + if (at[i].to[j] < 0) { + ok(err == at[i].to[j], "%s: %p #%d -> ERROR %d", + name, e, at[i].from, at[i].to[j]); + continue; /* Move onto next node on errors */ + } + assert(j < MAX_EDGES); + ok(at[i].to[j] == 0, + "%s: %p #%d -> --- (expected #%d -> #%d)", name, e, + at[i].from, at[i].from, at[i].to[j]); + } +} + +int main(void) +{ + struct trivial_graph tg; + struct parallel_graph pg; + struct full_graph fg; + struct chain_graph cg; + struct grid_graph gg1, gg2; + struct error_graph eg; + struct traversal1_graph t1g; + + plan_tests(1 + 5 + 30 + 22 + 21 + 33 + 6 + 21); + + trivial_graph_init(&tg); + test_adjacency("trivial", &tg.sg, trivial_adjacency); + + parallel_graph_init(&pg, 3); + test_adjacency("parallel nlinks 3", &pg.sg, + parallel_adjacency_nlinks3); + + full_graph_init(&fg, 5); + test_adjacency("full 5", &fg.sg, full_adjacency_5); + + chain_graph_init(&cg, 8); + test_adjacency("chain 8", &cg.fg.sg, chain_adjacency_8); + + grid_graph_init(&gg1, 3, 3, true, true, false, false); + test_adjacency("grid 3x3 right-down", &gg1.sg, + grid_adjacency_3x3_rightdown); + + grid_graph_init(&gg2, 3, 3, true, true, true, true); + test_adjacency("grid 3x3 all", &gg2.sg, + grid_adjacency_3x3_all); + + error_graph_init(&eg); + test_adjacency("error graph", &eg.sg, error_adjacency); + + traversal1_graph_init(&t1g); + test_adjacency("traversal1 graph", &t1g.sg, traversal1_adjacency); + + return exit_status(); +}