-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathgraphml_test.cpp
More file actions
201 lines (172 loc) · 8.14 KB
/
Copy pathgraphml_test.cpp
File metadata and controls
201 lines (172 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) 2026 Arnaud Becheler
// Copyright (C) 2006 Tiago de Paula Peixoto <tiago@forked.de>
//
// Boost Software License - Version 1.0 - August 17th, 2003
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// Authors: Tiago de Paula Peixoto
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphml.hpp>
#include <boost/core/lightweight_test.hpp>
#include <cmath>
#include <fstream>
#include <sstream>
#include <string>
// Direct check of the entity escaping applied on the write path.
void test_encode_char_entities()
{
using boost::detail::graphml::encode_char_entities;
// Empty stays empty.
BOOST_TEST_EQ(encode_char_entities(""), std::string(""));
// Ordinary text passes through untouched.
BOOST_TEST_EQ(encode_char_entities("plain text"), std::string("plain text"));
// An all-spaces value keeps its length via a leading numeric entity.
BOOST_TEST_EQ(encode_char_entities(" "), std::string("  "));
// Each reserved character maps to its named entity.
BOOST_TEST_EQ(encode_char_entities("<>&\"'"), std::string("<>&"'"));
// Reserved characters are escaped in place among ordinary ones.
BOOST_TEST_EQ(encode_char_entities("a<b>c&d\"e'f"), std::string("a<b>c&d"e'f"));
}
// A value carrying all five reserved characters survives a write then read.
void test_special_chars_round_trip()
{
using graph_t = boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, boost::property< boost::vertex_name_t, std::string > >;
const std::string special = "a<b>c&d\"e'f";
graph_t g;
auto v = boost::add_vertex(g);
boost::put(boost::vertex_name_t(), g, v, special);
boost::dynamic_properties dp;
dp.property("name", boost::get(boost::vertex_name_t(), g));
std::ostringstream out;
boost::write_graphml(out, g, dp);
graph_t g2;
boost::dynamic_properties dp2;
dp2.property("name", boost::get(boost::vertex_name_t(), g2));
std::istringstream in(out.str());
boost::read_graphml(in, g2, dp2);
auto round_tripped = boost::get(boost::vertex_name_t(), g2, boost::vertex(0, g2));
BOOST_TEST_EQ(boost::num_vertices(g2), std::size_t(1));
BOOST_TEST_EQ(round_tripped, special);
}
// A required XML attribute that is absent raises parse_error.
void test_missing_required_attribute()
{
using graph_t = boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS >;
// The lone edge omits its mandatory source attribute.
const std::string malformed =
"<?xml version=\"1.0\"?>\n"
"<graphml>\n"
" <graph id=\"G\" edgedefault=\"directed\">\n"
" <node id=\"n0\"/>\n"
" <node id=\"n1\"/>\n"
" <edge target=\"n1\"/>\n"
" </graph>\n"
"</graphml>\n";
graph_t g;
boost::dynamic_properties dp;
std::istringstream in(malformed);
BOOST_TEST_THROWS(boost::read_graphml(in, g, dp), boost::parse_error);
}
// Ill-formed XML and a missing graphml root both raise parse_error.
void test_malformed_documents()
{
using graph_t = boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS >;
// Mismatched tags make the underlying XML parser fail.
const std::string ill_formed = "<graphml><graph></graphml>";
{
graph_t g;
boost::dynamic_properties dp;
std::istringstream in(ill_formed);
BOOST_TEST_THROWS(boost::read_graphml(in, g, dp), boost::parse_error);
}
// Well-formed XML whose root is not graphml has no graph to read.
const std::string no_graphml = "<?xml version=\"1.0\"?>\n<root/>\n";
{
graph_t g;
boost::dynamic_properties dp;
std::istringstream in(no_graphml);
BOOST_TEST_THROWS(boost::read_graphml(in, g, dp), boost::parse_error);
}
}
int main(int, char** argv)
{
test_encode_char_entities();
test_special_chars_round_trip();
test_missing_required_attribute();
test_malformed_documents();
using graph_t = boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS,
boost::property< boost::vertex_color_t, int, boost::property< boost::vertex_name_t, std::string > >,
boost::property< boost::edge_weight_t, double >, boost::property< boost::graph_name_t, std::string > >;
graph_t g;
boost::dynamic_properties dp;
dp.property("foo", boost::get(boost::vertex_color_t(), g));
dp.property("weight", boost::get(boost::edge_weight_t(), g));
dp.property("name", boost::get(boost::vertex_name_t(), g));
boost::ref_property_map< graph_t*, std::string > gname(boost::get_property(g, boost::graph_name));
dp.property("description", gname);
std::ifstream ifile(argv[1]);
boost::read_graphml(ifile, g, dp);
ifile.close();
BOOST_TEST(boost::num_vertices(g) == 9);
BOOST_TEST(boost::num_edges(g) == 9);
BOOST_TEST(boost::get(boost::vertex_color_t(), g, boost::vertex(2, g)) == 100);
BOOST_TEST(boost::get(boost::vertex_color_t(), g, boost::vertex(3, g)) == 42);
auto edge_01 = boost::edge(boost::vertex(0, g), boost::vertex(1, g), g).first;
auto weight_01 = boost::get(boost::edge_weight_t(), g, edge_01);
BOOST_TEST(std::abs(weight_01 - 0.0) < 0.00001);
auto edge_12 = boost::edge(boost::vertex(1, g), boost::vertex(2, g), g).first;
auto weight_12 = boost::get(boost::edge_weight_t(), g, edge_12);
BOOST_TEST(std::abs(weight_12 - 0.8) < 0.00001);
BOOST_TEST(boost::get("description", dp, &g) == "Root graph.");
std::ofstream ofile("graphml_test_out.xml");
boost::write_graphml(ofile, g, dp);
ofile.close();
graph_t g2;
boost::dynamic_properties dp2;
dp2.property("foo", boost::get(boost::vertex_color_t(), g2));
dp2.property("weight", boost::get(boost::edge_weight_t(), g2));
dp2.property("name", boost::get(boost::vertex_name_t(), g2));
boost::ref_property_map< graph_t*, std::string > gname2(boost::get_property(g2, boost::graph_name));
dp2.property("description", gname2);
ifile.open("graphml_test_out.xml");
boost::read_graphml(ifile, g2, dp2);
ifile.close();
BOOST_TEST(boost::num_vertices(g) == boost::num_vertices(g2));
BOOST_TEST(boost::num_edges(g) == boost::num_edges(g2));
BOOST_TEST(boost::get("description", dp, &g) == boost::get("description", dp2, &g2));
boost::graph_traits< graph_t >::vertex_iterator v, v_end;
for (boost::tie(v, v_end) = boost::vertices(g); v != v_end; ++v)
{
auto color_1 = boost::get(boost::vertex_color_t(), g, *v);
auto color_2 = boost::get(boost::vertex_color_t(), g2, *v);
BOOST_TEST(color_1 == color_2);
}
boost::graph_traits< graph_t >::edge_iterator e, e_end;
for (boost::tie(e, e_end) = boost::edges(g); e != e_end; ++e)
{
auto weight_1 = boost::get(boost::edge_weight_t(), g, *e);
auto weight_2 = boost::get(boost::edge_weight_t(), g2, *e);
BOOST_TEST(std::abs(weight_1 - weight_2) < 0.00001);
}
return boost::report_errors();
}