os/ossrv/stdcpp/tsrc/Boost_test/graph/src/bundled_properties.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Boost Graph library
     2 
     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)
     7 /*
     8  * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
     9 */
    10 
    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>
    16 #include <string>
    17 #include <vector>
    18 #include <boost/graph/adjacency_list_io.hpp>
    19 #include <sstream>
    20 #include <boost/graph/iteration_macros.hpp>
    21 #include <algorithm>
    22 #include <iterator>
    23 #ifdef __SYMBIAN32__
    24 #include "std_log_result.h"
    25 #define LOG_FILENAME_LINE __FILE__, __LINE__
    26 #endif
    27 using namespace std;
    28 using namespace boost;
    29 
    30 struct City
    31 {
    32   City() {}
    33   City(const std::string& name, int pop, int zipcode) : name(name), population(pop)
    34   { 
    35     zipcodes.push_back(zipcode); 
    36   }
    37 
    38   string name;
    39   int population;
    40   vector<int> zipcodes;
    41 };
    42 
    43 std::ostream& operator<<(std::ostream& out, const City& city)
    44 {
    45   out << city.name << ' ' << city.population << ' ';
    46   copy(city.zipcodes.begin(), city.zipcodes.end(),
    47        ostream_iterator<int>(out, " "));
    48   out << -1;
    49   return out;
    50 }
    51 
    52 std::istream& operator>>(std::istream& in, City& city)
    53 {
    54   if (in >> city.name >> city.population) {
    55     int zip;
    56     city.zipcodes.clear();
    57     while (in >> zip && zip != -1)
    58       city.zipcodes.push_back(zip);
    59   }
    60   return in;
    61 }
    62 
    63 bool operator==(const City& c1, const City& c2)
    64 {
    65   return (c1.name == c2.name && c1.population == c2.population
    66           && c1.zipcodes == c2.zipcodes);
    67 }
    68 
    69 struct Highway
    70 {
    71   Highway() {}
    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) {}
    74 
    75   string name;
    76   double miles;
    77   int speed_limit;
    78   int lanes;
    79   bool divided;
    80 };
    81 
    82 std::ostream& operator<<(std::ostream& out, const Highway& highway)
    83 {
    84   return out << highway.name << ' ' << highway.miles << ' ' << highway.miles
    85              << ' ' << highway.speed_limit << ' ' << highway.lanes
    86              << ' ' << highway.divided;
    87 }
    88 
    89 std::istream& operator>>(std::istream& in, Highway& highway)
    90 {
    91   return in >> highway.name >> highway.miles >> highway.miles
    92             >> highway.speed_limit  >> highway.lanes
    93             >> highway.divided;
    94 }
    95 
    96 bool operator==(const Highway& h1, const Highway& h2)
    97 {
    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);
   101 }
   102 
   103 template<bool> struct truth {};
   104 
   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>)
   108 {
   109   return add_vertex(bundle, map);
   110 }
   111 
   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>)
   115 {
   116   get(boost::vertex_bundle, map)[*vi] = bundle;
   117   return *vi++;
   118 }
   119 
   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)
   122 {
   123   typedef adjacency_list<EL,VL,D,VP,EP,GP> Map;
   124 
   125   ostringstream out;
   126   cout << write(map);
   127   out << write(map);
   128   
   129   istringstream in(out.str());
   130   adjacency_list<EL,VL,D,VP,EP,GP> map2;
   131   in >> read(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]);
   140       ++e2;
   141     }
   142     ++v2;
   143   }
   144 }
   145 
   146 template<typename Map>
   147 void test_io(const Map&, long)
   148 {
   149   // Nothing to test
   150 }
   151 
   152 template<typename Map, bool CanAddVertex>
   153 void test_bundled_properties(Map*, truth<CanAddVertex> can_add_vertex)
   154 {
   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;
   158 
   159   Map map(CanAddVertex? 2 : 3);
   160 
   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);
   166 
   167   ++vi;
   168   vertex_descriptor u = *vi++;
   169   map[u].name = "Albany";
   170   map[u].population = 95658;
   171   map[u].zipcodes.push_back(12201);
   172 
   173   // Try adding a vertex with a property value
   174   vertex_descriptor bloomington = do_add_vertex(map, vi, City("Bloomington", 39000, 47401),
   175                                                 can_add_vertex);
   176   BOOST_CHECK(get(boost::vertex_bundle, map)[bloomington].zipcodes[0] == 47401);
   177   
   178   edge_descriptor e = add_edge(v, u, map).first;
   179   map[e].name = "I-87";
   180   map[e].miles = 10;
   181   map[e].speed_limit = 65;
   182   map[e].lanes = 4;
   183   map[e].divided = true;
   184 
   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);
   187   
   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);
   194   
   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);
   197   
   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);
   204 
   205   test_io(map, 0);
   206 }
   207 
   208 void test_subgraph_bundled_properties()
   209 {
   210   typedef boost::subgraph<
   211             boost::adjacency_list<boost::vecS, boost::vecS, 
   212                                   boost::bidirectionalS, City, 
   213                                   boost::property<boost::edge_index_t, int,
   214                                                   Highway> > > SubMap;
   215   typedef boost::graph_traits<SubMap>::vertex_descriptor Vertex;
   216   typedef boost::graph_traits<SubMap>::vertex_iterator vertex_iterator;
   217 
   218   SubMap map(3);
   219   vertex_iterator vi = vertices(map).first;
   220   Vertex troy = *vi++;
   221   map[troy].name = "Troy";
   222   map[*vi++].name = "Bloomington";
   223   map[*vi++].name = "Endicott";
   224 
   225   SubMap& g1 = map.create_subgraph();
   226   Vertex troy1 = add_vertex(*vertices(map).first, g1);
   227   BOOST_CHECK(map[troy1].name == g1[troy1].name);
   228 }
   229 
   230 int test_main(int, char*[])
   231 {
   232   typedef boost::adjacency_list<
   233     boost::listS, boost::vecS, boost::bidirectionalS,
   234     City, Highway> Map1;
   235   typedef boost::adjacency_matrix<boost::directedS,
   236     City, Highway> Map2;
   237 
   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();
   241   
   242    #ifdef __SYMBIAN32__
   243 	std_log(LOG_FILENAME_LINE,"[End Test Case ]");
   244 
   245 	testResultXml("bundled_properties");
   246 	close_log_file();
   247 #endif
   248   return 0;
   249 }