2  * test/test_tools.h - Part of LEMON, a generic C++ optimization library
 
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     7  * Permission to use, modify and distribute this software is granted
 
     8  * provided that this copyright notice appears in all copies. For
 
     9  * precise terms see the accompanying LICENSE file.
 
    11  * This software is provided "AS IS" with no warranty of any kind,
 
    12  * express or implied, and with no claim as to its suitability for any
 
    17 #ifndef LEMON_TEST_TEST_TOOLS_H
 
    18 #define LEMON_TEST_TEST_TOOLS_H
 
    26 #include <lemon/invalid.h>
 
    27 #include <lemon/concept_check.h>
 
    29 using namespace lemon;
 
    33 //! \brief Some utilities to write test programs.
 
    36 ///If \c rc is fail, writes an error message end exit.
 
    38 ///If \c rc is fail, writes an error message end exit.
 
    39 ///The error message contains the file name and the line number of the
 
    40 ///source code in a standard from, which makes it possible to go there
 
    41 ///using good source browsers like e.g. \c emacs.
 
    44 ///\code check(0==1,"This is obviously false.");\endcode will
 
    45 ///print this (and then exits).
 
    46 ///\verbatim graph_test.cc:123: error: This is obviously false. \endverbatim
 
    48 ///\todo It should be in \c error.h
 
    49 #define check(rc, msg) \
 
    51     std::cerr << __FILE__ ":" << __LINE__ << ": error: " << msg << std::endl; \
 
    55 ///Structure returned by \ref addPetersen().
 
    57 ///Structure returned by \ref addPetersen().
 
    59 template<class Graph> struct PetStruct
 
    61   ///Vector containing the outer nodes.
 
    62   std::vector<typename Graph::Node> outer;
 
    63   ///Vector containing the inner nodes.
 
    64   std::vector<typename Graph::Node> inner;
 
    65   ///Vector containing the edges of the inner circle.
 
    66   std::vector<typename Graph::Edge> incir;
 
    67   ///Vector containing the edges of the outer circle.
 
    68   std::vector<typename Graph::Edge> outcir;
 
    69   ///Vector containing the chord edges.
 
    70   std::vector<typename Graph::Edge> chords;
 
    75 ///Adds a Petersen graph to \c G.
 
    77 ///Adds a Petersen graph to \c G.
 
    78 ///\return The nodes and edges of the generated graph.
 
    80 template<typename Graph>
 
    81 PetStruct<Graph> addPetersen(Graph &G,int num = 5)
 
    85   for(int i=0;i<num;i++) {
 
    86     n.outer.push_back(G.addNode());
 
    87     n.inner.push_back(G.addNode());
 
    90  for(int i=0;i<num;i++) {
 
    91    n.chords.push_back(G.addEdge(n.outer[i],n.inner[i]));
 
    92    n.outcir.push_back(G.addEdge(n.outer[i],n.outer[(i+1) % num]));
 
    93    n.incir.push_back(G.addEdge(n.inner[i],n.inner[(i+2) % num]));
 
    98 /// \brief Adds to the graph the reverse pair of all edges.
 
   100 /// Adds to the graph the reverse pair of all edges.
 
   102 template<class Graph> void bidirGraph(Graph &G)
 
   104   typedef typename Graph::Edge Edge;
 
   105   typedef typename Graph::EdgeIt EdgeIt;
 
   107   std::vector<Edge> ee;
 
   109   for(EdgeIt e(G);e!=INVALID;++e) ee.push_back(e);
 
   111   for(typename std::vector<Edge>::iterator p=ee.begin();p!=ee.end();p++)
 
   112     G.addEdge(G.target(*p),G.source(*p));
 
   116 /// \brief Checks the bidirectioned Petersen graph.
 
   118 ///  Checks the bidirectioned Petersen graph.
 
   120 template<class Graph> void checkBidirPetersen(Graph &G, int num = 5)
 
   122   typedef typename Graph::Node Node;
 
   124   typedef typename Graph::EdgeIt EdgeIt;
 
   125   typedef typename Graph::NodeIt NodeIt;
 
   127   checkGraphNodeList(G, 2 * num);
 
   128   checkGraphEdgeList(G, 6 * num);
 
   130   for(NodeIt n(G);n!=INVALID;++n) {
 
   131     checkGraphInEdgeList(G, n, 3);
 
   132     checkGraphOutEdgeList(G, n, 3);
 
   136 ///Structure returned by \ref addUndirPetersen().
 
   138 ///Structure returned by \ref addUndirPetersen().
 
   140 template<class Graph> struct UndirPetStruct
 
   142   ///Vector containing the outer nodes.
 
   143   std::vector<typename Graph::Node> outer;
 
   144   ///Vector containing the inner nodes.
 
   145   std::vector<typename Graph::Node> inner;
 
   146   ///Vector containing the edges of the inner circle.
 
   147   std::vector<typename Graph::UndirEdge> incir;
 
   148   ///Vector containing the edges of the outer circle.
 
   149   std::vector<typename Graph::UndirEdge> outcir;
 
   150   ///Vector containing the chord edges.
 
   151   std::vector<typename Graph::UndirEdge> chords;
 
   154 ///Adds a Petersen graph to the undirected \c G.
 
   156 ///Adds a Petersen graph to the undirected \c G.
 
   157 ///\return The nodes and edges of the generated graph.
 
   159 template<typename Graph>
 
   160 UndirPetStruct<Graph> addUndirPetersen(Graph &G,int num=5)
 
   162   UndirPetStruct<Graph> n;
 
   164   for(int i=0;i<num;i++) {
 
   165     n.outer.push_back(G.addNode());
 
   166     n.inner.push_back(G.addNode());
 
   169  for(int i=0;i<num;i++) {
 
   170    n.chords.push_back(G.addEdge(n.outer[i],n.inner[i]));
 
   171    n.outcir.push_back(G.addEdge(n.outer[i],n.outer[(i+1)%5]));
 
   172    n.incir.push_back(G.addEdge(n.inner[i],n.inner[(i+2)%5]));
 
   177 int _urandom_init() {
 
   184   static int seed = _urandom_init();
 
   185   ignore_unused_variable_warning(seed);
 
   186   return (int)(rand() / (1.0 + RAND_MAX) * n);