os/ossrv/ossrv_pub/boost_apis/boost/graph/graph_test.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
//=======================================================================
sl@0
     2
// Copyright 2002 Indiana University.
sl@0
     3
// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
sl@0
     4
//
sl@0
     5
// Distributed under the Boost Software License, Version 1.0. (See
sl@0
     6
// accompanying file LICENSE_1_0.txt or copy at
sl@0
     7
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
     8
//=======================================================================
sl@0
     9
sl@0
    10
#ifndef BOOST_GRAPH_TEST_HPP
sl@0
    11
#define BOOST_GRAPH_TEST_HPP
sl@0
    12
sl@0
    13
#include <vector>
sl@0
    14
#include <boost/test/minimal.hpp>
sl@0
    15
#include <boost/graph/filtered_graph.hpp>
sl@0
    16
#include <boost/graph/iteration_macros.hpp>
sl@0
    17
#include <boost/graph/isomorphism.hpp>
sl@0
    18
#include <boost/graph/copy.hpp>
sl@0
    19
#include <boost/graph/graph_utility.hpp> // for connects
sl@0
    20
sl@0
    21
sl@0
    22
// UNDER CONSTRUCTION 
sl@0
    23
sl@0
    24
namespace boost {
sl@0
    25
sl@0
    26
  template <typename Graph>
sl@0
    27
  struct graph_test
sl@0
    28
  {
sl@0
    29
  
sl@0
    30
    typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
sl@0
    31
    typedef typename graph_traits<Graph>::edge_descriptor edge_t;
sl@0
    32
    typedef typename graph_traits<Graph>::vertices_size_type v_size_t;
sl@0
    33
    typedef typename graph_traits<Graph>::degree_size_type deg_size_t;
sl@0
    34
    typedef typename graph_traits<Graph>::edges_size_type e_size_t;
sl@0
    35
    typedef typename graph_traits<Graph>::out_edge_iterator out_edge_iter;
sl@0
    36
    typedef typename property_map<Graph, vertex_index_t>::type index_map_t;
sl@0
    37
    typedef iterator_property_map<typename std::vector<vertex_t>::iterator,
sl@0
    38
      index_map_t,vertex_t,vertex_t&> IsoMap;
sl@0
    39
sl@0
    40
    struct ignore_vertex {
sl@0
    41
      ignore_vertex() { }
sl@0
    42
      ignore_vertex(vertex_t v) : v(v) { }
sl@0
    43
      bool operator()(vertex_t x) const { return x != v; }
sl@0
    44
      vertex_t v;
sl@0
    45
    };
sl@0
    46
    struct ignore_edge {
sl@0
    47
      ignore_edge() { }
sl@0
    48
      ignore_edge(edge_t e) : e(e) { }
sl@0
    49
      bool operator()(edge_t x) const { return x != e; }
sl@0
    50
      edge_t e;
sl@0
    51
    };
sl@0
    52
    struct ignore_edges {
sl@0
    53
      ignore_edges(vertex_t s, vertex_t t, const Graph& g) 
sl@0
    54
        : s(s), t(t), g(g) { }
sl@0
    55
      bool operator()(edge_t x) const { 
sl@0
    56
        return !(source(x, g) == s && target(x, g) == t);
sl@0
    57
      }
sl@0
    58
      vertex_t s; vertex_t t; const Graph& g;
sl@0
    59
    };
sl@0
    60
sl@0
    61
    //=========================================================================
sl@0
    62
    // Traversal Operations
sl@0
    63
sl@0
    64
    void test_incidence_graph
sl@0
    65
       (const std::vector<vertex_t>& vertex_set,
sl@0
    66
        const std::vector< std::pair<vertex_t, vertex_t> >& edge_set,
sl@0
    67
        const Graph& g)
sl@0
    68
    {
sl@0
    69
      typedef typename std::vector<vertex_t>::const_iterator vertex_iter;
sl@0
    70
      typedef typename std::vector< std::pair<vertex_t, vertex_t> >
sl@0
    71
        ::const_iterator edge_iter;
sl@0
    72
      typedef typename graph_traits<Graph>::out_edge_iterator out_edge_iter;
sl@0
    73
sl@0
    74
      for (vertex_iter ui = vertex_set.begin(); ui != vertex_set.end(); ++ui) {
sl@0
    75
        vertex_t u = *ui;
sl@0
    76
        std::vector<vertex_t> adj;
sl@0
    77
        for (edge_iter e = edge_set.begin(); e != edge_set.end(); ++e)
sl@0
    78
          if (e->first == u)
sl@0
    79
            adj.push_back(e->second);
sl@0
    80
        
sl@0
    81
        std::pair<out_edge_iter, out_edge_iter> p = out_edges(u, g);
sl@0
    82
        BOOST_CHECK(out_degree(u, g) == adj.size());
sl@0
    83
        BOOST_CHECK(deg_size_t(std::distance(p.first, p.second))
sl@0
    84
                   == out_degree(u, g));
sl@0
    85
        for (; p.first != p.second; ++p.first) {
sl@0
    86
          edge_t e = *p.first;
sl@0
    87
          BOOST_CHECK(source(e, g) == u);
sl@0
    88
          BOOST_CHECK(contains(adj, target(e, g)) == true);
sl@0
    89
        }
sl@0
    90
      }
sl@0
    91
    }
sl@0
    92
sl@0
    93
    void test_bidirectional_graph
sl@0
    94
      (const std::vector<vertex_t>& vertex_set,
sl@0
    95
       const std::vector< std::pair<vertex_t, vertex_t> >& edge_set,
sl@0
    96
       const Graph& g)
sl@0
    97
    {
sl@0
    98
      typedef typename std::vector<vertex_t>::const_iterator vertex_iter;
sl@0
    99
      typedef typename std::vector< std::pair<vertex_t, vertex_t> >
sl@0
   100
        ::const_iterator edge_iter;
sl@0
   101
      typedef typename graph_traits<Graph>::in_edge_iterator in_edge_iter;
sl@0
   102
sl@0
   103
      for (vertex_iter vi = vertex_set.begin(); vi != vertex_set.end(); ++vi) {
sl@0
   104
        vertex_t v = *vi;
sl@0
   105
        std::vector<vertex_t> inv_adj;
sl@0
   106
        for (edge_iter e = edge_set.begin(); e != edge_set.end(); ++e)
sl@0
   107
          if (e->second == v)
sl@0
   108
            inv_adj.push_back(e->first);
sl@0
   109
sl@0
   110
        std::pair<in_edge_iter, in_edge_iter> p = in_edges(v, g);
sl@0
   111
        BOOST_CHECK(in_degree(v, g) == inv_adj.size());
sl@0
   112
        BOOST_CHECK(deg_size_t(std::distance(p.first, p.second))
sl@0
   113
                   == in_degree(v, g));
sl@0
   114
        for (; p.first != p.second; ++p.first) {
sl@0
   115
          edge_t e = *p.first;
sl@0
   116
          BOOST_CHECK(target(e, g) == v);
sl@0
   117
          BOOST_CHECK(contains(inv_adj, source(e, g)) == true);
sl@0
   118
        }
sl@0
   119
      }
sl@0
   120
    }
sl@0
   121
sl@0
   122
    void test_adjacency_graph
sl@0
   123
      (const std::vector<vertex_t>& vertex_set,
sl@0
   124
       const std::vector< std::pair<vertex_t,vertex_t> >& edge_set,
sl@0
   125
       const Graph& g)
sl@0
   126
    {
sl@0
   127
      typedef typename std::vector<vertex_t>::const_iterator vertex_iter;
sl@0
   128
      typedef typename std::vector<std::pair<vertex_t,vertex_t> >
sl@0
   129
        ::const_iterator edge_iter;
sl@0
   130
      typedef typename graph_traits<Graph>::adjacency_iterator adj_iter;
sl@0
   131
sl@0
   132
      for (vertex_iter ui = vertex_set.begin(); ui != vertex_set.end(); ++ui) {
sl@0
   133
        vertex_t u = *ui;
sl@0
   134
        std::vector<vertex_t> adj;
sl@0
   135
        for (edge_iter e = edge_set.begin(); e != edge_set.end(); ++e)
sl@0
   136
          if (e->first == u)
sl@0
   137
            adj.push_back(e->second);
sl@0
   138
sl@0
   139
        std::pair<adj_iter, adj_iter> p = adjacent_vertices(u, g);
sl@0
   140
        BOOST_CHECK(deg_size_t(std::distance(p.first, p.second)) == adj.size());
sl@0
   141
        for (; p.first != p.second; ++p.first) {
sl@0
   142
          vertex_t v = *p.first;
sl@0
   143
          BOOST_CHECK(contains(adj, v) == true);
sl@0
   144
        }
sl@0
   145
      }
sl@0
   146
    }      
sl@0
   147
sl@0
   148
    void test_vertex_list_graph
sl@0
   149
      (const std::vector<vertex_t>& vertex_set, const Graph& g)
sl@0
   150
    {
sl@0
   151
      typedef typename graph_traits<Graph>::vertex_iterator v_iter;
sl@0
   152
      std::pair<v_iter, v_iter> p = vertices(g);
sl@0
   153
      BOOST_CHECK(num_vertices(g) == vertex_set.size());
sl@0
   154
      v_size_t n = std::distance(p.first, p.second);
sl@0
   155
      BOOST_CHECK(n == num_vertices(g));
sl@0
   156
      for (; p.first != p.second; ++p.first) {
sl@0
   157
        vertex_t v = *p.first;
sl@0
   158
        BOOST_CHECK(contains(vertex_set, v) == true);
sl@0
   159
      }
sl@0
   160
    }
sl@0
   161
sl@0
   162
    void test_edge_list_graph
sl@0
   163
      (const std::vector<vertex_t>& vertex_set, 
sl@0
   164
       const std::vector< std::pair<vertex_t, vertex_t> >& edge_set, 
sl@0
   165
       const Graph& g)
sl@0
   166
    {
sl@0
   167
      typedef typename graph_traits<Graph>::edge_iterator e_iter;
sl@0
   168
      std::pair<e_iter, e_iter> p = edges(g);
sl@0
   169
      BOOST_CHECK(num_edges(g) == edge_set.size());
sl@0
   170
      e_size_t m = std::distance(p.first, p.second);
sl@0
   171
      BOOST_CHECK(m == num_edges(g));
sl@0
   172
      for (; p.first != p.second; ++p.first) {
sl@0
   173
        edge_t e = *p.first;
sl@0
   174
        BOOST_CHECK(any_if(edge_set, connects(source(e, g), target(e, g), g)));
sl@0
   175
        BOOST_CHECK(contains(vertex_set, source(e, g)) == true);
sl@0
   176
        BOOST_CHECK(contains(vertex_set, target(e, g)) == true);
sl@0
   177
      }
sl@0
   178
    }
sl@0
   179
sl@0
   180
    void test_adjacency_matrix
sl@0
   181
      (const std::vector<vertex_t>& vertex_set, 
sl@0
   182
       const std::vector< std::pair<vertex_t, vertex_t> >& edge_set, 
sl@0
   183
       const Graph& g)
sl@0
   184
    {
sl@0
   185
      std::pair<edge_t, bool> p;
sl@0
   186
      for (typename std::vector<std::pair<vertex_t, vertex_t> >
sl@0
   187
             ::const_iterator i = edge_set.begin();
sl@0
   188
           i != edge_set.end(); ++i) {
sl@0
   189
        p = edge(i->first, i->second, g);
sl@0
   190
        BOOST_CHECK(p.second == true);
sl@0
   191
        BOOST_CHECK(source(p.first, g) == i->first);
sl@0
   192
        BOOST_CHECK(target(p.first, g) == i->second);
sl@0
   193
      }
sl@0
   194
      typename std::vector<vertex_t>::const_iterator j, k;
sl@0
   195
      for (j = vertex_set.begin(); j != vertex_set.end(); ++j)
sl@0
   196
        for (k = vertex_set.begin(); k != vertex_set.end(); ++k) {
sl@0
   197
          p = edge(*j, *k, g);
sl@0
   198
          if (p.second == true)
sl@0
   199
            BOOST_CHECK(any_if(edge_set, 
sl@0
   200
              connects(source(p.first, g), target(p.first, g), g)) == true);
sl@0
   201
        }
sl@0
   202
    }
sl@0
   203
sl@0
   204
    //=========================================================================
sl@0
   205
    // Mutating Operations
sl@0
   206
sl@0
   207
    void test_add_vertex(Graph& g)
sl@0
   208
    {
sl@0
   209
      Graph cpy;
sl@0
   210
      std::vector<vertex_t> iso_vec(num_vertices(g));
sl@0
   211
      IsoMap iso_map(iso_vec.begin(), get(vertex_index, g));
sl@0
   212
      copy_graph(g, cpy, orig_to_copy(iso_map));
sl@0
   213
sl@0
   214
      assert((verify_isomorphism(g, cpy, iso_map)));
sl@0
   215
sl@0
   216
      vertex_t v = add_vertex(g);
sl@0
   217
      
sl@0
   218
      BOOST_CHECK(num_vertices(g) == num_vertices(cpy) + 1);
sl@0
   219
sl@0
   220
      BOOST_CHECK(out_degree(v, g) == 0);
sl@0
   221
sl@0
   222
      // Make sure the rest of the graph stayed the same
sl@0
   223
      BOOST_CHECK((verify_isomorphism
sl@0
   224
                  (make_filtered_graph(g, keep_all(), ignore_vertex(v)), cpy,
sl@0
   225
                   iso_map)));
sl@0
   226
    }
sl@0
   227
    
sl@0
   228
    void test_add_edge(vertex_t u, vertex_t v, Graph& g)
sl@0
   229
    {
sl@0
   230
      Graph cpy;
sl@0
   231
      std::vector<vertex_t> iso_vec(num_vertices(g));
sl@0
   232
      IsoMap iso_map(iso_vec.begin(), get(vertex_index, g));
sl@0
   233
      copy_graph(g, cpy, orig_to_copy(iso_map));
sl@0
   234
sl@0
   235
      bool parallel_edge_exists = contains(adjacent_vertices(u, g), v);
sl@0
   236
      
sl@0
   237
      std::pair<edge_t, bool> p = add_edge(u, v, g);
sl@0
   238
      edge_t e = p.first;
sl@0
   239
      bool added = p.second;
sl@0
   240
sl@0
   241
      if (is_undirected(g) && u == v) // self edge
sl@0
   242
        BOOST_CHECK(added == false);
sl@0
   243
      else if (parallel_edge_exists)
sl@0
   244
        BOOST_CHECK(allows_parallel_edges(g) && added == true
sl@0
   245
                   || !allows_parallel_edges(g) && added == false);
sl@0
   246
      else
sl@0
   247
        BOOST_CHECK(added == true);
sl@0
   248
sl@0
   249
      if (p.second == true) { // edge added
sl@0
   250
        BOOST_CHECK(num_edges(g) == num_edges(cpy) + 1);
sl@0
   251
        
sl@0
   252
        BOOST_CHECK(contains(out_edges(u, g), e) == true);
sl@0
   253
        
sl@0
   254
        BOOST_CHECK((verify_isomorphism
sl@0
   255
                    (make_filtered_graph(g, ignore_edge(e)), cpy, iso_map)));
sl@0
   256
      }
sl@0
   257
      else { // edge not added
sl@0
   258
        if (! (is_undirected(g) && u == v)) {
sl@0
   259
          // e should be a parallel edge
sl@0
   260
          BOOST_CHECK(source(e, g) == u);
sl@0
   261
          BOOST_CHECK(target(e, g) == v);
sl@0
   262
        }
sl@0
   263
        // The graph should not be changed.
sl@0
   264
        BOOST_CHECK((verify_isomorphism(g, cpy, iso_map)));
sl@0
   265
      }
sl@0
   266
    } // test_add_edge()
sl@0
   267
sl@0
   268
sl@0
   269
    void test_remove_edge(vertex_t u, vertex_t v, Graph& g)
sl@0
   270
    {
sl@0
   271
      Graph cpy;
sl@0
   272
      std::vector<vertex_t> iso_vec(num_vertices(g));
sl@0
   273
      IsoMap iso_map(iso_vec.begin(), get(vertex_index, g));
sl@0
   274
      copy_graph(g, cpy, orig_to_copy(iso_map));
sl@0
   275
sl@0
   276
      deg_size_t occurances = count(adjacent_vertices(u, g), v);
sl@0
   277
sl@0
   278
      remove_edge(u, v, g);
sl@0
   279
      
sl@0
   280
      BOOST_CHECK(num_edges(g) + occurances == num_edges(cpy));
sl@0
   281
      BOOST_CHECK((verify_isomorphism
sl@0
   282
                  (g, make_filtered_graph(cpy, ignore_edges(u,v,cpy)),
sl@0
   283
                   iso_map)));
sl@0
   284
    }
sl@0
   285
sl@0
   286
    void test_remove_edge(edge_t e, Graph& g)
sl@0
   287
    {
sl@0
   288
      Graph cpy;
sl@0
   289
      std::vector<vertex_t> iso_vec(num_vertices(g));
sl@0
   290
      IsoMap iso_map(iso_vec.begin(), get(vertex_index, g));
sl@0
   291
      copy_graph(g, cpy, orig_to_copy(iso_map));
sl@0
   292
sl@0
   293
      vertex_t u = source(e, g), v = target(e, g);
sl@0
   294
      deg_size_t occurances = count(adjacent_vertices(u, g), v);
sl@0
   295
      
sl@0
   296
      remove_edge(e, g);
sl@0
   297
sl@0
   298
      BOOST_CHECK(num_edges(g) + 1 == num_edges(cpy));
sl@0
   299
      BOOST_CHECK(count(adjacent_vertices(u, g), v) + 1 == occurances);
sl@0
   300
      BOOST_CHECK((verify_isomorphism
sl@0
   301
                  (g, make_filtered_graph(cpy, ignore_edge(e)),
sl@0
   302
                   iso_map)));
sl@0
   303
    }
sl@0
   304
sl@0
   305
    void test_clear_vertex(vertex_t v, Graph& g)
sl@0
   306
    {
sl@0
   307
      Graph cpy;
sl@0
   308
      std::vector<vertex_t> iso_vec(num_vertices(g));
sl@0
   309
      IsoMap iso_map(iso_vec.begin(), get(vertex_index, g));
sl@0
   310
      copy_graph(g, cpy, orig_to_copy(iso_map));
sl@0
   311
sl@0
   312
      clear_vertex(v, g);
sl@0
   313
sl@0
   314
      BOOST_CHECK(out_degree(v, g) == 0);
sl@0
   315
      BOOST_CHECK(num_vertices(g) == num_vertices(cpy));
sl@0
   316
      BOOST_CHECK((verify_isomorphism
sl@0
   317
                  (g, make_filtered_graph(cpy, keep_all(), ignore_vertex(v)),
sl@0
   318
                   iso_map)));
sl@0
   319
    }
sl@0
   320
sl@0
   321
    //=========================================================================
sl@0
   322
    // Property Map
sl@0
   323
sl@0
   324
    template <typename PropVal, typename PropertyTag>
sl@0
   325
    void test_readable_vertex_property_graph
sl@0
   326
      (const std::vector<PropVal>& vertex_prop, PropertyTag, const Graph& g)
sl@0
   327
    {
sl@0
   328
      typedef typename property_map<Graph, PropertyTag>::const_type const_Map;
sl@0
   329
      const_Map pmap = get(PropertyTag(), g);
sl@0
   330
      typename std::vector<PropVal>::const_iterator i = vertex_prop.begin();
sl@0
   331
sl@0
   332
  for (typename boost::graph_traits<Graph>::vertex_iterator 
sl@0
   333
           bgl_first_9 = vertices(g).first, bgl_last_9 = vertices(g).second;
sl@0
   334
       bgl_first_9 != bgl_last_9; bgl_first_9 = bgl_last_9)
sl@0
   335
    for (typename boost::graph_traits<Graph>::vertex_descriptor v;
sl@0
   336
         bgl_first_9 != bgl_last_9 ? (v = *bgl_first_9, true) : false;
sl@0
   337
         ++bgl_first_9) {
sl@0
   338
      //BGL_FORALL_VERTICES_T(v, g, Graph) {
sl@0
   339
        typename property_traits<const_Map>::value_type 
sl@0
   340
          pval1 = get(pmap, v), pval2 = get(PropertyTag(), g, v);
sl@0
   341
        BOOST_CHECK(pval1 == pval2);
sl@0
   342
        BOOST_CHECK(pval1 == *i++);
sl@0
   343
      }
sl@0
   344
    }
sl@0
   345
sl@0
   346
    template <typename PropVal, typename PropertyTag>
sl@0
   347
    void test_vertex_property_graph
sl@0
   348
      (const std::vector<PropVal>& vertex_prop, PropertyTag tag, Graph& g)
sl@0
   349
    {
sl@0
   350
      typedef typename property_map<Graph, PropertyTag>::type PMap;
sl@0
   351
      PMap pmap = get(PropertyTag(), g);
sl@0
   352
      typename std::vector<PropVal>::const_iterator i = vertex_prop.begin();
sl@0
   353
  for (typename boost::graph_traits<Graph>::vertex_iterator 
sl@0
   354
           bgl_first_9 = vertices(g).first, bgl_last_9 = vertices(g).second;
sl@0
   355
       bgl_first_9 != bgl_last_9; bgl_first_9 = bgl_last_9)
sl@0
   356
    for (typename boost::graph_traits<Graph>::vertex_descriptor v;
sl@0
   357
         bgl_first_9 != bgl_last_9 ? (v = *bgl_first_9, true) : false;
sl@0
   358
         ++bgl_first_9)
sl@0
   359
      //      BGL_FORALL_VERTICES_T(v, g, Graph)
sl@0
   360
        put(pmap, v, *i++);
sl@0
   361
sl@0
   362
      test_readable_vertex_property_graph(vertex_prop, tag, g);
sl@0
   363
sl@0
   364
      BGL_FORALL_VERTICES_T(v, g, Graph)
sl@0
   365
        put(pmap, v, vertex_prop[0]);
sl@0
   366
      
sl@0
   367
      typename std::vector<PropVal>::const_iterator j = vertex_prop.begin();
sl@0
   368
      BGL_FORALL_VERTICES_T(v, g, Graph)
sl@0
   369
        put(PropertyTag(), g, v, *j++);
sl@0
   370
      
sl@0
   371
      test_readable_vertex_property_graph(vertex_prop, tag, g);      
sl@0
   372
    }
sl@0
   373
    
sl@0
   374
    
sl@0
   375
  };
sl@0
   376
sl@0
   377
sl@0
   378
} // namespace boost
sl@0
   379
sl@0
   380
#include <boost/graph/iteration_macros_undef.hpp>
sl@0
   381
sl@0
   382
#endif // BOOST_GRAPH_TEST_HPP