1 | #define BOOST_TEST_MODULE edge_disjoint_ksp
|
---|
2 |
|
---|
3 | #include "edge_disjoint_ksp.hpp"
|
---|
4 |
|
---|
5 | #include <boost/graph/adjacency_list.hpp>
|
---|
6 | #include <boost/graph/filtered_graph.hpp>
|
---|
7 | #include <boost/test/unit_test.hpp>
|
---|
8 |
|
---|
9 | #include <map>
|
---|
10 |
|
---|
11 | typedef
|
---|
12 | boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS,
|
---|
13 | boost::no_property,
|
---|
14 | boost::property<boost::edge_weight_t, int> >
|
---|
15 | Graph;
|
---|
16 |
|
---|
17 | typedef Graph::edge_descriptor Edge;
|
---|
18 | typedef Graph::vertex_descriptor Vertex;
|
---|
19 | typedef std::list<Edge> path;
|
---|
20 |
|
---|
21 | using namespace std;
|
---|
22 |
|
---|
23 | // Add an edge, test it, and set weight.
|
---|
24 | Edge
|
---|
25 | ade(Graph &g, Vertex s, Vertex d, int w)
|
---|
26 | {
|
---|
27 | Edge e;
|
---|
28 | bool success;
|
---|
29 |
|
---|
30 | boost::tie(e, success) = add_edge(s, d, g);
|
---|
31 | assert(success);
|
---|
32 |
|
---|
33 | boost::get(boost::edge_weight, g, e) = w;
|
---|
34 |
|
---|
35 | return e;
|
---|
36 | }
|
---|
37 |
|
---|
38 | pair<Edge, Edge>
|
---|
39 | aue(Graph &g, Vertex s, Vertex d, int w)
|
---|
40 | {
|
---|
41 | return make_pair(ade(g, s, d, w), ade(g, d, s, w));
|
---|
42 | }
|
---|
43 |
|
---|
44 | bool
|
---|
45 | check_path(const std::multimap<int, path> &r, int w, const path &p)
|
---|
46 | {
|
---|
47 | std::multimap<int, path>::const_iterator i = r.find(w);
|
---|
48 | return i != r.end() && i->second == p;
|
---|
49 | }
|
---|
50 |
|
---|
51 | // a
|
---|
52 | // /|\
|
---|
53 | // 4 | 6 --5--
|
---|
54 | // / | \ / \
|
---|
55 | // b 2 c d
|
---|
56 | // \ | / \ /
|
---|
57 | // 1 | 3 --7--
|
---|
58 | // \|/
|
---|
59 | // e
|
---|
60 |
|
---|
61 | BOOST_AUTO_TEST_CASE(edge_disjoint_ksp_test)
|
---|
62 | {
|
---|
63 | Graph g(5);
|
---|
64 |
|
---|
65 | // Vertexes
|
---|
66 | Vertex a = *(vertices(g).first + 0);
|
---|
67 | Vertex b = *(vertices(g).first + 1);
|
---|
68 | Vertex c = *(vertices(g).first + 2);
|
---|
69 | Vertex d = *(vertices(g).first + 3);
|
---|
70 | Vertex e = *(vertices(g).first + 4);
|
---|
71 |
|
---|
72 | // Edges
|
---|
73 | Edge ab, ba;
|
---|
74 | Edge ae, ea;
|
---|
75 | Edge ac, ca;
|
---|
76 | Edge be, eb;
|
---|
77 | Edge cd1, dc1;
|
---|
78 | Edge cd2, dc2;
|
---|
79 | Edge ce, ec;
|
---|
80 |
|
---|
81 | tie(ab, ba) = aue(g, a, b, 4);
|
---|
82 | tie(ae, ea) = aue(g, a, e, 2);
|
---|
83 | tie(ac, ca) = aue(g, a, c, 6);
|
---|
84 | tie(be, eb) = aue(g, b, e, 1);
|
---|
85 | tie(cd1, dc1) = aue(g, c, d, 5);
|
---|
86 | tie(cd2, dc2) = aue(g, c, d, 7);
|
---|
87 | tie(ce, ec) = aue(g, c, e, 3);
|
---|
88 |
|
---|
89 | std::multimap<int, path> r;
|
---|
90 | r = boost::edge_disjoint_ksp(g, c, d);
|
---|
91 | BOOST_CHECK(r.size() == 2);
|
---|
92 | BOOST_CHECK(check_path(r, 5, path{cd1}));
|
---|
93 | BOOST_CHECK(check_path(r, 7, path{cd2}));
|
---|
94 |
|
---|
95 | r = boost::edge_disjoint_ksp(g, b, d);
|
---|
96 | BOOST_CHECK(r.size() == 2);
|
---|
97 | BOOST_CHECK(check_path(r, 9, path{be, ec, cd1}));
|
---|
98 | BOOST_CHECK(check_path(r, 17, path{ba, ac, cd2}));
|
---|
99 |
|
---|
100 | r = boost::edge_disjoint_ksp(g, a, e);
|
---|
101 | BOOST_CHECK(r.size() == 3);
|
---|
102 | BOOST_CHECK(check_path(r, 2, path{ae}));
|
---|
103 | BOOST_CHECK(check_path(r, 5, path{ab, be}));
|
---|
104 | BOOST_CHECK(check_path(r, 9, path{ac, ce}));
|
---|
105 | }
|
---|
106 |
|
---|
107 | BOOST_AUTO_TEST_CASE(edksp_filter_test)
|
---|
108 | {
|
---|
109 | Graph g(3);
|
---|
110 | Vertex a = *(vertices(g).first);
|
---|
111 | Vertex b = *(vertices(g).first + 1);
|
---|
112 | Vertex c = *(vertices(g).first + 2);
|
---|
113 |
|
---|
114 | set<Edge> x;
|
---|
115 | boost::edksp_filter<Graph> f(&x);
|
---|
116 | typedef boost::filtered_graph<Graph, boost::edksp_filter<Graph> > fg_t;
|
---|
117 | fg_t fg(g, f);
|
---|
118 |
|
---|
119 | Edge e1, e2, e3;
|
---|
120 | bool s;
|
---|
121 | tie(e1, s) = add_edge(a, b, g);
|
---|
122 | tie(e2, s) = add_edge(a, b, g);
|
---|
123 | tie(e3, s) = add_edge(b, c, g);
|
---|
124 |
|
---|
125 | fg_t::edge_iterator i, ie;
|
---|
126 |
|
---|
127 | // Exclude e1 and make sure it's not one of the edges.
|
---|
128 | x.insert(e1);
|
---|
129 | for(tie(i, ie) = boost::edges(fg); i != ie; ++i)
|
---|
130 | BOOST_CHECK(*i != e1);
|
---|
131 |
|
---|
132 | // Exclude e2 and make sure it's not one of the edges.
|
---|
133 | x.insert(e2);
|
---|
134 | for(tie(i, ie) = boost::edges(fg); i != ie; ++i)
|
---|
135 | BOOST_CHECK(*i != e2);
|
---|
136 |
|
---|
137 | // Exclude e3 and make sure there are no edges left.
|
---|
138 | x.insert(e3);
|
---|
139 | // Now the edge set should be empty.
|
---|
140 | tie(i, ie) = boost::edges(fg);
|
---|
141 | BOOST_CHECK(i == ie);
|
---|
142 | }
|
---|