]> git.ozlabs.org Git - ccan/blob - ccan/agar/test/api-adjacency.c
2b2637d277f2fc41dd8ca298a098234059bd3b87
[ccan] / ccan / agar / test / api-adjacency.c
1 #include "config.h"
2
3 #include <stddef.h>
4 #include <assert.h>
5
6 #include <ccan/agar/agar.h>
7 #include <ccan/ptrint/ptrint.h>
8
9 #include <ccan/tap/tap.h>
10
11 #include "simple-graphr.h"
12
13 static void test_adjacency(const char *name,
14                            const struct agar_graph *gr,
15                            const struct adjacency_listr *atr)
16 {
17         int i;
18
19         for (i = 0; atr[i].from != 0; i++) {
20                 const void *e;
21                 struct agar_edge_info eir;
22                 int j = 0;
23                 int err;
24                 ptrint_t *from = int2ptr(atr[i].from);
25
26                 agar_for_each_edge_info(e, eir, err, gr, from) {
27                         const void *cmpto;
28
29                         assert(j < MAX_EDGES);
30                         cmpto = int2ptr(atr[i].to[j]);
31                         ok(cmpto == eir.to,
32                            "%s: %p #%d -> #%ld (expected #%d -> #%d)", name, e,
33                            atr[i].from, ptr2int(eir.to),
34                            atr[i].from, atr[i].to[j]);
35
36                         j++;
37                 }
38                 if (atr[i].to[j] < 0) {
39                         ok(err == atr[i].to[j], "%s: %p #%d -> ERROR %d",
40                            name, e, atr[i].from, atr[i].to[j]);
41                         continue; /* Move onto next node on errors */
42                 }
43                 assert(j < MAX_EDGES);
44                 ok(atr[i].to[j] == 0,
45                    "%s: %p #%d -> --- (expected #%d -> #%d)", name, e,
46                    atr[i].from, atr[i].from, atr[i].to[j]);
47         }
48 }
49
50 int main(void)
51 {
52         struct trivial_graphr tgr;
53         struct parallel_graphr pgr;
54         struct full_graphr fgr;
55         struct chain_graphr cgr;
56         struct grid_graphr ggr1, ggr2;
57         struct error_graphr egr;
58         struct shortcut1_graphr s1gr;
59         struct shortcut2_graphr s2gr;
60
61         plan_tests(1 + 5 + 30 + 22 + 21 + 33 + 6 + 6 + 6);
62
63         trivial_graphr_init(&tgr);
64         test_adjacency("trivial", &tgr.gr, trivial_adjacencyr);
65
66         parallel_graphr_init(&pgr, 3, 0);
67         test_adjacency("parallel nlinks 3", &pgr.gr,
68                        parallel_adjacencyr_nlinks3);
69
70         full_graphr_init(&fgr, 5);
71         test_adjacency("full 5", &fgr.gr, full_adjacencyr_5);
72
73         chain_graphr_init(&cgr, 8);
74         test_adjacency("chain 8", &cgr.fgr.gr, chain_adjacencyr_8);
75
76         grid_graphr_init(&ggr1, 3, 3, true, true, false, false);
77         test_adjacency("grid 3x3 right-down", &ggr1.gr,
78                        grid_adjacencyr_3x3_rightdown);
79
80         grid_graphr_init(&ggr2, 3, 3, true, true, true, true);
81         test_adjacency("grid 3x3 all", &ggr2.gr,
82                        grid_adjacencyr_3x3_all);
83
84         error_graphr_init(&egr);
85         test_adjacency("error graph", &egr.gr, error_adjacencyr);
86
87         shortcut1_graphr_init(&s1gr);
88         test_adjacency("shortcut1 graph", &s1gr.gr, shortcut1_adjacencyr);
89
90         shortcut2_graphr_init(&s2gr);
91         test_adjacency("shortcut2 graph", &s2gr.gr, shortcut2_adjacencyr);
92         
93         return exit_status();
94 }