test/graph_test.cc
author Balazs Dezso <deba@inf.elte.hu>
Thu, 10 Dec 2009 09:14:47 +0100
branch1.0
changeset 893 cf0c1b85618c
parent 228 b6732e0d38c5
child 388 2d87dbd7f8c8
permissions -rw-r--r--
Fix clear() function in ExtendFindEnum (#335), backport of [28c7ad6f8d91]
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@57
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@57
     4
 *
alpar@107
     5
 * Copyright (C) 2003-2008
deba@57
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@57
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@57
     8
 *
deba@57
     9
 * Permission to use, modify and distribute this software is granted
deba@57
    10
 * provided that this copyright notice appears in all copies. For
deba@57
    11
 * precise terms see the accompanying LICENSE file.
deba@57
    12
 *
deba@57
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@57
    14
 * express or implied, and with no claim as to its suitability for any
deba@57
    15
 * purpose.
deba@57
    16
 *
deba@57
    17
 */
deba@57
    18
deba@57
    19
#include <lemon/concepts/graph.h>
deba@57
    20
#include <lemon/list_graph.h>
deba@109
    21
#include <lemon/smart_graph.h>
deba@57
    22
// #include <lemon/full_graph.h>
deba@57
    23
// #include <lemon/grid_graph.h>
deba@57
    24
deba@57
    25
#include "test_tools.h"
kpeter@171
    26
#include "graph_test.h"
deba@57
    27
deba@57
    28
using namespace lemon;
deba@57
    29
using namespace lemon::concepts;
deba@57
    30
deba@228
    31
template <class Graph>
kpeter@387
    32
void checkGraphBuild() {
deba@228
    33
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
deba@228
    34
deba@228
    35
  Graph G;
deba@228
    36
  checkGraphNodeList(G, 0);
deba@228
    37
  checkGraphEdgeList(G, 0);
kpeter@387
    38
  checkGraphArcList(G, 0);
deba@228
    39
deba@228
    40
  Node
deba@228
    41
    n1 = G.addNode(),
deba@228
    42
    n2 = G.addNode(),
deba@228
    43
    n3 = G.addNode();
deba@228
    44
  checkGraphNodeList(G, 3);
deba@228
    45
  checkGraphEdgeList(G, 0);
kpeter@387
    46
  checkGraphArcList(G, 0);
deba@228
    47
deba@228
    48
  Edge e1 = G.addEdge(n1, n2);
deba@228
    49
  check((G.u(e1) == n1 && G.v(e1) == n2) || (G.u(e1) == n2 && G.v(e1) == n1),
deba@228
    50
        "Wrong edge");
kpeter@387
    51
deba@228
    52
  checkGraphNodeList(G, 3);
kpeter@387
    53
  checkGraphEdgeList(G, 1);
deba@228
    54
  checkGraphArcList(G, 2);
deba@228
    55
kpeter@387
    56
  checkGraphIncEdgeArcLists(G, n1, 1);
kpeter@387
    57
  checkGraphIncEdgeArcLists(G, n2, 1);
kpeter@387
    58
  checkGraphIncEdgeArcLists(G, n3, 0);
deba@228
    59
kpeter@387
    60
  checkGraphConEdgeList(G, 1);
kpeter@387
    61
  checkGraphConArcList(G, 2);
deba@228
    62
kpeter@387
    63
  Edge e2 = G.addEdge(n2, n1),
kpeter@387
    64
       e3 = G.addEdge(n2, n3);
deba@228
    65
kpeter@387
    66
  checkGraphNodeList(G, 3);
kpeter@387
    67
  checkGraphEdgeList(G, 3);
kpeter@387
    68
  checkGraphArcList(G, 6);
deba@228
    69
kpeter@387
    70
  checkGraphIncEdgeArcLists(G, n1, 2);
kpeter@387
    71
  checkGraphIncEdgeArcLists(G, n2, 3);
kpeter@387
    72
  checkGraphIncEdgeArcLists(G, n3, 1);
deba@228
    73
kpeter@387
    74
  checkGraphConEdgeList(G, 3);
deba@228
    75
  checkGraphConArcList(G, 6);
deba@228
    76
deba@228
    77
  checkArcDirections(G);
deba@228
    78
deba@228
    79
  checkNodeIds(G);
deba@228
    80
  checkArcIds(G);
deba@228
    81
  checkEdgeIds(G);
deba@228
    82
  checkGraphNodeMap(G);
deba@228
    83
  checkGraphArcMap(G);
deba@228
    84
  checkGraphEdgeMap(G);
deba@228
    85
}
deba@228
    86
kpeter@387
    87
template <class Graph>
kpeter@387
    88
void checkGraphAlter() {
kpeter@387
    89
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
kpeter@387
    90
kpeter@387
    91
  Graph G;
kpeter@387
    92
  Node n1 = G.addNode(), n2 = G.addNode(),
kpeter@387
    93
       n3 = G.addNode(), n4 = G.addNode();
kpeter@387
    94
  Edge e1 = G.addEdge(n1, n2), e2 = G.addEdge(n2, n1),
kpeter@387
    95
       e3 = G.addEdge(n2, n3), e4 = G.addEdge(n1, n4),
kpeter@387
    96
       e5 = G.addEdge(n4, n3);
kpeter@387
    97
kpeter@387
    98
  checkGraphNodeList(G, 4);
kpeter@387
    99
  checkGraphEdgeList(G, 5);
kpeter@387
   100
  checkGraphArcList(G, 10);
kpeter@387
   101
kpeter@387
   102
  // Check changeU() and changeV()
kpeter@387
   103
  if (G.u(e2) == n2) {
kpeter@387
   104
    G.changeU(e2, n3);
kpeter@387
   105
  } else {
kpeter@387
   106
    G.changeV(e2, n3);
kpeter@387
   107
  }
kpeter@387
   108
kpeter@387
   109
  checkGraphNodeList(G, 4);
kpeter@387
   110
  checkGraphEdgeList(G, 5);
kpeter@387
   111
  checkGraphArcList(G, 10);
kpeter@387
   112
kpeter@387
   113
  checkGraphIncEdgeArcLists(G, n1, 3);
kpeter@387
   114
  checkGraphIncEdgeArcLists(G, n2, 2);
kpeter@387
   115
  checkGraphIncEdgeArcLists(G, n3, 3);
kpeter@387
   116
  checkGraphIncEdgeArcLists(G, n4, 2);
kpeter@387
   117
kpeter@387
   118
  checkGraphConEdgeList(G, 5);
kpeter@387
   119
  checkGraphConArcList(G, 10);
kpeter@387
   120
kpeter@387
   121
  if (G.u(e2) == n1) {
kpeter@387
   122
    G.changeU(e2, n2);
kpeter@387
   123
  } else {
kpeter@387
   124
    G.changeV(e2, n2);
kpeter@387
   125
  }
kpeter@387
   126
kpeter@387
   127
  checkGraphNodeList(G, 4);
kpeter@387
   128
  checkGraphEdgeList(G, 5);
kpeter@387
   129
  checkGraphArcList(G, 10);
kpeter@387
   130
kpeter@387
   131
  checkGraphIncEdgeArcLists(G, n1, 2);
kpeter@387
   132
  checkGraphIncEdgeArcLists(G, n2, 3);
kpeter@387
   133
  checkGraphIncEdgeArcLists(G, n3, 3);
kpeter@387
   134
  checkGraphIncEdgeArcLists(G, n4, 2);
kpeter@387
   135
kpeter@387
   136
  checkGraphConEdgeList(G, 5);
kpeter@387
   137
  checkGraphConArcList(G, 10);
kpeter@387
   138
kpeter@387
   139
  // Check contract()
kpeter@387
   140
  G.contract(n1, n4, false);
kpeter@387
   141
kpeter@387
   142
  checkGraphNodeList(G, 3);
kpeter@387
   143
  checkGraphEdgeList(G, 5);
kpeter@387
   144
  checkGraphArcList(G, 10);
kpeter@387
   145
kpeter@387
   146
  checkGraphIncEdgeArcLists(G, n1, 4);
kpeter@387
   147
  checkGraphIncEdgeArcLists(G, n2, 3);
kpeter@387
   148
  checkGraphIncEdgeArcLists(G, n3, 3);
kpeter@387
   149
kpeter@387
   150
  checkGraphConEdgeList(G, 5);
kpeter@387
   151
  checkGraphConArcList(G, 10);
kpeter@387
   152
kpeter@387
   153
  G.contract(n2, n3);
kpeter@387
   154
kpeter@387
   155
  checkGraphNodeList(G, 2);
kpeter@387
   156
  checkGraphEdgeList(G, 3);
kpeter@387
   157
  checkGraphArcList(G, 6);
kpeter@387
   158
kpeter@387
   159
  checkGraphIncEdgeArcLists(G, n1, 4);
kpeter@387
   160
  checkGraphIncEdgeArcLists(G, n2, 2);
kpeter@387
   161
kpeter@387
   162
  checkGraphConEdgeList(G, 3);
kpeter@387
   163
  checkGraphConArcList(G, 6);
kpeter@387
   164
}
kpeter@387
   165
kpeter@387
   166
template <class Graph>
kpeter@387
   167
void checkGraphErase() {
kpeter@387
   168
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
kpeter@387
   169
kpeter@387
   170
  Graph G;
kpeter@387
   171
  Node n1 = G.addNode(), n2 = G.addNode(),
kpeter@387
   172
       n3 = G.addNode(), n4 = G.addNode();
kpeter@387
   173
  Edge e1 = G.addEdge(n1, n2), e2 = G.addEdge(n2, n1),
kpeter@387
   174
       e3 = G.addEdge(n2, n3), e4 = G.addEdge(n1, n4),
kpeter@387
   175
       e5 = G.addEdge(n4, n3);
kpeter@387
   176
kpeter@387
   177
  // Check edge deletion
kpeter@387
   178
  G.erase(e2);
kpeter@387
   179
kpeter@387
   180
  checkGraphNodeList(G, 4);
kpeter@387
   181
  checkGraphEdgeList(G, 4);
kpeter@387
   182
  checkGraphArcList(G, 8);
kpeter@387
   183
kpeter@387
   184
  checkGraphIncEdgeArcLists(G, n1, 2);
kpeter@387
   185
  checkGraphIncEdgeArcLists(G, n2, 2);
kpeter@387
   186
  checkGraphIncEdgeArcLists(G, n3, 2);
kpeter@387
   187
  checkGraphIncEdgeArcLists(G, n4, 2);
kpeter@387
   188
kpeter@387
   189
  checkGraphConEdgeList(G, 4);
kpeter@387
   190
  checkGraphConArcList(G, 8);
kpeter@387
   191
kpeter@387
   192
  // Check node deletion
kpeter@387
   193
  G.erase(n3);
kpeter@387
   194
kpeter@387
   195
  checkGraphNodeList(G, 3);
kpeter@387
   196
  checkGraphEdgeList(G, 2);
kpeter@387
   197
  checkGraphArcList(G, 4);
kpeter@387
   198
kpeter@387
   199
  checkGraphIncEdgeArcLists(G, n1, 2);
kpeter@387
   200
  checkGraphIncEdgeArcLists(G, n2, 1);
kpeter@387
   201
  checkGraphIncEdgeArcLists(G, n4, 1);
kpeter@387
   202
kpeter@387
   203
  checkGraphConEdgeList(G, 2);
kpeter@387
   204
  checkGraphConArcList(G, 4);
kpeter@387
   205
}
kpeter@387
   206
kpeter@387
   207
kpeter@387
   208
template <class Graph>
kpeter@387
   209
void checkGraphSnapshot() {
kpeter@387
   210
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
kpeter@387
   211
kpeter@387
   212
  Graph G;
kpeter@387
   213
  Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode();
kpeter@387
   214
  Edge e1 = G.addEdge(n1, n2), e2 = G.addEdge(n2, n1),
kpeter@387
   215
       e3 = G.addEdge(n2, n3);
kpeter@387
   216
kpeter@387
   217
  checkGraphNodeList(G, 3);
kpeter@387
   218
  checkGraphEdgeList(G, 3);
kpeter@387
   219
  checkGraphArcList(G, 6);
kpeter@387
   220
kpeter@387
   221
  typename Graph::Snapshot snapshot(G);
kpeter@387
   222
kpeter@387
   223
  Node n = G.addNode();
kpeter@387
   224
  G.addEdge(n3, n);
kpeter@387
   225
  G.addEdge(n, n3);
kpeter@387
   226
  G.addEdge(n3, n2);
kpeter@387
   227
kpeter@387
   228
  checkGraphNodeList(G, 4);
kpeter@387
   229
  checkGraphEdgeList(G, 6);
kpeter@387
   230
  checkGraphArcList(G, 12);
kpeter@387
   231
kpeter@387
   232
  snapshot.restore();
kpeter@387
   233
kpeter@387
   234
  checkGraphNodeList(G, 3);
kpeter@387
   235
  checkGraphEdgeList(G, 3);
kpeter@387
   236
  checkGraphArcList(G, 6);
kpeter@387
   237
kpeter@387
   238
  checkGraphIncEdgeArcLists(G, n1, 2);
kpeter@387
   239
  checkGraphIncEdgeArcLists(G, n2, 3);
kpeter@387
   240
  checkGraphIncEdgeArcLists(G, n3, 1);
kpeter@387
   241
kpeter@387
   242
  checkGraphConEdgeList(G, 3);
kpeter@387
   243
  checkGraphConArcList(G, 6);
kpeter@387
   244
kpeter@387
   245
  checkNodeIds(G);
kpeter@387
   246
  checkEdgeIds(G);
kpeter@387
   247
  checkArcIds(G);
kpeter@387
   248
  checkGraphNodeMap(G);
kpeter@387
   249
  checkGraphEdgeMap(G);
kpeter@387
   250
  checkGraphArcMap(G);
kpeter@387
   251
kpeter@387
   252
  G.addNode();
kpeter@387
   253
  snapshot.save(G);
kpeter@387
   254
kpeter@387
   255
  G.addEdge(G.addNode(), G.addNode());
kpeter@387
   256
kpeter@387
   257
  snapshot.restore();
kpeter@387
   258
kpeter@387
   259
  checkGraphNodeList(G, 4);
kpeter@387
   260
  checkGraphEdgeList(G, 3);
kpeter@387
   261
  checkGraphArcList(G, 6);
kpeter@387
   262
}
kpeter@387
   263
deba@228
   264
void checkConcepts() {
kpeter@171
   265
  { // Checking graph components
deba@57
   266
    checkConcept<BaseGraphComponent, BaseGraphComponent >();
deba@57
   267
alpar@209
   268
    checkConcept<IDableGraphComponent<>,
deba@57
   269
      IDableGraphComponent<> >();
deba@57
   270
alpar@209
   271
    checkConcept<IterableGraphComponent<>,
deba@57
   272
      IterableGraphComponent<> >();
deba@57
   273
alpar@209
   274
    checkConcept<MappableGraphComponent<>,
deba@57
   275
      MappableGraphComponent<> >();
deba@57
   276
  }
kpeter@171
   277
  { // Checking skeleton graph
kpeter@171
   278
    checkConcept<Graph, Graph>();
deba@57
   279
  }
kpeter@171
   280
  { // Checking ListGraph
kpeter@171
   281
    checkConcept<Graph, ListGraph>();
kpeter@171
   282
    checkConcept<AlterableGraphComponent<>, ListGraph>();
kpeter@171
   283
    checkConcept<ExtendableGraphComponent<>, ListGraph>();
kpeter@171
   284
    checkConcept<ClearableGraphComponent<>, ListGraph>();
kpeter@171
   285
    checkConcept<ErasableGraphComponent<>, ListGraph>();
kpeter@171
   286
  }
kpeter@171
   287
  { // Checking SmartGraph
kpeter@171
   288
    checkConcept<Graph, SmartGraph>();
kpeter@171
   289
    checkConcept<AlterableGraphComponent<>, SmartGraph>();
kpeter@171
   290
    checkConcept<ExtendableGraphComponent<>, SmartGraph>();
kpeter@171
   291
    checkConcept<ClearableGraphComponent<>, SmartGraph>();
kpeter@171
   292
  }
kpeter@171
   293
//  { // Checking FullGraph
kpeter@171
   294
//    checkConcept<Graph, FullGraph>();
kpeter@171
   295
//    checkGraphIterators<FullGraph>();
kpeter@171
   296
//  }
kpeter@171
   297
//  { // Checking GridGraph
kpeter@171
   298
//    checkConcept<Graph, GridGraph>();
kpeter@171
   299
//    checkGraphIterators<GridGraph>();
kpeter@171
   300
//  }
deba@57
   301
}
deba@57
   302
deba@57
   303
template <typename Graph>
deba@228
   304
void checkGraphValidity() {
deba@149
   305
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
deba@57
   306
  Graph g;
deba@57
   307
deba@57
   308
  Node
deba@57
   309
    n1 = g.addNode(),
deba@57
   310
    n2 = g.addNode(),
deba@57
   311
    n3 = g.addNode();
deba@57
   312
deba@57
   313
  Edge
deba@57
   314
    e1 = g.addEdge(n1, n2),
deba@57
   315
    e2 = g.addEdge(n2, n3);
deba@57
   316
kpeter@171
   317
  check(g.valid(n1), "Wrong validity check");
kpeter@171
   318
  check(g.valid(e1), "Wrong validity check");
kpeter@171
   319
  check(g.valid(g.direct(e1, true)), "Wrong validity check");
kpeter@171
   320
kpeter@171
   321
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
kpeter@171
   322
  check(!g.valid(g.edgeFromId(-1)), "Wrong validity check");
kpeter@171
   323
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
deba@57
   324
}
deba@57
   325
deba@149
   326
template <typename Graph>
deba@228
   327
void checkGraphValidityErase() {
deba@149
   328
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
deba@149
   329
  Graph g;
deba@149
   330
deba@149
   331
  Node
deba@149
   332
    n1 = g.addNode(),
deba@149
   333
    n2 = g.addNode(),
deba@149
   334
    n3 = g.addNode();
deba@149
   335
deba@149
   336
  Edge
deba@149
   337
    e1 = g.addEdge(n1, n2),
deba@149
   338
    e2 = g.addEdge(n2, n3);
deba@149
   339
kpeter@171
   340
  check(g.valid(n1), "Wrong validity check");
kpeter@171
   341
  check(g.valid(e1), "Wrong validity check");
kpeter@171
   342
  check(g.valid(g.direct(e1, true)), "Wrong validity check");
deba@149
   343
deba@149
   344
  g.erase(n1);
deba@149
   345
kpeter@171
   346
  check(!g.valid(n1), "Wrong validity check");
kpeter@171
   347
  check(g.valid(n2), "Wrong validity check");
kpeter@171
   348
  check(g.valid(n3), "Wrong validity check");
kpeter@171
   349
  check(!g.valid(e1), "Wrong validity check");
kpeter@171
   350
  check(g.valid(e2), "Wrong validity check");
deba@149
   351
kpeter@171
   352
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
kpeter@171
   353
  check(!g.valid(g.edgeFromId(-1)), "Wrong validity check");
kpeter@171
   354
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
deba@149
   355
}
deba@149
   356
deba@57
   357
// void checkGridGraph(const GridGraph& g, int w, int h) {
deba@57
   358
//   check(g.width() == w, "Wrong width");
deba@57
   359
//   check(g.height() == h, "Wrong height");
deba@57
   360
deba@57
   361
//   for (int i = 0; i < w; ++i) {
deba@57
   362
//     for (int j = 0; j < h; ++j) {
deba@57
   363
//       check(g.col(g(i, j)) == i, "Wrong col");
deba@57
   364
//       check(g.row(g(i, j)) == j, "Wrong row");
deba@57
   365
//     }
deba@57
   366
//   }
alpar@209
   367
deba@57
   368
//   for (int i = 0; i < w; ++i) {
deba@57
   369
//     for (int j = 0; j < h - 1; ++j) {
deba@57
   370
//       check(g.source(g.down(g(i, j))) == g(i, j), "Wrong down");
deba@57
   371
//       check(g.target(g.down(g(i, j))) == g(i, j + 1), "Wrong down");
deba@57
   372
//     }
deba@57
   373
//     check(g.down(g(i, h - 1)) == INVALID, "Wrong down");
deba@57
   374
//   }
deba@57
   375
deba@57
   376
//   for (int i = 0; i < w; ++i) {
deba@57
   377
//     for (int j = 1; j < h; ++j) {
deba@57
   378
//       check(g.source(g.up(g(i, j))) == g(i, j), "Wrong up");
deba@57
   379
//       check(g.target(g.up(g(i, j))) == g(i, j - 1), "Wrong up");
deba@57
   380
//     }
deba@57
   381
//     check(g.up(g(i, 0)) == INVALID, "Wrong up");
deba@57
   382
//   }
deba@57
   383
deba@57
   384
//   for (int j = 0; j < h; ++j) {
deba@57
   385
//     for (int i = 0; i < w - 1; ++i) {
deba@57
   386
//       check(g.source(g.right(g(i, j))) == g(i, j), "Wrong right");
alpar@209
   387
//       check(g.target(g.right(g(i, j))) == g(i + 1, j), "Wrong right");
deba@57
   388
//     }
alpar@209
   389
//     check(g.right(g(w - 1, j)) == INVALID, "Wrong right");
deba@57
   390
//   }
deba@57
   391
deba@57
   392
//   for (int j = 0; j < h; ++j) {
deba@57
   393
//     for (int i = 1; i < w; ++i) {
deba@57
   394
//       check(g.source(g.left(g(i, j))) == g(i, j), "Wrong left");
alpar@209
   395
//       check(g.target(g.left(g(i, j))) == g(i - 1, j), "Wrong left");
deba@57
   396
//     }
alpar@209
   397
//     check(g.left(g(0, j)) == INVALID, "Wrong left");
deba@57
   398
//   }
deba@57
   399
// }
deba@57
   400
deba@228
   401
void checkGraphs() {
kpeter@171
   402
  { // Checking ListGraph
kpeter@387
   403
    checkGraphBuild<ListGraph>();
kpeter@387
   404
    checkGraphAlter<ListGraph>();
kpeter@387
   405
    checkGraphErase<ListGraph>();
kpeter@387
   406
    checkGraphSnapshot<ListGraph>();
deba@228
   407
    checkGraphValidityErase<ListGraph>();
kpeter@171
   408
  }
kpeter@171
   409
  { // Checking SmartGraph
kpeter@387
   410
    checkGraphBuild<SmartGraph>();
kpeter@387
   411
    checkGraphSnapshot<SmartGraph>();
deba@228
   412
    checkGraphValidity<SmartGraph>();
kpeter@171
   413
  }
kpeter@171
   414
//   { // Checking FullGraph
kpeter@171
   415
//     FullGraph g(5);
kpeter@171
   416
//     checkGraphNodeList(g, 5);
kpeter@171
   417
//     checkGraphEdgeList(g, 10);
kpeter@171
   418
//   }
kpeter@171
   419
//   { // Checking GridGraph
kpeter@171
   420
//     GridGraph g(5, 6);
kpeter@171
   421
//     checkGraphNodeList(g, 30);
kpeter@171
   422
//     checkGraphEdgeList(g, 49);
kpeter@171
   423
//     checkGridGraph(g, 5, 6);
kpeter@171
   424
//   }
kpeter@171
   425
}
kpeter@171
   426
deba@57
   427
int main() {
deba@228
   428
  checkConcepts();
deba@228
   429
  checkGraphs();
deba@57
   430
  return 0;
deba@57
   431
}