graphml_test.cpp 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (C) 2006 Tiago de Paula Peixoto <tiago@forked.de>
  2. //
  3. // Boost Software License - Version 1.0 - August 17th, 2003
  4. //
  5. // Permission is hereby granted, free of charge, to any person or organization
  6. // obtaining a copy of the software and accompanying documentation covered by
  7. // this license (the "Software") to use, reproduce, display, distribute,
  8. // execute, and transmit the Software, and to prepare derivative works of the
  9. // Software, and to permit third-parties to whom the Software is furnished to
  10. // do so, all subject to the following:
  11. //
  12. // The copyright notices in the Software and this entire statement, including
  13. // the above license grant, this restriction and the following disclaimer,
  14. // must be included in all copies of the Software, in whole or in part, and
  15. // all derivative works of the Software, unless such copies or derivative
  16. // works are solely in the form of machine-executable object code generated by
  17. // a source language processor.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  22. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  23. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  24. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. // DEALINGS IN THE SOFTWARE.
  26. // Authors: Tiago de Paula Peixoto
  27. #include <boost/graph/adjacency_list.hpp>
  28. #include <boost/graph/graphml.hpp>
  29. #include <boost/test/minimal.hpp>
  30. #include <cmath>
  31. #include <fstream>
  32. #include <string>
  33. using namespace std;
  34. using namespace boost;
  35. int test_main(int argc, char** argv)
  36. {
  37. typedef adjacency_list<vecS,vecS,directedS,
  38. property<vertex_color_t,int,
  39. property<vertex_name_t,string> >,
  40. property<edge_weight_t,double>,
  41. property<graph_name_t, string> > graph_t;
  42. graph_t g;
  43. dynamic_properties dp;
  44. dp.property("foo",get(vertex_color_t(),g));
  45. dp.property("weight",get(edge_weight_t(),g));
  46. dp.property("name",get(vertex_name_t(),g));
  47. boost::ref_property_map<graph_t*,std::string>
  48. gname(get_property(g, graph_name));
  49. dp.property("description",gname);
  50. ifstream ifile(argv[1]);
  51. read_graphml(ifile, g, dp);
  52. ifile.close();
  53. BOOST_CHECK(num_vertices(g) == 9);
  54. BOOST_CHECK(num_edges(g) == 9);
  55. BOOST_CHECK(get(vertex_color_t(), g, vertex(2,g)) == 100);
  56. BOOST_CHECK(get(vertex_color_t(), g, vertex(3,g)) == 42);
  57. BOOST_CHECK(std::abs(get(edge_weight_t(), g, edge(vertex(0,g),vertex(1,g),g).first) - 0.0) < 0.00001);
  58. BOOST_CHECK(std::abs(get(edge_weight_t(), g, edge(vertex(1,g),vertex(2,g),g).first) - 0.8) < 0.00001);
  59. BOOST_CHECK(get("description", dp, &g) == "Root graph.");
  60. ofstream ofile("graphml_test_out.xml");
  61. write_graphml(ofile, g, dp);
  62. ofile.close();
  63. graph_t g2;
  64. dynamic_properties dp2;
  65. dp2.property("foo",get(vertex_color_t(),g2));
  66. dp2.property("weight",get(edge_weight_t(),g2));
  67. dp2.property("name",get(vertex_name_t(),g2));
  68. boost::ref_property_map<graph_t*,std::string>
  69. gname2(get_property(g2, graph_name));
  70. dp2.property("description",gname2);
  71. ifile.open("graphml_test_out.xml");
  72. read_graphml(ifile, g2, dp2);
  73. ifile.close();
  74. BOOST_CHECK(num_vertices(g) == num_vertices(g2));
  75. BOOST_CHECK(num_edges(g) == num_edges(g2));
  76. BOOST_CHECK(get("description", dp, &g) == get("description", dp2, &g2));
  77. graph_traits<graph_t>::vertex_iterator v, v_end;
  78. for (boost::tie(v,v_end) = vertices(g); v != v_end; ++v)
  79. BOOST_CHECK(get(vertex_color_t(), g, *v) == get(vertex_color_t(), g2, *v));
  80. graph_traits<graph_t>::edge_iterator e, e_end;
  81. for (boost::tie(e,e_end) = edges(g); e != e_end; ++e)
  82. BOOST_CHECK(std::abs(get(edge_weight_t(), g, *e) - get(edge_weight_t(), g2, *e)) < 0.00001);
  83. return 0;
  84. }