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 | |
27 | // Authors: Tiago de Paula Peixoto |
28 | |
29 | #include <boost/graph/adjacency_list.hpp> |
30 | #include <boost/graph/graphml.hpp> |
31 | #include <boost/core/lightweight_test.hpp> |
32 | #include <cmath> |
33 | #include <fstream> |
34 | #include <string> |
35 | |
36 | using namespace std; |
37 | using namespace boost; |
38 | |
39 | int main(int argc, char** argv) |
40 | { |
41 | typedef adjacency_list< vecS, vecS, directedS, |
42 | property< vertex_color_t, int, property< vertex_name_t, string > >, |
43 | property< edge_weight_t, double >, property< graph_name_t, string > > |
44 | graph_t; |
45 | graph_t g; |
46 | dynamic_properties dp; |
47 | dp.property(name: "foo" , property_map_: get(p: vertex_color_t(), g)); |
48 | dp.property(name: "weight" , property_map_: get(p: edge_weight_t(), g)); |
49 | dp.property(name: "name" , property_map_: get(p: vertex_name_t(), g)); |
50 | boost::ref_property_map< graph_t*, std::string > gname( |
51 | get_property(g, tag: graph_name)); |
52 | dp.property(name: "description" , property_map_: gname); |
53 | |
54 | ifstream ifile(argv[1]); |
55 | read_graphml(in&: ifile, g, dp); |
56 | ifile.close(); |
57 | |
58 | BOOST_TEST(num_vertices(g) == 9); |
59 | BOOST_TEST(num_edges(g) == 9); |
60 | BOOST_TEST(get(vertex_color_t(), g, vertex(2, g)) == 100); |
61 | BOOST_TEST(get(vertex_color_t(), g, vertex(3, g)) == 42); |
62 | BOOST_TEST(std::abs(get(edge_weight_t(), g, |
63 | edge(vertex(0, g), vertex(1, g), g).first) |
64 | - 0.0) |
65 | < 0.00001); |
66 | BOOST_TEST(std::abs(get(edge_weight_t(), g, |
67 | edge(vertex(1, g), vertex(2, g), g).first) |
68 | - 0.8) |
69 | < 0.00001); |
70 | BOOST_TEST(get("description" , dp, &g) == "Root graph." ); |
71 | |
72 | ofstream ofile("graphml_test_out.xml" ); |
73 | write_graphml(out&: ofile, g, dp); |
74 | ofile.close(); |
75 | |
76 | graph_t g2; |
77 | dynamic_properties dp2; |
78 | dp2.property(name: "foo" , property_map_: get(p: vertex_color_t(), g&: g2)); |
79 | dp2.property(name: "weight" , property_map_: get(p: edge_weight_t(), g&: g2)); |
80 | dp2.property(name: "name" , property_map_: get(p: vertex_name_t(), g&: g2)); |
81 | boost::ref_property_map< graph_t*, std::string > gname2( |
82 | get_property(g&: g2, tag: graph_name)); |
83 | dp2.property(name: "description" , property_map_: gname2); |
84 | ifile.open(s: "graphml_test_out.xml" ); |
85 | read_graphml(in&: ifile, g&: g2, dp&: dp2); |
86 | ifile.close(); |
87 | |
88 | BOOST_TEST(num_vertices(g) == num_vertices(g2)); |
89 | BOOST_TEST(num_edges(g) == num_edges(g2)); |
90 | BOOST_TEST(get("description" , dp, &g) == get("description" , dp2, &g2)); |
91 | |
92 | graph_traits< graph_t >::vertex_iterator v, v_end; |
93 | for (boost::tie(t0&: v, t1&: v_end) = vertices(g_: g); v != v_end; ++v) |
94 | BOOST_TEST( |
95 | get(vertex_color_t(), g, *v) == get(vertex_color_t(), g2, *v)); |
96 | |
97 | graph_traits< graph_t >::edge_iterator e, e_end; |
98 | for (boost::tie(t0&: e, t1&: e_end) = edges(g_: g); e != e_end; ++e) |
99 | BOOST_TEST( |
100 | std::abs(get(edge_weight_t(), g, *e) - get(edge_weight_t(), g2, *e)) |
101 | < 0.00001); |
102 | |
103 | return boost::report_errors(); |
104 | } |
105 | |