src/test/test_tools.h
changeset 1435 8e85e6bbefdf
parent 1200 ae69f556b429
equal deleted inserted replaced
16:7d561fe4e6a5 -1:000000000000
     1 /* -*- C++ -*-
       
     2  * src/test/test_tools.h - Part of LEMON, a generic C++ optimization library
       
     3  *
       
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
       
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
       
     6  *
       
     7  * Permission to use, modify and distribute this software is granted
       
     8  * provided that this copyright notice appears in all copies. For
       
     9  * precise terms see the accompanying LICENSE file.
       
    10  *
       
    11  * This software is provided "AS IS" with no warranty of any kind,
       
    12  * express or implied, and with no claim as to its suitability for any
       
    13  * purpose.
       
    14  *
       
    15  */
       
    16 
       
    17 #ifndef LEMON_TEST_TEST_TOOLS_H
       
    18 #define LEMON_TEST_TEST_TOOLS_H
       
    19 
       
    20 #include <iostream>
       
    21 #include <vector>
       
    22 
       
    23 #include <lemon/invalid.h>
       
    24 
       
    25 using namespace lemon;
       
    26 
       
    27 //! \ingroup misc
       
    28 //! \file
       
    29 //! \brief Some utilities to write test programs.
       
    30 
       
    31 
       
    32 ///If \c rc is fail, writes an error message end exit.
       
    33 
       
    34 ///If \c rc is fail, writes an error message end exit.
       
    35 ///The error message contains the file name and the line number of the
       
    36 ///source code in a standard from, which makes it possible to go there
       
    37 ///using good source browsers like e.g. \c emacs.
       
    38 ///
       
    39 ///For example
       
    40 ///\code check(0==1,"This is obviously false.");\endcode will
       
    41 ///print this (and then exits).
       
    42 ///\verbatim graph_test.cc:123: error: This is obviously false. \endverbatim
       
    43 ///
       
    44 ///\todo It should be in \c error.h
       
    45 #define check(rc, msg) \
       
    46   if(!(rc)) { \
       
    47     std::cerr << __FILE__ ":" << __LINE__ << ": error: " << msg << std::endl; \
       
    48     exit(1); \
       
    49   } else { } \
       
    50 
       
    51 ///Structure returned by \ref addPetersen().
       
    52 
       
    53 ///Structure returned by \ref addPetersen().
       
    54 ///
       
    55 template<class Graph> struct PetStruct
       
    56 {
       
    57   ///Vector containing the outer nodes.
       
    58   std::vector<typename Graph::Node> outer;
       
    59   ///Vector containing the inner nodes.
       
    60   std::vector<typename Graph::Node> inner;
       
    61   ///Vector containing the edges of the inner circle.
       
    62   std::vector<typename Graph::Edge> incir;
       
    63   ///Vector containing the edges of the outer circle.
       
    64   std::vector<typename Graph::Edge> outcir;
       
    65   ///Vector containing the chord edges.
       
    66   std::vector<typename Graph::Edge> chords;
       
    67 };
       
    68 
       
    69 
       
    70 
       
    71 ///Adds a Petersen graph to \c G.
       
    72 
       
    73 ///Adds a Petersen graph to \c G.
       
    74 ///\return The nodes and edges of the generated graph.
       
    75 
       
    76 template<typename Graph>
       
    77 PetStruct<Graph> addPetersen(Graph &G,int num = 5)
       
    78 {
       
    79   PetStruct<Graph> n;
       
    80 
       
    81   for(int i=0;i<num;i++) {
       
    82     n.outer.push_back(G.addNode());
       
    83     n.inner.push_back(G.addNode());
       
    84   }
       
    85 
       
    86  for(int i=0;i<num;i++) {
       
    87    n.chords.push_back(G.addEdge(n.outer[i],n.inner[i]));
       
    88    n.outcir.push_back(G.addEdge(n.outer[i],n.outer[(i+1) % num]));
       
    89    n.incir.push_back(G.addEdge(n.inner[i],n.inner[(i+2) % num]));
       
    90   }
       
    91  return n;
       
    92 }
       
    93 
       
    94 /// \brief Adds to the graph the reverse pair of all edges.
       
    95 ///
       
    96 /// Adds to the graph the reverse pair of all edges.
       
    97 ///
       
    98 template<class Graph> void bidirGraph(Graph &G)
       
    99 {
       
   100   typedef typename Graph::Edge Edge;
       
   101   typedef typename Graph::EdgeIt EdgeIt;
       
   102   
       
   103   std::vector<Edge> ee;
       
   104   
       
   105   for(EdgeIt e(G);e!=INVALID;++e) ee.push_back(e);
       
   106 
       
   107   for(typename std::vector<Edge>::iterator p=ee.begin();p!=ee.end();p++)
       
   108     G.addEdge(G.target(*p),G.source(*p));
       
   109 }
       
   110 
       
   111 
       
   112 /// \brief Checks the bidirectioned Petersen graph.
       
   113 ///
       
   114 ///  Checks the bidirectioned Petersen graph.
       
   115 ///
       
   116 template<class Graph> void checkBidirPetersen(Graph &G, int num = 5)
       
   117 {
       
   118   typedef typename Graph::Node Node;
       
   119 
       
   120   typedef typename Graph::EdgeIt EdgeIt;
       
   121   typedef typename Graph::NodeIt NodeIt;
       
   122 
       
   123   checkGraphNodeList(G, 2 * num);
       
   124   checkGraphEdgeList(G, 6 * num);
       
   125 
       
   126   for(NodeIt n(G);n!=INVALID;++n) {
       
   127     checkGraphInEdgeList(G, n, 3);
       
   128     checkGraphOutEdgeList(G, n, 3);
       
   129   }  
       
   130 }
       
   131 
       
   132 ///Structure returned by \ref addSymPetersen().
       
   133 
       
   134 ///Structure returned by \ref addSymPetersen().
       
   135 ///
       
   136 template<class Graph> struct SymPetStruct
       
   137 {
       
   138   ///Vector containing the outer nodes.
       
   139   std::vector<typename Graph::Node> outer;
       
   140   ///Vector containing the inner nodes.
       
   141   std::vector<typename Graph::Node> inner;
       
   142   ///Vector containing the edges of the inner circle.
       
   143   std::vector<typename Graph::SymEdge> incir;
       
   144   ///Vector containing the edges of the outer circle.
       
   145   std::vector<typename Graph::SymEdge> outcir;
       
   146   ///Vector containing the chord edges.
       
   147   std::vector<typename Graph::SymEdge> chords;
       
   148 };
       
   149 
       
   150 ///Adds a Petersen graph to the symmetric \c G.
       
   151 
       
   152 ///Adds a Petersen graph to the symmetric \c G.
       
   153 ///\return The nodes and edges of the generated graph.
       
   154 
       
   155 template<typename Graph>
       
   156 SymPetStruct<Graph> addSymPetersen(Graph &G,int num=5)
       
   157 {
       
   158   SymPetStruct<Graph> n;
       
   159 
       
   160   for(int i=0;i<num;i++) {
       
   161     n.outer.push_back(G.addNode());
       
   162     n.inner.push_back(G.addNode());
       
   163   }
       
   164 
       
   165  for(int i=0;i<num;i++) {
       
   166    n.chords.push_back(G.addEdge(n.outer[i],n.inner[i]));
       
   167    n.outcir.push_back(G.addEdge(n.outer[i],n.outer[(i+1)%5]));
       
   168    n.incir.push_back(G.addEdge(n.inner[i],n.inner[(i+2)%5]));
       
   169   }
       
   170  return n;
       
   171 }
       
   172 
       
   173 #endif