| 1 | // Copyright 2004-5 Trustees of Indiana University |
|---|
| 2 | |
|---|
| 3 | // Use, modification and distribution is subject to the Boost Software |
|---|
| 4 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
|---|
| 5 | // http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 6 | |
|---|
| 7 | // |
|---|
| 8 | // graphviz_test.cpp - Test cases for the Boost.Spirit implementation of a |
|---|
| 9 | // Graphviz DOT Language reader. |
|---|
| 10 | // |
|---|
| 11 | |
|---|
| 12 | // Author: Ronald Garcia |
|---|
| 13 | |
|---|
| 14 | //#define BOOST_GRAPH_READ_GRAPHVIZ_ITERATORS |
|---|
| 15 | #define BOOST_GRAPHVIZ_USE_ISTREAM |
|---|
| 16 | #include <boost/graph/graphviz.hpp> |
|---|
| 17 | #include <boost/assign/std/map.hpp> |
|---|
| 18 | #include <boost/graph/adjacency_list.hpp> |
|---|
| 19 | #include <boost/graph/graph_traits.hpp> |
|---|
| 20 | #include <boost/tuple/tuple.hpp> |
|---|
| 21 | #include <boost/dynamic_property_map.hpp> |
|---|
| 22 | #include <boost/test/test_tools.hpp> |
|---|
| 23 | #include <boost/test/floating_point_comparison.hpp> |
|---|
| 24 | #include <algorithm> |
|---|
| 25 | #include <string> |
|---|
| 26 | #include <iostream> |
|---|
| 27 | #include <iterator> |
|---|
| 28 | #include <map> |
|---|
| 29 | #include <utility> |
|---|
| 30 | |
|---|
| 31 | using namespace std; |
|---|
| 32 | using namespace boost; |
|---|
| 33 | |
|---|
| 34 | #ifndef BOOST_GRAPHVIZ_USE_ISTREAM |
|---|
| 35 | using namespace boost::spirit; |
|---|
| 36 | #endif |
|---|
| 37 | using namespace boost::assign; |
|---|
| 38 | |
|---|
| 39 | typedef std::string node_t; |
|---|
| 40 | typedef std::pair<node_t,node_t> edge_t; |
|---|
| 41 | |
|---|
| 42 | typedef std::map<node_t,float> mass_map_t; |
|---|
| 43 | typedef std::map<edge_t,double> weight_map_t; |
|---|
| 44 | |
|---|
| 45 | template <typename Directedness, typename OutEdgeList> |
|---|
| 46 | bool test_graph(std::istream& dotfile, mass_map_t const& masses, |
|---|
| 47 | weight_map_t const& weights, |
|---|
| 48 | std::string const& node_id = "node_id") { |
|---|
| 49 | |
|---|
| 50 | typedef adjacency_list < OutEdgeList, vecS, Directedness, |
|---|
| 51 | property < vertex_name_t, std::string, |
|---|
| 52 | property < vertex_color_t, float > >, |
|---|
| 53 | property < edge_weight_t, double > > graph_t; |
|---|
| 54 | typedef typename graph_traits < graph_t >::edge_descriptor edge_t; |
|---|
| 55 | typedef typename graph_traits < graph_t >::vertex_descriptor vertex_t; |
|---|
| 56 | |
|---|
| 57 | // Construct a graph and set up the dynamic_property_maps. |
|---|
| 58 | graph_t graph(0); |
|---|
| 59 | dynamic_properties dp; |
|---|
| 60 | typename property_map<graph_t, vertex_name_t>::type name = |
|---|
| 61 | get(vertex_name, graph); |
|---|
| 62 | dp.property(node_id,name); |
|---|
| 63 | typename property_map<graph_t, vertex_color_t>::type mass = |
|---|
| 64 | get(vertex_color, graph); |
|---|
| 65 | dp.property("mass",mass); |
|---|
| 66 | typename property_map<graph_t, edge_weight_t>::type weight = |
|---|
| 67 | get(edge_weight, graph); |
|---|
| 68 | dp.property("weight",weight); |
|---|
| 69 | |
|---|
| 70 | // Read in space characters too! |
|---|
| 71 | dotfile >> noskipws; |
|---|
| 72 | |
|---|
| 73 | bool result = true; |
|---|
| 74 | #ifdef BOOST_GRAPHVIZ_USE_ISTREAM |
|---|
| 75 | if(read_graphviz(dotfile,graph,dp,node_id)) { |
|---|
| 76 | #else |
|---|
| 77 | std::string data; |
|---|
| 78 | std::copy(std::istream_iterator<char>(dotfile), |
|---|
| 79 | std::istream_iterator<char>(), |
|---|
| 80 | std::back_inserter(data)); |
|---|
| 81 | if(read_graphviz(data.begin(),data.end(),graph,dp,node_id)) { |
|---|
| 82 | #endif |
|---|
| 83 | // check masses |
|---|
| 84 | if(!masses.empty()) { |
|---|
| 85 | // assume that all the masses have been set |
|---|
| 86 | // for each vertex: |
|---|
| 87 | typename graph_traits<graph_t>::vertex_iterator i,j; |
|---|
| 88 | for(boost::tie(i,j) = vertices(graph); i != j; ++i) { |
|---|
| 89 | // - get its name |
|---|
| 90 | std::string node_name = get(name,*i); |
|---|
| 91 | // - get its mass |
|---|
| 92 | float node_mass = get(mass,*i); |
|---|
| 93 | float ref_mass = masses.find(node_name)->second; |
|---|
| 94 | // - compare the mass to the result in the table |
|---|
| 95 | |
|---|
| 96 | BOOST_CHECK_CLOSE(node_mass, ref_mass, 0.01f); |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | // check weights |
|---|
| 100 | if(!weights.empty()) { |
|---|
| 101 | // assume that all weights have been set |
|---|
| 102 | /// for each edge: |
|---|
| 103 | typename graph_traits<graph_t>::edge_iterator i,j; |
|---|
| 104 | for(boost::tie(i,j) = edges(graph); i != j; ++i) { |
|---|
| 105 | // - get its name |
|---|
| 106 | std::pair<std::string,std::string> |
|---|
| 107 | edge_name = make_pair(get(name, source(*i,graph)), |
|---|
| 108 | get(name, target(*i,graph))); |
|---|
| 109 | // - get its weight |
|---|
| 110 | double edge_weight = get(weight,*i); |
|---|
| 111 | double ref_weight = weights.find(edge_name)->second; |
|---|
| 112 | // - compare the weight to teh result in the table |
|---|
| 113 | BOOST_CHECK_CLOSE(edge_weight, ref_weight, 0.01); |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | } else { |
|---|
| 119 | std::cerr << "Parsing Failed!\n"; |
|---|
| 120 | result = false; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | return result; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | int test_main(int, char*[]) { |
|---|
| 127 | |
|---|
| 128 | typedef istringstream gs_t; |
|---|
| 129 | |
|---|
| 130 | // Basic directed graph tests |
|---|
| 131 | { |
|---|
| 132 | mass_map_t masses; |
|---|
| 133 | insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f); |
|---|
| 134 | gs_t gs("digraph { a node [mass = 7.7] c e [mass = 6.66] }"); |
|---|
| 135 | BOOST_CHECK((test_graph<directedS,vecS>(gs,masses,weight_map_t()))); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | { |
|---|
| 139 | weight_map_t weights; |
|---|
| 140 | insert( weights )(make_pair("a","b"),0.0) |
|---|
| 141 | (make_pair("c","d"),7.7)(make_pair("e","f"),6.66); |
|---|
| 142 | gs_t gs("digraph { a -> b edge [weight = 7.7] " |
|---|
| 143 | "c -> d e-> f [weight = 6.66] }"); |
|---|
| 144 | BOOST_CHECK((test_graph<directedS,vecS>(gs,mass_map_t(),weights))); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | // undirected graph with alternate node_id property name |
|---|
| 148 | { |
|---|
| 149 | mass_map_t masses; |
|---|
| 150 | insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f); |
|---|
| 151 | gs_t gs("graph { a node [mass = 7.7] c e [mass = 6.66] }"); |
|---|
| 152 | BOOST_CHECK((test_graph<undirectedS,vecS>(gs,masses,weight_map_t(), |
|---|
| 153 | "nodenames"))); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | // Basic undirected graph tests |
|---|
| 157 | { |
|---|
| 158 | mass_map_t masses; |
|---|
| 159 | insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f); |
|---|
| 160 | gs_t gs("graph { a node [mass = 7.7] c e [mass = 6.66] }"); |
|---|
| 161 | BOOST_CHECK((test_graph<undirectedS,vecS>(gs,masses,weight_map_t()))); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | { |
|---|
| 165 | weight_map_t weights; |
|---|
| 166 | insert( weights )(make_pair("a","b"),0.0) |
|---|
| 167 | (make_pair("c","d"),7.7)(make_pair("e","f"),6.66); |
|---|
| 168 | gs_t gs("graph { a -- b eDge [weight = 7.7] " |
|---|
| 169 | "c -- d e -- f [weight = 6.66] }"); |
|---|
| 170 | BOOST_CHECK((test_graph<undirectedS,vecS>(gs,mass_map_t(),weights))); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | // Mismatch directed graph test |
|---|
| 174 | { |
|---|
| 175 | mass_map_t masses; |
|---|
| 176 | insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f); |
|---|
| 177 | gs_t gs("graph { a nodE [mass = 7.7] c e [mass = 6.66] }"); |
|---|
| 178 | try { |
|---|
| 179 | test_graph<directedS,vecS>(gs,masses,weight_map_t()); |
|---|
| 180 | } catch (boost::undirected_graph_error&) {} |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | // Mismatch undirected graph test |
|---|
| 184 | { |
|---|
| 185 | mass_map_t masses; |
|---|
| 186 | insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f); |
|---|
| 187 | gs_t gs("digraph { a node [mass = 7.7] c e [mass = 6.66] }"); |
|---|
| 188 | try { |
|---|
| 189 | test_graph<undirectedS,vecS>(gs,masses,weight_map_t()); |
|---|
| 190 | BOOST_ERROR("Failed to throw boost::directed_graph_error."); |
|---|
| 191 | } catch (boost::directed_graph_error&) {} |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | // Complain about parallel edges |
|---|
| 195 | { |
|---|
| 196 | weight_map_t weights; |
|---|
| 197 | insert( weights )(make_pair("a","b"),7.7); |
|---|
| 198 | gs_t gs("diGraph { a -> b [weight = 7.7] a -> b [weight = 7.7] }"); |
|---|
| 199 | try { |
|---|
| 200 | test_graph<directedS,setS>(gs,mass_map_t(),weights); |
|---|
| 201 | BOOST_ERROR("Failed to throw boost::bad_parallel_edge."); |
|---|
| 202 | } catch (boost::bad_parallel_edge&) {} |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | // Handle parallel edges gracefully |
|---|
| 206 | { |
|---|
| 207 | weight_map_t weights; |
|---|
| 208 | insert( weights )(make_pair("a","b"),7.7); |
|---|
| 209 | gs_t gs("digraph { a -> b [weight = 7.7] a -> b [weight = 7.7] }"); |
|---|
| 210 | BOOST_CHECK((test_graph<directedS,vecS>(gs,mass_map_t(),weights))); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | return 0; |
|---|
| 214 | } |
|---|