graphviz_test.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // Copyright 2004-5 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. //
  6. // graphviz_test.cpp - Test cases for the Boost.Spirit implementation of a
  7. // Graphviz DOT Language reader.
  8. //
  9. // Author: Ronald Garcia
  10. #define BOOST_GRAPHVIZ_USE_ISTREAM
  11. #define BOOST_TEST_MODULE TestGraphviz
  12. #include <boost/regex.hpp>
  13. #include <boost/graph/graphviz.hpp>
  14. #include <boost/assign/std/map.hpp>
  15. #include <boost/graph/adjacency_list.hpp>
  16. #include <boost/graph/compressed_sparse_row_graph.hpp>
  17. #include <boost/graph/graph_traits.hpp>
  18. #include <boost/tuple/tuple.hpp>
  19. #include <boost/property_map/dynamic_property_map.hpp>
  20. #include <boost/test/test_tools.hpp>
  21. #include <boost/test/unit_test.hpp>
  22. #include <boost/test/floating_point_comparison.hpp>
  23. #include <algorithm>
  24. #include <string>
  25. #include <iostream>
  26. #include <iterator>
  27. #include <map>
  28. #include <utility>
  29. using namespace std;
  30. using namespace boost;
  31. using namespace boost::assign;
  32. typedef std::string node_t;
  33. typedef std::pair<node_t,node_t> edge_t;
  34. typedef std::map<node_t,float> mass_map_t;
  35. typedef std::map<edge_t,double> weight_map_t;
  36. template <typename graph_t, typename NameMap, typename MassMap, typename WeightMap>
  37. bool test_graph(std::istream& dotfile, graph_t& graph,
  38. std::size_t correct_num_vertices,
  39. mass_map_t const& masses,
  40. weight_map_t const& weights,
  41. std::string const& node_id,
  42. std::string const& g_name,
  43. NameMap name,
  44. MassMap mass,
  45. WeightMap weight);
  46. template <typename graph_t>
  47. bool test_graph(std::istream& dotfile, std::size_t correct_num_vertices,
  48. mass_map_t const& masses,
  49. weight_map_t const& weights,
  50. std::string const& node_id = "node_id",
  51. std::string const& g_name = std::string()) {
  52. graph_t g;
  53. return test_graph(dotfile, g, correct_num_vertices, masses, weights, node_id, g_name,
  54. get(vertex_name, g), get(vertex_color, g), get(edge_weight, g));
  55. }
  56. template <typename graph_t, typename NameMap, typename MassMap, typename WeightMap>
  57. bool test_graph(std::istream& dotfile, graph_t& graph,
  58. std::size_t correct_num_vertices,
  59. mass_map_t const& masses,
  60. weight_map_t const& weights,
  61. std::string const& node_id,
  62. std::string const& g_name,
  63. NameMap name,
  64. MassMap mass,
  65. WeightMap weight) {
  66. // Construct a graph and set up the dynamic_property_maps.
  67. dynamic_properties dp(ignore_other_properties);
  68. dp.property(node_id,name);
  69. dp.property("mass",mass);
  70. dp.property("weight",weight);
  71. boost::ref_property_map<graph_t*,std::string> gname(
  72. get_property(graph,graph_name));
  73. dp.property("name",gname);
  74. bool result = true;
  75. #ifdef BOOST_GRAPHVIZ_USE_ISTREAM
  76. if(read_graphviz(dotfile,graph,dp,node_id)) {
  77. #else
  78. std::string data;
  79. dotfile >> std::noskipws;
  80. std::copy(std::istream_iterator<char>(dotfile),
  81. std::istream_iterator<char>(),
  82. std::back_inserter(data));
  83. if(read_graphviz(data.begin(),data.end(),graph,dp,node_id)) {
  84. #endif
  85. // check correct vertex count
  86. BOOST_CHECK_EQUAL(num_vertices(graph), correct_num_vertices);
  87. // check masses
  88. if(!masses.empty()) {
  89. // assume that all the masses have been set
  90. // for each vertex:
  91. typename graph_traits<graph_t>::vertex_iterator i,j;
  92. for(boost::tie(i,j) = vertices(graph); i != j; ++i) {
  93. // - get its name
  94. std::string node_name = get(name,*i);
  95. // - get its mass
  96. float node_mass = get(mass,*i);
  97. BOOST_CHECK(masses.find(node_name) != masses.end());
  98. float ref_mass = masses.find(node_name)->second;
  99. // - compare the mass to the result in the table
  100. BOOST_CHECK_CLOSE(node_mass, ref_mass, 0.01f);
  101. }
  102. }
  103. // check weights
  104. if(!weights.empty()) {
  105. // assume that all weights have been set
  106. /// for each edge:
  107. typename graph_traits<graph_t>::edge_iterator i,j;
  108. for(boost::tie(i,j) = edges(graph); i != j; ++i) {
  109. // - get its name
  110. std::pair<std::string,std::string>
  111. edge_name = make_pair(get(name, source(*i,graph)),
  112. get(name, target(*i,graph)));
  113. // - get its weight
  114. double edge_weight = get(weight,*i);
  115. BOOST_CHECK(weights.find(edge_name) != weights.end());
  116. double ref_weight = weights.find(edge_name)->second;
  117. // - compare the weight to teh result in the table
  118. BOOST_CHECK_CLOSE(edge_weight, ref_weight, 0.01);
  119. }
  120. }
  121. if(!g_name.empty()) {
  122. std::string parsed_name = get_property(graph,graph_name);
  123. BOOST_CHECK(parsed_name == g_name);
  124. }
  125. } else {
  126. std::cerr << "Parsing Failed!\n";
  127. result = false;
  128. }
  129. return result;
  130. }
  131. // int test_main(int, char*[]) {
  132. typedef istringstream gs_t;
  133. typedef property < vertex_name_t, std::string,
  134. property < vertex_color_t, float > > vertex_p;
  135. typedef property < edge_weight_t, double > edge_p;
  136. typedef property < graph_name_t, std::string > graph_p;
  137. struct vertex_p_bundled {std::string name; float color;};
  138. struct edge_p_bundled {double weight;};
  139. // Basic directed graph tests
  140. BOOST_AUTO_TEST_CASE (basic_directed_graph_1) {
  141. mass_map_t masses;
  142. insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f);
  143. gs_t gs("digraph { a node [mass = 7.7] c e [mass = 6.66] }");
  144. typedef adjacency_list < vecS, vecS, directedS,
  145. vertex_p, edge_p, graph_p > graph_t;
  146. BOOST_CHECK((test_graph<graph_t>(gs,3,masses,weight_map_t())));
  147. }
  148. BOOST_AUTO_TEST_CASE (basic_directed_graph_2) {
  149. mass_map_t masses;
  150. insert ( masses ) ("a",0.0f) ("e", 6.66f);
  151. gs_t gs("digraph { a node [mass = 7.7] \"a\" e [mass = 6.66] }");
  152. typedef adjacency_list < vecS, vecS, directedS,
  153. vertex_p, edge_p, graph_p > graph_t;
  154. BOOST_CHECK((test_graph<graph_t>(gs,2,masses,weight_map_t())));
  155. }
  156. BOOST_AUTO_TEST_CASE (basic_directed_graph_3) {
  157. weight_map_t weights;
  158. insert( weights )(make_pair("a","b"),0.0)
  159. (make_pair("c","d"),7.7)(make_pair("e","f"),6.66)
  160. (make_pair("d","e"),0.5)(make_pair("e","a"),0.5);
  161. gs_t gs("digraph { a -> b eDge [weight = 7.7] "
  162. "c -> d e-> f [weight = 6.66] "
  163. "d ->e->a [weight=.5]}");
  164. typedef adjacency_list < vecS, vecS, directedS,
  165. vertex_p, edge_p, graph_p > graph_t;
  166. BOOST_CHECK((test_graph<graph_t>(gs,6,mass_map_t(),weights)));
  167. }
  168. // undirected graph with alternate node_id property name
  169. BOOST_AUTO_TEST_CASE (undirected_graph_alternate_node_id) {
  170. mass_map_t masses;
  171. insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f);
  172. gs_t gs("graph { a node [mass = 7.7] c e [mass = 6.66] }");
  173. typedef adjacency_list < vecS, vecS, undirectedS,
  174. vertex_p, edge_p, graph_p > graph_t;
  175. BOOST_CHECK((test_graph<graph_t>(gs,3,masses,weight_map_t(),
  176. "nodenames")));
  177. }
  178. // Basic undirected graph tests
  179. BOOST_AUTO_TEST_CASE (basic_undirected_graph_1) {
  180. mass_map_t masses;
  181. insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f);
  182. gs_t gs("graph { a node [mass = 7.7] c e [mass =\\\n6.66] }");
  183. typedef adjacency_list < vecS, vecS, undirectedS,
  184. vertex_p, edge_p, graph_p > graph_t;
  185. BOOST_CHECK((test_graph<graph_t>(gs,3,masses,weight_map_t())));
  186. }
  187. BOOST_AUTO_TEST_CASE (basic_undirected_graph_2) {
  188. weight_map_t weights;
  189. insert( weights )(make_pair("a","b"),0.0)
  190. (make_pair("c","d"),7.7)(make_pair("e","f"),6.66);
  191. gs_t gs("graph { a -- b eDge [weight = 7.7] "
  192. "c -- d e -- f [weight = 6.66] }");
  193. typedef adjacency_list < vecS, vecS, undirectedS,
  194. vertex_p, edge_p, graph_p > graph_t;
  195. BOOST_CHECK((test_graph<graph_t>(gs,6,mass_map_t(),weights)));
  196. }
  197. // Mismatch directed graph test
  198. BOOST_AUTO_TEST_CASE (mismatch_directed_graph) {
  199. mass_map_t masses;
  200. insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f);
  201. gs_t gs("graph { a nodE [mass = 7.7] c e [mass = 6.66] }");
  202. try {
  203. typedef adjacency_list < vecS, vecS, directedS,
  204. vertex_p, edge_p, graph_p > graph_t;
  205. test_graph<graph_t>(gs,3,masses,weight_map_t());
  206. BOOST_ERROR("Failed to throw boost::undirected_graph_error.");
  207. } catch (boost::undirected_graph_error&) {
  208. } catch (boost::directed_graph_error&) {
  209. BOOST_ERROR("Threw boost::directed_graph_error, should have thrown boost::undirected_graph_error.");
  210. }
  211. }
  212. // Mismatch undirected graph test
  213. BOOST_AUTO_TEST_CASE (mismatch_undirected_graph) {
  214. mass_map_t masses;
  215. insert ( masses ) ("a",0.0f) ("c",7.7f) ("e", 6.66f);
  216. gs_t gs("digraph { a node [mass = 7.7] c e [mass = 6.66] }");
  217. try {
  218. typedef adjacency_list < vecS, vecS, undirectedS,
  219. vertex_p, edge_p, graph_p > graph_t;
  220. test_graph<graph_t>(gs,3,masses,weight_map_t());
  221. BOOST_ERROR("Failed to throw boost::directed_graph_error.");
  222. } catch (boost::directed_graph_error&) {}
  223. }
  224. // Complain about parallel edges
  225. BOOST_AUTO_TEST_CASE (complain_about_parallel_edges) {
  226. BOOST_TEST_CHECKPOINT("Complain about parallel edges");
  227. weight_map_t weights;
  228. insert( weights )(make_pair("a","b"),7.7);
  229. gs_t gs("diGraph { a -> b [weight = 7.7] a -> b [weight = 7.7] }");
  230. try {
  231. typedef adjacency_list < setS, vecS, directedS,
  232. vertex_p, edge_p, graph_p > graph_t;
  233. test_graph<graph_t>(gs,2,mass_map_t(),weights);
  234. BOOST_ERROR("Failed to throw boost::bad_parallel_edge.");
  235. } catch (boost::bad_parallel_edge&) {}
  236. }
  237. // Handle parallel edges gracefully
  238. BOOST_AUTO_TEST_CASE (handle_parallel_edges_gracefully) {
  239. weight_map_t weights;
  240. insert( weights )(make_pair("a","b"),7.7);
  241. gs_t gs("digraph { a -> b [weight = 7.7] a -> b [weight = 7.7] }");
  242. typedef adjacency_list < vecS, vecS, directedS,
  243. vertex_p, edge_p, graph_p > graph_t;
  244. BOOST_CHECK((test_graph<graph_t>(gs,2,mass_map_t(),weights)));
  245. }
  246. // Graph Property Test 1
  247. BOOST_AUTO_TEST_CASE (graph_property_test_1) {
  248. mass_map_t masses;
  249. insert ( masses ) ("a",0.0f) ("c",0.0f) ("e", 6.66f);
  250. gs_t gs("digraph { graph [name=\"foo \\\"escaped\\\"\"] a c e [mass = 6.66] }");
  251. std::string graph_name("foo \"escaped\"");
  252. typedef adjacency_list < vecS, vecS, directedS,
  253. vertex_p, edge_p, graph_p > graph_t;
  254. BOOST_CHECK((test_graph<graph_t>(gs,3,masses,weight_map_t(),"",
  255. graph_name)));
  256. }
  257. // Graph Property Test 2
  258. BOOST_AUTO_TEST_CASE (graph_property_test_2) {
  259. mass_map_t masses;
  260. insert ( masses ) ("a",0.0f) ("c",0.0f) ("e", 6.66f);
  261. gs_t gs("digraph { name=\"fo\"+ \"\\\no\" a c e [mass = 6.66] }");
  262. std::string graph_name("foo");
  263. typedef adjacency_list < vecS, vecS, directedS,
  264. vertex_p, edge_p, graph_p > graph_t;
  265. BOOST_CHECK((test_graph<graph_t>(gs,3,masses,weight_map_t(),"",
  266. graph_name)));
  267. }
  268. // Graph Property Test 3 (HTML)
  269. BOOST_AUTO_TEST_CASE (graph_property_test_3) {
  270. mass_map_t masses;
  271. insert ( masses ) ("a",0.0f) ("c",0.0f) ("e", 6.66f);
  272. std::string graph_name = "<html title=\"x'\" title2='y\"'>foo<b><![CDATA[><bad tag&>]]>bar</b>\n<br/>\nbaz</html>";
  273. gs_t gs("digraph { name=" + graph_name + " a c e [mass = 6.66] }");
  274. typedef adjacency_list < vecS, vecS, directedS,
  275. vertex_p, edge_p, graph_p > graph_t;
  276. BOOST_CHECK((test_graph<graph_t>(gs,3,masses,weight_map_t(),"",
  277. graph_name)));
  278. }
  279. // Comments embedded in strings
  280. BOOST_AUTO_TEST_CASE (comments_embedded_in_strings) {
  281. gs_t gs(
  282. "digraph { "
  283. "a0 [ label = \"//depot/path/to/file_14#4\" ];"
  284. "a1 [ label = \"//depot/path/to/file_29#9\" ];"
  285. "a0 -> a1 [ color=gray ];"
  286. "}");
  287. typedef adjacency_list < vecS, vecS, directedS,
  288. vertex_p, edge_p, graph_p > graph_t;
  289. BOOST_CHECK((test_graph<graph_t>(gs,2,mass_map_t(),weight_map_t())));
  290. }
  291. #if 0 // Currently broken
  292. BOOST_AUTO_TEST_CASE (basic_csr_directed_graph) {
  293. weight_map_t weights;
  294. insert( weights )(make_pair("a","b"),0.0)
  295. (make_pair("c","d"),7.7)(make_pair("e","f"),6.66)
  296. (make_pair("d","e"),0.5)(make_pair("e","a"),0.5);
  297. gs_t gs("digraph { a -> b eDge [weight = 7.7] "
  298. "c -> d e-> f [weight = 6.66] "
  299. "d ->e->a [weight=.5]}");
  300. typedef compressed_sparse_row_graph<directedS, vertex_p_bundled, edge_p_bundled, graph_p > graph_t;
  301. BOOST_CHECK((test_graph<graph_t>(gs,6,mass_map_t(),weights,"node_id","",&vertex_p_bundled::name,&vertex_p_bundled::color,&edge_p_bundled::weight)));
  302. }
  303. #endif
  304. BOOST_AUTO_TEST_CASE (basic_csr_directed_graph_ext_props) {
  305. weight_map_t weights;
  306. insert( weights )(make_pair("a","b"),0.0)
  307. (make_pair("c","d"),7.7)(make_pair("e","f"),6.66)
  308. (make_pair("d","e"),0.5)(make_pair("e","a"),0.5);
  309. gs_t gs("digraph { a -> b eDge [weight = 7.7] "
  310. "c -> d e-> f [weight = 6.66] "
  311. "d ->e->a [weight=.5]}");
  312. typedef compressed_sparse_row_graph<directedS, no_property, no_property, graph_p > graph_t;
  313. graph_t g;
  314. vector_property_map<std::string, property_map<graph_t, vertex_index_t>::const_type> vertex_name(get(vertex_index, g));
  315. vector_property_map<float, property_map<graph_t, vertex_index_t>::const_type> vertex_color(get(vertex_index, g));
  316. vector_property_map<double, property_map<graph_t, edge_index_t>::const_type> edge_weight(get(edge_index, g));
  317. BOOST_CHECK((test_graph(gs,g,6,mass_map_t(),weights,"node_id","",vertex_name,vertex_color,edge_weight)));
  318. }
  319. // return 0;
  320. // }