Update contrib.
3 // Copyright Douglas Gregor 2004. Use, modification and
4 // distribution is subject to the Boost Software License, Version
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved.
11 #include <boost/test/minimal.hpp>
12 #include <boost/graph/adjacency_list.hpp>
13 #include <boost/graph/adjacency_matrix.hpp>
14 #include <boost/graph/filtered_graph.hpp>
15 #include <boost/graph/subgraph.hpp>
18 #include <boost/graph/adjacency_list_io.hpp>
20 #include <boost/graph/iteration_macros.hpp>
24 #include "std_log_result.h"
25 #define LOG_FILENAME_LINE __FILE__, __LINE__
28 using namespace boost;
33 City(const std::string& name, int pop, int zipcode) : name(name), population(pop)
35 zipcodes.push_back(zipcode);
43 std::ostream& operator<<(std::ostream& out, const City& city)
45 out << city.name << ' ' << city.population << ' ';
46 copy(city.zipcodes.begin(), city.zipcodes.end(),
47 ostream_iterator<int>(out, " "));
52 std::istream& operator>>(std::istream& in, City& city)
54 if (in >> city.name >> city.population) {
56 city.zipcodes.clear();
57 while (in >> zip && zip != -1)
58 city.zipcodes.push_back(zip);
63 bool operator==(const City& c1, const City& c2)
65 return (c1.name == c2.name && c1.population == c2.population
66 && c1.zipcodes == c2.zipcodes);
72 Highway(const string& name, double miles, int speed_limit = 65, int lanes = 4, bool divided = true)
73 : name(name), miles(miles), speed_limit(speed_limit), lanes(lanes), divided(divided) {}
82 std::ostream& operator<<(std::ostream& out, const Highway& highway)
84 return out << highway.name << ' ' << highway.miles << ' ' << highway.miles
85 << ' ' << highway.speed_limit << ' ' << highway.lanes
86 << ' ' << highway.divided;
89 std::istream& operator>>(std::istream& in, Highway& highway)
91 return in >> highway.name >> highway.miles >> highway.miles
92 >> highway.speed_limit >> highway.lanes
96 bool operator==(const Highway& h1, const Highway& h2)
98 return (h1.name == h2.name && h1.miles == h2.miles
99 && h1.speed_limit == h2.speed_limit && h1.lanes == h2.lanes
100 && h1.divided == h2.divided);
103 template<bool> struct truth {};
105 template<typename Map, typename VertexIterator, typename Bundle>
106 typename boost::graph_traits<Map>::vertex_descriptor
107 do_add_vertex(Map& map, VertexIterator, const Bundle& bundle, truth<true>)
109 return add_vertex(bundle, map);
112 template<typename Map, typename VertexIterator, typename Bundle>
113 typename boost::graph_traits<Map>::vertex_descriptor
114 do_add_vertex(Map& map, VertexIterator& vi, const Bundle& bundle, truth<false>)
116 get(boost::vertex_bundle, map)[*vi] = bundle;
120 template<class EL, class VL, class D, class VP, class EP, class GP>
121 void test_io(adjacency_list<EL,VL,D,VP,EP,GP>& map, int)
123 typedef adjacency_list<EL,VL,D,VP,EP,GP> Map;
129 istringstream in(out.str());
130 adjacency_list<EL,VL,D,VP,EP,GP> map2;
132 typename graph_traits<adjacency_list<EL,VL,D,VP,EP,GP> >::vertex_iterator
133 v2 = vertices(map2).first;
134 BGL_FORALL_VERTICES_T(v, map, Map) {
135 BOOST_CHECK(map[v] == map2[*v2]);
136 typename graph_traits<adjacency_list<EL,VL,D,VP,EP,GP> >::out_edge_iterator
137 e2 = out_edges(*v2, map2).first;
138 BGL_FORALL_OUTEDGES_T(v, e, map, Map) {
139 BOOST_CHECK(map[e] == map[*e2]);
146 template<typename Map>
147 void test_io(const Map&, long)
152 template<typename Map, bool CanAddVertex>
153 void test_bundled_properties(Map*, truth<CanAddVertex> can_add_vertex)
155 typedef typename boost::graph_traits<Map>::vertex_iterator vertex_iterator;
156 typedef typename boost::graph_traits<Map>::vertex_descriptor vertex_descriptor;
157 typedef typename boost::graph_traits<Map>::edge_descriptor edge_descriptor;
159 Map map(CanAddVertex? 2 : 3);
161 vertex_iterator vi = vertices(map).first;
162 vertex_descriptor v = *vi;
163 map[v].name = "Troy";
164 map[v].population = 49170;
165 map[v].zipcodes.push_back(12180);
168 vertex_descriptor u = *vi++;
169 map[u].name = "Albany";
170 map[u].population = 95658;
171 map[u].zipcodes.push_back(12201);
173 // Try adding a vertex with a property value
174 vertex_descriptor bloomington = do_add_vertex(map, vi, City("Bloomington", 39000, 47401),
176 BOOST_CHECK(get(boost::vertex_bundle, map)[bloomington].zipcodes[0] == 47401);
178 edge_descriptor e = add_edge(v, u, map).first;
179 map[e].name = "I-87";
181 map[e].speed_limit = 65;
183 map[e].divided = true;
185 edge_descriptor our_trip = add_edge(v, bloomington, Highway("Long", 1000), map).first;
186 BOOST_CHECK(get(boost::edge_bundle, map, our_trip).miles == 1000);
188 BOOST_CHECK(get(get(&City::name, map), v) == "Troy");
189 BOOST_CHECK(get(get(&Highway::name, map), e) == "I-87");
190 BOOST_CHECK(get(&City::name, map, u) == "Albany");
191 BOOST_CHECK(get(&Highway::name, map, e) == "I-87");
192 put(&City::population, map, v, 49168);
193 BOOST_CHECK(get(&City::population, map)[v] == 49168);
195 boost::filtered_graph<Map, boost::keep_all> fmap(map, boost::keep_all());
196 BOOST_CHECK(get(boost::edge_bundle, map, our_trip).miles == 1000);
198 BOOST_CHECK(get(get(&City::name, fmap), v) == "Troy");
199 BOOST_CHECK(get(get(&Highway::name, fmap), e) == "I-87");
200 BOOST_CHECK(get(&City::name, fmap, u) == "Albany");
201 BOOST_CHECK(get(&Highway::name, fmap, e) == "I-87");
202 put(&City::population, fmap, v, 49169);
203 BOOST_CHECK(get(&City::population, fmap)[v] == 49169);
208 void test_subgraph_bundled_properties()
210 typedef boost::subgraph<
211 boost::adjacency_list<boost::vecS, boost::vecS,
212 boost::bidirectionalS, City,
213 boost::property<boost::edge_index_t, int,
215 typedef boost::graph_traits<SubMap>::vertex_descriptor Vertex;
216 typedef boost::graph_traits<SubMap>::vertex_iterator vertex_iterator;
219 vertex_iterator vi = vertices(map).first;
221 map[troy].name = "Troy";
222 map[*vi++].name = "Bloomington";
223 map[*vi++].name = "Endicott";
225 SubMap& g1 = map.create_subgraph();
226 Vertex troy1 = add_vertex(*vertices(map).first, g1);
227 BOOST_CHECK(map[troy1].name == g1[troy1].name);
230 int test_main(int, char*[])
232 typedef boost::adjacency_list<
233 boost::listS, boost::vecS, boost::bidirectionalS,
235 typedef boost::adjacency_matrix<boost::directedS,
238 test_bundled_properties(static_cast<Map1*>(0), truth<true>());
239 test_bundled_properties(static_cast<Map2*>(0), truth<false>());
240 test_subgraph_bundled_properties();
243 std_log(LOG_FILENAME_LINE,"[End Test Case ]");
245 testResultXml("bundled_properties");