test/test_tools.h
author alpar
Thu, 14 Sep 2006 18:39:07 +0000
changeset 2214 a886e48e0d91
parent 1993 2115143eceea
child 2229 4dbb6dd2dd4b
permissions -rw-r--r--
Doc improvements
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@1956
     5
 * Copyright (C) 2003-2006
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>
deba@1993
    29
#include <lemon/concept/graph.h>
klao@978
    30
klao@978
    31
using namespace lemon;
klao@978
    32
alpar@574
    33
//! \ingroup misc
alpar@574
    34
//! \file
alpar@1200
    35
//! \brief Some utilities to write test programs.
alpar@574
    36
alpar@574
    37
alpar@679
    38
///If \c rc is fail, writes an error message end exit.
alpar@574
    39
alpar@574
    40
///If \c rc is fail, writes an error message end exit.
alpar@574
    41
///The error message contains the file name and the line number of the
alpar@679
    42
///source code in a standard from, which makes it possible to go there
alpar@574
    43
///using good source browsers like e.g. \c emacs.
alpar@574
    44
///
alpar@574
    45
///For example
alpar@574
    46
///\code check(0==1,"This is obviously false.");\endcode will
alpar@574
    47
///print this (and then exits).
alpar@574
    48
///\verbatim graph_test.cc:123: error: This is obviously false. \endverbatim
alpar@774
    49
///
alpar@774
    50
///\todo It should be in \c error.h
alpar@574
    51
#define check(rc, msg) \
alpar@574
    52
  if(!(rc)) { \
alpar@574
    53
    std::cerr << __FILE__ ":" << __LINE__ << ": error: " << msg << std::endl; \
deba@2198
    54
    abort(); \
alpar@574
    55
  } else { } \
alpar@574
    56
alpar@574
    57
///Structure returned by \ref addPetersen().
alpar@574
    58
alpar@574
    59
///Structure returned by \ref addPetersen().
alpar@574
    60
///
alpar@574
    61
template<class Graph> struct PetStruct
alpar@574
    62
{
alpar@825
    63
  ///Vector containing the outer nodes.
alpar@825
    64
  std::vector<typename Graph::Node> outer;
alpar@825
    65
  ///Vector containing the inner nodes.
alpar@825
    66
  std::vector<typename Graph::Node> inner;
alpar@825
    67
  ///Vector containing the edges of the inner circle.
alpar@825
    68
  std::vector<typename Graph::Edge> incir;
alpar@825
    69
  ///Vector containing the edges of the outer circle.
alpar@825
    70
  std::vector<typename Graph::Edge> outcir;
alpar@825
    71
  ///Vector containing the chord edges.
alpar@825
    72
  std::vector<typename Graph::Edge> chords;
alpar@574
    73
};
alpar@574
    74
alpar@721
    75
alpar@721
    76
alpar@574
    77
///Adds a Petersen graph to \c G.
alpar@574
    78
alpar@574
    79
///Adds a Petersen graph to \c G.
deba@937
    80
///\return The nodes and edges of the generated graph.
alpar@721
    81
alpar@721
    82
template<typename Graph>
klao@946
    83
PetStruct<Graph> addPetersen(Graph &G,int num = 5)
alpar@574
    84
{
alpar@574
    85
  PetStruct<Graph> n;
alpar@574
    86
alpar@574
    87
  for(int i=0;i<num;i++) {
alpar@574
    88
    n.outer.push_back(G.addNode());
alpar@574
    89
    n.inner.push_back(G.addNode());
alpar@574
    90
  }
alpar@574
    91
alpar@574
    92
 for(int i=0;i<num;i++) {
alpar@574
    93
   n.chords.push_back(G.addEdge(n.outer[i],n.inner[i]));
klao@946
    94
   n.outcir.push_back(G.addEdge(n.outer[i],n.outer[(i+1) % num]));
klao@946
    95
   n.incir.push_back(G.addEdge(n.inner[i],n.inner[(i+2) % num]));
alpar@574
    96
  }
alpar@574
    97
 return n;
alpar@574
    98
}
alpar@574
    99
alpar@1200
   100
/// \brief Adds to the graph the reverse pair of all edges.
klao@946
   101
///
alpar@1200
   102
/// Adds to the graph the reverse pair of all edges.
klao@946
   103
///
klao@946
   104
template<class Graph> void bidirGraph(Graph &G)
klao@946
   105
{
klao@946
   106
  typedef typename Graph::Edge Edge;
klao@946
   107
  typedef typename Graph::EdgeIt EdgeIt;
klao@946
   108
  
klao@946
   109
  std::vector<Edge> ee;
klao@946
   110
  
klao@946
   111
  for(EdgeIt e(G);e!=INVALID;++e) ee.push_back(e);
klao@946
   112
klao@946
   113
  for(typename std::vector<Edge>::iterator p=ee.begin();p!=ee.end();p++)
alpar@986
   114
    G.addEdge(G.target(*p),G.source(*p));
klao@946
   115
}
klao@946
   116
klao@946
   117
klao@946
   118
/// \brief Checks the bidirectioned Petersen graph.
klao@946
   119
///
klao@946
   120
///  Checks the bidirectioned Petersen graph.
klao@946
   121
///
klao@946
   122
template<class Graph> void checkBidirPetersen(Graph &G, int num = 5)
klao@946
   123
{
klao@946
   124
  typedef typename Graph::Node Node;
klao@946
   125
klao@946
   126
  typedef typename Graph::EdgeIt EdgeIt;
klao@946
   127
  typedef typename Graph::NodeIt NodeIt;
klao@946
   128
klao@946
   129
  checkGraphNodeList(G, 2 * num);
klao@946
   130
  checkGraphEdgeList(G, 6 * num);
klao@946
   131
klao@946
   132
  for(NodeIt n(G);n!=INVALID;++n) {
klao@946
   133
    checkGraphInEdgeList(G, n, 3);
klao@946
   134
    checkGraphOutEdgeList(G, n, 3);
klao@946
   135
  }  
klao@946
   136
}
klao@946
   137
klao@1909
   138
///Structure returned by \ref addUPetersen().
alpar@574
   139
klao@1909
   140
///Structure returned by \ref addUPetersen().
deba@937
   141
///
klao@1909
   142
template<class Graph> struct UPetStruct
deba@937
   143
{
deba@937
   144
  ///Vector containing the outer nodes.
deba@937
   145
  std::vector<typename Graph::Node> outer;
deba@937
   146
  ///Vector containing the inner nodes.
deba@937
   147
  std::vector<typename Graph::Node> inner;
deba@937
   148
  ///Vector containing the edges of the inner circle.
klao@1909
   149
  std::vector<typename Graph::UEdge> incir;
deba@937
   150
  ///Vector containing the edges of the outer circle.
klao@1909
   151
  std::vector<typename Graph::UEdge> outcir;
deba@937
   152
  ///Vector containing the chord edges.
klao@1909
   153
  std::vector<typename Graph::UEdge> chords;
deba@937
   154
};
deba@937
   155
alpar@1716
   156
///Adds a Petersen graph to the undirected \c G.
deba@937
   157
alpar@1716
   158
///Adds a Petersen graph to the undirected \c G.
deba@937
   159
///\return The nodes and edges of the generated graph.
deba@937
   160
deba@937
   161
template<typename Graph>
klao@1909
   162
UPetStruct<Graph> addUPetersen(Graph &G,int num=5)
deba@937
   163
{
klao@1909
   164
  UPetStruct<Graph> n;
deba@937
   165
deba@937
   166
  for(int i=0;i<num;i++) {
deba@937
   167
    n.outer.push_back(G.addNode());
deba@937
   168
    n.inner.push_back(G.addNode());
deba@937
   169
  }
deba@937
   170
deba@937
   171
 for(int i=0;i<num;i++) {
deba@937
   172
   n.chords.push_back(G.addEdge(n.outer[i],n.inner[i]));
deba@937
   173
   n.outcir.push_back(G.addEdge(n.outer[i],n.outer[(i+1)%5]));
deba@937
   174
   n.incir.push_back(G.addEdge(n.inner[i],n.inner[(i+2)%5]));
deba@937
   175
  }
deba@937
   176
 return n;
deba@937
   177
}
alpar@574
   178
deba@1728
   179
int _urandom_init() {
deba@1728
   180
  int seed = time(0);
deba@1728
   181
  srand(seed);
deba@1728
   182
  return seed;
deba@1728
   183
}
deba@1728
   184
deba@1728
   185
int urandom(int n) {
deba@1728
   186
  static int seed = _urandom_init();
deba@1728
   187
  ignore_unused_variable_warning(seed);
deba@1728
   188
  return (int)(rand() / (1.0 + RAND_MAX) * n);
deba@1728
   189
}
deba@1728
   190
alpar@574
   191
#endif