| 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 <list>
|
|---|
| 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::list<std::pair<int, Path>> &r, int w, const Path &p)
|
|---|
| 46 | {
|
|---|
| 47 | for(auto const &ele: r)
|
|---|
| 48 | if (ele.first == w && ele.second == p)
|
|---|
| 49 | return true;
|
|---|
| 50 |
|
|---|
| 51 | return false;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | // a
|
|---|
| 55 | // /|\
|
|---|
| 56 | // 4 | 6 --5--
|
|---|
| 57 | // / | \ / \
|
|---|
| 58 | // b 2 c d
|
|---|
| 59 | // \ | / \ /
|
|---|
| 60 | // 1 | 3 --7--
|
|---|
| 61 | // \|/
|
|---|
| 62 | // e
|
|---|
| 63 |
|
|---|
| 64 | BOOST_AUTO_TEST_CASE(edge_disjoint_ksp_test)
|
|---|
| 65 | {
|
|---|
| 66 | Graph g(5);
|
|---|
| 67 |
|
|---|
| 68 | // Vertexes
|
|---|
| 69 | Vertex a = *(vertices(g).first + 0);
|
|---|
| 70 | Vertex b = *(vertices(g).first + 1);
|
|---|
| 71 | Vertex c = *(vertices(g).first + 2);
|
|---|
| 72 | Vertex d = *(vertices(g).first + 3);
|
|---|
| 73 | Vertex e = *(vertices(g).first + 4);
|
|---|
| 74 |
|
|---|
| 75 | // Edges
|
|---|
| 76 | Edge ab, ba;
|
|---|
| 77 | Edge ae, ea;
|
|---|
| 78 | Edge ac, ca;
|
|---|
| 79 | Edge be, eb;
|
|---|
| 80 | Edge cd1, dc1;
|
|---|
| 81 | Edge cd2, dc2;
|
|---|
| 82 | Edge ce, ec;
|
|---|
| 83 |
|
|---|
| 84 | tie(ab, ba) = aue(g, a, b, 4);
|
|---|
| 85 | tie(ae, ea) = aue(g, a, e, 2);
|
|---|
| 86 | tie(ac, ca) = aue(g, a, c, 6);
|
|---|
| 87 | tie(be, eb) = aue(g, b, e, 1);
|
|---|
| 88 | tie(cd1, dc1) = aue(g, c, d, 5);
|
|---|
| 89 | tie(cd2, dc2) = aue(g, c, d, 7);
|
|---|
| 90 | tie(ce, ec) = aue(g, c, e, 3);
|
|---|
| 91 |
|
|---|
| 92 | std::list<std::pair<int, Path>> r;
|
|---|
| 93 | r = boost::edge_disjoint_ksp(g, c, d);
|
|---|
| 94 | BOOST_CHECK(r.size() == 2);
|
|---|
| 95 | BOOST_CHECK(check_path(r, 5, Path{cd1}));
|
|---|
| 96 | BOOST_CHECK(check_path(r, 7, Path{cd2}));
|
|---|
| 97 |
|
|---|
| 98 | r = boost::edge_disjoint_ksp(g, b, d);
|
|---|
| 99 | BOOST_CHECK(r.size() == 2);
|
|---|
| 100 | BOOST_CHECK(check_path(r, 9, Path{be, ec, cd1}));
|
|---|
| 101 | BOOST_CHECK(check_path(r, 17, Path{ba, ac, cd2}));
|
|---|
| 102 |
|
|---|
| 103 | r = boost::edge_disjoint_ksp(g, a, e);
|
|---|
| 104 | BOOST_CHECK(r.size() == 3);
|
|---|
| 105 | BOOST_CHECK(check_path(r, 2, Path{ae}));
|
|---|
| 106 | BOOST_CHECK(check_path(r, 5, Path{ab, be}));
|
|---|
| 107 | BOOST_CHECK(check_path(r, 9, Path{ac, ce}));
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | BOOST_AUTO_TEST_CASE(edksp_filter_test)
|
|---|
| 111 | {
|
|---|
| 112 | Graph g(3);
|
|---|
| 113 | Vertex a = *(vertices(g).first);
|
|---|
| 114 | Vertex b = *(vertices(g).first + 1);
|
|---|
| 115 | Vertex c = *(vertices(g).first + 2);
|
|---|
| 116 |
|
|---|
| 117 | set<Edge> x;
|
|---|
| 118 | boost::edksp_filter<Graph> f(&x);
|
|---|
| 119 | typedef boost::filtered_graph<Graph, boost::edksp_filter<Graph> > fg_t;
|
|---|
| 120 | fg_t fg(g, f);
|
|---|
| 121 |
|
|---|
| 122 | Edge e1, e2, e3;
|
|---|
| 123 | bool s;
|
|---|
| 124 | tie(e1, s) = add_edge(a, b, g);
|
|---|
| 125 | tie(e2, s) = add_edge(a, b, g);
|
|---|
| 126 | tie(e3, s) = add_edge(b, c, g);
|
|---|
| 127 |
|
|---|
| 128 | fg_t::edge_iterator i, ie;
|
|---|
| 129 |
|
|---|
| 130 | // Exclude e1 and make sure it's not one of the edges.
|
|---|
| 131 | x.insert(e1);
|
|---|
| 132 | for(tie(i, ie) = boost::edges(fg); i != ie; ++i)
|
|---|
| 133 | BOOST_CHECK(*i != e1);
|
|---|
| 134 |
|
|---|
| 135 | // Exclude e2 and make sure it's not one of the edges.
|
|---|
| 136 | x.insert(e2);
|
|---|
| 137 | for(tie(i, ie) = boost::edges(fg); i != ie; ++i)
|
|---|
| 138 | BOOST_CHECK(*i != e2);
|
|---|
| 139 |
|
|---|
| 140 | // Exclude e3 and make sure there are no edges left.
|
|---|
| 141 | x.insert(e3);
|
|---|
| 142 | // Now the edge set should be empty.
|
|---|
| 143 | tie(i, ie) = boost::edges(fg);
|
|---|
| 144 | BOOST_CHECK(i == ie);
|
|---|
| 145 | }
|
|---|