test/graph_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 17 Nov 2008 15:41:15 +0000
changeset 396 b04e431907bc
parent 348 052cecabcb71
parent 365 37557a46e298
child 377 a12eef1f82b2
permissions -rw-r--r--
Avoid STL panic at Elevator when compiled with -D_GLIBCXX_DEBUG
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@365
    22
#include <lemon/full_graph.h>
kpeter@346
    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>
deba@228
    32
void checkGraph() {
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);
deba@228
    38
deba@228
    39
  Node
deba@228
    40
    n1 = G.addNode(),
deba@228
    41
    n2 = G.addNode(),
deba@228
    42
    n3 = G.addNode();
deba@228
    43
  checkGraphNodeList(G, 3);
deba@228
    44
  checkGraphEdgeList(G, 0);
deba@228
    45
deba@228
    46
  Edge e1 = G.addEdge(n1, n2);
deba@228
    47
  check((G.u(e1) == n1 && G.v(e1) == n2) || (G.u(e1) == n2 && G.v(e1) == n1),
deba@228
    48
        "Wrong edge");
deba@228
    49
  checkGraphNodeList(G, 3);
deba@228
    50
  checkGraphArcList(G, 2);
deba@228
    51
  checkGraphEdgeList(G, 1);
deba@228
    52
deba@228
    53
  checkGraphOutArcList(G, n1, 1);
deba@228
    54
  checkGraphOutArcList(G, n2, 1);
deba@228
    55
  checkGraphOutArcList(G, n3, 0);
deba@228
    56
deba@228
    57
  checkGraphInArcList(G, n1, 1);
deba@228
    58
  checkGraphInArcList(G, n2, 1);
deba@228
    59
  checkGraphInArcList(G, n3, 0);
deba@228
    60
deba@228
    61
  checkGraphIncEdgeList(G, n1, 1);
deba@228
    62
  checkGraphIncEdgeList(G, n2, 1);
deba@228
    63
  checkGraphIncEdgeList(G, n3, 0);
deba@228
    64
deba@228
    65
  checkGraphConArcList(G, 2);
deba@228
    66
  checkGraphConEdgeList(G, 1);
deba@228
    67
deba@228
    68
  Edge e2 = G.addEdge(n2, n1), e3 = G.addEdge(n2, n3);
deba@228
    69
  checkGraphNodeList(G, 3);
deba@228
    70
  checkGraphArcList(G, 6);
deba@228
    71
  checkGraphEdgeList(G, 3);
deba@228
    72
deba@228
    73
  checkGraphOutArcList(G, n1, 2);
deba@228
    74
  checkGraphOutArcList(G, n2, 3);
deba@228
    75
  checkGraphOutArcList(G, n3, 1);
deba@228
    76
deba@228
    77
  checkGraphInArcList(G, n1, 2);
deba@228
    78
  checkGraphInArcList(G, n2, 3);
deba@228
    79
  checkGraphInArcList(G, n3, 1);
deba@228
    80
deba@228
    81
  checkGraphIncEdgeList(G, n1, 2);
deba@228
    82
  checkGraphIncEdgeList(G, n2, 3);
deba@228
    83
  checkGraphIncEdgeList(G, n3, 1);
deba@228
    84
deba@228
    85
  checkGraphConArcList(G, 6);
deba@228
    86
  checkGraphConEdgeList(G, 3);
deba@228
    87
deba@228
    88
  checkArcDirections(G);
deba@228
    89
deba@228
    90
  checkNodeIds(G);
deba@228
    91
  checkArcIds(G);
deba@228
    92
  checkEdgeIds(G);
deba@228
    93
  checkGraphNodeMap(G);
deba@228
    94
  checkGraphArcMap(G);
deba@228
    95
  checkGraphEdgeMap(G);
deba@228
    96
}
deba@228
    97
deba@365
    98
void checkFullGraph(int num) {
deba@365
    99
  typedef FullGraph Graph;
deba@365
   100
  GRAPH_TYPEDEFS(Graph);
deba@365
   101
deba@365
   102
  Graph G(num);
deba@365
   103
  checkGraphNodeList(G, num);
deba@365
   104
  checkGraphEdgeList(G, num * (num - 1) / 2);
deba@365
   105
deba@365
   106
  for (NodeIt n(G); n != INVALID; ++n) {
deba@365
   107
    checkGraphOutArcList(G, n, num - 1);    
deba@365
   108
    checkGraphInArcList(G, n, num - 1);    
deba@365
   109
    checkGraphIncEdgeList(G, n, num - 1);    
deba@365
   110
  }
deba@365
   111
deba@365
   112
  checkGraphConArcList(G, num * (num - 1));
deba@365
   113
  checkGraphConEdgeList(G, num * (num - 1) / 2);
deba@365
   114
deba@365
   115
  checkArcDirections(G);
deba@365
   116
deba@365
   117
  checkNodeIds(G);
deba@365
   118
  checkArcIds(G);
deba@365
   119
  checkEdgeIds(G);
deba@365
   120
  checkGraphNodeMap(G);
deba@365
   121
  checkGraphArcMap(G);
deba@365
   122
  checkGraphEdgeMap(G);
deba@365
   123
deba@365
   124
  
deba@365
   125
  for (int i = 0; i < G.nodeNum(); ++i) {
deba@365
   126
    check(G.index(G(i)) == i, "Wrong index");
deba@365
   127
  }
deba@365
   128
deba@365
   129
  for (NodeIt u(G); u != INVALID; ++u) {
deba@365
   130
    for (NodeIt v(G); v != INVALID; ++v) {
deba@365
   131
      Edge e = G.edge(u, v);
deba@365
   132
      Arc a = G.arc(u, v);
deba@365
   133
      if (u == v) {
deba@365
   134
        check(e == INVALID, "Wrong edge lookup");
deba@365
   135
        check(a == INVALID, "Wrong arc lookup");
deba@365
   136
      } else {
deba@365
   137
        check((G.u(e) == u && G.v(e) == v) ||
deba@365
   138
              (G.u(e) == v && G.v(e) == u), "Wrong edge lookup");
deba@365
   139
        check(G.source(a) == u && G.target(a) == v, "Wrong arc lookup");
deba@365
   140
      }
deba@365
   141
    }
deba@365
   142
  }
deba@365
   143
}
deba@365
   144
deba@228
   145
void checkConcepts() {
kpeter@171
   146
  { // Checking graph components
deba@57
   147
    checkConcept<BaseGraphComponent, BaseGraphComponent >();
deba@57
   148
alpar@209
   149
    checkConcept<IDableGraphComponent<>,
deba@57
   150
      IDableGraphComponent<> >();
deba@57
   151
alpar@209
   152
    checkConcept<IterableGraphComponent<>,
deba@57
   153
      IterableGraphComponent<> >();
deba@57
   154
alpar@209
   155
    checkConcept<MappableGraphComponent<>,
deba@57
   156
      MappableGraphComponent<> >();
deba@57
   157
  }
kpeter@171
   158
  { // Checking skeleton graph
kpeter@171
   159
    checkConcept<Graph, Graph>();
deba@57
   160
  }
kpeter@171
   161
  { // Checking ListGraph
kpeter@171
   162
    checkConcept<Graph, ListGraph>();
kpeter@171
   163
    checkConcept<AlterableGraphComponent<>, ListGraph>();
kpeter@171
   164
    checkConcept<ExtendableGraphComponent<>, ListGraph>();
kpeter@171
   165
    checkConcept<ClearableGraphComponent<>, ListGraph>();
kpeter@171
   166
    checkConcept<ErasableGraphComponent<>, ListGraph>();
kpeter@171
   167
  }
kpeter@171
   168
  { // Checking SmartGraph
kpeter@171
   169
    checkConcept<Graph, SmartGraph>();
kpeter@171
   170
    checkConcept<AlterableGraphComponent<>, SmartGraph>();
kpeter@171
   171
    checkConcept<ExtendableGraphComponent<>, SmartGraph>();
kpeter@171
   172
    checkConcept<ClearableGraphComponent<>, SmartGraph>();
kpeter@171
   173
  }
deba@365
   174
  { // Checking FullGraph
deba@365
   175
    checkConcept<Graph, FullGraph>();
deba@365
   176
  }
kpeter@346
   177
  { // Checking GridGraph
kpeter@346
   178
    checkConcept<Graph, GridGraph>();
kpeter@346
   179
  }
deba@57
   180
}
deba@57
   181
deba@57
   182
template <typename Graph>
deba@228
   183
void checkGraphValidity() {
deba@149
   184
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
deba@57
   185
  Graph g;
deba@57
   186
deba@57
   187
  Node
deba@57
   188
    n1 = g.addNode(),
deba@57
   189
    n2 = g.addNode(),
deba@57
   190
    n3 = g.addNode();
deba@57
   191
deba@57
   192
  Edge
deba@57
   193
    e1 = g.addEdge(n1, n2),
deba@57
   194
    e2 = g.addEdge(n2, n3);
deba@57
   195
kpeter@171
   196
  check(g.valid(n1), "Wrong validity check");
kpeter@171
   197
  check(g.valid(e1), "Wrong validity check");
kpeter@171
   198
  check(g.valid(g.direct(e1, true)), "Wrong validity check");
kpeter@171
   199
kpeter@171
   200
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
kpeter@171
   201
  check(!g.valid(g.edgeFromId(-1)), "Wrong validity check");
kpeter@171
   202
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
deba@57
   203
}
deba@57
   204
deba@149
   205
template <typename Graph>
deba@228
   206
void checkGraphValidityErase() {
deba@149
   207
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
deba@149
   208
  Graph g;
deba@149
   209
deba@149
   210
  Node
deba@149
   211
    n1 = g.addNode(),
deba@149
   212
    n2 = g.addNode(),
deba@149
   213
    n3 = g.addNode();
deba@149
   214
deba@149
   215
  Edge
deba@149
   216
    e1 = g.addEdge(n1, n2),
deba@149
   217
    e2 = g.addEdge(n2, n3);
deba@149
   218
kpeter@171
   219
  check(g.valid(n1), "Wrong validity check");
kpeter@171
   220
  check(g.valid(e1), "Wrong validity check");
kpeter@171
   221
  check(g.valid(g.direct(e1, true)), "Wrong validity check");
deba@149
   222
deba@149
   223
  g.erase(n1);
deba@149
   224
kpeter@171
   225
  check(!g.valid(n1), "Wrong validity check");
kpeter@171
   226
  check(g.valid(n2), "Wrong validity check");
kpeter@171
   227
  check(g.valid(n3), "Wrong validity check");
kpeter@171
   228
  check(!g.valid(e1), "Wrong validity check");
kpeter@171
   229
  check(g.valid(e2), "Wrong validity check");
deba@149
   230
kpeter@171
   231
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
kpeter@171
   232
  check(!g.valid(g.edgeFromId(-1)), "Wrong validity check");
kpeter@171
   233
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
deba@149
   234
}
deba@149
   235
deba@347
   236
void checkGridGraph(int width, int height) {
deba@347
   237
  typedef GridGraph Graph;
deba@347
   238
  GRAPH_TYPEDEFS(Graph);
deba@347
   239
  Graph G(width, height);
deba@57
   240
kpeter@348
   241
  check(G.width() == width, "Wrong column number");
kpeter@348
   242
  check(G.height() == height, "Wrong row number");
alpar@209
   243
deba@347
   244
  for (int i = 0; i < width; ++i) {
deba@347
   245
    for (int j = 0; j < height; ++j) {
deba@347
   246
      check(G.col(G(i, j)) == i, "Wrong column");
deba@347
   247
      check(G.row(G(i, j)) == j, "Wrong row");
deba@347
   248
      check(G.pos(G(i, j)).x == i, "Wrong column");
deba@347
   249
      check(G.pos(G(i, j)).y == j, "Wrong row");
kpeter@346
   250
    }
kpeter@346
   251
  }
deba@57
   252
deba@347
   253
  for (int j = 0; j < height; ++j) {
deba@347
   254
    for (int i = 0; i < width - 1; ++i) {
deba@347
   255
      check(G.source(G.right(G(i, j))) == G(i, j), "Wrong right");
deba@347
   256
      check(G.target(G.right(G(i, j))) == G(i + 1, j), "Wrong right");
kpeter@346
   257
    }
deba@347
   258
    check(G.right(G(width - 1, j)) == INVALID, "Wrong right");
kpeter@346
   259
  }
deba@57
   260
deba@347
   261
  for (int j = 0; j < height; ++j) {
deba@347
   262
    for (int i = 1; i < width; ++i) {
deba@347
   263
      check(G.source(G.left(G(i, j))) == G(i, j), "Wrong left");
deba@347
   264
      check(G.target(G.left(G(i, j))) == G(i - 1, j), "Wrong left");
kpeter@346
   265
    }
deba@347
   266
    check(G.left(G(0, j)) == INVALID, "Wrong left");
kpeter@346
   267
  }
deba@57
   268
deba@347
   269
  for (int i = 0; i < width; ++i) {
deba@347
   270
    for (int j = 0; j < height - 1; ++j) {
deba@347
   271
      check(G.source(G.up(G(i, j))) == G(i, j), "Wrong up");
deba@347
   272
      check(G.target(G.up(G(i, j))) == G(i, j + 1), "Wrong up");
kpeter@346
   273
    }
deba@347
   274
    check(G.up(G(i, height - 1)) == INVALID, "Wrong up");
kpeter@346
   275
  }
deba@228
   276
deba@347
   277
  for (int i = 0; i < width; ++i) {
deba@347
   278
    for (int j = 1; j < height; ++j) {
deba@347
   279
      check(G.source(G.down(G(i, j))) == G(i, j), "Wrong down");
deba@347
   280
      check(G.target(G.down(G(i, j))) == G(i, j - 1), "Wrong down");
kpeter@346
   281
    }
deba@347
   282
    check(G.down(G(i, 0)) == INVALID, "Wrong down");
kpeter@346
   283
  }
kpeter@346
   284
deba@347
   285
  checkGraphNodeList(G, width * height);
deba@347
   286
  checkGraphEdgeList(G, width * (height - 1) + (width - 1) * height);
deba@347
   287
  checkGraphArcList(G, 2 * (width * (height - 1) + (width - 1) * height));
kpeter@346
   288
deba@347
   289
  for (NodeIt n(G); n != INVALID; ++n) {
deba@347
   290
    int nb = 4;
deba@347
   291
    if (G.col(n) == 0) --nb;
deba@347
   292
    if (G.col(n) == width - 1) --nb;
deba@347
   293
    if (G.row(n) == 0) --nb;
deba@347
   294
    if (G.row(n) == height - 1) --nb;
kpeter@346
   295
deba@347
   296
    checkGraphOutArcList(G, n, nb);
deba@347
   297
    checkGraphInArcList(G, n, nb);
deba@347
   298
    checkGraphIncEdgeList(G, n, nb);
deba@347
   299
  }
kpeter@346
   300
deba@347
   301
  checkArcDirections(G);
kpeter@346
   302
deba@347
   303
  checkGraphConArcList(G, 2 * (width * (height - 1) + (width - 1) * height));
deba@347
   304
  checkGraphConEdgeList(G, width * (height - 1) + (width - 1) * height);
kpeter@346
   305
deba@347
   306
  checkNodeIds(G);
deba@347
   307
  checkArcIds(G);
deba@347
   308
  checkEdgeIds(G);
deba@347
   309
  checkGraphNodeMap(G);
deba@347
   310
  checkGraphArcMap(G);
deba@347
   311
  checkGraphEdgeMap(G);
kpeter@346
   312
kpeter@346
   313
}
deba@57
   314
deba@228
   315
void checkGraphs() {
kpeter@171
   316
  { // Checking ListGraph
kpeter@171
   317
    checkGraph<ListGraph>();
deba@228
   318
    checkGraphValidityErase<ListGraph>();
kpeter@171
   319
  }
kpeter@171
   320
  { // Checking SmartGraph
kpeter@171
   321
    checkGraph<SmartGraph>();
deba@228
   322
    checkGraphValidity<SmartGraph>();
kpeter@171
   323
  }
deba@365
   324
  { // Checking FullGraph   
deba@365
   325
    checkFullGraph(7);
deba@365
   326
    checkFullGraph(8);
deba@365
   327
  }
kpeter@346
   328
  { // Checking GridGraph
deba@347
   329
    checkGridGraph(5, 8);
deba@347
   330
    checkGridGraph(8, 5);
deba@347
   331
    checkGridGraph(5, 5);
deba@347
   332
    checkGridGraph(0, 0);
deba@347
   333
    checkGridGraph(1, 1);
kpeter@346
   334
  }
kpeter@171
   335
}
kpeter@171
   336
deba@57
   337
int main() {
deba@228
   338
  checkConcepts();
deba@228
   339
  checkGraphs();
deba@57
   340
  return 0;
deba@57
   341
}