First public contribution.
1 // Copyright 2004-5 Trustees of Indiana University
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)
8 // graphviz_test.cpp - Test cases for the Boost.Spirit implementation of a
9 // Graphviz DOT Language reader.
12 // Author: Ronald Garcia
14 * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved.
17 //#define BOOST_GRAPH_READ_GRAPHVIZ_ITERATORS
18 #define BOOST_GRAPHVIZ_USE_ISTREAM
19 #include <boost/graph/graphviz.hpp>
20 #include <boost/assign/std/map.hpp>
21 #include <boost/graph/adjacency_list.hpp>
22 #include <boost/graph/graph_traits.hpp>
23 #include <boost/tuple/tuple.hpp>
24 #include <boost/dynamic_property_map.hpp>
25 #include <boost/test/test_tools.hpp>
26 #include <boost/test/floating_point_comparison.hpp>
35 #include "std_log_result.h"
36 #define LOG_FILENAME_LINE __FILE__, __LINE__
39 using namespace boost;
41 #ifndef BOOST_GRAPHVIZ_USE_ISTREAM
42 using namespace boost::spirit;
44 using namespace boost::assign;
46 typedef std::string node_t;
47 typedef std::pair<node_t,node_t> edge_t;
49 typedef std::map<node_t,float> mass_map_t;
50 typedef std::map<edge_t,double> weight_map_t;
52 template <typename Directedness, typename OutEdgeList>
53 bool test_graph(std::istream& dotfile, mass_map_t const& masses,
54 weight_map_t const& weights,
55 std::string const& node_id = "node_id") {
57 typedef adjacency_list < OutEdgeList, vecS, Directedness,
58 property < vertex_name_t, std::string,
59 property < vertex_color_t, float > >,
60 property < edge_weight_t, double > > graph_t;
61 typedef typename graph_traits < graph_t >::edge_descriptor edge_t;
62 typedef typename graph_traits < graph_t >::vertex_descriptor vertex_t;
64 // Construct a graph and set up the dynamic_property_maps.
66 dynamic_properties dp;
67 typename property_map<graph_t, vertex_name_t>::type name =
68 get(vertex_name, graph);
69 dp.property(node_id,name);
70 typename property_map<graph_t, vertex_color_t>::type mass =
71 get(vertex_color, graph);
72 dp.property("mass",mass);
73 typename property_map<graph_t, edge_weight_t>::type weight =
74 get(edge_weight, graph);
75 dp.property("weight",weight);
77 // Read in space characters too!
81 #ifdef BOOST_GRAPHVIZ_USE_ISTREAM
82 if(read_graphviz(dotfile,graph,dp,node_id)) {
85 std::copy(std::istream_iterator<char>(dotfile),
86 std::istream_iterator<char>(),
87 std::back_inserter(data));
88 if(read_graphviz(data.begin(),data.end(),graph,dp,node_id)) {
92 // assume that all the masses have been set
94 typename graph_traits<graph_t>::vertex_iterator i,j;
95 for(boost::tie(i,j) = vertices(graph); i != j; ++i) {
97 std::string node_name = get(name,*i);
99 float node_mass = get(mass,*i);
100 float ref_mass = masses.find(node_name)->second;
101 // - compare the mass to the result in the table
103 BOOST_CHECK_CLOSE(node_mass, ref_mass, 0.01f);
107 if(!weights.empty()) {
108 // assume that all weights have been set
110 typename graph_traits<graph_t>::edge_iterator i,j;
111 for(boost::tie(i,j) = edges(graph); i != j; ++i) {
113 std::pair<std::string,std::string>
114 edge_name = make_pair(get(name, source(*i,graph)),
115 get(name, target(*i,graph)));
117 double edge_weight = get(weight,*i);
118 double ref_weight = weights.find(edge_name)->second;
119 // - compare the weight to teh result in the table
120 BOOST_CHECK_CLOSE(edge_weight, ref_weight, 0.01);
126 std::cerr << "Parsing Failed!\n";
133 int test_main(int, char*[]) {
135 typedef istringstream gs_t;
137 // Basic directed graph tests
140 insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f);
141 gs_t gs("digraph { a node [mass = 7.7] c e [mass = 6.66] }");
142 BOOST_CHECK((test_graph<directedS,vecS>(gs,masses,weight_map_t())));
146 weight_map_t weights;
147 insert( weights )(make_pair("a","b"),0.0)
148 (make_pair("c","d"),7.7)(make_pair("e","f"),6.66);
149 gs_t gs("digraph { a -> b edge [weight = 7.7] "
150 "c -> d e-> f [weight = 6.66] }");
151 BOOST_CHECK((test_graph<directedS,vecS>(gs,mass_map_t(),weights)));
154 // undirected graph with alternate node_id property name
157 insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f);
158 gs_t gs("graph { a node [mass = 7.7] c e [mass = 6.66] }");
159 BOOST_CHECK((test_graph<undirectedS,vecS>(gs,masses,weight_map_t(),
163 // Basic undirected graph tests
166 insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f);
167 gs_t gs("graph { a node [mass = 7.7] c e [mass = 6.66] }");
168 BOOST_CHECK((test_graph<undirectedS,vecS>(gs,masses,weight_map_t())));
172 weight_map_t weights;
173 insert( weights )(make_pair("a","b"),0.0)
174 (make_pair("c","d"),7.7)(make_pair("e","f"),6.66);
175 gs_t gs("graph { a -- b eDge [weight = 7.7] "
176 "c -- d e -- f [weight = 6.66] }");
177 BOOST_CHECK((test_graph<undirectedS,vecS>(gs,mass_map_t(),weights)));
180 // Mismatch directed graph test
183 insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f);
184 gs_t gs("graph { a nodE [mass = 7.7] c e [mass = 6.66] }");
186 test_graph<directedS,vecS>(gs,masses,weight_map_t());
187 } catch (boost::undirected_graph_error&) {}
190 // Mismatch undirected graph test
193 insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f);
194 gs_t gs("digraph { a node [mass = 7.7] c e [mass = 6.66] }");
196 test_graph<undirectedS,vecS>(gs,masses,weight_map_t());
197 BOOST_ERROR("Failed to throw boost::directed_graph_error.");
198 } catch (boost::directed_graph_error&) {}
201 // Complain about parallel edges
203 weight_map_t weights;
204 insert( weights )(make_pair("a","b"),7.7);
205 gs_t gs("diGraph { a -> b [weight = 7.7] a -> b [weight = 7.7] }");
207 test_graph<directedS,setS>(gs,mass_map_t(),weights);
208 BOOST_ERROR("Failed to throw boost::bad_parallel_edge.");
209 } catch (boost::bad_parallel_edge&) {}
212 // Handle parallel edges gracefully
214 weight_map_t weights;
215 insert( weights )(make_pair("a","b"),7.7);
216 gs_t gs("digraph { a -> b [weight = 7.7] a -> b [weight = 7.7] }");
217 BOOST_CHECK((test_graph<directedS,vecS>(gs,mass_map_t(),weights)));
222 std_log(LOG_FILENAME_LINE,"[End Test Case ]");
224 testResultXml("graphiz_test");