1 // Copyright 2005 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 // Author: Douglas Gregor
8 #include <boost/graph/graphviz.hpp>
9 #include <boost/graph/adjacency_list.hpp>
10 #include <boost/test/minimal.hpp>
11 #include <string>
12 #include <fstream>
13 #include <boost/graph/iteration_macros.hpp>
14 
15 using namespace boost;
16 
17 typedef boost::adjacency_list<vecS, vecS, directedS,
18                               property<vertex_name_t, std::string>,
19                               property<edge_weight_t, double> > Digraph;
20 
21 typedef boost::adjacency_list<vecS, vecS, undirectedS,
22                               property<vertex_name_t, std::string>,
23                               property<edge_weight_t, double> > Graph;
24 
test_graph_read_write(const std::string & filename)25 void test_graph_read_write(const std::string& filename)
26 {
27   std::ifstream in(filename.c_str());
28   BOOST_REQUIRE(in);
29 
30   Graph g;
31   dynamic_properties dp;
32   dp.property("id", get(vertex_name, g));
33   dp.property("weight", get(edge_weight, g));
34   BOOST_CHECK(read_graphviz(in, g, dp, "id"));
35 
36   BOOST_CHECK(num_vertices(g) == 4);
37   BOOST_CHECK(num_edges(g) == 4);
38 
39   typedef graph_traits<Graph>::vertex_descriptor Vertex;
40 
41   std::map<std::string, Vertex> name_to_vertex;
42   BGL_FORALL_VERTICES(v, g, Graph)
43     name_to_vertex[get(vertex_name, g, v)] = v;
44 
45   // Check vertices
46   BOOST_CHECK(name_to_vertex.find("0") != name_to_vertex.end());
47   BOOST_CHECK(name_to_vertex.find("1") != name_to_vertex.end());
48   BOOST_CHECK(name_to_vertex.find("foo") != name_to_vertex.end());
49   BOOST_CHECK(name_to_vertex.find("bar") != name_to_vertex.end());
50 
51   // Check edges
52   BOOST_CHECK(edge(name_to_vertex["0"], name_to_vertex["1"], g).second);
53   BOOST_CHECK(edge(name_to_vertex["1"], name_to_vertex["foo"], g).second);
54   BOOST_CHECK(edge(name_to_vertex["foo"], name_to_vertex["bar"], g).second);
55   BOOST_CHECK(edge(name_to_vertex["1"], name_to_vertex["bar"], g).second);
56 
57   BOOST_CHECK(get(edge_weight, g,
58                   edge(name_to_vertex["0"], name_to_vertex["1"], g).first)
59                 == 3.14159);
60   BOOST_CHECK(get(edge_weight, g,
61                   edge(name_to_vertex["1"], name_to_vertex["foo"], g).first)
62                 == 2.71828);
63   BOOST_CHECK(get(edge_weight, g,
64                   edge(name_to_vertex["foo"], name_to_vertex["bar"], g).first)
65                 == 10.0);
66   BOOST_CHECK(get(edge_weight, g,
67                   edge(name_to_vertex["1"], name_to_vertex["bar"], g).first)
68                 == 10.0);
69 
70   // Write out the graph
71   write_graphviz_dp(std::cout, g, dp, std::string("id"));
72 }
73 
test_main(int argc,char * argv[])74 int test_main(int argc, char* argv[])
75 {
76   test_graph_read_write(argc >= 2 ? argv[1] : "graphviz_example.dot");
77 
78   return 0;
79 }
80