sl@0: // Boost Graph library sl@0: sl@0: // Copyright Douglas Gregor 2004. Use, modification and sl@0: // distribution is subject to the Boost Software License, Version sl@0: // 1.0. (See accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: /* sl@0: * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #ifdef __SYMBIAN32__ sl@0: #include "std_log_result.h" sl@0: #define LOG_FILENAME_LINE __FILE__, __LINE__ sl@0: #endif sl@0: using namespace std; sl@0: using namespace boost; sl@0: sl@0: struct City sl@0: { sl@0: City() {} sl@0: City(const std::string& name, int pop, int zipcode) : name(name), population(pop) sl@0: { sl@0: zipcodes.push_back(zipcode); sl@0: } sl@0: sl@0: string name; sl@0: int population; sl@0: vector zipcodes; sl@0: }; sl@0: sl@0: std::ostream& operator<<(std::ostream& out, const City& city) sl@0: { sl@0: out << city.name << ' ' << city.population << ' '; sl@0: copy(city.zipcodes.begin(), city.zipcodes.end(), sl@0: ostream_iterator(out, " ")); sl@0: out << -1; sl@0: return out; sl@0: } sl@0: sl@0: std::istream& operator>>(std::istream& in, City& city) sl@0: { sl@0: if (in >> city.name >> city.population) { sl@0: int zip; sl@0: city.zipcodes.clear(); sl@0: while (in >> zip && zip != -1) sl@0: city.zipcodes.push_back(zip); sl@0: } sl@0: return in; sl@0: } sl@0: sl@0: bool operator==(const City& c1, const City& c2) sl@0: { sl@0: return (c1.name == c2.name && c1.population == c2.population sl@0: && c1.zipcodes == c2.zipcodes); sl@0: } sl@0: sl@0: struct Highway sl@0: { sl@0: Highway() {} sl@0: Highway(const string& name, double miles, int speed_limit = 65, int lanes = 4, bool divided = true) sl@0: : name(name), miles(miles), speed_limit(speed_limit), lanes(lanes), divided(divided) {} sl@0: sl@0: string name; sl@0: double miles; sl@0: int speed_limit; sl@0: int lanes; sl@0: bool divided; sl@0: }; sl@0: sl@0: std::ostream& operator<<(std::ostream& out, const Highway& highway) sl@0: { sl@0: return out << highway.name << ' ' << highway.miles << ' ' << highway.miles sl@0: << ' ' << highway.speed_limit << ' ' << highway.lanes sl@0: << ' ' << highway.divided; sl@0: } sl@0: sl@0: std::istream& operator>>(std::istream& in, Highway& highway) sl@0: { sl@0: return in >> highway.name >> highway.miles >> highway.miles sl@0: >> highway.speed_limit >> highway.lanes sl@0: >> highway.divided; sl@0: } sl@0: sl@0: bool operator==(const Highway& h1, const Highway& h2) sl@0: { sl@0: return (h1.name == h2.name && h1.miles == h2.miles sl@0: && h1.speed_limit == h2.speed_limit && h1.lanes == h2.lanes sl@0: && h1.divided == h2.divided); sl@0: } sl@0: sl@0: template struct truth {}; sl@0: sl@0: template sl@0: typename boost::graph_traits::vertex_descriptor sl@0: do_add_vertex(Map& map, VertexIterator, const Bundle& bundle, truth) sl@0: { sl@0: return add_vertex(bundle, map); sl@0: } sl@0: sl@0: template sl@0: typename boost::graph_traits::vertex_descriptor sl@0: do_add_vertex(Map& map, VertexIterator& vi, const Bundle& bundle, truth) sl@0: { sl@0: get(boost::vertex_bundle, map)[*vi] = bundle; sl@0: return *vi++; sl@0: } sl@0: sl@0: template sl@0: void test_io(adjacency_list& map, int) sl@0: { sl@0: typedef adjacency_list Map; sl@0: sl@0: ostringstream out; sl@0: cout << write(map); sl@0: out << write(map); sl@0: sl@0: istringstream in(out.str()); sl@0: adjacency_list map2; sl@0: in >> read(map2); sl@0: typename graph_traits >::vertex_iterator sl@0: v2 = vertices(map2).first; sl@0: BGL_FORALL_VERTICES_T(v, map, Map) { sl@0: BOOST_CHECK(map[v] == map2[*v2]); sl@0: typename graph_traits >::out_edge_iterator sl@0: e2 = out_edges(*v2, map2).first; sl@0: BGL_FORALL_OUTEDGES_T(v, e, map, Map) { sl@0: BOOST_CHECK(map[e] == map[*e2]); sl@0: ++e2; sl@0: } sl@0: ++v2; sl@0: } sl@0: } sl@0: sl@0: template sl@0: void test_io(const Map&, long) sl@0: { sl@0: // Nothing to test sl@0: } sl@0: sl@0: template sl@0: void test_bundled_properties(Map*, truth can_add_vertex) sl@0: { sl@0: typedef typename boost::graph_traits::vertex_iterator vertex_iterator; sl@0: typedef typename boost::graph_traits::vertex_descriptor vertex_descriptor; sl@0: typedef typename boost::graph_traits::edge_descriptor edge_descriptor; sl@0: sl@0: Map map(CanAddVertex? 2 : 3); sl@0: sl@0: vertex_iterator vi = vertices(map).first; sl@0: vertex_descriptor v = *vi; sl@0: map[v].name = "Troy"; sl@0: map[v].population = 49170; sl@0: map[v].zipcodes.push_back(12180); sl@0: sl@0: ++vi; sl@0: vertex_descriptor u = *vi++; sl@0: map[u].name = "Albany"; sl@0: map[u].population = 95658; sl@0: map[u].zipcodes.push_back(12201); sl@0: sl@0: // Try adding a vertex with a property value sl@0: vertex_descriptor bloomington = do_add_vertex(map, vi, City("Bloomington", 39000, 47401), sl@0: can_add_vertex); sl@0: BOOST_CHECK(get(boost::vertex_bundle, map)[bloomington].zipcodes[0] == 47401); sl@0: sl@0: edge_descriptor e = add_edge(v, u, map).first; sl@0: map[e].name = "I-87"; sl@0: map[e].miles = 10; sl@0: map[e].speed_limit = 65; sl@0: map[e].lanes = 4; sl@0: map[e].divided = true; sl@0: sl@0: edge_descriptor our_trip = add_edge(v, bloomington, Highway("Long", 1000), map).first; sl@0: BOOST_CHECK(get(boost::edge_bundle, map, our_trip).miles == 1000); sl@0: sl@0: BOOST_CHECK(get(get(&City::name, map), v) == "Troy"); sl@0: BOOST_CHECK(get(get(&Highway::name, map), e) == "I-87"); sl@0: BOOST_CHECK(get(&City::name, map, u) == "Albany"); sl@0: BOOST_CHECK(get(&Highway::name, map, e) == "I-87"); sl@0: put(&City::population, map, v, 49168); sl@0: BOOST_CHECK(get(&City::population, map)[v] == 49168); sl@0: sl@0: boost::filtered_graph fmap(map, boost::keep_all()); sl@0: BOOST_CHECK(get(boost::edge_bundle, map, our_trip).miles == 1000); sl@0: sl@0: BOOST_CHECK(get(get(&City::name, fmap), v) == "Troy"); sl@0: BOOST_CHECK(get(get(&Highway::name, fmap), e) == "I-87"); sl@0: BOOST_CHECK(get(&City::name, fmap, u) == "Albany"); sl@0: BOOST_CHECK(get(&Highway::name, fmap, e) == "I-87"); sl@0: put(&City::population, fmap, v, 49169); sl@0: BOOST_CHECK(get(&City::population, fmap)[v] == 49169); sl@0: sl@0: test_io(map, 0); sl@0: } sl@0: sl@0: void test_subgraph_bundled_properties() sl@0: { sl@0: typedef boost::subgraph< sl@0: boost::adjacency_list > > SubMap; sl@0: typedef boost::graph_traits::vertex_descriptor Vertex; sl@0: typedef boost::graph_traits::vertex_iterator vertex_iterator; sl@0: sl@0: SubMap map(3); sl@0: vertex_iterator vi = vertices(map).first; sl@0: Vertex troy = *vi++; sl@0: map[troy].name = "Troy"; sl@0: map[*vi++].name = "Bloomington"; sl@0: map[*vi++].name = "Endicott"; sl@0: sl@0: SubMap& g1 = map.create_subgraph(); sl@0: Vertex troy1 = add_vertex(*vertices(map).first, g1); sl@0: BOOST_CHECK(map[troy1].name == g1[troy1].name); sl@0: } sl@0: sl@0: int test_main(int, char*[]) sl@0: { sl@0: typedef boost::adjacency_list< sl@0: boost::listS, boost::vecS, boost::bidirectionalS, sl@0: City, Highway> Map1; sl@0: typedef boost::adjacency_matrix Map2; sl@0: sl@0: test_bundled_properties(static_cast(0), truth()); sl@0: test_bundled_properties(static_cast(0), truth()); sl@0: test_subgraph_bundled_properties(); sl@0: sl@0: #ifdef __SYMBIAN32__ sl@0: std_log(LOG_FILENAME_LINE,"[End Test Case ]"); sl@0: sl@0: testResultXml("bundled_properties"); sl@0: close_log_file(); sl@0: #endif sl@0: return 0; sl@0: }