test/graph_test.cc
author Balazs Dezso <deba@inf.elte.hu>
Thu, 10 Dec 2009 09:14:47 +0100
branch1.0
changeset 408 cf0c1b85618c
parent 228 b6732e0d38c5
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@338
    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@338
    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@338
    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@338
    51
deba@228
    52
  checkGraphNodeList(G, 3);
kpeter@338
    53
  checkGraphEdgeList(G, 1);
deba@228
    54
  checkGraphArcList(G, 2);
deba@228
    55
kpeter@338
    56
  checkGraphIncEdgeArcLists(G, n1, 1);
kpeter@338
    57
  checkGraphIncEdgeArcLists(G, n2, 1);
kpeter@338
    58
  checkGraphIncEdgeArcLists(G, n3, 0);
deba@228
    59
kpeter@338
    60
  checkGraphConEdgeList(G, 1);
kpeter@338
    61
  checkGraphConArcList(G, 2);
deba@228
    62
kpeter@338
    63
  Edge e2 = G.addEdge(n2, n1),
kpeter@338
    64
       e3 = G.addEdge(n2, n3);
deba@228
    65
kpeter@338
    66
  checkGraphNodeList(G, 3);
kpeter@338
    67
  checkGraphEdgeList(G, 3);
kpeter@338
    68
  checkGraphArcList(G, 6);
deba@228
    69
kpeter@338
    70
  checkGraphIncEdgeArcLists(G, n1, 2);
kpeter@338
    71
  checkGraphIncEdgeArcLists(G, n2, 3);
kpeter@338
    72
  checkGraphIncEdgeArcLists(G, n3, 1);
deba@228
    73
kpeter@338
    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@338
    87
template <class Graph>
kpeter@338
    88
void checkGraphAlter() {
kpeter@338
    89
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
kpeter@338
    90
kpeter@338
    91
  Graph G;
kpeter@338
    92
  Node n1 = G.addNode(), n2 = G.addNode(),
kpeter@338
    93
       n3 = G.addNode(), n4 = G.addNode();
kpeter@338
    94
  Edge e1 = G.addEdge(n1, n2), e2 = G.addEdge(n2, n1),
kpeter@338
    95
       e3 = G.addEdge(n2, n3), e4 = G.addEdge(n1, n4),
kpeter@338
    96
       e5 = G.addEdge(n4, n3);
kpeter@338
    97
kpeter@338
    98
  checkGraphNodeList(G, 4);
kpeter@338
    99
  checkGraphEdgeList(G, 5);
kpeter@338
   100
  checkGraphArcList(G, 10);
kpeter@338
   101
kpeter@338
   102
  // Check changeU() and changeV()
kpeter@338
   103
  if (G.u(e2) == n2) {
kpeter@338
   104
    G.changeU(e2, n3);
kpeter@338
   105
  } else {
kpeter@338
   106
    G.changeV(e2, n3);
kpeter@338
   107
  }
kpeter@338
   108
kpeter@338
   109
  checkGraphNodeList(G, 4);
kpeter@338
   110
  checkGraphEdgeList(G, 5);
kpeter@338
   111
  checkGraphArcList(G, 10);
kpeter@338
   112
kpeter@338
   113
  checkGraphIncEdgeArcLists(G, n1, 3);
kpeter@338
   114
  checkGraphIncEdgeArcLists(G, n2, 2);
kpeter@338
   115
  checkGraphIncEdgeArcLists(G, n3, 3);
kpeter@338
   116
  checkGraphIncEdgeArcLists(G, n4, 2);
kpeter@338
   117
kpeter@338
   118
  checkGraphConEdgeList(G, 5);
kpeter@338
   119
  checkGraphConArcList(G, 10);
kpeter@338
   120
kpeter@338
   121
  if (G.u(e2) == n1) {
kpeter@338
   122
    G.changeU(e2, n2);
kpeter@338
   123
  } else {
kpeter@338
   124
    G.changeV(e2, n2);
kpeter@338
   125
  }
kpeter@338
   126
kpeter@338
   127
  checkGraphNodeList(G, 4);
kpeter@338
   128
  checkGraphEdgeList(G, 5);
kpeter@338
   129
  checkGraphArcList(G, 10);
kpeter@338
   130
kpeter@338
   131
  checkGraphIncEdgeArcLists(G, n1, 2);
kpeter@338
   132
  checkGraphIncEdgeArcLists(G, n2, 3);
kpeter@338
   133
  checkGraphIncEdgeArcLists(G, n3, 3);
kpeter@338
   134
  checkGraphIncEdgeArcLists(G, n4, 2);
kpeter@338
   135
kpeter@338
   136
  checkGraphConEdgeList(G, 5);
kpeter@338
   137
  checkGraphConArcList(G, 10);
kpeter@338
   138
kpeter@338
   139
  // Check contract()
kpeter@338
   140
  G.contract(n1, n4, false);
kpeter@338
   141
kpeter@338
   142
  checkGraphNodeList(G, 3);
kpeter@338
   143
  checkGraphEdgeList(G, 5);
kpeter@338
   144
  checkGraphArcList(G, 10);
kpeter@338
   145
kpeter@338
   146
  checkGraphIncEdgeArcLists(G, n1, 4);
kpeter@338
   147
  checkGraphIncEdgeArcLists(G, n2, 3);
kpeter@338
   148
  checkGraphIncEdgeArcLists(G, n3, 3);
kpeter@338
   149
kpeter@338
   150
  checkGraphConEdgeList(G, 5);
kpeter@338
   151
  checkGraphConArcList(G, 10);
kpeter@338
   152
kpeter@338
   153
  G.contract(n2, n3);
kpeter@338
   154
kpeter@338
   155
  checkGraphNodeList(G, 2);
kpeter@338
   156
  checkGraphEdgeList(G, 3);
kpeter@338
   157
  checkGraphArcList(G, 6);
kpeter@338
   158
kpeter@338
   159
  checkGraphIncEdgeArcLists(G, n1, 4);
kpeter@338
   160
  checkGraphIncEdgeArcLists(G, n2, 2);
kpeter@338
   161
kpeter@338
   162
  checkGraphConEdgeList(G, 3);
kpeter@338
   163
  checkGraphConArcList(G, 6);
kpeter@338
   164
}
kpeter@338
   165
kpeter@338
   166
template <class Graph>
kpeter@338
   167
void checkGraphErase() {
kpeter@338
   168
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
kpeter@338
   169
kpeter@338
   170
  Graph G;
kpeter@338
   171
  Node n1 = G.addNode(), n2 = G.addNode(),
kpeter@338
   172
       n3 = G.addNode(), n4 = G.addNode();
kpeter@338
   173
  Edge e1 = G.addEdge(n1, n2), e2 = G.addEdge(n2, n1),
kpeter@338
   174
       e3 = G.addEdge(n2, n3), e4 = G.addEdge(n1, n4),
kpeter@338
   175
       e5 = G.addEdge(n4, n3);
kpeter@338
   176
kpeter@338
   177
  // Check edge deletion
kpeter@338
   178
  G.erase(e2);
kpeter@338
   179
kpeter@338
   180
  checkGraphNodeList(G, 4);
kpeter@338
   181
  checkGraphEdgeList(G, 4);
kpeter@338
   182
  checkGraphArcList(G, 8);
kpeter@338
   183
kpeter@338
   184
  checkGraphIncEdgeArcLists(G, n1, 2);
kpeter@338
   185
  checkGraphIncEdgeArcLists(G, n2, 2);
kpeter@338
   186
  checkGraphIncEdgeArcLists(G, n3, 2);
kpeter@338
   187
  checkGraphIncEdgeArcLists(G, n4, 2);
kpeter@338
   188
kpeter@338
   189
  checkGraphConEdgeList(G, 4);
kpeter@338
   190
  checkGraphConArcList(G, 8);
kpeter@338
   191
kpeter@338
   192
  // Check node deletion
kpeter@338
   193
  G.erase(n3);
kpeter@338
   194
kpeter@338
   195
  checkGraphNodeList(G, 3);
kpeter@338
   196
  checkGraphEdgeList(G, 2);
kpeter@338
   197
  checkGraphArcList(G, 4);
kpeter@338
   198
kpeter@338
   199
  checkGraphIncEdgeArcLists(G, n1, 2);
kpeter@338
   200
  checkGraphIncEdgeArcLists(G, n2, 1);
kpeter@338
   201
  checkGraphIncEdgeArcLists(G, n4, 1);
kpeter@338
   202
kpeter@338
   203
  checkGraphConEdgeList(G, 2);
kpeter@338
   204
  checkGraphConArcList(G, 4);
kpeter@338
   205
}
kpeter@338
   206
kpeter@338
   207
kpeter@338
   208
template <class Graph>
kpeter@338
   209
void checkGraphSnapshot() {
kpeter@338
   210
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
kpeter@338
   211
kpeter@338
   212
  Graph G;
kpeter@338
   213
  Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode();
kpeter@338
   214
  Edge e1 = G.addEdge(n1, n2), e2 = G.addEdge(n2, n1),
kpeter@338
   215
       e3 = G.addEdge(n2, n3);
kpeter@338
   216
kpeter@338
   217
  checkGraphNodeList(G, 3);
kpeter@338
   218
  checkGraphEdgeList(G, 3);
kpeter@338
   219
  checkGraphArcList(G, 6);
kpeter@338
   220
kpeter@338
   221
  typename Graph::Snapshot snapshot(G);
kpeter@338
   222
kpeter@338
   223
  Node n = G.addNode();
kpeter@338
   224
  G.addEdge(n3, n);
kpeter@338
   225
  G.addEdge(n, n3);
kpeter@338
   226
  G.addEdge(n3, n2);
kpeter@338
   227
kpeter@338
   228
  checkGraphNodeList(G, 4);
kpeter@338
   229
  checkGraphEdgeList(G, 6);
kpeter@338
   230
  checkGraphArcList(G, 12);
kpeter@338
   231
kpeter@338
   232
  snapshot.restore();
kpeter@338
   233
kpeter@338
   234
  checkGraphNodeList(G, 3);
kpeter@338
   235
  checkGraphEdgeList(G, 3);
kpeter@338
   236
  checkGraphArcList(G, 6);
kpeter@338
   237
kpeter@338
   238
  checkGraphIncEdgeArcLists(G, n1, 2);
kpeter@338
   239
  checkGraphIncEdgeArcLists(G, n2, 3);
kpeter@338
   240
  checkGraphIncEdgeArcLists(G, n3, 1);
kpeter@338
   241
kpeter@338
   242
  checkGraphConEdgeList(G, 3);
kpeter@338
   243
  checkGraphConArcList(G, 6);
kpeter@338
   244
kpeter@338
   245
  checkNodeIds(G);
kpeter@338
   246
  checkEdgeIds(G);
kpeter@338
   247
  checkArcIds(G);
kpeter@338
   248
  checkGraphNodeMap(G);
kpeter@338
   249
  checkGraphEdgeMap(G);
kpeter@338
   250
  checkGraphArcMap(G);
kpeter@338
   251
kpeter@338
   252
  G.addNode();
kpeter@338
   253
  snapshot.save(G);
kpeter@338
   254
kpeter@338
   255
  G.addEdge(G.addNode(), G.addNode());
kpeter@338
   256
kpeter@338
   257
  snapshot.restore();
kpeter@338
   258
kpeter@338
   259
  checkGraphNodeList(G, 4);
kpeter@338
   260
  checkGraphEdgeList(G, 3);
kpeter@338
   261
  checkGraphArcList(G, 6);
kpeter@338
   262
}
kpeter@338
   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@338
   403
    checkGraphBuild<ListGraph>();
kpeter@338
   404
    checkGraphAlter<ListGraph>();
kpeter@338
   405
    checkGraphErase<ListGraph>();
kpeter@338
   406
    checkGraphSnapshot<ListGraph>();
deba@228
   407
    checkGraphValidityErase<ListGraph>();
kpeter@171
   408
  }
kpeter@171
   409
  { // Checking SmartGraph
kpeter@338
   410
    checkGraphBuild<SmartGraph>();
kpeter@338
   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
}