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