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