6 #include <ccan/tal/tal.h>
7 #include <ccan/tap/tap.h>
8 #include <ccan/array_size/array_size.h>
9 #include <ccan/ptrint/ptrint.h>
11 #include <ccan/agar/agar.h>
13 #include "simple-graphr.h"
15 #define test_bfs_partial(sr, first, ...) \
17 int cmp[] = { __VA_ARGS__ }; \
18 bool stillok = true; \
21 agar_bfs(node, (sr), int2ptr(first)) { \
22 int index = ptr2int(node); \
23 diag("Visited %d\n", index); \
24 if (i >= ARRAY_SIZE(cmp) || (index != cmp[i])) \
31 #define test_bfs(gr, first, ...) \
33 struct agar_state *sr; \
34 ok1((sr = agar_bfs_new(NULL, (gr)))); \
35 test_bfs_partial(sr, first, __VA_ARGS__); \
41 struct trivial_graphr tgr;
42 struct parallel_graphr pgr;
43 struct full_graphr fgr;
44 struct chain_graphr cgr;
45 struct grid_graphr ggr1, ggr2;
46 struct error_graphr egr;
47 struct traversal1_graphr t1gr;
48 struct agar_state *sr;
51 plan_tests(2 * 13 + 12 + 10);
53 trivial_graphr_init(&tgr);
54 test_bfs(&tgr.gr, 1, 1);
56 parallel_graphr_init(&pgr, 3, 0);
57 test_bfs(&pgr.gr, 1, 1, 2);
59 full_graphr_init(&fgr, 5);
60 test_bfs(&fgr.gr, 1, 1, 2, 3, 4, 5);
61 test_bfs(&fgr.gr, 3, 3, 1, 2, 4, 5);
63 chain_graphr_init(&cgr, 8);
64 test_bfs(&cgr.fgr.gr, 1, 1, 2, 3, 4, 5, 6, 7, 8);
65 test_bfs(&cgr.fgr.gr, 8, 8, 7, 6, 5, 4, 3, 2, 1);
66 test_bfs(&cgr.fgr.gr, 5, 5, 4, 6, 3, 7, 2, 8, 1);
68 grid_graphr_init(&ggr1, 3, 3, true, true, false, false);
69 test_bfs(&ggr1.gr, 1, 1, 2, 4, 3, 5, 7, 6, 8, 9);
70 test_bfs(&ggr1.gr, 5, 5, 6, 8, 9);
71 test_bfs(&ggr1.gr, 9, 9);
73 grid_graphr_init(&ggr2, 3, 3, true, true, true, true);
74 test_bfs(&ggr2.gr, 1, 1, 2, 4, 3, 5, 7, 6, 8, 9);
75 test_bfs(&ggr2.gr, 5, 5, 6, 8, 4, 2, 9, 3, 7, 1);
76 test_bfs(&ggr2.gr, 9, 9, 8, 6, 7, 5, 3, 4, 2, 1);
78 error_graphr_init(&egr);
79 test_bfs(&egr.gr, 1, 1, 2);
80 ok((sr = agar_bfs_new(NULL, &egr.gr)), "started error traversal");
81 ok1(agar_bfs_explore(sr, int2ptr(3), &nr));
82 ok(ptr2int(nr) == 3, "Expected node #3, actually #%ld", ptr2int(nr));
83 ok1(agar_bfs_explore(sr, nr, &nr));
84 ok(ptr2int(nr) == 4, "Expected node #4, actually #%ld", ptr2int(nr));
85 ok1(!agar_bfs_explore(sr, nr, &nr));
86 ok1(agar_error(sr) == -1);
87 ok1(!agar_bfs_explore(sr, nr, &nr));
89 test_bfs(&egr.gr, 1, 1, 2);
91 traversal1_graphr_init(&t1gr);
92 test_bfs(&t1gr.gr, 1, 1, 2, 3, 4, 5, 6);
93 test_bfs(&t1gr.gr, 9, 9, 8, 7, 6, 5, 4);
95 ok1((sr = agar_bfs_new(NULL, &t1gr.gr)));
96 test_bfs_partial(sr, 1, 1, 2, 3, 4, 5, 6);
97 test_bfs_partial(sr, 9, 9, 8, 7);
100 ok1((sr = agar_bfs_new(NULL, &t1gr.gr)));
101 test_bfs_partial(sr, 9, 9, 8, 7, 6, 5, 4);
102 test_bfs_partial(sr, 1, 1, 2, 3);
105 return exit_status();