6 #include <ccan/tap/tap.h>
7 #include <ccan/array_size/array_size.h>
9 #include <ccan/aga/aga.h>
11 #include "simple-graph.h"
13 #define test_bfs_partial(sg, first, ...) \
15 int cmp[] = { __VA_ARGS__ }; \
16 bool stillok = true; \
17 struct aga_node *node; \
19 aga_bfs(node, &(sg)->g, &(sg)->nodes[first]) { \
20 int index = node - (sg)->nodes; \
21 diag("Visited %d\n", index); \
22 if (i >= ARRAY_SIZE(cmp) || (index != cmp[i])) \
29 #define test_bfs(sg, first, ...) \
31 ok1(aga_bfs_start(&(sg)->g) == 0); \
32 test_bfs_partial(sg, first, __VA_ARGS__); \
33 aga_finish(&(sg)->g); \
38 struct trivial_graph tg;
39 struct parallel_graph pg;
41 struct chain_graph cg;
42 struct grid_graph gg1, gg2;
43 struct error_graph eg;
44 struct traversal1_graph t1g;
45 struct aga_node *node;
47 plan_tests(2 * 13 + 10 + 10);
49 trivial_graph_init(&tg);
50 test_bfs(&tg.sg, 1, 1);
52 parallel_graph_init(&pg, 3);
53 test_bfs(&pg.sg, 1, 1, 2);
55 full_graph_init(&fg, 5);
56 test_bfs(&fg.sg, 1, 1, 2, 3, 4, 5);
57 test_bfs(&fg.sg, 3, 3, 1, 2, 4, 5);
59 chain_graph_init(&cg, 8);
60 test_bfs(&cg.fg.sg, 1, 1, 2, 3, 4, 5, 6, 7, 8);
61 test_bfs(&cg.fg.sg, 8, 8, 7, 6, 5, 4, 3, 2, 1);
62 test_bfs(&cg.fg.sg, 5, 5, 4, 6, 3, 7, 2, 8, 1);
64 grid_graph_init(&gg1, 3, 3, true, true, false, false);
65 test_bfs(&gg1.sg, 1, 1, 2, 4, 3, 5, 7, 6, 8, 9);
66 test_bfs(&gg1.sg, 5, 5, 6, 8, 9);
67 test_bfs(&gg1.sg, 9, 9);
69 grid_graph_init(&gg2, 3, 3, true, true, true, true);
70 test_bfs(&gg2.sg, 1, 1, 2, 4, 3, 5, 7, 6, 8, 9);
71 test_bfs(&gg2.sg, 5, 5, 6, 8, 4, 2, 9, 3, 7, 1);
72 test_bfs(&gg2.sg, 9, 9, 8, 6, 7, 5, 3, 4, 2, 1);
74 error_graph_init(&eg);
75 test_bfs(&eg.sg, 1, 1, 2);
76 ok(aga_bfs_start(&eg.sg.g) == 0, "started error traversal");
77 node = aga_bfs_explore(&eg.sg.g, &eg.sg.nodes[3]);
78 ok(node == &eg.sg.nodes[3], "Expected node #3 (%p), actually #%ld (%p)",
79 &eg.sg.nodes[3], node - eg.sg.nodes, node);
80 node = aga_bfs_explore(&eg.sg.g, node);
81 ok(node == &eg.sg.nodes[4], "Expected node #4 (%p), actually #%ld (%p)",
82 &eg.sg.nodes[4], node - eg.sg.nodes, node);
83 ok1(aga_bfs_explore(&eg.sg.g, node) == NULL);
84 ok1(aga_error(&eg.sg.g) == -1);
85 ok1(aga_bfs_explore(&eg.sg.g, node) == NULL);
87 test_bfs(&eg.sg, 1, 1, 2);
89 traversal1_graph_init(&t1g);
90 test_bfs(&t1g.sg, 1, 1, 2, 3, 4, 5, 6);
91 test_bfs(&t1g.sg, 9, 9, 8, 7, 6, 5, 4);
93 ok1(aga_bfs_start(&t1g.sg.g) == 0);
94 test_bfs_partial(&t1g.sg, 1, 1, 2, 3, 4, 5, 6);
95 test_bfs_partial(&t1g.sg, 9, 9, 8, 7);
96 aga_finish(&t1g.sg.g);
98 ok1(aga_bfs_start(&t1g.sg.g) == 0);
99 test_bfs_partial(&t1g.sg, 9, 9, 8, 7, 6, 5, 4);
100 test_bfs_partial(&t1g.sg, 1, 1, 2, 3);
101 aga_finish(&t1g.sg.g);
103 return exit_status();