os/ossrv/stdcpp/tsrc/Boost_test/graph/src/bundled_properties.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/stdcpp/tsrc/Boost_test/graph/src/bundled_properties.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,249 @@
     1.4 +// Boost Graph library
     1.5 +
     1.6 +//  Copyright Douglas Gregor 2004. Use, modification and
     1.7 +//  distribution is subject to the Boost Software License, Version
     1.8 +//  1.0. (See accompanying file LICENSE_1_0.txt or copy at
     1.9 +//  http://www.boost.org/LICENSE_1_0.txt)
    1.10 +/*
    1.11 + * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
    1.12 +*/
    1.13 +
    1.14 +#include <boost/test/minimal.hpp>
    1.15 +#include <boost/graph/adjacency_list.hpp>
    1.16 +#include <boost/graph/adjacency_matrix.hpp>
    1.17 +#include <boost/graph/filtered_graph.hpp>
    1.18 +#include <boost/graph/subgraph.hpp>
    1.19 +#include <string>
    1.20 +#include <vector>
    1.21 +#include <boost/graph/adjacency_list_io.hpp>
    1.22 +#include <sstream>
    1.23 +#include <boost/graph/iteration_macros.hpp>
    1.24 +#include <algorithm>
    1.25 +#include <iterator>
    1.26 +#ifdef __SYMBIAN32__
    1.27 +#include "std_log_result.h"
    1.28 +#define LOG_FILENAME_LINE __FILE__, __LINE__
    1.29 +#endif
    1.30 +using namespace std;
    1.31 +using namespace boost;
    1.32 +
    1.33 +struct City
    1.34 +{
    1.35 +  City() {}
    1.36 +  City(const std::string& name, int pop, int zipcode) : name(name), population(pop)
    1.37 +  { 
    1.38 +    zipcodes.push_back(zipcode); 
    1.39 +  }
    1.40 +
    1.41 +  string name;
    1.42 +  int population;
    1.43 +  vector<int> zipcodes;
    1.44 +};
    1.45 +
    1.46 +std::ostream& operator<<(std::ostream& out, const City& city)
    1.47 +{
    1.48 +  out << city.name << ' ' << city.population << ' ';
    1.49 +  copy(city.zipcodes.begin(), city.zipcodes.end(),
    1.50 +       ostream_iterator<int>(out, " "));
    1.51 +  out << -1;
    1.52 +  return out;
    1.53 +}
    1.54 +
    1.55 +std::istream& operator>>(std::istream& in, City& city)
    1.56 +{
    1.57 +  if (in >> city.name >> city.population) {
    1.58 +    int zip;
    1.59 +    city.zipcodes.clear();
    1.60 +    while (in >> zip && zip != -1)
    1.61 +      city.zipcodes.push_back(zip);
    1.62 +  }
    1.63 +  return in;
    1.64 +}
    1.65 +
    1.66 +bool operator==(const City& c1, const City& c2)
    1.67 +{
    1.68 +  return (c1.name == c2.name && c1.population == c2.population
    1.69 +          && c1.zipcodes == c2.zipcodes);
    1.70 +}
    1.71 +
    1.72 +struct Highway
    1.73 +{
    1.74 +  Highway() {}
    1.75 +  Highway(const string& name, double miles, int speed_limit = 65, int lanes = 4, bool divided = true) 
    1.76 +    : name(name), miles(miles), speed_limit(speed_limit), lanes(lanes), divided(divided) {}
    1.77 +
    1.78 +  string name;
    1.79 +  double miles;
    1.80 +  int speed_limit;
    1.81 +  int lanes;
    1.82 +  bool divided;
    1.83 +};
    1.84 +
    1.85 +std::ostream& operator<<(std::ostream& out, const Highway& highway)
    1.86 +{
    1.87 +  return out << highway.name << ' ' << highway.miles << ' ' << highway.miles
    1.88 +             << ' ' << highway.speed_limit << ' ' << highway.lanes
    1.89 +             << ' ' << highway.divided;
    1.90 +}
    1.91 +
    1.92 +std::istream& operator>>(std::istream& in, Highway& highway)
    1.93 +{
    1.94 +  return in >> highway.name >> highway.miles >> highway.miles
    1.95 +            >> highway.speed_limit  >> highway.lanes
    1.96 +            >> highway.divided;
    1.97 +}
    1.98 +
    1.99 +bool operator==(const Highway& h1, const Highway& h2)
   1.100 +{
   1.101 +  return (h1.name == h2.name && h1.miles == h2.miles 
   1.102 +          && h1.speed_limit == h2.speed_limit && h1.lanes == h2.lanes
   1.103 +          && h1.divided == h2.divided);
   1.104 +}
   1.105 +
   1.106 +template<bool> struct truth {};
   1.107 +
   1.108 +template<typename Map, typename VertexIterator, typename Bundle>
   1.109 +typename boost::graph_traits<Map>::vertex_descriptor 
   1.110 +do_add_vertex(Map& map, VertexIterator, const Bundle& bundle, truth<true>)
   1.111 +{
   1.112 +  return add_vertex(bundle, map);
   1.113 +}
   1.114 +
   1.115 +template<typename Map, typename VertexIterator, typename Bundle>
   1.116 +typename boost::graph_traits<Map>::vertex_descriptor 
   1.117 +do_add_vertex(Map& map, VertexIterator& vi, const Bundle& bundle, truth<false>)
   1.118 +{
   1.119 +  get(boost::vertex_bundle, map)[*vi] = bundle;
   1.120 +  return *vi++;
   1.121 +}
   1.122 +
   1.123 +template<class EL, class VL, class D, class VP, class EP, class GP>
   1.124 +void test_io(adjacency_list<EL,VL,D,VP,EP,GP>& map, int)
   1.125 +{
   1.126 +  typedef adjacency_list<EL,VL,D,VP,EP,GP> Map;
   1.127 +
   1.128 +  ostringstream out;
   1.129 +  cout << write(map);
   1.130 +  out << write(map);
   1.131 +  
   1.132 +  istringstream in(out.str());
   1.133 +  adjacency_list<EL,VL,D,VP,EP,GP> map2;
   1.134 +  in >> read(map2);
   1.135 +  typename graph_traits<adjacency_list<EL,VL,D,VP,EP,GP> >::vertex_iterator
   1.136 +    v2 = vertices(map2).first;
   1.137 +  BGL_FORALL_VERTICES_T(v, map, Map) {
   1.138 +    BOOST_CHECK(map[v] == map2[*v2]);
   1.139 +    typename graph_traits<adjacency_list<EL,VL,D,VP,EP,GP> >::out_edge_iterator
   1.140 +      e2 = out_edges(*v2, map2).first;
   1.141 +    BGL_FORALL_OUTEDGES_T(v, e, map, Map) {
   1.142 +      BOOST_CHECK(map[e] == map[*e2]);
   1.143 +      ++e2;
   1.144 +    }
   1.145 +    ++v2;
   1.146 +  }
   1.147 +}
   1.148 +
   1.149 +template<typename Map>
   1.150 +void test_io(const Map&, long)
   1.151 +{
   1.152 +  // Nothing to test
   1.153 +}
   1.154 +
   1.155 +template<typename Map, bool CanAddVertex>
   1.156 +void test_bundled_properties(Map*, truth<CanAddVertex> can_add_vertex)
   1.157 +{
   1.158 +  typedef typename boost::graph_traits<Map>::vertex_iterator   vertex_iterator;
   1.159 +  typedef typename boost::graph_traits<Map>::vertex_descriptor vertex_descriptor;
   1.160 +  typedef typename boost::graph_traits<Map>::edge_descriptor   edge_descriptor;
   1.161 +
   1.162 +  Map map(CanAddVertex? 2 : 3);
   1.163 +
   1.164 +  vertex_iterator vi = vertices(map).first;
   1.165 +  vertex_descriptor v = *vi;
   1.166 +  map[v].name = "Troy";
   1.167 +  map[v].population = 49170;
   1.168 +  map[v].zipcodes.push_back(12180);
   1.169 +
   1.170 +  ++vi;
   1.171 +  vertex_descriptor u = *vi++;
   1.172 +  map[u].name = "Albany";
   1.173 +  map[u].population = 95658;
   1.174 +  map[u].zipcodes.push_back(12201);
   1.175 +
   1.176 +  // Try adding a vertex with a property value
   1.177 +  vertex_descriptor bloomington = do_add_vertex(map, vi, City("Bloomington", 39000, 47401),
   1.178 +                                                can_add_vertex);
   1.179 +  BOOST_CHECK(get(boost::vertex_bundle, map)[bloomington].zipcodes[0] == 47401);
   1.180 +  
   1.181 +  edge_descriptor e = add_edge(v, u, map).first;
   1.182 +  map[e].name = "I-87";
   1.183 +  map[e].miles = 10;
   1.184 +  map[e].speed_limit = 65;
   1.185 +  map[e].lanes = 4;
   1.186 +  map[e].divided = true;
   1.187 +
   1.188 +  edge_descriptor our_trip = add_edge(v, bloomington, Highway("Long", 1000), map).first;
   1.189 +  BOOST_CHECK(get(boost::edge_bundle, map, our_trip).miles == 1000);
   1.190 +  
   1.191 +  BOOST_CHECK(get(get(&City::name, map), v) == "Troy");
   1.192 +  BOOST_CHECK(get(get(&Highway::name, map), e) == "I-87");
   1.193 +  BOOST_CHECK(get(&City::name, map, u) == "Albany");
   1.194 +  BOOST_CHECK(get(&Highway::name, map, e) == "I-87");
   1.195 +  put(&City::population, map, v, 49168);
   1.196 +  BOOST_CHECK(get(&City::population, map)[v] == 49168);
   1.197 +  
   1.198 +  boost::filtered_graph<Map, boost::keep_all> fmap(map, boost::keep_all());
   1.199 +  BOOST_CHECK(get(boost::edge_bundle, map, our_trip).miles == 1000);
   1.200 +  
   1.201 +  BOOST_CHECK(get(get(&City::name, fmap), v) == "Troy");
   1.202 +  BOOST_CHECK(get(get(&Highway::name, fmap), e) == "I-87");
   1.203 +  BOOST_CHECK(get(&City::name, fmap, u) == "Albany");
   1.204 +  BOOST_CHECK(get(&Highway::name, fmap, e) == "I-87");
   1.205 +  put(&City::population, fmap, v, 49169);
   1.206 +  BOOST_CHECK(get(&City::population, fmap)[v] == 49169);
   1.207 +
   1.208 +  test_io(map, 0);
   1.209 +}
   1.210 +
   1.211 +void test_subgraph_bundled_properties()
   1.212 +{
   1.213 +  typedef boost::subgraph<
   1.214 +            boost::adjacency_list<boost::vecS, boost::vecS, 
   1.215 +                                  boost::bidirectionalS, City, 
   1.216 +                                  boost::property<boost::edge_index_t, int,
   1.217 +                                                  Highway> > > SubMap;
   1.218 +  typedef boost::graph_traits<SubMap>::vertex_descriptor Vertex;
   1.219 +  typedef boost::graph_traits<SubMap>::vertex_iterator vertex_iterator;
   1.220 +
   1.221 +  SubMap map(3);
   1.222 +  vertex_iterator vi = vertices(map).first;
   1.223 +  Vertex troy = *vi++;
   1.224 +  map[troy].name = "Troy";
   1.225 +  map[*vi++].name = "Bloomington";
   1.226 +  map[*vi++].name = "Endicott";
   1.227 +
   1.228 +  SubMap& g1 = map.create_subgraph();
   1.229 +  Vertex troy1 = add_vertex(*vertices(map).first, g1);
   1.230 +  BOOST_CHECK(map[troy1].name == g1[troy1].name);
   1.231 +}
   1.232 +
   1.233 +int test_main(int, char*[])
   1.234 +{
   1.235 +  typedef boost::adjacency_list<
   1.236 +    boost::listS, boost::vecS, boost::bidirectionalS,
   1.237 +    City, Highway> Map1;
   1.238 +  typedef boost::adjacency_matrix<boost::directedS,
   1.239 +    City, Highway> Map2;
   1.240 +
   1.241 +  test_bundled_properties(static_cast<Map1*>(0), truth<true>());
   1.242 +  test_bundled_properties(static_cast<Map2*>(0), truth<false>());
   1.243 +  test_subgraph_bundled_properties();
   1.244 +  
   1.245 +   #ifdef __SYMBIAN32__
   1.246 +	std_log(LOG_FILENAME_LINE,"[End Test Case ]");
   1.247 +
   1.248 +	testResultXml("bundled_properties");
   1.249 +	close_log_file();
   1.250 +#endif
   1.251 +  return 0;
   1.252 +}