test/graph_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Thu, 06 Nov 2008 18:35:38 +0100
changeset 368 a7e8ad460d66
parent 356 99f1bdf8f7db
child 372 7b6466ed488a
permissions -rw-r--r--
Add missing tags and functions for item counting in SmartGraph (#3)
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@353
    22
#include <lemon/full_graph.h>
kpeter@334
    23
#include <lemon/grid_graph.h>
kpeter@365
    24
#include <lemon/hypercube_graph.h>
deba@57
    25
deba@57
    26
#include "test_tools.h"
kpeter@171
    27
#include "graph_test.h"
deba@57
    28
deba@57
    29
using namespace lemon;
deba@57
    30
using namespace lemon::concepts;
deba@57
    31
deba@228
    32
template <class Graph>
deba@228
    33
void checkGraph() {
deba@228
    34
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
deba@228
    35
deba@228
    36
  Graph G;
deba@228
    37
  checkGraphNodeList(G, 0);
deba@228
    38
  checkGraphEdgeList(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);
deba@228
    46
deba@228
    47
  Edge e1 = G.addEdge(n1, n2);
deba@228
    48
  check((G.u(e1) == n1 && G.v(e1) == n2) || (G.u(e1) == n2 && G.v(e1) == n1),
deba@228
    49
        "Wrong edge");
deba@228
    50
  checkGraphNodeList(G, 3);
deba@228
    51
  checkGraphArcList(G, 2);
deba@228
    52
  checkGraphEdgeList(G, 1);
deba@228
    53
deba@228
    54
  checkGraphOutArcList(G, n1, 1);
deba@228
    55
  checkGraphOutArcList(G, n2, 1);
deba@228
    56
  checkGraphOutArcList(G, n3, 0);
deba@228
    57
deba@228
    58
  checkGraphInArcList(G, n1, 1);
deba@228
    59
  checkGraphInArcList(G, n2, 1);
deba@228
    60
  checkGraphInArcList(G, n3, 0);
deba@228
    61
deba@228
    62
  checkGraphIncEdgeList(G, n1, 1);
deba@228
    63
  checkGraphIncEdgeList(G, n2, 1);
deba@228
    64
  checkGraphIncEdgeList(G, n3, 0);
deba@228
    65
deba@228
    66
  checkGraphConArcList(G, 2);
deba@228
    67
  checkGraphConEdgeList(G, 1);
deba@228
    68
deba@228
    69
  Edge e2 = G.addEdge(n2, n1), e3 = G.addEdge(n2, n3);
deba@228
    70
  checkGraphNodeList(G, 3);
deba@228
    71
  checkGraphArcList(G, 6);
deba@228
    72
  checkGraphEdgeList(G, 3);
deba@228
    73
deba@228
    74
  checkGraphOutArcList(G, n1, 2);
deba@228
    75
  checkGraphOutArcList(G, n2, 3);
deba@228
    76
  checkGraphOutArcList(G, n3, 1);
deba@228
    77
deba@228
    78
  checkGraphInArcList(G, n1, 2);
deba@228
    79
  checkGraphInArcList(G, n2, 3);
deba@228
    80
  checkGraphInArcList(G, n3, 1);
deba@228
    81
deba@228
    82
  checkGraphIncEdgeList(G, n1, 2);
deba@228
    83
  checkGraphIncEdgeList(G, n2, 3);
deba@228
    84
  checkGraphIncEdgeList(G, n3, 1);
deba@228
    85
deba@228
    86
  checkGraphConArcList(G, 6);
deba@228
    87
  checkGraphConEdgeList(G, 3);
deba@228
    88
deba@228
    89
  checkArcDirections(G);
deba@228
    90
deba@228
    91
  checkNodeIds(G);
deba@228
    92
  checkArcIds(G);
deba@228
    93
  checkEdgeIds(G);
deba@228
    94
  checkGraphNodeMap(G);
deba@228
    95
  checkGraphArcMap(G);
deba@228
    96
  checkGraphEdgeMap(G);
deba@228
    97
}
deba@228
    98
deba@353
    99
void checkFullGraph(int num) {
deba@353
   100
  typedef FullGraph Graph;
deba@353
   101
  GRAPH_TYPEDEFS(Graph);
deba@353
   102
deba@353
   103
  Graph G(num);
deba@353
   104
  checkGraphNodeList(G, num);
deba@353
   105
  checkGraphEdgeList(G, num * (num - 1) / 2);
deba@353
   106
deba@353
   107
  for (NodeIt n(G); n != INVALID; ++n) {
kpeter@365
   108
    checkGraphOutArcList(G, n, num - 1);
kpeter@365
   109
    checkGraphInArcList(G, n, num - 1);
kpeter@365
   110
    checkGraphIncEdgeList(G, n, num - 1);
deba@353
   111
  }
deba@353
   112
deba@353
   113
  checkGraphConArcList(G, num * (num - 1));
deba@353
   114
  checkGraphConEdgeList(G, num * (num - 1) / 2);
deba@353
   115
deba@353
   116
  checkArcDirections(G);
deba@353
   117
deba@353
   118
  checkNodeIds(G);
deba@353
   119
  checkArcIds(G);
deba@353
   120
  checkEdgeIds(G);
deba@353
   121
  checkGraphNodeMap(G);
deba@353
   122
  checkGraphArcMap(G);
deba@353
   123
  checkGraphEdgeMap(G);
deba@353
   124
kpeter@365
   125
deba@353
   126
  for (int i = 0; i < G.nodeNum(); ++i) {
deba@353
   127
    check(G.index(G(i)) == i, "Wrong index");
deba@353
   128
  }
deba@353
   129
deba@353
   130
  for (NodeIt u(G); u != INVALID; ++u) {
deba@353
   131
    for (NodeIt v(G); v != INVALID; ++v) {
deba@353
   132
      Edge e = G.edge(u, v);
deba@353
   133
      Arc a = G.arc(u, v);
deba@353
   134
      if (u == v) {
deba@353
   135
        check(e == INVALID, "Wrong edge lookup");
deba@353
   136
        check(a == INVALID, "Wrong arc lookup");
deba@353
   137
      } else {
deba@353
   138
        check((G.u(e) == u && G.v(e) == v) ||
deba@353
   139
              (G.u(e) == v && G.v(e) == u), "Wrong edge lookup");
deba@353
   140
        check(G.source(a) == u && G.target(a) == v, "Wrong arc lookup");
deba@353
   141
      }
deba@353
   142
    }
deba@353
   143
  }
deba@353
   144
}
deba@353
   145
deba@228
   146
void checkConcepts() {
kpeter@171
   147
  { // Checking graph components
deba@57
   148
    checkConcept<BaseGraphComponent, BaseGraphComponent >();
deba@57
   149
alpar@209
   150
    checkConcept<IDableGraphComponent<>,
deba@57
   151
      IDableGraphComponent<> >();
deba@57
   152
alpar@209
   153
    checkConcept<IterableGraphComponent<>,
deba@57
   154
      IterableGraphComponent<> >();
deba@57
   155
alpar@209
   156
    checkConcept<MappableGraphComponent<>,
deba@57
   157
      MappableGraphComponent<> >();
deba@57
   158
  }
kpeter@171
   159
  { // Checking skeleton graph
kpeter@171
   160
    checkConcept<Graph, Graph>();
deba@57
   161
  }
kpeter@171
   162
  { // Checking ListGraph
kpeter@171
   163
    checkConcept<Graph, ListGraph>();
kpeter@171
   164
    checkConcept<AlterableGraphComponent<>, ListGraph>();
kpeter@171
   165
    checkConcept<ExtendableGraphComponent<>, ListGraph>();
kpeter@171
   166
    checkConcept<ClearableGraphComponent<>, ListGraph>();
kpeter@171
   167
    checkConcept<ErasableGraphComponent<>, ListGraph>();
kpeter@171
   168
  }
kpeter@171
   169
  { // Checking SmartGraph
kpeter@171
   170
    checkConcept<Graph, SmartGraph>();
kpeter@171
   171
    checkConcept<AlterableGraphComponent<>, SmartGraph>();
kpeter@171
   172
    checkConcept<ExtendableGraphComponent<>, SmartGraph>();
kpeter@171
   173
    checkConcept<ClearableGraphComponent<>, SmartGraph>();
kpeter@171
   174
  }
deba@353
   175
  { // Checking FullGraph
deba@353
   176
    checkConcept<Graph, FullGraph>();
deba@353
   177
  }
kpeter@334
   178
  { // Checking GridGraph
kpeter@334
   179
    checkConcept<Graph, GridGraph>();
kpeter@334
   180
  }
kpeter@365
   181
  { // Checking HypercubeGraph
kpeter@365
   182
    checkConcept<Graph, HypercubeGraph>();
kpeter@365
   183
  }
deba@57
   184
}
deba@57
   185
deba@57
   186
template <typename Graph>
deba@228
   187
void checkGraphValidity() {
deba@149
   188
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
deba@57
   189
  Graph g;
deba@57
   190
deba@57
   191
  Node
deba@57
   192
    n1 = g.addNode(),
deba@57
   193
    n2 = g.addNode(),
deba@57
   194
    n3 = g.addNode();
deba@57
   195
deba@57
   196
  Edge
deba@57
   197
    e1 = g.addEdge(n1, n2),
deba@57
   198
    e2 = g.addEdge(n2, n3);
deba@57
   199
kpeter@171
   200
  check(g.valid(n1), "Wrong validity check");
kpeter@171
   201
  check(g.valid(e1), "Wrong validity check");
kpeter@171
   202
  check(g.valid(g.direct(e1, true)), "Wrong validity check");
kpeter@171
   203
kpeter@171
   204
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
kpeter@171
   205
  check(!g.valid(g.edgeFromId(-1)), "Wrong validity check");
kpeter@171
   206
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
deba@57
   207
}
deba@57
   208
deba@149
   209
template <typename Graph>
deba@228
   210
void checkGraphValidityErase() {
deba@149
   211
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
deba@149
   212
  Graph g;
deba@149
   213
deba@149
   214
  Node
deba@149
   215
    n1 = g.addNode(),
deba@149
   216
    n2 = g.addNode(),
deba@149
   217
    n3 = g.addNode();
deba@149
   218
deba@149
   219
  Edge
deba@149
   220
    e1 = g.addEdge(n1, n2),
deba@149
   221
    e2 = g.addEdge(n2, n3);
deba@149
   222
kpeter@171
   223
  check(g.valid(n1), "Wrong validity check");
kpeter@171
   224
  check(g.valid(e1), "Wrong validity check");
kpeter@171
   225
  check(g.valid(g.direct(e1, true)), "Wrong validity check");
deba@149
   226
deba@149
   227
  g.erase(n1);
deba@149
   228
kpeter@171
   229
  check(!g.valid(n1), "Wrong validity check");
kpeter@171
   230
  check(g.valid(n2), "Wrong validity check");
kpeter@171
   231
  check(g.valid(n3), "Wrong validity check");
kpeter@171
   232
  check(!g.valid(e1), "Wrong validity check");
kpeter@171
   233
  check(g.valid(e2), "Wrong validity check");
deba@149
   234
kpeter@171
   235
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
kpeter@171
   236
  check(!g.valid(g.edgeFromId(-1)), "Wrong validity check");
kpeter@171
   237
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
deba@149
   238
}
deba@149
   239
deba@335
   240
void checkGridGraph(int width, int height) {
deba@335
   241
  typedef GridGraph Graph;
deba@335
   242
  GRAPH_TYPEDEFS(Graph);
deba@335
   243
  Graph G(width, height);
deba@57
   244
kpeter@336
   245
  check(G.width() == width, "Wrong column number");
kpeter@336
   246
  check(G.height() == height, "Wrong row number");
alpar@209
   247
deba@335
   248
  for (int i = 0; i < width; ++i) {
deba@335
   249
    for (int j = 0; j < height; ++j) {
deba@335
   250
      check(G.col(G(i, j)) == i, "Wrong column");
deba@335
   251
      check(G.row(G(i, j)) == j, "Wrong row");
deba@335
   252
      check(G.pos(G(i, j)).x == i, "Wrong column");
deba@335
   253
      check(G.pos(G(i, j)).y == j, "Wrong row");
kpeter@334
   254
    }
kpeter@334
   255
  }
deba@57
   256
deba@335
   257
  for (int j = 0; j < height; ++j) {
deba@335
   258
    for (int i = 0; i < width - 1; ++i) {
deba@335
   259
      check(G.source(G.right(G(i, j))) == G(i, j), "Wrong right");
deba@335
   260
      check(G.target(G.right(G(i, j))) == G(i + 1, j), "Wrong right");
kpeter@334
   261
    }
deba@335
   262
    check(G.right(G(width - 1, j)) == INVALID, "Wrong right");
kpeter@334
   263
  }
deba@57
   264
deba@335
   265
  for (int j = 0; j < height; ++j) {
deba@335
   266
    for (int i = 1; i < width; ++i) {
deba@335
   267
      check(G.source(G.left(G(i, j))) == G(i, j), "Wrong left");
deba@335
   268
      check(G.target(G.left(G(i, j))) == G(i - 1, j), "Wrong left");
kpeter@334
   269
    }
deba@335
   270
    check(G.left(G(0, j)) == INVALID, "Wrong left");
kpeter@334
   271
  }
deba@57
   272
deba@335
   273
  for (int i = 0; i < width; ++i) {
deba@335
   274
    for (int j = 0; j < height - 1; ++j) {
deba@335
   275
      check(G.source(G.up(G(i, j))) == G(i, j), "Wrong up");
deba@335
   276
      check(G.target(G.up(G(i, j))) == G(i, j + 1), "Wrong up");
kpeter@334
   277
    }
deba@335
   278
    check(G.up(G(i, height - 1)) == INVALID, "Wrong up");
kpeter@334
   279
  }
deba@228
   280
deba@335
   281
  for (int i = 0; i < width; ++i) {
deba@335
   282
    for (int j = 1; j < height; ++j) {
deba@335
   283
      check(G.source(G.down(G(i, j))) == G(i, j), "Wrong down");
deba@335
   284
      check(G.target(G.down(G(i, j))) == G(i, j - 1), "Wrong down");
kpeter@334
   285
    }
deba@335
   286
    check(G.down(G(i, 0)) == INVALID, "Wrong down");
kpeter@334
   287
  }
kpeter@334
   288
deba@335
   289
  checkGraphNodeList(G, width * height);
deba@335
   290
  checkGraphEdgeList(G, width * (height - 1) + (width - 1) * height);
deba@335
   291
  checkGraphArcList(G, 2 * (width * (height - 1) + (width - 1) * height));
kpeter@334
   292
deba@335
   293
  for (NodeIt n(G); n != INVALID; ++n) {
deba@335
   294
    int nb = 4;
deba@335
   295
    if (G.col(n) == 0) --nb;
deba@335
   296
    if (G.col(n) == width - 1) --nb;
deba@335
   297
    if (G.row(n) == 0) --nb;
deba@335
   298
    if (G.row(n) == height - 1) --nb;
kpeter@334
   299
deba@335
   300
    checkGraphOutArcList(G, n, nb);
deba@335
   301
    checkGraphInArcList(G, n, nb);
deba@335
   302
    checkGraphIncEdgeList(G, n, nb);
deba@335
   303
  }
kpeter@334
   304
deba@335
   305
  checkArcDirections(G);
kpeter@334
   306
deba@335
   307
  checkGraphConArcList(G, 2 * (width * (height - 1) + (width - 1) * height));
deba@335
   308
  checkGraphConEdgeList(G, width * (height - 1) + (width - 1) * height);
kpeter@334
   309
deba@335
   310
  checkNodeIds(G);
deba@335
   311
  checkArcIds(G);
deba@335
   312
  checkEdgeIds(G);
deba@335
   313
  checkGraphNodeMap(G);
deba@335
   314
  checkGraphArcMap(G);
deba@335
   315
  checkGraphEdgeMap(G);
kpeter@334
   316
kpeter@334
   317
}
deba@57
   318
kpeter@365
   319
void checkHypercubeGraph(int dim) {
kpeter@365
   320
  GRAPH_TYPEDEFS(HypercubeGraph);
kpeter@365
   321
kpeter@365
   322
  HypercubeGraph G(dim);
kpeter@365
   323
  checkGraphNodeList(G, 1 << dim);
kpeter@365
   324
  checkGraphEdgeList(G, dim * (1 << dim-1));
kpeter@365
   325
  checkGraphArcList(G, dim * (1 << dim));
kpeter@365
   326
kpeter@365
   327
  Node n = G.nodeFromId(dim);
kpeter@365
   328
kpeter@365
   329
  for (NodeIt n(G); n != INVALID; ++n) {
kpeter@365
   330
    checkGraphIncEdgeList(G, n, dim);
kpeter@365
   331
    for (IncEdgeIt e(G, n); e != INVALID; ++e) {
kpeter@365
   332
      check( (G.u(e) == n &&
kpeter@365
   333
              G.id(G.v(e)) == G.id(n) ^ (1 << G.dimension(e))) ||
kpeter@365
   334
             (G.v(e) == n &&
kpeter@365
   335
              G.id(G.u(e)) == G.id(n) ^ (1 << G.dimension(e))),
kpeter@365
   336
             "Wrong edge or wrong dimension");
kpeter@365
   337
    }
kpeter@365
   338
kpeter@365
   339
    checkGraphOutArcList(G, n, dim);
kpeter@365
   340
    for (OutArcIt a(G, n); a != INVALID; ++a) {
kpeter@365
   341
      check(G.source(a) == n &&
kpeter@365
   342
            G.id(G.target(a)) == G.id(n) ^ (1 << G.dimension(a)),
kpeter@365
   343
            "Wrong arc or wrong dimension");
kpeter@365
   344
    }
kpeter@365
   345
kpeter@365
   346
    checkGraphInArcList(G, n, dim);
kpeter@365
   347
    for (InArcIt a(G, n); a != INVALID; ++a) {
kpeter@365
   348
      check(G.target(a) == n &&
kpeter@365
   349
            G.id(G.source(a)) == G.id(n) ^ (1 << G.dimension(a)),
kpeter@365
   350
            "Wrong arc or wrong dimension");
kpeter@365
   351
    }
kpeter@365
   352
  }
kpeter@365
   353
kpeter@365
   354
  checkGraphConArcList(G, (1 << dim) * dim);
kpeter@365
   355
  checkGraphConEdgeList(G, dim * (1 << dim-1));
kpeter@365
   356
kpeter@365
   357
  checkArcDirections(G);
kpeter@365
   358
kpeter@365
   359
  checkNodeIds(G);
kpeter@365
   360
  checkArcIds(G);
kpeter@365
   361
  checkEdgeIds(G);
kpeter@365
   362
  checkGraphNodeMap(G);
kpeter@365
   363
  checkGraphArcMap(G);
kpeter@365
   364
  checkGraphEdgeMap(G);
kpeter@365
   365
}
kpeter@365
   366
deba@228
   367
void checkGraphs() {
kpeter@171
   368
  { // Checking ListGraph
kpeter@171
   369
    checkGraph<ListGraph>();
deba@228
   370
    checkGraphValidityErase<ListGraph>();
kpeter@171
   371
  }
kpeter@171
   372
  { // Checking SmartGraph
kpeter@171
   373
    checkGraph<SmartGraph>();
deba@228
   374
    checkGraphValidity<SmartGraph>();
kpeter@171
   375
  }
kpeter@365
   376
  { // Checking FullGraph
deba@353
   377
    checkFullGraph(7);
deba@353
   378
    checkFullGraph(8);
deba@353
   379
  }
kpeter@334
   380
  { // Checking GridGraph
deba@335
   381
    checkGridGraph(5, 8);
deba@335
   382
    checkGridGraph(8, 5);
deba@335
   383
    checkGridGraph(5, 5);
deba@335
   384
    checkGridGraph(0, 0);
deba@335
   385
    checkGridGraph(1, 1);
kpeter@334
   386
  }
kpeter@365
   387
  { // Checking HypercubeGraph
kpeter@365
   388
    checkHypercubeGraph(1);
kpeter@365
   389
    checkHypercubeGraph(2);
kpeter@365
   390
    checkHypercubeGraph(3);
kpeter@365
   391
    checkHypercubeGraph(4);
kpeter@365
   392
  }
kpeter@171
   393
}
kpeter@171
   394
deba@57
   395
int main() {
deba@228
   396
  checkConcepts();
deba@228
   397
  checkGraphs();
deba@57
   398
  return 0;
deba@57
   399
}