]> git.ozlabs.org Git - ccan/blob - ccan/aga/test/api-adjacency.c
aga: Simple test graphs
[ccan] / ccan / aga / test / api-adjacency.c
1 #include "config.h"
2
3 #include <stddef.h>
4 #include <assert.h>
5
6 #include <ccan/aga/aga.h>
7
8 #include <ccan/tap/tap.h>
9
10 #include "simple-graph.h"
11
12 static void test_adjacency(const char *name,
13                            const struct simple_graph *sg,
14                            const struct adjacency_list *at)
15 {
16         int i;
17
18         for (i = 0; at[i].from != 0; i++) {
19                 const void *e;
20                 struct aga_edge_info ei;
21                 int j = 0;
22                 const struct aga_node *from;
23                 int err;
24
25                 assert(i < MAX_NODES);
26
27                 from = &sg->nodes[at[i].from];
28
29                 aga_for_each_edge_info(e, ei, err, &sg->g, from) {
30                         const struct aga_node *cmpto;
31
32                         assert(j < MAX_EDGES);
33                         cmpto = &sg->nodes[at[i].to[j]];
34                         ok(cmpto == ei.to,
35                            "%s: %p #%d -> #%ld (expected #%d -> #%d)", name, e,
36                            at[i].from, (ei.to - sg->nodes),
37                            at[i].from, at[i].to[j]);
38
39                         j++;
40                 }
41                 if (at[i].to[j] < 0) {
42                         ok(err == at[i].to[j], "%s: %p #%d -> ERROR %d",
43                            name, e, at[i].from, at[i].to[j]);
44                         continue; /* Move onto next node on errors */
45                 }
46                 assert(j < MAX_EDGES);
47                 ok(at[i].to[j] == 0,
48                    "%s: %p #%d -> --- (expected #%d -> #%d)", name, e,
49                    at[i].from, at[i].from, at[i].to[j]);
50         }
51 }
52
53 int main(void)
54 {
55         struct trivial_graph tg;
56         struct parallel_graph pg;
57         struct full_graph fg;
58         struct chain_graph cg;
59         struct grid_graph gg1, gg2;
60         struct error_graph eg;
61         struct traversal1_graph t1g;
62
63         plan_tests(1 + 5 + 30 + 22 + 21 + 33 + 6 + 21);
64
65         trivial_graph_init(&tg);
66         test_adjacency("trivial", &tg.sg, trivial_adjacency);
67
68         parallel_graph_init(&pg, 3);
69         test_adjacency("parallel nlinks 3", &pg.sg,
70                        parallel_adjacency_nlinks3);
71
72         full_graph_init(&fg, 5);
73         test_adjacency("full 5", &fg.sg, full_adjacency_5);
74
75         chain_graph_init(&cg, 8);
76         test_adjacency("chain 8", &cg.fg.sg, chain_adjacency_8);
77
78         grid_graph_init(&gg1, 3, 3, true, true, false, false);
79         test_adjacency("grid 3x3 right-down", &gg1.sg,
80                        grid_adjacency_3x3_rightdown);
81
82         grid_graph_init(&gg2, 3, 3, true, true, true, true);
83         test_adjacency("grid 3x3 all", &gg2.sg,
84                        grid_adjacency_3x3_all);
85
86         error_graph_init(&eg);
87         test_adjacency("error graph", &eg.sg, error_adjacency);
88
89         traversal1_graph_init(&t1g);
90         test_adjacency("traversal1 graph", &t1g.sg, traversal1_adjacency);
91
92         return exit_status();
93 }