test/graph_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Tue, 02 Sep 2008 22:32:04 +0200
changeset 334 ada5f74d1c9e
parent 228 b6732e0d38c5
child 335 160bf92c7cdc
permissions -rw-r--r--
Port grid graph structure from SVN 3503 (ticket #57)
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>
kpeter@334
    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@228
    98
void checkConcepts() {
kpeter@171
    99
  { // Checking graph components
deba@57
   100
    checkConcept<BaseGraphComponent, BaseGraphComponent >();
deba@57
   101
alpar@209
   102
    checkConcept<IDableGraphComponent<>,
deba@57
   103
      IDableGraphComponent<> >();
deba@57
   104
alpar@209
   105
    checkConcept<IterableGraphComponent<>,
deba@57
   106
      IterableGraphComponent<> >();
deba@57
   107
alpar@209
   108
    checkConcept<MappableGraphComponent<>,
deba@57
   109
      MappableGraphComponent<> >();
deba@57
   110
  }
kpeter@171
   111
  { // Checking skeleton graph
kpeter@171
   112
    checkConcept<Graph, Graph>();
deba@57
   113
  }
kpeter@171
   114
  { // Checking ListGraph
kpeter@171
   115
    checkConcept<Graph, ListGraph>();
kpeter@171
   116
    checkConcept<AlterableGraphComponent<>, ListGraph>();
kpeter@171
   117
    checkConcept<ExtendableGraphComponent<>, ListGraph>();
kpeter@171
   118
    checkConcept<ClearableGraphComponent<>, ListGraph>();
kpeter@171
   119
    checkConcept<ErasableGraphComponent<>, ListGraph>();
kpeter@171
   120
  }
kpeter@171
   121
  { // Checking SmartGraph
kpeter@171
   122
    checkConcept<Graph, SmartGraph>();
kpeter@171
   123
    checkConcept<AlterableGraphComponent<>, SmartGraph>();
kpeter@171
   124
    checkConcept<ExtendableGraphComponent<>, SmartGraph>();
kpeter@171
   125
    checkConcept<ClearableGraphComponent<>, SmartGraph>();
kpeter@171
   126
  }
kpeter@171
   127
//  { // Checking FullGraph
kpeter@171
   128
//    checkConcept<Graph, FullGraph>();
kpeter@171
   129
//  }
kpeter@334
   130
  { // Checking GridGraph
kpeter@334
   131
    checkConcept<Graph, GridGraph>();
kpeter@334
   132
  }
deba@57
   133
}
deba@57
   134
deba@57
   135
template <typename Graph>
deba@228
   136
void checkGraphValidity() {
deba@149
   137
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
deba@57
   138
  Graph g;
deba@57
   139
deba@57
   140
  Node
deba@57
   141
    n1 = g.addNode(),
deba@57
   142
    n2 = g.addNode(),
deba@57
   143
    n3 = g.addNode();
deba@57
   144
deba@57
   145
  Edge
deba@57
   146
    e1 = g.addEdge(n1, n2),
deba@57
   147
    e2 = g.addEdge(n2, n3);
deba@57
   148
kpeter@171
   149
  check(g.valid(n1), "Wrong validity check");
kpeter@171
   150
  check(g.valid(e1), "Wrong validity check");
kpeter@171
   151
  check(g.valid(g.direct(e1, true)), "Wrong validity check");
kpeter@171
   152
kpeter@171
   153
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
kpeter@171
   154
  check(!g.valid(g.edgeFromId(-1)), "Wrong validity check");
kpeter@171
   155
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
deba@57
   156
}
deba@57
   157
deba@149
   158
template <typename Graph>
deba@228
   159
void checkGraphValidityErase() {
deba@149
   160
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
deba@149
   161
  Graph g;
deba@149
   162
deba@149
   163
  Node
deba@149
   164
    n1 = g.addNode(),
deba@149
   165
    n2 = g.addNode(),
deba@149
   166
    n3 = g.addNode();
deba@149
   167
deba@149
   168
  Edge
deba@149
   169
    e1 = g.addEdge(n1, n2),
deba@149
   170
    e2 = g.addEdge(n2, n3);
deba@149
   171
kpeter@171
   172
  check(g.valid(n1), "Wrong validity check");
kpeter@171
   173
  check(g.valid(e1), "Wrong validity check");
kpeter@171
   174
  check(g.valid(g.direct(e1, true)), "Wrong validity check");
deba@149
   175
deba@149
   176
  g.erase(n1);
deba@149
   177
kpeter@171
   178
  check(!g.valid(n1), "Wrong validity check");
kpeter@171
   179
  check(g.valid(n2), "Wrong validity check");
kpeter@171
   180
  check(g.valid(n3), "Wrong validity check");
kpeter@171
   181
  check(!g.valid(e1), "Wrong validity check");
kpeter@171
   182
  check(g.valid(e2), "Wrong validity check");
deba@149
   183
kpeter@171
   184
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
kpeter@171
   185
  check(!g.valid(g.edgeFromId(-1)), "Wrong validity check");
kpeter@171
   186
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
deba@149
   187
}
deba@149
   188
kpeter@334
   189
void checkGridGraph(const GridGraph& g, int w, int h) {
kpeter@334
   190
  check(g.width() == w, "Wrong width");
kpeter@334
   191
  check(g.height() == h, "Wrong height");
deba@57
   192
kpeter@334
   193
  for (int i = 0; i < w; ++i) {
kpeter@334
   194
    for (int j = 0; j < h; ++j) {
kpeter@334
   195
      check(g.col(g(i, j)) == i, "Wrong col");
kpeter@334
   196
      check(g.row(g(i, j)) == j, "Wrong row");
kpeter@334
   197
    }
kpeter@334
   198
  }
alpar@209
   199
kpeter@334
   200
  for (int i = 0; i < w; ++i) {
kpeter@334
   201
    for (int j = 0; j < h - 1; ++j) {
kpeter@334
   202
      check(g.source(g.down(g(i, j))) == g(i, j), "Wrong down");
kpeter@334
   203
      check(g.target(g.down(g(i, j))) == g(i, j + 1), "Wrong down");
kpeter@334
   204
    }
kpeter@334
   205
    check(g.down(g(i, h - 1)) == INVALID, "Wrong down");
kpeter@334
   206
  }
deba@57
   207
kpeter@334
   208
  for (int i = 0; i < w; ++i) {
kpeter@334
   209
    for (int j = 1; j < h; ++j) {
kpeter@334
   210
      check(g.source(g.up(g(i, j))) == g(i, j), "Wrong up");
kpeter@334
   211
      check(g.target(g.up(g(i, j))) == g(i, j - 1), "Wrong up");
kpeter@334
   212
    }
kpeter@334
   213
    check(g.up(g(i, 0)) == INVALID, "Wrong up");
kpeter@334
   214
  }
deba@57
   215
kpeter@334
   216
  for (int j = 0; j < h; ++j) {
kpeter@334
   217
    for (int i = 0; i < w - 1; ++i) {
kpeter@334
   218
      check(g.source(g.right(g(i, j))) == g(i, j), "Wrong right");
kpeter@334
   219
      check(g.target(g.right(g(i, j))) == g(i + 1, j), "Wrong right");
kpeter@334
   220
    }
kpeter@334
   221
    check(g.right(g(w - 1, j)) == INVALID, "Wrong right");
kpeter@334
   222
  }
deba@57
   223
kpeter@334
   224
  for (int j = 0; j < h; ++j) {
kpeter@334
   225
    for (int i = 1; i < w; ++i) {
kpeter@334
   226
      check(g.source(g.left(g(i, j))) == g(i, j), "Wrong left");
kpeter@334
   227
      check(g.target(g.left(g(i, j))) == g(i - 1, j), "Wrong left");
kpeter@334
   228
    }
kpeter@334
   229
    check(g.left(g(0, j)) == INVALID, "Wrong left");
kpeter@334
   230
  }
kpeter@334
   231
kpeter@334
   232
  checkGraphNodeList(g, w*h);
kpeter@334
   233
  checkGraphArcList(g, 2*(2*w*h-w-h));
kpeter@334
   234
  checkGraphEdgeList(g, 2*w*h-w-h);
kpeter@334
   235
kpeter@334
   236
  checkGraphOutArcList(g, g(0,0), 2);
kpeter@334
   237
  checkGraphOutArcList(g, g(0,1), 3);
kpeter@334
   238
  checkGraphOutArcList(g, g(w-2,h-2), 4);
kpeter@334
   239
kpeter@334
   240
  checkGraphInArcList(g, g(0,0), 2);
kpeter@334
   241
  checkGraphInArcList(g, g(0,1), 3);
kpeter@334
   242
  checkGraphInArcList(g, g(w-2,h-2), 4);
kpeter@334
   243
kpeter@334
   244
  checkGraphIncEdgeList(g, g(0,0), 2);
kpeter@334
   245
  checkGraphIncEdgeList(g, g(0,1), 3);
kpeter@334
   246
  checkGraphIncEdgeList(g, g(w-2,h-2), 4);
kpeter@334
   247
kpeter@334
   248
  checkGraphConArcList(g, 2*(2*w*h-w-h));
kpeter@334
   249
  checkGraphConEdgeList(g, 2*w*h-w-h);
kpeter@334
   250
kpeter@334
   251
  checkArcDirections(g);
kpeter@334
   252
kpeter@334
   253
  checkNodeIds(g);
kpeter@334
   254
  checkArcIds(g);
kpeter@334
   255
  checkEdgeIds(g);
kpeter@334
   256
  checkGraphNodeMap(g);
kpeter@334
   257
  checkGraphArcMap(g);
kpeter@334
   258
  checkGraphEdgeMap(g);
kpeter@334
   259
}
deba@57
   260
deba@228
   261
void checkGraphs() {
kpeter@171
   262
  { // Checking ListGraph
kpeter@171
   263
    checkGraph<ListGraph>();
deba@228
   264
    checkGraphValidityErase<ListGraph>();
kpeter@171
   265
  }
kpeter@171
   266
  { // Checking SmartGraph
kpeter@171
   267
    checkGraph<SmartGraph>();
deba@228
   268
    checkGraphValidity<SmartGraph>();
kpeter@171
   269
  }
kpeter@171
   270
//   { // Checking FullGraph
kpeter@171
   271
//     FullGraph g(5);
kpeter@171
   272
//     checkGraphNodeList(g, 5);
kpeter@171
   273
//     checkGraphEdgeList(g, 10);
kpeter@171
   274
//   }
kpeter@334
   275
  { // Checking GridGraph
kpeter@334
   276
    GridGraph g(5, 6);
kpeter@334
   277
    checkGridGraph(g, 5, 6);
kpeter@334
   278
  }
kpeter@171
   279
}
kpeter@171
   280
deba@57
   281
int main() {
deba@228
   282
  checkConcepts();
deba@228
   283
  checkGraphs();
deba@57
   284
  return 0;
deba@57
   285
}