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