os/ossrv/stdcpp/tsrc/Boost_test/graph/src/dfs.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/dfs.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,183 @@
     1.4 +//=======================================================================
     1.5 +// Copyright 2001 University of Notre Dame.
     1.6 +// Author: Jeremy G. Siek
     1.7 +//
     1.8 +// Distributed under the Boost Software License, Version 1.0. (See
     1.9 +// accompanying file LICENSE_1_0.txt or copy at
    1.10 +// http://www.boost.org/LICENSE_1_0.txt)
    1.11 +//=======================================================================
    1.12 +/*
    1.13 + * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
    1.14 +*/
    1.15 +
    1.16 +#include <boost/config.hpp>
    1.17 +#include <boost/test/minimal.hpp>
    1.18 +#include <stdlib.h>
    1.19 +
    1.20 +#include <boost/graph/depth_first_search.hpp>
    1.21 +#include <boost/graph/adjacency_list.hpp>
    1.22 +#include <boost/graph/graph_archetypes.hpp>
    1.23 +#include <boost/graph/graph_utility.hpp>
    1.24 +#include <boost/graph/random.hpp>
    1.25 +
    1.26 +#include <boost/random/mersenne_twister.hpp>
    1.27 +#ifdef __SYMBIAN32__
    1.28 +#include "std_log_result.h"
    1.29 +#define LOG_FILENAME_LINE __FILE__, __LINE__
    1.30 +#endif
    1.31 +template <typename ColorMap, typename ParentMap,
    1.32 +  typename DiscoverTimeMap, typename FinishTimeMap>
    1.33 +class dfs_test_visitor {
    1.34 +  typedef typename boost::property_traits<ColorMap>::value_type ColorValue;
    1.35 +  typedef typename boost::color_traits<ColorValue> Color;
    1.36 +public:
    1.37 +  dfs_test_visitor(ColorMap color, ParentMap p, DiscoverTimeMap d,
    1.38 +                   FinishTimeMap f)
    1.39 +    : m_color(color), m_parent(p),
    1.40 +    m_discover_time(d), m_finish_time(f), m_time(0) { }
    1.41 +
    1.42 +  template <class Vertex, class Graph>
    1.43 +  void initialize_vertex(Vertex u, Graph& g) {
    1.44 +    BOOST_CHECK( boost::get(m_color, u) == Color::white() );
    1.45 +  }
    1.46 +  template <class Vertex, class Graph>
    1.47 +  void start_vertex(Vertex u, Graph& g) {
    1.48 +    BOOST_CHECK( boost::get(m_color, u) == Color::white() );
    1.49 +  }
    1.50 +  template <class Vertex, class Graph>
    1.51 +  void discover_vertex(Vertex u, Graph& g) {
    1.52 +    using namespace boost;
    1.53 +    BOOST_CHECK( get(m_color, u) == Color::gray() );
    1.54 +    BOOST_CHECK( get(m_color, get(m_parent, u)) == Color::gray() );
    1.55 +
    1.56 +    put(m_discover_time, u, m_time++);
    1.57 +  }
    1.58 +  template <class Edge, class Graph>
    1.59 +  void examine_edge(Edge e, Graph& g) {
    1.60 +    using namespace boost;
    1.61 +    BOOST_CHECK( get(m_color, source(e, g)) == Color::gray() );
    1.62 +  }
    1.63 +  template <class Edge, class Graph>
    1.64 +  void tree_edge(Edge e, Graph& g) {
    1.65 +    using namespace boost;
    1.66 +    BOOST_CHECK( get(m_color, target(e, g)) == Color::white() );
    1.67 +
    1.68 +    put(m_parent, target(e, g), source(e, g));
    1.69 +  }
    1.70 +  template <class Edge, class Graph>
    1.71 +  void back_edge(Edge e, Graph& g) {
    1.72 +    using namespace boost;
    1.73 +    BOOST_CHECK( get(m_color, target(e, g)) == Color::gray() );
    1.74 +  }
    1.75 +  template <class Edge, class Graph>
    1.76 +  void forward_or_cross_edge(Edge e, Graph& g) {
    1.77 +    using namespace boost;
    1.78 +    BOOST_CHECK( get(m_color, target(e, g)) == Color::black() );
    1.79 +  }
    1.80 +  template <class Vertex, class Graph>
    1.81 +  void finish_vertex(Vertex u, Graph& g) {
    1.82 +    using namespace boost;
    1.83 +    BOOST_CHECK( get(m_color, u) == Color::black() );
    1.84 +
    1.85 +    put(m_finish_time, u, m_time++);
    1.86 +  }
    1.87 +private:
    1.88 +  ColorMap m_color;
    1.89 +  ParentMap m_parent;
    1.90 +  DiscoverTimeMap m_discover_time;
    1.91 +  FinishTimeMap m_finish_time;
    1.92 +  typename boost::property_traits<DiscoverTimeMap>::value_type m_time;
    1.93 +};
    1.94 +
    1.95 +template <typename Graph>
    1.96 +struct dfs_test
    1.97 +{
    1.98 +  typedef boost::graph_traits<Graph> Traits;
    1.99 +  typedef typename Traits::vertices_size_type
   1.100 +    vertices_size_type;
   1.101 +
   1.102 +  static void go(vertices_size_type max_V) {
   1.103 +    using namespace boost;
   1.104 +    typedef typename Traits::vertex_descriptor vertex_descriptor;
   1.105 +    typedef typename boost::property_map<Graph,
   1.106 +      boost::vertex_color_t>::type ColorMap;
   1.107 +    typedef typename boost::property_traits<ColorMap>::value_type ColorValue;
   1.108 +    typedef typename boost::color_traits<ColorValue> Color;
   1.109 +
   1.110 +    vertices_size_type i, k;
   1.111 +    typename Traits::edges_size_type j;
   1.112 +    typename Traits::vertex_iterator vi, vi_end, ui, ui_end;
   1.113 +
   1.114 +    boost::mt19937 gen;
   1.115 +
   1.116 +    for (i = 0; i < max_V; ++i)
   1.117 +      for (j = 0; j < i*i; ++j) {
   1.118 +        Graph g;
   1.119 +        generate_random_graph(g, i, j, gen);
   1.120 +
   1.121 +        ColorMap color = get(boost::vertex_color, g);
   1.122 +        std::vector<vertex_descriptor> parent(num_vertices(g));
   1.123 +        for (k = 0; k < num_vertices(g); ++k)
   1.124 +          parent[k] = k;
   1.125 +        std::vector<int> discover_time(num_vertices(g)),
   1.126 +          finish_time(num_vertices(g));
   1.127 +
   1.128 +        dfs_test_visitor<ColorMap, vertex_descriptor*,
   1.129 +          int*, int*> vis(color, &parent[0],
   1.130 +                          &discover_time[0], &finish_time[0]);
   1.131 +
   1.132 +        boost::depth_first_search(g, visitor(vis).color_map(color));
   1.133 +
   1.134 +        // all vertices should be black
   1.135 +        for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
   1.136 +          BOOST_CHECK(get(color, *vi) == Color::black());
   1.137 +
   1.138 +        // check parenthesis structure of discover/finish times
   1.139 +        // See CLR p.480
   1.140 +        for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
   1.141 +          for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
   1.142 +            vertex_descriptor u = *ui, v = *vi;
   1.143 +            if (u != v) {
   1.144 +              BOOST_CHECK( finish_time[u] < discover_time[v]
   1.145 +                          || finish_time[v] < discover_time[u]
   1.146 +                          || (discover_time[v] < discover_time[u]
   1.147 +                               && finish_time[u] < finish_time[v]
   1.148 +                               && boost::is_descendant(u, v, &parent[0]))
   1.149 +                          || (discover_time[u] < discover_time[v]
   1.150 +                               && finish_time[v] < finish_time[u]
   1.151 +                               && boost::is_descendant(v, u, &parent[0]))
   1.152 +                        );
   1.153 +            }
   1.154 +          }
   1.155 +      }
   1.156 +
   1.157 +  }
   1.158 +};
   1.159 +
   1.160 +
   1.161 +// usage: dfs.exe [max-vertices=15]
   1.162 +
   1.163 +int test_main(int argc, char* argv[])
   1.164 +{
   1.165 +  int max_V = 7;
   1.166 +  if (argc > 1)
   1.167 +    max_V = atoi(argv[1]);
   1.168 +
   1.169 +  // Test directed graphs.
   1.170 +  dfs_test< boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
   1.171 +           boost::property<boost::vertex_color_t, boost::default_color_type> >
   1.172 +    >::go(max_V);
   1.173 +  // Test undirected graphs.
   1.174 +  dfs_test< boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
   1.175 +           boost::property<boost::vertex_color_t, boost::default_color_type> >
   1.176 +    >::go(max_V);
   1.177 +
   1.178 +   #ifdef __SYMBIAN32__
   1.179 +	std_log(LOG_FILENAME_LINE,"[End Test Case ]");
   1.180 +
   1.181 +	testResultXml("dfs");
   1.182 +	close_log_file();
   1.183 +#endif
   1.184 +  return 0;
   1.185 +}
   1.186 +