graphviz.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2005 Trustees of Indiana University
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Author: Douglas Gregor
  6. #include <boost/graph/graphviz.hpp>
  7. #include <boost/graph/adjacency_list.hpp>
  8. #include <boost/test/minimal.hpp>
  9. #include <string>
  10. #include <fstream>
  11. #include <boost/graph/iteration_macros.hpp>
  12. using namespace boost;
  13. typedef boost::adjacency_list<vecS, vecS, directedS,
  14. property<vertex_name_t, std::string>,
  15. property<edge_weight_t, double> > Digraph;
  16. typedef boost::adjacency_list<vecS, vecS, undirectedS,
  17. property<vertex_name_t, std::string>,
  18. property<edge_weight_t, double> > Graph;
  19. void test_graph_read_write(const std::string& filename)
  20. {
  21. std::ifstream in(filename.c_str());
  22. BOOST_REQUIRE(in);
  23. Graph g;
  24. dynamic_properties dp;
  25. dp.property("id", get(vertex_name, g));
  26. dp.property("weight", get(edge_weight, g));
  27. BOOST_CHECK(read_graphviz(in, g, dp, "id"));
  28. BOOST_CHECK(num_vertices(g) == 4);
  29. BOOST_CHECK(num_edges(g) == 4);
  30. typedef graph_traits<Graph>::vertex_descriptor Vertex;
  31. std::map<std::string, Vertex> name_to_vertex;
  32. BGL_FORALL_VERTICES(v, g, Graph)
  33. name_to_vertex[get(vertex_name, g, v)] = v;
  34. // Check vertices
  35. BOOST_CHECK(name_to_vertex.find("0") != name_to_vertex.end());
  36. BOOST_CHECK(name_to_vertex.find("1") != name_to_vertex.end());
  37. BOOST_CHECK(name_to_vertex.find("foo") != name_to_vertex.end());
  38. BOOST_CHECK(name_to_vertex.find("bar") != name_to_vertex.end());
  39. // Check edges
  40. BOOST_CHECK(edge(name_to_vertex["0"], name_to_vertex["1"], g).second);
  41. BOOST_CHECK(edge(name_to_vertex["1"], name_to_vertex["foo"], g).second);
  42. BOOST_CHECK(edge(name_to_vertex["foo"], name_to_vertex["bar"], g).second);
  43. BOOST_CHECK(edge(name_to_vertex["1"], name_to_vertex["bar"], g).second);
  44. BOOST_CHECK(get(edge_weight, g,
  45. edge(name_to_vertex["0"], name_to_vertex["1"], g).first)
  46. == 3.14159);
  47. BOOST_CHECK(get(edge_weight, g,
  48. edge(name_to_vertex["1"], name_to_vertex["foo"], g).first)
  49. == 2.71828);
  50. BOOST_CHECK(get(edge_weight, g,
  51. edge(name_to_vertex["foo"], name_to_vertex["bar"], g).first)
  52. == 10.0);
  53. BOOST_CHECK(get(edge_weight, g,
  54. edge(name_to_vertex["1"], name_to_vertex["bar"], g).first)
  55. == 10.0);
  56. // Write out the graph
  57. write_graphviz_dp(std::cout, g, dp, std::string("id"));
  58. }
  59. int test_main(int argc, char* argv[])
  60. {
  61. test_graph_read_write(argc >= 2 ? argv[1] : "graphviz_example.dot");
  62. return 0;
  63. }