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