src/test/graph_test.cc
author alpar
Wed, 04 Aug 2004 18:51:51 +0000
changeset 751 e742d383fffc
parent 717 6874df3f61db
child 774 4297098d9677
permissions -rw-r--r--
- Trimmed in order to work with gcc-3.4
- The number of executions of the tests can be controlled by command arg.
alpar@503
     1
#include<iostream>
ladanyi@542
     2
#include<hugo/smart_graph.h>
alpar@564
     3
#include<hugo/skeletons/graph.h>
alpar@578
     4
#include<hugo/list_graph.h>
alpar@592
     5
#include<hugo/full_graph.h>
alpar@578
     6
alpar@567
     7
#include"test_tools.h"
alpar@567
     8
alpar@503
     9
/*
alpar@503
    10
This test makes consistency checks of list graph structures.
alpar@503
    11
alpar@503
    12
G.addNode(), G.addEdge(), G.valid(), G.tail(), G.head()
alpar@503
    13
alpar@592
    14
\todo Checks for empty graphs and isolated points.
alpar@503
    15
*/
alpar@503
    16
alpar@503
    17
using namespace hugo;
alpar@733
    18
using namespace hugo::skeleton;
alpar@503
    19
alpar@592
    20
template<class Graph> void checkCompileStaticGraph(Graph &G) 
alpar@503
    21
{
alpar@503
    22
  typedef typename Graph::Node Node;
alpar@503
    23
  typedef typename Graph::NodeIt NodeIt;
alpar@503
    24
  typedef typename Graph::Edge Edge;
alpar@503
    25
  typedef typename Graph::EdgeIt EdgeIt;
alpar@503
    26
  typedef typename Graph::InEdgeIt InEdgeIt;
alpar@503
    27
  typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@503
    28
  
alpar@503
    29
  {
alpar@503
    30
    Node i; Node j(i); Node k(INVALID);
alpar@503
    31
    i=j;
alpar@503
    32
    bool b=G.valid(i); b=b;
alpar@503
    33
    b=(i==j); b=(i!=j); b=(i<j);
alpar@503
    34
  }
alpar@503
    35
  {
alpar@503
    36
    NodeIt i; NodeIt j(i); NodeIt k(INVALID); NodeIt l(G);
alpar@503
    37
    i=j;
alpar@503
    38
    j=G.first(i);
alpar@503
    39
    j=G.next(i);
alpar@503
    40
    bool b=G.valid(i); b=b;
alpar@503
    41
    Node n(i);
alpar@503
    42
    n=i;
alpar@503
    43
    b=(i==j); b=(i!=j); b=(i<j);
alpar@503
    44
  }
alpar@503
    45
  {
alpar@503
    46
    Edge i; Edge j(i); Edge k(INVALID);
alpar@503
    47
    i=j;
alpar@503
    48
    bool b=G.valid(i); b=b;
alpar@503
    49
    b=(i==j); b=(i!=j); b=(i<j);
alpar@503
    50
  }
alpar@503
    51
  {
alpar@503
    52
    EdgeIt i; EdgeIt j(i); EdgeIt k(INVALID); EdgeIt l(G);
alpar@503
    53
    i=j;
alpar@503
    54
    j=G.first(i);
alpar@503
    55
    j=G.next(i);
alpar@503
    56
    bool b=G.valid(i); b=b;
alpar@503
    57
    Edge e(i);
alpar@503
    58
    e=i;
alpar@503
    59
    b=(i==j); b=(i!=j); b=(i<j);
alpar@503
    60
  }
alpar@503
    61
  {
alpar@503
    62
    Node n;
alpar@503
    63
    InEdgeIt i; InEdgeIt j(i); InEdgeIt k(INVALID); InEdgeIt l(G,n);
alpar@503
    64
    i=j;
alpar@503
    65
    j=G.first(i,n);
alpar@503
    66
    j=G.next(i);
alpar@503
    67
    bool b=G.valid(i); b=b;
alpar@503
    68
    Edge e(i);
alpar@503
    69
    e=i;
alpar@503
    70
    b=(i==j); b=(i!=j); b=(i<j);
alpar@503
    71
  }
alpar@503
    72
  {
alpar@503
    73
    Node n;
alpar@503
    74
    OutEdgeIt i; OutEdgeIt j(i); OutEdgeIt k(INVALID); OutEdgeIt l(G,n);
alpar@503
    75
    i=j;
alpar@503
    76
    j=G.first(i,n);
alpar@503
    77
    j=G.next(i);
alpar@503
    78
    bool b=G.valid(i); b=b;
alpar@503
    79
    Edge e(i);
alpar@503
    80
    e=i;
alpar@503
    81
    b=(i==j); b=(i!=j); b=(i<j);
alpar@503
    82
  }
alpar@503
    83
alpar@503
    84
  Node n,m;
alpar@592
    85
  n=m=INVALID;
alpar@503
    86
  Edge e;
alpar@592
    87
  e=INVALID;
alpar@503
    88
  n=G.tail(e);
alpar@503
    89
  n=G.head(e);
alpar@503
    90
alpar@503
    91
  //aNode, bNode ?
alpar@503
    92
alpar@503
    93
  // id tests
alpar@503
    94
  { int i=G.id(n); i=i; }
alpar@503
    95
  { int i=G.id(e); i=i; }
alpar@503
    96
  
alpar@592
    97
  //  G.clear();
alpar@503
    98
alpar@503
    99
  //NodeMap tests
alpar@503
   100
  {
alpar@503
   101
    Node k;
alpar@515
   102
    typename Graph::template NodeMap<int> m(G);
alpar@515
   103
    typename Graph::template NodeMap<int> const &cm = m;  //Const map
alpar@515
   104
    //Inicialize with default value
alpar@515
   105
    typename Graph::template NodeMap<int> mdef(G,12);
alpar@515
   106
    typename Graph::template NodeMap<int> mm(cm);   //Copy
alpar@515
   107
    typename Graph::template NodeMap<double> dm(cm); //Copy from another type
alpar@503
   108
    int v;
alpar@503
   109
    v=m[k]; m[k]=v; m.set(k,v);
alpar@503
   110
    v=cm[k];
alpar@503
   111
    
alpar@503
   112
    m=cm;  
alpar@503
   113
    dm=cm; //Copy from another type
alpar@503
   114
  }  
alpar@503
   115
  { //bool NodeMap
alpar@503
   116
    Node k;
alpar@515
   117
    typename Graph::template NodeMap<bool> m(G);
alpar@515
   118
    typename Graph::template NodeMap<bool> const &cm = m;  //Const map
alpar@515
   119
    //Inicialize with default value
alpar@515
   120
    typename Graph::template NodeMap<bool> mdef(G,12);
alpar@515
   121
    typename Graph::template NodeMap<bool> mm(cm);   //Copy
alpar@515
   122
    typename Graph::template NodeMap<int> dm(cm); //Copy from another type
alpar@503
   123
    bool v;
alpar@503
   124
    v=m[k]; m[k]=v; m.set(k,v);
alpar@503
   125
    v=cm[k];
alpar@503
   126
    
alpar@503
   127
    m=cm;  
alpar@503
   128
    dm=cm; //Copy from another type
alpar@504
   129
    m=dm; //Copy to another type
alpar@503
   130
  }
alpar@503
   131
  //EdgeMap tests
alpar@503
   132
  {
alpar@503
   133
    Edge k;
alpar@515
   134
    typename Graph::template EdgeMap<int> m(G);
alpar@515
   135
    typename Graph::template EdgeMap<int> const &cm = m;  //Const map
alpar@515
   136
    //Inicialize with default value
alpar@515
   137
    typename Graph::template EdgeMap<int> mdef(G,12);
alpar@515
   138
    typename Graph::template EdgeMap<int> mm(cm);   //Copy
alpar@515
   139
    typename Graph::template EdgeMap<double> dm(cm); //Copy from another type
alpar@503
   140
    int v;
alpar@503
   141
    v=m[k]; m[k]=v; m.set(k,v);
alpar@503
   142
    v=cm[k];
alpar@503
   143
    
alpar@503
   144
    m=cm;  
alpar@503
   145
    dm=cm; //Copy from another type
alpar@503
   146
  }  
alpar@503
   147
  { //bool EdgeMap
alpar@503
   148
    Edge k;
alpar@515
   149
    typename Graph::template EdgeMap<bool> m(G);
alpar@515
   150
    typename Graph::template EdgeMap<bool> const &cm = m;  //Const map
alpar@515
   151
    //Inicialize with default value
alpar@515
   152
    typename Graph::template EdgeMap<bool> mdef(G,12);
alpar@515
   153
    typename Graph::template EdgeMap<bool> mm(cm);   //Copy
alpar@515
   154
    typename Graph::template EdgeMap<int> dm(cm); //Copy from another type
alpar@503
   155
    bool v;
alpar@503
   156
    v=m[k]; m[k]=v; m.set(k,v);
alpar@503
   157
    v=cm[k];
alpar@503
   158
    
alpar@503
   159
    m=cm;  
alpar@503
   160
    dm=cm; //Copy from another type
alpar@504
   161
    m=dm; //Copy to another type
alpar@503
   162
  }
alpar@503
   163
  
alpar@503
   164
}
alpar@503
   165
alpar@592
   166
template<class Graph> void checkCompile(Graph &G) 
alpar@592
   167
{
alpar@592
   168
  checkCompileStaticGraph(G);
alpar@592
   169
alpar@592
   170
  typedef typename Graph::Node Node;
alpar@592
   171
  typedef typename Graph::NodeIt NodeIt;
alpar@592
   172
  typedef typename Graph::Edge Edge;
alpar@592
   173
  typedef typename Graph::EdgeIt EdgeIt;
alpar@592
   174
  typedef typename Graph::InEdgeIt InEdgeIt;
alpar@592
   175
  typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@592
   176
  
alpar@592
   177
  Node n,m;
alpar@592
   178
  n=G.addNode();
alpar@592
   179
  m=G.addNode();
alpar@592
   180
  
alpar@592
   181
  G.addEdge(n,m);
alpar@592
   182
}
alpar@592
   183
alpar@592
   184
alpar@503
   185
template<class Graph> void checkNodeList(Graph &G, int nn)
alpar@503
   186
{
alpar@503
   187
  typename Graph::NodeIt n(G);
alpar@503
   188
  for(int i=0;i<nn;i++) {
alpar@503
   189
    check(G.valid(n),"Wrong Node list linking.");
alpar@503
   190
    G.next(n);
alpar@503
   191
  }
alpar@503
   192
  check(!G.valid(n),"Wrong Node list linking.");
alpar@503
   193
}
alpar@503
   194
alpar@503
   195
template<class Graph> void checkEdgeList(Graph &G, int nn)
alpar@503
   196
{
alpar@503
   197
  typedef typename Graph::EdgeIt EdgeIt;
alpar@503
   198
alpar@503
   199
  EdgeIt e(G);
alpar@503
   200
  for(int i=0;i<nn;i++) {
alpar@503
   201
    check(G.valid(e),"Wrong Edge list linking.");
alpar@503
   202
    G.next(e);
alpar@503
   203
  }
alpar@503
   204
  check(!G.valid(e),"Wrong Edge list linking.");
alpar@503
   205
}
alpar@503
   206
alpar@503
   207
template<class Graph> void checkOutEdgeList(Graph &G,
alpar@503
   208
					    typename Graph::Node n,
alpar@503
   209
					    int nn)
alpar@503
   210
{
alpar@503
   211
  typename Graph::OutEdgeIt e(G,n);
alpar@503
   212
  for(int i=0;i<nn;i++) {
alpar@503
   213
    check(G.valid(e),"Wrong OutEdge list linking.");
alpar@503
   214
    G.next(e);
alpar@503
   215
  }
alpar@503
   216
  check(!G.valid(e),"Wrong OutEdge list linking.");
alpar@503
   217
}
alpar@503
   218
alpar@503
   219
template<class Graph> void checkInEdgeList(Graph &G,
alpar@503
   220
					    typename Graph::Node n,
alpar@503
   221
					    int nn)
alpar@503
   222
{
alpar@503
   223
  typename Graph::InEdgeIt e(G,n);
alpar@503
   224
  for(int i=0;i<nn;i++) {
alpar@503
   225
    check(G.valid(e),"Wrong InEdge list linking.");
alpar@503
   226
    G.next(e);
alpar@503
   227
  }
alpar@503
   228
  check(!G.valid(e),"Wrong InEdge list linking.");
alpar@503
   229
}
alpar@503
   230
alpar@503
   231
//Checks head(), tail() as well;
alpar@503
   232
template<class Graph> void bidirPetersen(Graph &G)
alpar@503
   233
{
alpar@503
   234
  typedef typename Graph::Edge Edge;
alpar@503
   235
  typedef typename Graph::EdgeIt EdgeIt;
alpar@503
   236
  
alpar@503
   237
  checkEdgeList(G,15);
alpar@503
   238
  
alpar@503
   239
  std::vector<Edge> ee;
alpar@503
   240
  
alpar@503
   241
  for(EdgeIt e(G);G.valid(e);G.next(e)) ee.push_back(e);
alpar@503
   242
alpar@503
   243
  for(typename std::vector<Edge>::iterator p=ee.begin();p!=ee.end();p++)
alpar@503
   244
    G.addEdge(G.head(*p),G.tail(*p));
alpar@503
   245
}
alpar@503
   246
alpar@503
   247
template<class Graph> void checkPetersen(Graph &G)
alpar@503
   248
{
alpar@503
   249
  typedef typename Graph::Node Node;
alpar@503
   250
alpar@503
   251
  typedef typename Graph::EdgeIt EdgeIt;
alpar@503
   252
  typedef typename Graph::NodeIt NodeIt;
alpar@503
   253
alpar@503
   254
  checkNodeList(G,10);
alpar@503
   255
  checkEdgeList(G,30);
alpar@503
   256
alpar@503
   257
  for(NodeIt n(G);G.valid(n);G.next(n)) {
alpar@503
   258
    checkInEdgeList(G,n,3);
alpar@503
   259
    checkOutEdgeList(G,n,3);
alpar@503
   260
    G.next(n);
alpar@503
   261
  }  
alpar@503
   262
}
alpar@503
   263
alpar@733
   264
template
alpar@733
   265
void checkCompileStaticGraph<StaticGraphSkeleton>(StaticGraphSkeleton &);
alpar@564
   266
template void checkCompile<GraphSkeleton>(GraphSkeleton &);
alpar@733
   267
alpar@503
   268
template void checkCompile<SmartGraph>(SmartGraph &);
alpar@503
   269
template void checkCompile<SymSmartGraph>(SymSmartGraph &);
alpar@578
   270
template void checkCompile<ListGraph>(ListGraph &);
alpar@578
   271
template void checkCompile<SymListGraph>(SymListGraph &);
alpar@592
   272
template void checkCompileStaticGraph<FullGraph>(FullGraph &);
alpar@550
   273
alpar@579
   274
template void checkCompile<EdgeSet <ListGraph> >(EdgeSet <ListGraph> &);
alpar@717
   275
template void checkCompile<EdgeSet <NodeSet> >(EdgeSet <NodeSet> &);
alpar@503
   276
alpar@503
   277
int main() 
alpar@503
   278
{
alpar@503
   279
  {
alpar@503
   280
    SmartGraph G;
alpar@503
   281
    addPetersen(G);
alpar@503
   282
    bidirPetersen(G);
alpar@503
   283
    checkPetersen(G);
alpar@503
   284
  }
alpar@578
   285
  {
alpar@578
   286
    ListGraph G;
alpar@578
   287
    addPetersen(G);
alpar@578
   288
    bidirPetersen(G);
alpar@578
   289
    checkPetersen(G);
alpar@578
   290
  }
alpar@503
   291
  {
alpar@503
   292
    SymSmartGraph G;
alpar@503
   293
    addPetersen(G);
alpar@503
   294
    checkPetersen(G);
alpar@503
   295
  }
alpar@578
   296
  {
alpar@578
   297
    SymListGraph G;
alpar@578
   298
    addPetersen(G);
alpar@578
   299
    checkPetersen(G);
alpar@578
   300
  }
alpar@503
   301
alpar@503
   302
  //\todo map tests.
alpar@503
   303
  //\todo copy constr tests.
alpar@503
   304
alpar@503
   305
  std::cout << __FILE__ ": All tests passed.\n";
alpar@503
   306
alpar@579
   307
  return 0;
alpar@503
   308
}