test/graph_test.cc
author Akos Ladanyi <ladanyi@tmit.bme.hu>
Sun, 13 Apr 2008 13:22:52 +0200
changeset 141 96f81c791f0c
parent 107 31a2e6d28f61
child 149 2f7ae34e1333
permissions -rw-r--r--
CMake based build system
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@57
    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@57
    85
void print_items(Graph &g) {
deba@57
    86
deba@57
    87
  typedef typename Graph::NodeIt NodeIt;
deba@57
    88
  typedef typename Graph::EdgeIt EdgeIt;
deba@57
    89
  typedef typename Graph::ArcIt ArcIt;
deba@57
    90
deba@57
    91
  std::cout << "Nodes" << std::endl;
deba@57
    92
  int i=0;
deba@57
    93
  for(NodeIt it(g); it!=INVALID; ++it, ++i) {
deba@57
    94
    std::cout << "  " << i << ": " << g.id(it) << std::endl;
deba@57
    95
  }
deba@57
    96
deba@57
    97
  std::cout << "Edge" << std::endl;
deba@57
    98
  i=0;
deba@57
    99
  for(EdgeIt it(g); it!=INVALID; ++it, ++i) {
deba@57
   100
    std::cout << "  " << i << ": " << g.id(it) 
deba@57
   101
	 << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it)) 
deba@57
   102
	 << ")" << std::endl;
deba@57
   103
  }
deba@57
   104
deba@57
   105
  std::cout << "Arc" << std::endl;
deba@57
   106
  i=0;
deba@57
   107
  for(ArcIt it(g); it!=INVALID; ++it, ++i) {
deba@57
   108
    std::cout << "  " << i << ": " << g.id(it)
deba@57
   109
	 << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it)) 
deba@57
   110
	 << ")" << std::endl;
deba@57
   111
  }
deba@57
   112
deba@57
   113
}
deba@57
   114
deba@57
   115
template <typename Graph>
deba@57
   116
void check_graph() {
deba@57
   117
deba@57
   118
  typedef typename Graph::Node Node;
deba@57
   119
  typedef typename Graph::Edge Edge;
deba@57
   120
  typedef typename Graph::Arc Arc;
deba@57
   121
  typedef typename Graph::NodeIt NodeIt;
deba@57
   122
  typedef typename Graph::EdgeIt EdgeIt;
deba@57
   123
  typedef typename Graph::ArcIt ArcIt;
deba@57
   124
deba@57
   125
  Graph g;
deba@57
   126
deba@57
   127
  check_item_counts(g,0,0);
deba@57
   128
deba@57
   129
  Node
deba@57
   130
    n1 = g.addNode(),
deba@57
   131
    n2 = g.addNode(),
deba@57
   132
    n3 = g.addNode();
deba@57
   133
deba@57
   134
  Edge
deba@57
   135
    e1 = g.addEdge(n1, n2),
deba@57
   136
    e2 = g.addEdge(n2, n3);
deba@57
   137
deba@57
   138
  // print_items(g);
deba@57
   139
deba@57
   140
  check_item_counts(g,3,2);
deba@57
   141
}
deba@57
   142
deba@57
   143
// void checkGridGraph(const GridGraph& g, int w, int h) {
deba@57
   144
//   check(g.width() == w, "Wrong width");
deba@57
   145
//   check(g.height() == h, "Wrong height");
deba@57
   146
deba@57
   147
//   for (int i = 0; i < w; ++i) {
deba@57
   148
//     for (int j = 0; j < h; ++j) {
deba@57
   149
//       check(g.col(g(i, j)) == i, "Wrong col");
deba@57
   150
//       check(g.row(g(i, j)) == j, "Wrong row");
deba@57
   151
//     }
deba@57
   152
//   }
deba@57
   153
  
deba@57
   154
//   for (int i = 0; i < w; ++i) {
deba@57
   155
//     for (int j = 0; j < h - 1; ++j) {
deba@57
   156
//       check(g.source(g.down(g(i, j))) == g(i, j), "Wrong down");
deba@57
   157
//       check(g.target(g.down(g(i, j))) == g(i, j + 1), "Wrong down");
deba@57
   158
//     }
deba@57
   159
//     check(g.down(g(i, h - 1)) == INVALID, "Wrong down");
deba@57
   160
//   }
deba@57
   161
deba@57
   162
//   for (int i = 0; i < w; ++i) {
deba@57
   163
//     for (int j = 1; j < h; ++j) {
deba@57
   164
//       check(g.source(g.up(g(i, j))) == g(i, j), "Wrong up");
deba@57
   165
//       check(g.target(g.up(g(i, j))) == g(i, j - 1), "Wrong up");
deba@57
   166
//     }
deba@57
   167
//     check(g.up(g(i, 0)) == INVALID, "Wrong up");
deba@57
   168
//   }
deba@57
   169
deba@57
   170
//   for (int j = 0; j < h; ++j) {
deba@57
   171
//     for (int i = 0; i < w - 1; ++i) {
deba@57
   172
//       check(g.source(g.right(g(i, j))) == g(i, j), "Wrong right");
deba@57
   173
//       check(g.target(g.right(g(i, j))) == g(i + 1, j), "Wrong right");      
deba@57
   174
//     }
deba@57
   175
//     check(g.right(g(w - 1, j)) == INVALID, "Wrong right");    
deba@57
   176
//   }
deba@57
   177
deba@57
   178
//   for (int j = 0; j < h; ++j) {
deba@57
   179
//     for (int i = 1; i < w; ++i) {
deba@57
   180
//       check(g.source(g.left(g(i, j))) == g(i, j), "Wrong left");
deba@57
   181
//       check(g.target(g.left(g(i, j))) == g(i - 1, j), "Wrong left");      
deba@57
   182
//     }
deba@57
   183
//     check(g.left(g(0, j)) == INVALID, "Wrong left");    
deba@57
   184
//   }
deba@57
   185
// }
deba@57
   186
deba@57
   187
int main() {
deba@57
   188
  check_concepts();
deba@57
   189
deba@57
   190
  check_graph<ListGraph>();
deba@109
   191
  check_graph<SmartGraph>();
deba@57
   192
deba@57
   193
//   {
deba@57
   194
//     FullGraph g(5);
deba@57
   195
//     check_item_counts(g, 5, 10);
deba@57
   196
//   }
deba@57
   197
deba@57
   198
//   {
deba@57
   199
//     GridGraph g(5, 6);
deba@57
   200
//     check_item_counts(g, 30, 49);
deba@57
   201
//     checkGridGraph(g, 5, 6);
deba@57
   202
//   }
deba@57
   203
deba@57
   204
  std::cout << __FILE__ ": All tests passed.\n";
deba@57
   205
deba@57
   206
  return 0;
deba@57
   207
}