test/graph_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Mon, 26 May 2008 13:31:41 +0200
changeset 160 b1bd0c2a7f57
parent 109 abddaa08b507
child 171 02f4d5d9bfd7
permissions -rw-r--r--
Improved documentation for counters
deba@57
     1
/* -*- C++ -*-
deba@57
     2
 *
deba@57
     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@149
    25
#include <lemon/graph_utils.h>
deba@57
    26
deba@57
    27
#include "test_tools.h"
deba@57
    28
deba@57
    29
deba@57
    30
using namespace lemon;
deba@57
    31
using namespace lemon::concepts;
deba@57
    32
deba@57
    33
void check_concepts() {
deba@57
    34
deba@57
    35
  { // checking digraph components
deba@57
    36
    checkConcept<BaseGraphComponent, BaseGraphComponent >();
deba@57
    37
deba@57
    38
    checkConcept<IDableGraphComponent<>, 
deba@57
    39
      IDableGraphComponent<> >();
deba@57
    40
deba@57
    41
    checkConcept<IterableGraphComponent<>, 
deba@57
    42
      IterableGraphComponent<> >();
deba@57
    43
deba@57
    44
    checkConcept<MappableGraphComponent<>, 
deba@57
    45
      MappableGraphComponent<> >();
deba@57
    46
deba@57
    47
  }
deba@57
    48
  {
deba@57
    49
    checkConcept<Graph, ListGraph>();    
deba@109
    50
    checkConcept<Graph, SmartGraph>();    
deba@57
    51
//     checkConcept<Graph, FullGraph>();    
deba@57
    52
//     checkConcept<Graph, Graph>();    
deba@57
    53
//     checkConcept<Graph, GridGraph>();
deba@57
    54
  }
deba@57
    55
}
deba@57
    56
deba@57
    57
template <typename Graph>
deba@57
    58
void check_item_counts(Graph &g, int n, int e) {
deba@57
    59
  int nn = 0;
deba@57
    60
  for (typename Graph::NodeIt it(g); it != INVALID; ++it) {
deba@57
    61
    ++nn;
deba@57
    62
  }
deba@57
    63
deba@57
    64
  check(nn == n, "Wrong node number.");
deba@57
    65
  //  check(countNodes(g) == n, "Wrong node number.");
deba@57
    66
deba@57
    67
  int ee = 0;
deba@57
    68
  for (typename Graph::ArcIt it(g); it != INVALID; ++it) {
deba@57
    69
    ++ee;
deba@57
    70
  }
deba@57
    71
deba@57
    72
  check(ee == 2*e, "Wrong arc number.");
deba@57
    73
  //  check(countArcs(g) == 2*e, "Wrong arc number.");
deba@57
    74
deba@57
    75
  int uee = 0;
deba@57
    76
  for (typename Graph::EdgeIt it(g); it != INVALID; ++it) {
deba@57
    77
    ++uee;
deba@57
    78
  }
deba@57
    79
deba@57
    80
  check(uee == e, "Wrong edge number.");
deba@57
    81
  //  check(countEdges(g) == e, "Wrong edge number.");
deba@57
    82
}
deba@57
    83
deba@57
    84
template <typename Graph>
deba@149
    85
void check_graph_counts() {
deba@57
    86
deba@149
    87
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
deba@57
    88
  Graph g;
deba@57
    89
deba@57
    90
  check_item_counts(g,0,0);
deba@57
    91
deba@57
    92
  Node
deba@57
    93
    n1 = g.addNode(),
deba@57
    94
    n2 = g.addNode(),
deba@57
    95
    n3 = g.addNode();
deba@57
    96
deba@57
    97
  Edge
deba@57
    98
    e1 = g.addEdge(n1, n2),
deba@57
    99
    e2 = g.addEdge(n2, n3);
deba@57
   100
deba@57
   101
  check_item_counts(g,3,2);
deba@57
   102
}
deba@57
   103
deba@149
   104
template <typename Graph>
deba@149
   105
void check_graph_validity() {
deba@149
   106
deba@149
   107
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
deba@149
   108
  Graph g;
deba@149
   109
deba@149
   110
  check_item_counts(g,0,0);
deba@149
   111
deba@149
   112
  Node
deba@149
   113
    n1 = g.addNode(),
deba@149
   114
    n2 = g.addNode(),
deba@149
   115
    n3 = g.addNode();
deba@149
   116
deba@149
   117
  Edge
deba@149
   118
    e1 = g.addEdge(n1, n2),
deba@149
   119
    e2 = g.addEdge(n2, n3);
deba@149
   120
deba@149
   121
  check(g.valid(n1), "Validity check");
deba@149
   122
  check(g.valid(e1), "Validity check");
deba@149
   123
  check(g.valid(g.direct(e1, true)), "Validity check");
deba@149
   124
deba@149
   125
  check(!g.valid(g.nodeFromId(-1)), "Validity check");
deba@149
   126
  check(!g.valid(g.edgeFromId(-1)), "Validity check");
deba@149
   127
  check(!g.valid(g.arcFromId(-1)), "Validity check");
deba@149
   128
    
deba@149
   129
}
deba@149
   130
deba@149
   131
template <typename Graph>
deba@149
   132
void check_graph_validity_erase() {
deba@149
   133
deba@149
   134
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
deba@149
   135
  Graph g;
deba@149
   136
deba@149
   137
  check_item_counts(g,0,0);
deba@149
   138
deba@149
   139
  Node
deba@149
   140
    n1 = g.addNode(),
deba@149
   141
    n2 = g.addNode(),
deba@149
   142
    n3 = g.addNode();
deba@149
   143
deba@149
   144
  Edge
deba@149
   145
    e1 = g.addEdge(n1, n2),
deba@149
   146
    e2 = g.addEdge(n2, n3);
deba@149
   147
deba@149
   148
  check(g.valid(n1), "Validity check");
deba@149
   149
  check(g.valid(e1), "Validity check");
deba@149
   150
  check(g.valid(g.direct(e1, true)), "Validity check");
deba@149
   151
deba@149
   152
  g.erase(n1);
deba@149
   153
deba@149
   154
  check(!g.valid(n1), "Validity check");
deba@149
   155
  check(g.valid(n2), "Validity check");
deba@149
   156
  check(g.valid(n3), "Validity check");
deba@149
   157
  check(!g.valid(e1), "Validity check");
deba@149
   158
  check(g.valid(e2), "Validity check");
deba@149
   159
deba@149
   160
  check(!g.valid(g.nodeFromId(-1)), "Validity check");
deba@149
   161
  check(!g.valid(g.edgeFromId(-1)), "Validity check");
deba@149
   162
  check(!g.valid(g.arcFromId(-1)), "Validity check");
deba@149
   163
    
deba@149
   164
}
deba@149
   165
deba@149
   166
deba@149
   167
deba@57
   168
// void checkGridGraph(const GridGraph& g, int w, int h) {
deba@57
   169
//   check(g.width() == w, "Wrong width");
deba@57
   170
//   check(g.height() == h, "Wrong height");
deba@57
   171
deba@57
   172
//   for (int i = 0; i < w; ++i) {
deba@57
   173
//     for (int j = 0; j < h; ++j) {
deba@57
   174
//       check(g.col(g(i, j)) == i, "Wrong col");
deba@57
   175
//       check(g.row(g(i, j)) == j, "Wrong row");
deba@57
   176
//     }
deba@57
   177
//   }
deba@57
   178
  
deba@57
   179
//   for (int i = 0; i < w; ++i) {
deba@57
   180
//     for (int j = 0; j < h - 1; ++j) {
deba@57
   181
//       check(g.source(g.down(g(i, j))) == g(i, j), "Wrong down");
deba@57
   182
//       check(g.target(g.down(g(i, j))) == g(i, j + 1), "Wrong down");
deba@57
   183
//     }
deba@57
   184
//     check(g.down(g(i, h - 1)) == INVALID, "Wrong down");
deba@57
   185
//   }
deba@57
   186
deba@57
   187
//   for (int i = 0; i < w; ++i) {
deba@57
   188
//     for (int j = 1; j < h; ++j) {
deba@57
   189
//       check(g.source(g.up(g(i, j))) == g(i, j), "Wrong up");
deba@57
   190
//       check(g.target(g.up(g(i, j))) == g(i, j - 1), "Wrong up");
deba@57
   191
//     }
deba@57
   192
//     check(g.up(g(i, 0)) == INVALID, "Wrong up");
deba@57
   193
//   }
deba@57
   194
deba@57
   195
//   for (int j = 0; j < h; ++j) {
deba@57
   196
//     for (int i = 0; i < w - 1; ++i) {
deba@57
   197
//       check(g.source(g.right(g(i, j))) == g(i, j), "Wrong right");
deba@57
   198
//       check(g.target(g.right(g(i, j))) == g(i + 1, j), "Wrong right");      
deba@57
   199
//     }
deba@57
   200
//     check(g.right(g(w - 1, j)) == INVALID, "Wrong right");    
deba@57
   201
//   }
deba@57
   202
deba@57
   203
//   for (int j = 0; j < h; ++j) {
deba@57
   204
//     for (int i = 1; i < w; ++i) {
deba@57
   205
//       check(g.source(g.left(g(i, j))) == g(i, j), "Wrong left");
deba@57
   206
//       check(g.target(g.left(g(i, j))) == g(i - 1, j), "Wrong left");      
deba@57
   207
//     }
deba@57
   208
//     check(g.left(g(0, j)) == INVALID, "Wrong left");    
deba@57
   209
//   }
deba@57
   210
// }
deba@57
   211
deba@57
   212
int main() {
deba@57
   213
  check_concepts();
deba@57
   214
deba@149
   215
  check_graph_counts<ListGraph>();
deba@149
   216
  check_graph_counts<SmartGraph>();
deba@149
   217
deba@149
   218
  check_graph_validity_erase<ListGraph>();
deba@149
   219
  check_graph_validity<SmartGraph>();
deba@57
   220
deba@57
   221
//   {
deba@57
   222
//     FullGraph g(5);
deba@57
   223
//     check_item_counts(g, 5, 10);
deba@57
   224
//   }
deba@57
   225
deba@57
   226
//   {
deba@57
   227
//     GridGraph g(5, 6);
deba@57
   228
//     check_item_counts(g, 30, 49);
deba@57
   229
//     checkGridGraph(g, 5, 6);
deba@57
   230
//   }
deba@57
   231
deba@57
   232
  std::cout << __FILE__ ": All tests passed.\n";
deba@57
   233
deba@57
   234
  return 0;
deba@57
   235
}