Ticket #11804: edge_disjoint_ksp.4.cc

File edge_disjoint_ksp.4.cc, 2.8 KB (added by Irek Szcześniak <irek@…>, 7 years ago)

the unit test - v4

Line 
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
11typedef
12boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS,
13 boost::no_property,
14 boost::property<boost::edge_weight_t, int> >
15Graph;
16
17typedef Graph::edge_descriptor Edge;
18typedef Graph::vertex_descriptor Vertex;
19typedef std::list<Edge> Path;
20
21using namespace std;
22
23// Add an edge, test it, and set weight.
24Edge
25ade(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
38pair<Edge, Edge>
39aue(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
44bool
45check_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
64BOOST_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
94 r = boost::edge_disjoint_ksp(g, c, d);
95 BOOST_CHECK(r.size() == 2);
96 BOOST_CHECK(check_path(r, 5, Path{cd1}));
97 BOOST_CHECK(check_path(r, 7, Path{cd2}));
98
99 r = boost::edge_disjoint_ksp(g, b, d);
100 BOOST_CHECK(r.size() == 2);
101 BOOST_CHECK(check_path(r, 9, Path{be, ec, cd1}));
102 BOOST_CHECK(check_path(r, 17, Path{ba, ac, cd2}));
103
104 // Limit the search to 1 path.
105 r = boost::edge_disjoint_ksp(g, a, e, 1);
106 BOOST_CHECK(r.size() == 1);
107 BOOST_CHECK(check_path(r, 2, Path{ae}));
108
109 // Limit the search to 2 paths.
110 r = boost::edge_disjoint_ksp(g, a, e, 2);
111 BOOST_CHECK(r.size() == 2);
112 BOOST_CHECK(check_path(r, 2, Path{ae}));
113 BOOST_CHECK(check_path(r, 5, Path{ab, be}));
114
115 // Don't limit the search, and get 3 paths.
116 r = boost::edge_disjoint_ksp(g, a, e);
117 BOOST_CHECK(r.size() == 3);
118 BOOST_CHECK(check_path(r, 2, Path{ae}));
119 BOOST_CHECK(check_path(r, 5, Path{ab, be}));
120 BOOST_CHECK(check_path(r, 9, Path{ac, ce}));
121}