src/test/graph_test.h
author marci
Fri, 01 Oct 2004 11:31:03 +0000
changeset 933 1b7c88fbb950
parent 919 6153d9cf78c6
child 937 d4e911acef3d
permissions -rw-r--r--
NodeSubGraphWrapper, test, and ducumentation modifications.
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/test/graph_test.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@921
    16
#ifndef LEMON_TEST_GRAPH_TEST_H
alpar@921
    17
#define LEMON_TEST_GRAPH_TEST_H
alpar@800
    18
alpar@800
    19
alpar@800
    20
#include "test_tools.h"
alpar@800
    21
alpar@800
    22
//! \ingroup misc
alpar@800
    23
//! \file
alpar@800
    24
//! \brief Some utility to  test graph classes.
alpar@921
    25
namespace lemon {
alpar@800
    26
deba@891
    27
  struct DummyType {
deba@891
    28
    int value;
deba@891
    29
    DummyType() {}
deba@891
    30
    DummyType(int p) : value(p) {}
deba@891
    31
    DummyType& operator=(int p) { value = p; return *this;}
deba@891
    32
  };
alpar@800
    33
deba@891
    34
deba@891
    35
  template<class Graph> void checkCompileStaticGraph(Graph &G) 
deba@891
    36
    {
deba@891
    37
      typedef typename Graph::Node Node;
deba@891
    38
      typedef typename Graph::NodeIt NodeIt;
deba@891
    39
      typedef typename Graph::Edge Edge;
deba@891
    40
      typedef typename Graph::EdgeIt EdgeIt;
deba@891
    41
      typedef typename Graph::InEdgeIt InEdgeIt;
deba@891
    42
      typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@800
    43
  
deba@891
    44
      {
deba@891
    45
	Node i; Node j(i); Node k(INVALID);
deba@891
    46
	i=j;
deba@891
    47
	bool b; b=true;
deba@891
    48
	b=(i==INVALID); b=(i!=INVALID);
deba@891
    49
	b=(i==j); b=(i!=j); b=(i<j);
deba@891
    50
      }
deba@891
    51
      {
deba@891
    52
	NodeIt i; NodeIt j(i); NodeIt k(INVALID); NodeIt l(G);
deba@891
    53
	i=j;
deba@891
    54
	j=G.first(i);
deba@891
    55
	j=++i;
deba@891
    56
	bool b; b=true;
deba@891
    57
	b=(i==INVALID); b=(i!=INVALID);
deba@891
    58
	Node n(i);
deba@891
    59
	n=i;
deba@891
    60
	b=(i==j); b=(i!=j); b=(i<j);
deba@891
    61
	//Node ->NodeIt conversion
deba@891
    62
	NodeIt ni(G,n);
deba@891
    63
      }
deba@891
    64
      {
deba@891
    65
	Edge i; Edge j(i); Edge k(INVALID);
deba@891
    66
	i=j;
deba@891
    67
	bool b; b=true;
deba@891
    68
	b=(i==INVALID); b=(i!=INVALID);
deba@891
    69
	b=(i==j); b=(i!=j); b=(i<j);
deba@891
    70
      }
deba@891
    71
      {
deba@891
    72
	EdgeIt i; EdgeIt j(i); EdgeIt k(INVALID); EdgeIt l(G);
deba@891
    73
	i=j;
deba@891
    74
	j=G.first(i);
deba@891
    75
	j=++i;
deba@891
    76
	bool b; b=true;
deba@891
    77
	b=(i==INVALID); b=(i!=INVALID);
deba@891
    78
	Edge e(i);
deba@891
    79
	e=i;
deba@891
    80
	b=(i==j); b=(i!=j); b=(i<j);
deba@891
    81
	//Edge ->EdgeIt conversion
deba@891
    82
	EdgeIt ei(G,e);
deba@891
    83
      }
deba@891
    84
      {
deba@891
    85
	Node n;
deba@891
    86
	InEdgeIt i; InEdgeIt j(i); InEdgeIt k(INVALID); InEdgeIt l(G,n);
deba@891
    87
	i=j;
deba@891
    88
	j=G.first(i,n);
deba@891
    89
	j=++i;
deba@891
    90
	bool b; b=true;
deba@891
    91
	b=(i==INVALID); b=(i!=INVALID);
deba@891
    92
	Edge e(i);
deba@891
    93
	e=i;
deba@891
    94
	b=(i==j); b=(i!=j); b=(i<j);
deba@891
    95
	//Edge ->InEdgeIt conversion
deba@891
    96
	InEdgeIt ei(G,e);
deba@891
    97
      }
deba@891
    98
      {
deba@891
    99
	Node n;
deba@891
   100
	OutEdgeIt i; OutEdgeIt j(i); OutEdgeIt k(INVALID); OutEdgeIt l(G,n);
deba@891
   101
	i=j;
deba@891
   102
	j=G.first(i,n);
deba@891
   103
	j=++i;
deba@891
   104
	bool b; b=true;
deba@891
   105
	b=(i==INVALID); b=(i!=INVALID);
deba@891
   106
	Edge e(i);
deba@891
   107
	e=i;
deba@891
   108
	b=(i==j); b=(i!=j); b=(i<j);
deba@891
   109
	//Edge ->OutEdgeIt conversion
deba@891
   110
	OutEdgeIt ei(G,e);
deba@891
   111
      }
deba@891
   112
      {
deba@891
   113
	Node n,m;
deba@891
   114
	n=m=INVALID;
deba@891
   115
	Edge e;
deba@891
   116
	e=INVALID;
deba@891
   117
	n=G.tail(e);
deba@891
   118
	n=G.head(e);
deba@891
   119
      }
deba@891
   120
      // id tests
deba@891
   121
      { Node n; int i=G.id(n); i=i; }
deba@891
   122
      { Edge e; int i=G.id(e); i=i; }
deba@891
   123
      //NodeMap tests
deba@891
   124
      {
deba@891
   125
	Node k;
deba@891
   126
	typename Graph::template NodeMap<int> m(G);
deba@891
   127
	//Const map
deba@891
   128
	typename Graph::template NodeMap<int> const &cm = m;
deba@891
   129
	//Inicialize with default value
deba@891
   130
	typename Graph::template NodeMap<int> mdef(G,12);
deba@891
   131
	//Copy
deba@891
   132
	typename Graph::template NodeMap<int> mm(cm);
deba@891
   133
	//Copy from another type
deba@891
   134
	typename Graph::template NodeMap<double> dm(cm);
deba@891
   135
	//Copy to more complex type
deba@891
   136
	typename Graph::template NodeMap<DummyType> em(cm);
deba@891
   137
	int v;
deba@891
   138
	v=m[k]; m[k]=v; m.set(k,v);
deba@891
   139
	v=cm[k];
alpar@800
   140
    
deba@891
   141
	m=cm;  
deba@891
   142
	dm=cm; //Copy from another type  
deba@891
   143
	em=cm; //Copy to more complex type
deba@891
   144
	{
deba@891
   145
	  //Check the typedef's
deba@891
   146
	  typename Graph::template NodeMap<int>::ValueType val;
deba@891
   147
	  val=1;
deba@891
   148
	  typename Graph::template NodeMap<int>::KeyType key;
deba@891
   149
	  key = typename Graph::NodeIt(G);
deba@891
   150
	}
deba@891
   151
      }  
deba@891
   152
      { //bool NodeMap
deba@891
   153
	Node k;
deba@891
   154
	typename Graph::template NodeMap<bool> m(G);
deba@891
   155
	typename Graph::template NodeMap<bool> const &cm = m;  //Const map
deba@891
   156
	//Inicialize with default value
deba@891
   157
	typename Graph::template NodeMap<bool> mdef(G,12);
deba@891
   158
	typename Graph::template NodeMap<bool> mm(cm);   //Copy
deba@891
   159
	typename Graph::template NodeMap<int> dm(cm); //Copy from another type
deba@891
   160
	bool v;
deba@891
   161
	v=m[k]; m[k]=v; m.set(k,v);
deba@891
   162
	v=cm[k];
deba@891
   163
    
deba@891
   164
	m=cm;  
deba@891
   165
	dm=cm; //Copy from another type
deba@891
   166
	m=dm; //Copy to another type
deba@891
   167
deba@891
   168
	{
deba@891
   169
	  //Check the typedef's
deba@891
   170
	  typename Graph::template NodeMap<bool>::ValueType val;
deba@891
   171
	  val=true;
deba@891
   172
	  typename Graph::template NodeMap<bool>::KeyType key;
deba@891
   173
	  key= typename Graph::NodeIt(G);
deba@891
   174
	}
deba@891
   175
      }
deba@891
   176
      //EdgeMap tests
deba@891
   177
      {
deba@891
   178
	Edge k;
deba@891
   179
	typename Graph::template EdgeMap<int> m(G);
deba@891
   180
	typename Graph::template EdgeMap<int> const &cm = m;  //Const map
deba@891
   181
	//Inicialize with default value
deba@891
   182
	typename Graph::template EdgeMap<int> mdef(G,12);
deba@891
   183
	typename Graph::template EdgeMap<int> mm(cm);   //Copy
deba@891
   184
	typename Graph::template EdgeMap<double> dm(cm); //Copy from another type
deba@891
   185
	int v;
deba@891
   186
	v=m[k]; m[k]=v; m.set(k,v);
deba@891
   187
	v=cm[k];
deba@891
   188
    
deba@891
   189
	m=cm;  
deba@891
   190
	dm=cm; //Copy from another type
deba@891
   191
	{
deba@891
   192
	  //Check the typedef's
deba@891
   193
	  typename Graph::template EdgeMap<int>::ValueType val;
deba@891
   194
	  val=1;
deba@891
   195
	  typename Graph::template EdgeMap<int>::KeyType key;
deba@891
   196
	  key= typename Graph::EdgeIt(G);
deba@891
   197
	}
deba@891
   198
      }  
deba@891
   199
      { //bool EdgeMap
deba@891
   200
	Edge k;
deba@891
   201
	typename Graph::template EdgeMap<bool> m(G);
deba@891
   202
	typename Graph::template EdgeMap<bool> const &cm = m;  //Const map
deba@891
   203
	//Inicialize with default value
deba@891
   204
	typename Graph::template EdgeMap<bool> mdef(G,12);
deba@891
   205
	typename Graph::template EdgeMap<bool> mm(cm);   //Copy
deba@891
   206
	typename Graph::template EdgeMap<int> dm(cm); //Copy from another type
deba@891
   207
	bool v;
deba@891
   208
	v=m[k]; m[k]=v; m.set(k,v);
deba@891
   209
	v=cm[k];
deba@891
   210
    
deba@891
   211
	m=cm;  
deba@891
   212
	dm=cm; //Copy from another type
deba@891
   213
	m=dm; //Copy to another type
deba@891
   214
	{
deba@891
   215
	  //Check the typedef's
deba@891
   216
	  typename Graph::template EdgeMap<bool>::ValueType val;
deba@891
   217
	  val=true;
deba@891
   218
	  typename Graph::template EdgeMap<bool>::KeyType key;
deba@891
   219
	  key= typename Graph::EdgeIt(G);
deba@891
   220
	}
deba@891
   221
      }
deba@891
   222
    }
deba@891
   223
deba@891
   224
  template<class Graph> void checkCompileGraph(Graph &G) 
alpar@800
   225
    {
deba@891
   226
      checkCompileStaticGraph(G);
deba@891
   227
deba@891
   228
      typedef typename Graph::Node Node;
deba@891
   229
      typedef typename Graph::NodeIt NodeIt;
deba@891
   230
      typedef typename Graph::Edge Edge;
deba@891
   231
      typedef typename Graph::EdgeIt EdgeIt;
deba@891
   232
      typedef typename Graph::InEdgeIt InEdgeIt;
deba@891
   233
      typedef typename Graph::OutEdgeIt OutEdgeIt;
deba@891
   234
  
deba@891
   235
      Node n,m;
deba@891
   236
      n=G.addNode();
deba@891
   237
      m=G.addNode();
deba@891
   238
      Edge e;
deba@891
   239
      e=G.addEdge(n,m); 
deba@891
   240
  
deba@891
   241
      //  G.clear();
alpar@800
   242
    }
alpar@800
   243
deba@891
   244
  template<class Graph> void checkCompileGraphEraseEdge(Graph &G) 
alpar@800
   245
    {
deba@891
   246
      typename Graph::Edge e;
deba@891
   247
      G.erase(e);
alpar@800
   248
    }
deba@891
   249
deba@891
   250
  template<class Graph> void checkCompileGraphEraseNode(Graph &G) 
alpar@800
   251
    {
deba@891
   252
      typename Graph::Node n;
deba@891
   253
      G.erase(n);
alpar@800
   254
    }
deba@891
   255
deba@891
   256
  template<class Graph> void checkCompileErasableGraph(Graph &G) 
alpar@800
   257
    {
deba@891
   258
      checkCompileGraph(G);
deba@891
   259
      checkCompileGraphEraseNode(G);
deba@891
   260
      checkCompileGraphEraseEdge(G);
alpar@800
   261
    }
alpar@800
   262
deba@891
   263
  template<class Graph> void checkCompileGraphFindEdge(Graph &G) 
deba@891
   264
    {
deba@891
   265
      typedef typename Graph::NodeIt Node;
deba@891
   266
      typedef typename Graph::NodeIt NodeIt;
alpar@800
   267
deba@891
   268
      G.findEdge(NodeIt(G),++NodeIt(G),G.findEdge(NodeIt(G),++NodeIt(G)));
deba@891
   269
      G.findEdge(Node(),Node(),G.findEdge(Node(),Node()));  
deba@891
   270
    }
alpar@800
   271
deba@891
   272
  template<class Graph> void checkGraphNodeList(Graph &G, int nn)
deba@891
   273
    {
deba@891
   274
      typename Graph::NodeIt n(G);
deba@891
   275
      for(int i=0;i<nn;i++) {
deba@891
   276
	check(n!=INVALID,"Wrong Node list linking.");
deba@891
   277
	++n;
deba@891
   278
      }
deba@891
   279
      check(n==INVALID,"Wrong Node list linking.");
deba@891
   280
    }
alpar@800
   281
deba@891
   282
  template<class Graph> void checkGraphEdgeList(Graph &G, int nn)
deba@891
   283
    {
deba@891
   284
      typedef typename Graph::EdgeIt EdgeIt;
alpar@800
   285
deba@891
   286
      EdgeIt e(G);
deba@891
   287
      for(int i=0;i<nn;i++) {
deba@891
   288
	check(e!=INVALID,"Wrong Edge list linking.");
deba@891
   289
	++e;
deba@891
   290
      }
deba@891
   291
      check(e==INVALID,"Wrong Edge list linking.");
deba@891
   292
    }
alpar@800
   293
deba@891
   294
  template<class Graph> void checkGraphOutEdgeList(Graph &G,
deba@891
   295
						   typename Graph::Node n,
deba@891
   296
						   int nn)
deba@891
   297
    {
deba@891
   298
      typename Graph::OutEdgeIt e(G,n);
deba@891
   299
      for(int i=0;i<nn;i++) {
deba@891
   300
	check(e!=INVALID,"Wrong OutEdge list linking.");
deba@891
   301
	++e;
deba@891
   302
      }
deba@891
   303
      check(e==INVALID,"Wrong OutEdge list linking.");
deba@891
   304
    }
alpar@800
   305
deba@891
   306
  template<class Graph> void checkGraphInEdgeList(Graph &G,
deba@891
   307
						  typename Graph::Node n,
deba@891
   308
						  int nn)
deba@891
   309
    {
deba@891
   310
      typename Graph::InEdgeIt e(G,n);
deba@891
   311
      for(int i=0;i<nn;i++) {
deba@891
   312
	check(e!=INVALID,"Wrong InEdge list linking.");
deba@891
   313
	++e;
deba@891
   314
      }
deba@891
   315
      check(e==INVALID,"Wrong InEdge list linking.");
deba@891
   316
    }
alpar@800
   317
deba@891
   318
  ///\file
deba@891
   319
  ///\todo Check head(), tail() as well;
alpar@800
   320
alpar@800
   321
  
alpar@921
   322
} //namespace lemon
alpar@800
   323
alpar@800
   324
alpar@800
   325
#endif