src/test/test_tools.h
author ladanyi
Fri, 05 Nov 2004 05:46:46 +0000
changeset 963 5a7556e9e340
parent 937 d4e911acef3d
child 978 175cf8c3a994
permissions -rw-r--r--
Updated the makefile.
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/test/test_tools.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 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
alpar@574
    20
//! \ingroup misc
alpar@574
    21
//! \file
alpar@574
    22
//! \brief Some utility to write test programs.
alpar@574
    23
alpar@574
    24
alpar@574
    25
#include<iostream>
alpar@574
    26
#include<vector>
alpar@574
    27
alpar@679
    28
///If \c rc is fail, writes an error message end exit.
alpar@574
    29
alpar@574
    30
///If \c rc is fail, writes an error message end exit.
alpar@574
    31
///The error message contains the file name and the line number of the
alpar@679
    32
///source code in a standard from, which makes it possible to go there
alpar@574
    33
///using good source browsers like e.g. \c emacs.
alpar@574
    34
///
alpar@574
    35
///For example
alpar@574
    36
///\code check(0==1,"This is obviously false.");\endcode will
alpar@574
    37
///print this (and then exits).
alpar@574
    38
///\verbatim graph_test.cc:123: error: This is obviously false. \endverbatim
alpar@774
    39
///
alpar@774
    40
///\todo It should be in \c error.h
alpar@574
    41
#define check(rc, msg) \
alpar@574
    42
  if(!(rc)) { \
alpar@574
    43
    std::cerr << __FILE__ ":" << __LINE__ << ": error: " << msg << std::endl; \
alpar@574
    44
    exit(1); \
alpar@574
    45
  } else { } \
alpar@574
    46
alpar@574
    47
///Structure returned by \ref addPetersen().
alpar@574
    48
alpar@574
    49
///Structure returned by \ref addPetersen().
alpar@574
    50
///
alpar@574
    51
template<class Graph> struct PetStruct
alpar@574
    52
{
alpar@825
    53
  ///Vector containing the outer nodes.
alpar@825
    54
  std::vector<typename Graph::Node> outer;
alpar@825
    55
  ///Vector containing the inner nodes.
alpar@825
    56
  std::vector<typename Graph::Node> inner;
alpar@825
    57
  ///Vector containing the edges of the inner circle.
alpar@825
    58
  std::vector<typename Graph::Edge> incir;
alpar@825
    59
  ///Vector containing the edges of the outer circle.
alpar@825
    60
  std::vector<typename Graph::Edge> outcir;
alpar@825
    61
  ///Vector containing the chord edges.
alpar@825
    62
  std::vector<typename Graph::Edge> chords;
alpar@574
    63
};
alpar@574
    64
alpar@721
    65
alpar@721
    66
alpar@574
    67
///Adds a Petersen graph to \c G.
alpar@574
    68
alpar@574
    69
///Adds a Petersen graph to \c G.
deba@937
    70
///\return The nodes and edges of the generated graph.
alpar@721
    71
alpar@721
    72
template<typename Graph>
klao@946
    73
PetStruct<Graph> addPetersen(Graph &G,int num = 5)
alpar@574
    74
{
alpar@574
    75
  PetStruct<Graph> n;
alpar@574
    76
alpar@574
    77
  for(int i=0;i<num;i++) {
alpar@574
    78
    n.outer.push_back(G.addNode());
alpar@574
    79
    n.inner.push_back(G.addNode());
alpar@574
    80
  }
alpar@574
    81
alpar@574
    82
 for(int i=0;i<num;i++) {
alpar@574
    83
   n.chords.push_back(G.addEdge(n.outer[i],n.inner[i]));
klao@946
    84
   n.outcir.push_back(G.addEdge(n.outer[i],n.outer[(i+1) % num]));
klao@946
    85
   n.incir.push_back(G.addEdge(n.inner[i],n.inner[(i+2) % num]));
alpar@574
    86
  }
alpar@574
    87
 return n;
alpar@574
    88
}
alpar@574
    89
klao@946
    90
/// \brief Adds to the graph the reverse pair of all edge.
klao@946
    91
///
klao@946
    92
/// Adds to the graph the reverse pair of all edge.
klao@946
    93
///
klao@946
    94
template<class Graph> void bidirGraph(Graph &G)
klao@946
    95
{
klao@946
    96
  typedef typename Graph::Edge Edge;
klao@946
    97
  typedef typename Graph::EdgeIt EdgeIt;
klao@946
    98
  
klao@946
    99
  std::vector<Edge> ee;
klao@946
   100
  
klao@946
   101
  for(EdgeIt e(G);e!=INVALID;++e) ee.push_back(e);
klao@946
   102
klao@946
   103
  for(typename std::vector<Edge>::iterator p=ee.begin();p!=ee.end();p++)
klao@946
   104
    G.addEdge(G.head(*p),G.tail(*p));
klao@946
   105
}
klao@946
   106
klao@946
   107
klao@946
   108
/// \brief Checks the bidirectioned Petersen graph.
klao@946
   109
///
klao@946
   110
///  Checks the bidirectioned Petersen graph.
klao@946
   111
///
klao@946
   112
template<class Graph> void checkBidirPetersen(Graph &G, int num = 5)
klao@946
   113
{
klao@946
   114
  typedef typename Graph::Node Node;
klao@946
   115
klao@946
   116
  typedef typename Graph::EdgeIt EdgeIt;
klao@946
   117
  typedef typename Graph::NodeIt NodeIt;
klao@946
   118
klao@946
   119
  checkGraphNodeList(G, 2 * num);
klao@946
   120
  checkGraphEdgeList(G, 6 * num);
klao@946
   121
klao@946
   122
  for(NodeIt n(G);n!=INVALID;++n) {
klao@946
   123
    checkGraphInEdgeList(G, n, 3);
klao@946
   124
    checkGraphOutEdgeList(G, n, 3);
klao@946
   125
  }  
klao@946
   126
}
klao@946
   127
deba@937
   128
///Structure returned by \ref addSymPetersen().
alpar@574
   129
deba@937
   130
///Structure returned by \ref addSymPetersen().
deba@937
   131
///
deba@937
   132
template<class Graph> struct SymPetStruct
deba@937
   133
{
deba@937
   134
  ///Vector containing the outer nodes.
deba@937
   135
  std::vector<typename Graph::Node> outer;
deba@937
   136
  ///Vector containing the inner nodes.
deba@937
   137
  std::vector<typename Graph::Node> inner;
deba@937
   138
  ///Vector containing the edges of the inner circle.
deba@937
   139
  std::vector<typename Graph::SymEdge> incir;
deba@937
   140
  ///Vector containing the edges of the outer circle.
deba@937
   141
  std::vector<typename Graph::SymEdge> outcir;
deba@937
   142
  ///Vector containing the chord edges.
deba@937
   143
  std::vector<typename Graph::SymEdge> chords;
deba@937
   144
};
deba@937
   145
deba@937
   146
///Adds a Petersen graph to the symmetric \c G.
deba@937
   147
deba@937
   148
///Adds a Petersen graph to the symmetric \c G.
deba@937
   149
///\return The nodes and edges of the generated graph.
deba@937
   150
deba@937
   151
template<typename Graph>
deba@937
   152
SymPetStruct<Graph> addSymPetersen(Graph &G,int num=5)
deba@937
   153
{
deba@937
   154
  SymPetStruct<Graph> n;
deba@937
   155
deba@937
   156
  for(int i=0;i<num;i++) {
deba@937
   157
    n.outer.push_back(G.addNode());
deba@937
   158
    n.inner.push_back(G.addNode());
deba@937
   159
  }
deba@937
   160
deba@937
   161
 for(int i=0;i<num;i++) {
deba@937
   162
   n.chords.push_back(G.addEdge(n.outer[i],n.inner[i]));
deba@937
   163
   n.outcir.push_back(G.addEdge(n.outer[i],n.outer[(i+1)%5]));
deba@937
   164
   n.incir.push_back(G.addEdge(n.inner[i],n.inner[(i+2)%5]));
deba@937
   165
  }
deba@937
   166
 return n;
deba@937
   167
}
alpar@574
   168
alpar@574
   169
#endif