src/test/graph_test.cc
author marci
Thu, 02 Sep 2004 16:59:30 +0000
changeset 789 65c5c7d37578
parent 783 81bf2d766164
child 793 9cd0aeea47b0
permissions -rw-r--r--
.
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@774
     9
/**
alpar@774
    10
\file
alpar@503
    11
This test makes consistency checks of list graph structures.
alpar@503
    12
alpar@774
    13
G.addNode(), G.addEdge(), G.tail(), G.head()
alpar@503
    14
alpar@592
    15
\todo Checks for empty graphs and isolated points.
alpar@774
    16
\todo Checks for Node->NodeIt, Edge->{EdgeIt,InEdgeIt,OutEdgeIt}
alpar@774
    17
conversion.
alpar@503
    18
*/
alpar@503
    19
alpar@503
    20
using namespace hugo;
alpar@733
    21
using namespace hugo::skeleton;
alpar@503
    22
alpar@592
    23
template<class Graph> void checkCompileStaticGraph(Graph &G) 
alpar@503
    24
{
alpar@503
    25
  typedef typename Graph::Node Node;
alpar@503
    26
  typedef typename Graph::NodeIt NodeIt;
alpar@503
    27
  typedef typename Graph::Edge Edge;
alpar@503
    28
  typedef typename Graph::EdgeIt EdgeIt;
alpar@503
    29
  typedef typename Graph::InEdgeIt InEdgeIt;
alpar@503
    30
  typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@503
    31
  
alpar@503
    32
  {
alpar@503
    33
    Node i; Node j(i); Node k(INVALID);
alpar@503
    34
    i=j;
alpar@774
    35
    //    bool b=G.valid(i); b=b;
alpar@774
    36
    bool b; b=b;
alpar@774
    37
    b=(i==INVALID); b=(i!=INVALID);
alpar@503
    38
    b=(i==j); b=(i!=j); b=(i<j);
alpar@503
    39
  }
alpar@503
    40
  {
alpar@503
    41
    NodeIt i; NodeIt j(i); NodeIt k(INVALID); NodeIt l(G);
alpar@503
    42
    i=j;
alpar@503
    43
    j=G.first(i);
alpar@774
    44
    j=++i;
alpar@774
    45
    //    bool b=G.valid(i); b=b;
alpar@774
    46
    bool b; b=b;
alpar@774
    47
    b=(i==INVALID); b=(i!=INVALID);
alpar@503
    48
    Node n(i);
alpar@503
    49
    n=i;
alpar@503
    50
    b=(i==j); b=(i!=j); b=(i<j);
alpar@774
    51
    //Node ->NodeIt conversion
alpar@774
    52
    NodeIt ni(G,n);
alpar@503
    53
  }
alpar@503
    54
  {
alpar@503
    55
    Edge i; Edge j(i); Edge k(INVALID);
alpar@503
    56
    i=j;
alpar@774
    57
    //    bool b=G.valid(i); b=b;
alpar@774
    58
    bool b; b=b;
alpar@774
    59
    b=(i==INVALID); b=(i!=INVALID);
alpar@503
    60
    b=(i==j); b=(i!=j); b=(i<j);
alpar@503
    61
  }
alpar@503
    62
  {
alpar@503
    63
    EdgeIt i; EdgeIt j(i); EdgeIt k(INVALID); EdgeIt l(G);
alpar@503
    64
    i=j;
alpar@503
    65
    j=G.first(i);
alpar@774
    66
    j=++i;
alpar@774
    67
    //    bool b=G.valid(i); b=b;
alpar@774
    68
    bool b; b=b;
alpar@774
    69
    b=(i==INVALID); b=(i!=INVALID);
alpar@503
    70
    Edge e(i);
alpar@503
    71
    e=i;
alpar@503
    72
    b=(i==j); b=(i!=j); b=(i<j);
alpar@774
    73
    //Edge ->EdgeIt conversion
alpar@774
    74
    EdgeIt ei(G,e);
alpar@503
    75
  }
alpar@503
    76
  {
alpar@503
    77
    Node n;
alpar@503
    78
    InEdgeIt i; InEdgeIt j(i); InEdgeIt k(INVALID); InEdgeIt l(G,n);
alpar@503
    79
    i=j;
alpar@503
    80
    j=G.first(i,n);
alpar@774
    81
    j=++i;
alpar@774
    82
    //    bool b=G.valid(i); b=b;
alpar@774
    83
    bool b; b=b;
alpar@774
    84
    b=(i==INVALID); b=(i!=INVALID);
alpar@503
    85
    Edge e(i);
alpar@503
    86
    e=i;
alpar@503
    87
    b=(i==j); b=(i!=j); b=(i<j);
alpar@774
    88
    //Edge ->InEdgeIt conversion
alpar@774
    89
    InEdgeIt ei(G,e);
alpar@503
    90
  }
alpar@503
    91
  {
alpar@503
    92
    Node n;
alpar@503
    93
    OutEdgeIt i; OutEdgeIt j(i); OutEdgeIt k(INVALID); OutEdgeIt l(G,n);
alpar@503
    94
    i=j;
alpar@503
    95
    j=G.first(i,n);
alpar@774
    96
    j=++i;
alpar@774
    97
    //    bool b=G.valid(i); b=b;
alpar@774
    98
    bool b; b=b;
alpar@774
    99
    b=(i==INVALID); b=(i!=INVALID);
alpar@503
   100
    Edge e(i);
alpar@503
   101
    e=i;
alpar@503
   102
    b=(i==j); b=(i!=j); b=(i<j);
alpar@774
   103
    //Edge ->OutEdgeIt conversion
alpar@774
   104
    OutEdgeIt ei(G,e);
alpar@503
   105
  }
alpar@774
   106
  {
alpar@774
   107
    Node n,m;
alpar@774
   108
    n=m=INVALID;
alpar@774
   109
    Edge e;
alpar@774
   110
    e=INVALID;
alpar@774
   111
    n=G.tail(e);
alpar@774
   112
    n=G.head(e);
alpar@774
   113
  }
alpar@503
   114
  // id tests
alpar@774
   115
  { Node n; int i=G.id(n); i=i; }
alpar@774
   116
  { Edge e; int i=G.id(e); i=i; }
alpar@503
   117
  //NodeMap tests
alpar@503
   118
  {
alpar@503
   119
    Node k;
alpar@515
   120
    typename Graph::template NodeMap<int> m(G);
alpar@774
   121
    //Const map
alpar@774
   122
    typename Graph::template NodeMap<int> const &cm = m;
alpar@515
   123
    //Inicialize with default value
alpar@515
   124
    typename Graph::template NodeMap<int> mdef(G,12);
alpar@774
   125
    //Copy
alpar@774
   126
    typename Graph::template NodeMap<int> mm(cm);
alpar@774
   127
    //Copy from another type
alpar@774
   128
    typename Graph::template NodeMap<double> dm(cm);
alpar@503
   129
    int v;
alpar@503
   130
    v=m[k]; m[k]=v; m.set(k,v);
alpar@503
   131
    v=cm[k];
alpar@503
   132
    
alpar@503
   133
    m=cm;  
alpar@503
   134
    dm=cm; //Copy from another type
alpar@787
   135
    {
alpar@787
   136
      //Check the typedef's
alpar@787
   137
      typename Graph::template NodeMap<int>::ValueType val;
alpar@787
   138
      val=1;
alpar@787
   139
      typename Graph::template NodeMap<int>::KeyType key;
alpar@787
   140
      key = typename Graph::NodeIt(G);
alpar@787
   141
    }
alpar@503
   142
  }  
alpar@503
   143
  { //bool NodeMap
alpar@503
   144
    Node k;
alpar@515
   145
    typename Graph::template NodeMap<bool> m(G);
alpar@515
   146
    typename Graph::template NodeMap<bool> const &cm = m;  //Const map
alpar@515
   147
    //Inicialize with default value
alpar@515
   148
    typename Graph::template NodeMap<bool> mdef(G,12);
alpar@515
   149
    typename Graph::template NodeMap<bool> mm(cm);   //Copy
alpar@515
   150
    typename Graph::template NodeMap<int> dm(cm); //Copy from another type
alpar@503
   151
    bool v;
alpar@503
   152
    v=m[k]; m[k]=v; m.set(k,v);
alpar@503
   153
    v=cm[k];
alpar@503
   154
    
alpar@503
   155
    m=cm;  
alpar@503
   156
    dm=cm; //Copy from another type
alpar@504
   157
    m=dm; //Copy to another type
alpar@787
   158
alpar@787
   159
    {
alpar@787
   160
      //Check the typedef's
alpar@787
   161
      typename Graph::template NodeMap<bool>::ValueType val;
alpar@787
   162
      val=true;
alpar@787
   163
      typename Graph::template NodeMap<bool>::KeyType key;
alpar@787
   164
      key= typename Graph::NodeIt(G);
alpar@787
   165
    }
alpar@503
   166
  }
alpar@503
   167
  //EdgeMap tests
alpar@503
   168
  {
alpar@503
   169
    Edge k;
alpar@515
   170
    typename Graph::template EdgeMap<int> m(G);
alpar@515
   171
    typename Graph::template EdgeMap<int> const &cm = m;  //Const map
alpar@515
   172
    //Inicialize with default value
alpar@515
   173
    typename Graph::template EdgeMap<int> mdef(G,12);
alpar@515
   174
    typename Graph::template EdgeMap<int> mm(cm);   //Copy
alpar@515
   175
    typename Graph::template EdgeMap<double> dm(cm); //Copy from another type
alpar@503
   176
    int v;
alpar@503
   177
    v=m[k]; m[k]=v; m.set(k,v);
alpar@503
   178
    v=cm[k];
alpar@503
   179
    
alpar@503
   180
    m=cm;  
alpar@503
   181
    dm=cm; //Copy from another type
alpar@787
   182
    {
alpar@787
   183
      //Check the typedef's
alpar@787
   184
      typename Graph::template EdgeMap<int>::ValueType val;
alpar@787
   185
      val=1;
alpar@787
   186
      typename Graph::template EdgeMap<int>::KeyType key;
alpar@787
   187
      key= typename Graph::EdgeIt(G);
alpar@787
   188
    }
alpar@503
   189
  }  
alpar@503
   190
  { //bool EdgeMap
alpar@503
   191
    Edge k;
alpar@515
   192
    typename Graph::template EdgeMap<bool> m(G);
alpar@515
   193
    typename Graph::template EdgeMap<bool> const &cm = m;  //Const map
alpar@515
   194
    //Inicialize with default value
alpar@515
   195
    typename Graph::template EdgeMap<bool> mdef(G,12);
alpar@515
   196
    typename Graph::template EdgeMap<bool> mm(cm);   //Copy
alpar@515
   197
    typename Graph::template EdgeMap<int> dm(cm); //Copy from another type
alpar@503
   198
    bool v;
alpar@503
   199
    v=m[k]; m[k]=v; m.set(k,v);
alpar@503
   200
    v=cm[k];
alpar@503
   201
    
alpar@503
   202
    m=cm;  
alpar@503
   203
    dm=cm; //Copy from another type
alpar@504
   204
    m=dm; //Copy to another type
alpar@787
   205
    {
alpar@787
   206
      //Check the typedef's
alpar@787
   207
      typename Graph::template EdgeMap<bool>::ValueType val;
alpar@787
   208
      val=true;
alpar@787
   209
      typename Graph::template EdgeMap<bool>::KeyType key;
alpar@787
   210
      key= typename Graph::EdgeIt(G);
alpar@787
   211
    }
alpar@503
   212
  }
alpar@503
   213
}
alpar@503
   214
alpar@592
   215
template<class Graph> void checkCompile(Graph &G) 
alpar@592
   216
{
alpar@592
   217
  checkCompileStaticGraph(G);
alpar@592
   218
alpar@592
   219
  typedef typename Graph::Node Node;
alpar@592
   220
  typedef typename Graph::NodeIt NodeIt;
alpar@592
   221
  typedef typename Graph::Edge Edge;
alpar@592
   222
  typedef typename Graph::EdgeIt EdgeIt;
alpar@592
   223
  typedef typename Graph::InEdgeIt InEdgeIt;
alpar@592
   224
  typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@592
   225
  
alpar@592
   226
  Node n,m;
alpar@592
   227
  n=G.addNode();
alpar@592
   228
  m=G.addNode();
alpar@774
   229
  Edge e;
alpar@774
   230
  e=G.addEdge(n,m); 
alpar@592
   231
  
alpar@774
   232
  //  G.clear();
alpar@774
   233
}
alpar@774
   234
alpar@774
   235
template<class Graph> void checkCompileErase(Graph &G) 
alpar@774
   236
{
alpar@774
   237
  typedef typename Graph::Node Node;
alpar@774
   238
  typedef typename Graph::Edge Edge;
alpar@774
   239
  Node n;
alpar@774
   240
  Edge e;
alpar@774
   241
  G.erase(n);
alpar@774
   242
  G.erase(e);
alpar@774
   243
}
alpar@774
   244
alpar@774
   245
template<class Graph> void checkCompileEraseEdge(Graph &G) 
alpar@774
   246
{
alpar@774
   247
  typedef typename Graph::Edge Edge;
alpar@774
   248
  Edge e;
alpar@774
   249
  G.erase(e);
alpar@774
   250
}
alpar@774
   251
alpar@774
   252
template<class Graph> void checkCompileFindEdge(Graph &G) 
alpar@774
   253
{
alpar@774
   254
  typedef typename Graph::NodeIt Node;
alpar@774
   255
  typedef typename Graph::NodeIt NodeIt;
alpar@774
   256
alpar@774
   257
  G.findEdge(NodeIt(G),++NodeIt(G),G.findEdge(NodeIt(G),++NodeIt(G)));
alpar@774
   258
  G.findEdge(Node(),Node(),G.findEdge(Node(),Node()));  
alpar@592
   259
}
alpar@592
   260
alpar@592
   261
alpar@503
   262
template<class Graph> void checkNodeList(Graph &G, int nn)
alpar@503
   263
{
alpar@503
   264
  typename Graph::NodeIt n(G);
alpar@503
   265
  for(int i=0;i<nn;i++) {
alpar@774
   266
    check(n!=INVALID,"Wrong Node list linking.");
alpar@774
   267
    ++n;
alpar@503
   268
  }
alpar@774
   269
  check(n==INVALID,"Wrong Node list linking.");
alpar@503
   270
}
alpar@503
   271
alpar@503
   272
template<class Graph> void checkEdgeList(Graph &G, int nn)
alpar@503
   273
{
alpar@503
   274
  typedef typename Graph::EdgeIt EdgeIt;
alpar@503
   275
alpar@503
   276
  EdgeIt e(G);
alpar@503
   277
  for(int i=0;i<nn;i++) {
alpar@774
   278
    check(e!=INVALID,"Wrong Edge list linking.");
alpar@774
   279
    ++e;
alpar@503
   280
  }
alpar@774
   281
  check(e==INVALID,"Wrong Edge list linking.");
alpar@503
   282
}
alpar@503
   283
alpar@503
   284
template<class Graph> void checkOutEdgeList(Graph &G,
alpar@503
   285
					    typename Graph::Node n,
alpar@503
   286
					    int nn)
alpar@503
   287
{
alpar@503
   288
  typename Graph::OutEdgeIt e(G,n);
alpar@503
   289
  for(int i=0;i<nn;i++) {
alpar@774
   290
    check(e!=INVALID,"Wrong OutEdge list linking.");
alpar@774
   291
    ++e;
alpar@503
   292
  }
alpar@774
   293
  check(e==INVALID,"Wrong OutEdge list linking.");
alpar@503
   294
}
alpar@503
   295
alpar@503
   296
template<class Graph> void checkInEdgeList(Graph &G,
alpar@774
   297
					   typename Graph::Node n,
alpar@774
   298
					   int nn)
alpar@503
   299
{
alpar@503
   300
  typename Graph::InEdgeIt e(G,n);
alpar@503
   301
  for(int i=0;i<nn;i++) {
alpar@774
   302
    check(e!=INVALID,"Wrong InEdge list linking.");
alpar@774
   303
    ++e;
alpar@503
   304
  }
alpar@774
   305
  check(e==INVALID,"Wrong InEdge list linking.");
alpar@503
   306
}
alpar@503
   307
alpar@774
   308
///\file
alpar@774
   309
///\todo Checks head(), tail() as well;
alpar@774
   310
alpar@503
   311
template<class Graph> void bidirPetersen(Graph &G)
alpar@503
   312
{
alpar@503
   313
  typedef typename Graph::Edge Edge;
alpar@503
   314
  typedef typename Graph::EdgeIt EdgeIt;
alpar@503
   315
  
alpar@503
   316
  checkEdgeList(G,15);
alpar@503
   317
  
alpar@503
   318
  std::vector<Edge> ee;
alpar@503
   319
  
alpar@774
   320
  for(EdgeIt e(G);e!=INVALID;++e) ee.push_back(e);
alpar@503
   321
alpar@503
   322
  for(typename std::vector<Edge>::iterator p=ee.begin();p!=ee.end();p++)
alpar@503
   323
    G.addEdge(G.head(*p),G.tail(*p));
alpar@503
   324
}
alpar@503
   325
alpar@503
   326
template<class Graph> void checkPetersen(Graph &G)
alpar@503
   327
{
alpar@503
   328
  typedef typename Graph::Node Node;
alpar@503
   329
alpar@503
   330
  typedef typename Graph::EdgeIt EdgeIt;
alpar@503
   331
  typedef typename Graph::NodeIt NodeIt;
alpar@503
   332
alpar@503
   333
  checkNodeList(G,10);
alpar@503
   334
  checkEdgeList(G,30);
alpar@503
   335
alpar@774
   336
  for(NodeIt n(G);n!=INVALID;++n) {
alpar@503
   337
    checkInEdgeList(G,n,3);
alpar@503
   338
    checkOutEdgeList(G,n,3);
alpar@774
   339
    ++n;
alpar@503
   340
  }  
alpar@503
   341
}
alpar@503
   342
alpar@774
   343
//Compile GraphSkeleton
alpar@774
   344
template 
alpar@733
   345
void checkCompileStaticGraph<StaticGraphSkeleton>(StaticGraphSkeleton &);
alpar@564
   346
template void checkCompile<GraphSkeleton>(GraphSkeleton &);
alpar@774
   347
template
alpar@774
   348
void checkCompileErase<EraseableGraphSkeleton>(EraseableGraphSkeleton &);
alpar@733
   349
alpar@774
   350
//Compile SmartGraph
alpar@503
   351
template void checkCompile<SmartGraph>(SmartGraph &);
deba@783
   352
alpar@774
   353
//Compile SymSmartGraph
alpar@503
   354
template void checkCompile<SymSmartGraph>(SymSmartGraph &);
alpar@774
   355
alpar@774
   356
//Compile ListGraph
alpar@578
   357
template void checkCompile<ListGraph>(ListGraph &);
alpar@774
   358
template void checkCompileErase<ListGraph>(ListGraph &);
alpar@774
   359
template void checkCompileFindEdge<ListGraph>(ListGraph &);
alpar@774
   360
deba@783
   361
alpar@774
   362
//Compile SymListGraph
alpar@578
   363
template void checkCompile<SymListGraph>(SymListGraph &);
alpar@774
   364
template void checkCompileErase<SymListGraph>(SymListGraph &);
alpar@774
   365
template void checkCompileFindEdge<SymListGraph>(SymListGraph &);
alpar@774
   366
alpar@774
   367
//Compile FullGraph
alpar@592
   368
template void checkCompileStaticGraph<FullGraph>(FullGraph &);
alpar@774
   369
template void checkCompileFindEdge<FullGraph>(FullGraph &);
alpar@550
   370
alpar@774
   371
//Compile EdgeSet <ListGraph>
alpar@579
   372
template void checkCompile<EdgeSet <ListGraph> >(EdgeSet <ListGraph> &);
alpar@774
   373
template
alpar@774
   374
void checkCompileEraseEdge<EdgeSet <ListGraph> >(EdgeSet <ListGraph> &);
alpar@774
   375
template
alpar@774
   376
void checkCompileFindEdge<EdgeSet <ListGraph> >(EdgeSet <ListGraph> &);
alpar@774
   377
alpar@774
   378
//Compile EdgeSet <NodeSet>
alpar@717
   379
template void checkCompile<EdgeSet <NodeSet> >(EdgeSet <NodeSet> &);
alpar@774
   380
template
alpar@774
   381
void checkCompileEraseEdge<EdgeSet <NodeSet> >(EdgeSet <NodeSet> &);
alpar@774
   382
template void checkCompileFindEdge<EdgeSet <NodeSet> >(EdgeSet <NodeSet> &);
alpar@774
   383
alpar@503
   384
alpar@503
   385
int main() 
alpar@503
   386
{
alpar@503
   387
  {
alpar@503
   388
    SmartGraph G;
alpar@503
   389
    addPetersen(G);
alpar@503
   390
    bidirPetersen(G);
alpar@503
   391
    checkPetersen(G);
alpar@503
   392
  }
alpar@578
   393
  {
alpar@578
   394
    ListGraph G;
alpar@578
   395
    addPetersen(G);
alpar@578
   396
    bidirPetersen(G);
alpar@578
   397
    checkPetersen(G);
alpar@578
   398
  }
alpar@503
   399
  {
alpar@503
   400
    SymSmartGraph G;
alpar@503
   401
    addPetersen(G);
alpar@503
   402
    checkPetersen(G);
alpar@503
   403
  }
alpar@578
   404
  {
alpar@578
   405
    SymListGraph G;
alpar@578
   406
    addPetersen(G);
alpar@578
   407
    checkPetersen(G);
alpar@578
   408
  }
alpar@503
   409
alpar@774
   410
  ///\file
alpar@774
   411
  ///\todo map tests.
alpar@774
   412
  ///\todo copy constr tests.
alpar@503
   413
alpar@503
   414
  std::cout << __FILE__ ": All tests passed.\n";
alpar@503
   415
alpar@579
   416
  return 0;
alpar@503
   417
}