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