test/digraph_test.h
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 20 Mar 2008 22:59:58 +0000
changeset 105 e4948ef6a4ca
child 107 31a2e6d28f61
permissions -rw-r--r--
Get rid of StdMap in unionfind_test.cc
deba@57
     1
/* -*- C++ -*-
deba@57
     2
 *
deba@57
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@57
     4
 *
deba@57
     5
 * Copyright (C) 2003-2007
deba@57
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@57
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@57
     8
 *
deba@57
     9
 * Permission to use, modify and distribute this software is granted
deba@57
    10
 * provided that this copyright notice appears in all copies. For
deba@57
    11
 * precise terms see the accompanying LICENSE file.
deba@57
    12
 *
deba@57
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@57
    14
 * express or implied, and with no claim as to its suitability for any
deba@57
    15
 * purpose.
deba@57
    16
 *
deba@57
    17
 */
deba@57
    18
deba@57
    19
#ifndef LEMON_TEST_GRAPH_TEST_H
deba@57
    20
#define LEMON_TEST_GRAPH_TEST_H
deba@57
    21
deba@57
    22
//#include <lemon/graph_utils.h>
deba@57
    23
#include "test_tools.h"
deba@57
    24
deba@57
    25
//! \ingroup misc
deba@57
    26
//! \file
deba@57
    27
//! \brief Some utility and test cases to test digraph classes.
deba@57
    28
namespace lemon {
deba@57
    29
deba@57
    30
  ///Structure returned by \ref addPetersen().
deba@57
    31
deba@57
    32
  ///Structure returned by \ref addPetersen().
deba@57
    33
  ///
deba@57
    34
  template<class Digraph> 
deba@57
    35
  struct PetStruct
deba@57
    36
  {
deba@57
    37
    ///Vector containing the outer nodes.
deba@57
    38
    std::vector<typename Digraph::Node> outer;
deba@57
    39
    ///Vector containing the inner nodes.
deba@57
    40
    std::vector<typename Digraph::Node> inner;
deba@57
    41
    ///Vector containing the edges of the inner circle.
deba@57
    42
    std::vector<typename Digraph::Arc> incir;
deba@57
    43
    ///Vector containing the edges of the outer circle.
deba@57
    44
    std::vector<typename Digraph::Arc> outcir;
deba@57
    45
    ///Vector containing the chord edges.
deba@57
    46
    std::vector<typename Digraph::Arc> chords;
deba@57
    47
  };
deba@57
    48
deba@57
    49
deba@57
    50
deba@57
    51
  ///Adds a Petersen graph to \c G.
deba@57
    52
deba@57
    53
  ///Adds a Petersen graph to \c G.
deba@57
    54
  ///\return The nodes and edges of the generated graph.
deba@57
    55
deba@57
    56
  template<typename Digraph>
deba@57
    57
  PetStruct<Digraph> addPetersen(Digraph &G,int num = 5)
deba@57
    58
  {
deba@57
    59
    PetStruct<Digraph> n;
deba@57
    60
deba@57
    61
    for(int i=0;i<num;i++) {
deba@57
    62
      n.outer.push_back(G.addNode());
deba@57
    63
      n.inner.push_back(G.addNode());
deba@57
    64
    }
deba@57
    65
deba@57
    66
    for(int i=0;i<num;i++) {
deba@57
    67
      n.chords.push_back(G.addArc(n.outer[i],n.inner[i]));
deba@57
    68
      n.outcir.push_back(G.addArc(n.outer[i],n.outer[(i+1) % num]));
deba@57
    69
      n.incir.push_back(G.addArc(n.inner[i],n.inner[(i+2) % num]));
deba@57
    70
    }
deba@57
    71
    return n;
deba@57
    72
  }
deba@57
    73
deba@57
    74
  /// \brief Adds to the digraph the reverse pair of all edges.
deba@57
    75
  ///
deba@57
    76
  /// Adds to the digraph the reverse pair of all edges.
deba@57
    77
  ///
deba@57
    78
  template<class Digraph> 
deba@57
    79
  void bidirDigraph(Digraph &G)
deba@57
    80
  {
deba@57
    81
    typedef typename Digraph::Arc Arc;
deba@57
    82
    typedef typename Digraph::ArcIt ArcIt;
deba@57
    83
  
deba@57
    84
    std::vector<Arc> ee;
deba@57
    85
  
deba@57
    86
    for(ArcIt e(G);e!=INVALID;++e) ee.push_back(e);
deba@57
    87
deba@57
    88
    for(typename std::vector<Arc>::iterator p=ee.begin();p!=ee.end();p++)
deba@57
    89
      G.addArc(G.target(*p),G.source(*p));
deba@57
    90
  }
deba@57
    91
deba@57
    92
deba@57
    93
  /// \brief Checks the bidirectioned Petersen graph.
deba@57
    94
  ///
deba@57
    95
  ///  Checks the bidirectioned Petersen graph.
deba@57
    96
  ///
deba@57
    97
  template<class Digraph> 
deba@57
    98
  void checkBidirPetersen(Digraph &G, int num = 5)
deba@57
    99
  {
deba@57
   100
    typedef typename Digraph::Node Node;
deba@57
   101
deba@57
   102
    typedef typename Digraph::ArcIt ArcIt;
deba@57
   103
    typedef typename Digraph::NodeIt NodeIt;
deba@57
   104
deba@57
   105
    checkDigraphNodeList(G, 2 * num);
deba@57
   106
    checkDigraphArcList(G, 6 * num);
deba@57
   107
deba@57
   108
    for(NodeIt n(G);n!=INVALID;++n) {
deba@57
   109
      checkDigraphInArcList(G, n, 3);
deba@57
   110
      checkDigraphOutArcList(G, n, 3);
deba@57
   111
    }  
deba@57
   112
  }
deba@57
   113
deba@57
   114
  template<class Digraph> void checkDigraphNodeList(Digraph &G, int nn)
deba@57
   115
  {
deba@57
   116
    typename Digraph::NodeIt n(G);
deba@57
   117
    for(int i=0;i<nn;i++) {
deba@57
   118
      check(n!=INVALID,"Wrong Node list linking.");
deba@57
   119
      ++n;
deba@57
   120
    }
deba@57
   121
    check(n==INVALID,"Wrong Node list linking.");
deba@57
   122
  }
deba@57
   123
deba@57
   124
  template<class Digraph>
deba@57
   125
  void checkDigraphArcList(Digraph &G, int nn)
deba@57
   126
  {
deba@57
   127
    typedef typename Digraph::ArcIt ArcIt;
deba@57
   128
deba@57
   129
    ArcIt e(G);
deba@57
   130
    for(int i=0;i<nn;i++) {
deba@57
   131
      check(e!=INVALID,"Wrong Arc list linking.");
deba@57
   132
      ++e;
deba@57
   133
    }
deba@57
   134
    check(e==INVALID,"Wrong Arc list linking.");
deba@57
   135
  }
deba@57
   136
deba@57
   137
  template<class Digraph> 
deba@57
   138
  void checkDigraphOutArcList(Digraph &G, typename Digraph::Node n, int nn)
deba@57
   139
  {
deba@57
   140
    typename Digraph::OutArcIt e(G,n);
deba@57
   141
    for(int i=0;i<nn;i++) {
deba@57
   142
      check(e!=INVALID,"Wrong OutArc list linking.");
deba@57
   143
      check(n==G.source(e), "Wrong OutArc list linking.");
deba@57
   144
      ++e;
deba@57
   145
    }
deba@57
   146
    check(e==INVALID,"Wrong OutArc list linking.");
deba@57
   147
  }
deba@57
   148
deba@57
   149
  template<class Digraph> void 
deba@57
   150
  checkDigraphInArcList(Digraph &G, typename Digraph::Node n, int nn)
deba@57
   151
  {
deba@57
   152
    typename Digraph::InArcIt e(G,n);
deba@57
   153
    for(int i=0;i<nn;i++) {
deba@57
   154
      check(e!=INVALID,"Wrong InArc list linking.");
deba@57
   155
      check(n==G.target(e), "Wrong InArc list linking.");
deba@57
   156
      ++e;
deba@57
   157
    }
deba@57
   158
    check(e==INVALID,"Wrong InArc list linking.");
deba@57
   159
  }
deba@57
   160
deba@57
   161
  template <class Digraph> 
deba@57
   162
  void checkDigraph() {
deba@57
   163
    const int num = 5;
deba@57
   164
    Digraph G;
deba@57
   165
    addPetersen(G, num);
deba@57
   166
    bidirDigraph(G);
deba@57
   167
    checkBidirPetersen(G, num);
deba@57
   168
  }
deba@57
   169
deba@57
   170
  template <class Digraph> 
deba@57
   171
  void checkDigraphIterators(const Digraph& digraph) {
deba@57
   172
    typedef typename Digraph::Node Node;
deba@57
   173
    typedef typename Digraph::NodeIt NodeIt;
deba@57
   174
    typedef typename Digraph::Arc Arc;
deba@57
   175
    typedef typename Digraph::ArcIt ArcIt;
deba@57
   176
    typedef typename Digraph::InArcIt InArcIt;
deba@57
   177
    typedef typename Digraph::OutArcIt OutArcIt;
deba@57
   178
    //    typedef ConArcIt<Digraph> ConArcIt;
deba@57
   179
  }
deba@57
   180
deba@57
   181
  ///\file
deba@57
   182
  ///\todo Check target(), source() as well;
deba@57
   183
deba@57
   184
  
deba@57
   185
} //namespace lemon
deba@57
   186
deba@57
   187
deba@57
   188
#endif