test/ugraph_test.cc
author athos
Mon, 02 Oct 2006 14:41:53 +0000
changeset 2228 f71b0f9a7c3a
parent 2116 b6a68c15a6a3
child 2231 06faf3f06d67
permissions -rw-r--r--
Improved documentation.
alpar@1956
     1
/* -*- C++ -*-
alpar@1956
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1956
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@1956
     8
 *
alpar@1956
     9
 * Permission to use, modify and distribute this software is granted
alpar@1956
    10
 * provided that this copyright notice appears in all copies. For
alpar@1956
    11
 * precise terms see the accompanying LICENSE file.
alpar@1956
    12
 *
alpar@1956
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@1956
    14
 * express or implied, and with no claim as to its suitability for any
alpar@1956
    15
 * purpose.
alpar@1956
    16
 *
alpar@1956
    17
 */
klao@962
    18
klao@1795
    19
#include <lemon/bits/graph_extender.h>
klao@1909
    20
#include <lemon/concept/ugraph.h>
deba@2116
    21
#include <lemon/list_graph.h>
deba@2116
    22
#include <lemon/smart_graph.h>
deba@2116
    23
#include <lemon/full_graph.h>
deba@1979
    24
#include <lemon/grid_ugraph.h>
klao@962
    25
klao@1053
    26
#include <lemon/graph_utils.h>
klao@1053
    27
klao@962
    28
#include "test_tools.h"
klao@962
    29
klao@962
    30
klao@962
    31
using namespace lemon;
klao@962
    32
using namespace lemon::concept;
klao@962
    33
klao@1053
    34
void check_concepts() {
klao@1034
    35
deba@2121
    36
  { // checking graph components
deba@2121
    37
    checkConcept<BaseUGraphComponent, BaseUGraphComponent >();
klao@1034
    38
deba@2121
    39
    checkConcept<BaseIterableUGraphComponent<>, 
deba@2121
    40
      BaseIterableUGraphComponent<> >();
deba@1568
    41
deba@2121
    42
    checkConcept<IDableUGraphComponent<>, 
deba@2121
    43
      IDableUGraphComponent<> >();
deba@1680
    44
deba@2121
    45
    checkConcept<IterableUGraphComponent<>, 
deba@2121
    46
      IterableUGraphComponent<> >();
deba@2121
    47
deba@2121
    48
    checkConcept<MappableUGraphComponent<>, 
deba@2121
    49
      MappableUGraphComponent<> >();
deba@2121
    50
deba@2121
    51
  }
deba@2121
    52
  {
deba@2121
    53
    checkConcept<UGraph, ListUGraph>();
deba@2121
    54
    
deba@2121
    55
    checkConcept<UGraph, SmartUGraph>();
deba@2121
    56
    
deba@2121
    57
    checkConcept<UGraph, FullUGraph>();
deba@2121
    58
    
deba@2121
    59
    checkConcept<UGraph, UGraph>();
deba@2121
    60
    
deba@2121
    61
    checkConcept<UGraph, GridUGraph>();
deba@2121
    62
  }
klao@1053
    63
}
klao@1053
    64
klao@1054
    65
template <typename Graph>
klao@1053
    66
void check_item_counts(Graph &g, int n, int e) {
deba@1680
    67
  int nn = 0;
deba@1680
    68
  for (typename Graph::NodeIt it(g); it != INVALID; ++it) {
deba@1680
    69
    ++nn;
deba@1680
    70
  }
deba@1680
    71
deba@1680
    72
  check(nn == n, "Wrong node number.");
deba@1680
    73
  check(countNodes(g) == n, "Wrong node number.");
deba@1680
    74
deba@1680
    75
  int ee = 0;
deba@1680
    76
  for (typename Graph::EdgeIt it(g); it != INVALID; ++it) {
deba@1680
    77
    ++ee;
deba@1680
    78
  }
deba@1680
    79
deba@1680
    80
  check(ee == 2*e, "Wrong edge number.");
deba@1680
    81
  check(countEdges(g) == 2*e, "Wrong edge number.");
deba@1680
    82
deba@1680
    83
  int uee = 0;
klao@1909
    84
  for (typename Graph::UEdgeIt it(g); it != INVALID; ++it) {
deba@1680
    85
    ++uee;
deba@1680
    86
  }
deba@1680
    87
klao@1909
    88
  check(uee == e, "Wrong uedge number.");
klao@1909
    89
  check(countUEdges(g) == e, "Wrong uedge number.");
klao@1053
    90
}
klao@1053
    91
klao@1054
    92
template <typename Graph>
klao@1053
    93
void print_items(Graph &g) {
klao@1054
    94
klao@1054
    95
  typedef typename Graph::NodeIt NodeIt;
klao@1909
    96
  typedef typename Graph::UEdgeIt UEdgeIt;
klao@1054
    97
  typedef typename Graph::EdgeIt EdgeIt;
klao@1054
    98
alpar@1367
    99
  std::cout << "Nodes" << std::endl;
klao@1053
   100
  int i=0;
klao@1053
   101
  for(NodeIt it(g); it!=INVALID; ++it, ++i) {
alpar@1367
   102
    std::cout << "  " << i << ": " << g.id(it) << std::endl;
klao@1053
   103
  }
klao@1053
   104
klao@1909
   105
  std::cout << "UEdge" << std::endl;
klao@1053
   106
  i=0;
klao@1053
   107
  for(UEdgeIt it(g); it!=INVALID; ++it, ++i) {
alpar@1367
   108
    std::cout << "  " << i << ": " << g.id(it) 
klao@1053
   109
	 << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it)) 
alpar@1367
   110
	 << ")" << std::endl;
klao@1053
   111
  }
klao@1053
   112
alpar@1367
   113
  std::cout << "Edge" << std::endl;
klao@1053
   114
  i=0;
klao@1053
   115
  for(EdgeIt it(g); it!=INVALID; ++it, ++i) {
alpar@1367
   116
    std::cout << "  " << i << ": " << g.id(it)
klao@1053
   117
	 << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it)) 
alpar@1367
   118
	 << ")" << std::endl;
klao@1053
   119
  }
klao@1053
   120
klao@1053
   121
}
klao@1053
   122
klao@1054
   123
template <typename Graph>
klao@1054
   124
void check_graph() {
klao@1053
   125
klao@1054
   126
  typedef typename Graph::Node Node;
klao@1909
   127
  typedef typename Graph::UEdge UEdge;
klao@1054
   128
  typedef typename Graph::Edge Edge;
klao@1054
   129
  typedef typename Graph::NodeIt NodeIt;
klao@1909
   130
  typedef typename Graph::UEdgeIt UEdgeIt;
klao@1054
   131
  typedef typename Graph::EdgeIt EdgeIt;
klao@1053
   132
klao@1053
   133
  Graph g;
klao@1053
   134
klao@1053
   135
  check_item_counts(g,0,0);
klao@1053
   136
klao@1053
   137
  Node
klao@1053
   138
    n1 = g.addNode(),
klao@1053
   139
    n2 = g.addNode(),
klao@1053
   140
    n3 = g.addNode();
klao@1053
   141
klao@1053
   142
  UEdge
klao@1053
   143
    e1 = g.addEdge(n1, n2),
klao@1053
   144
    e2 = g.addEdge(n2, n3);
klao@1053
   145
klao@1053
   146
  // print_items(g);
klao@1053
   147
klao@1053
   148
  check_item_counts(g,3,2);
deba@1680
   149
}
klao@1030
   150
deba@1979
   151
void checkGridUGraph(const GridUGraph& g, int w, int h) {
deba@1680
   152
  check(g.width() == w, "Wrong width");
deba@1680
   153
  check(g.height() == h, "Wrong height");
klao@1054
   154
deba@1680
   155
  for (int i = 0; i < w; ++i) {
deba@1680
   156
    for (int j = 0; j < h; ++j) {
deba@1680
   157
      check(g.col(g(i, j)) == i, "Wrong col");
deba@1680
   158
      check(g.row(g(i, j)) == j, "Wrong row");
deba@1680
   159
    }
deba@1680
   160
  }
deba@1680
   161
  
deba@1680
   162
  for (int i = 0; i < w; ++i) {
deba@1680
   163
    for (int j = 0; j < h - 1; ++j) {
deba@1680
   164
      check(g.source(g.down(g(i, j))) == g(i, j), "Wrong down");
deba@1680
   165
      check(g.target(g.down(g(i, j))) == g(i, j + 1), "Wrong down");
deba@1680
   166
    }
deba@1680
   167
    check(g.down(g(i, h - 1)) == INVALID, "Wrong down");
deba@1680
   168
  }
deba@1680
   169
deba@1680
   170
  for (int i = 0; i < w; ++i) {
deba@1680
   171
    for (int j = 1; j < h; ++j) {
deba@1680
   172
      check(g.source(g.up(g(i, j))) == g(i, j), "Wrong up");
deba@1680
   173
      check(g.target(g.up(g(i, j))) == g(i, j - 1), "Wrong up");
deba@1680
   174
    }
deba@1680
   175
    check(g.up(g(i, 0)) == INVALID, "Wrong up");
deba@1680
   176
  }
deba@1680
   177
deba@1680
   178
  for (int j = 0; j < h; ++j) {
deba@1680
   179
    for (int i = 0; i < w - 1; ++i) {
deba@1680
   180
      check(g.source(g.right(g(i, j))) == g(i, j), "Wrong right");
deba@1680
   181
      check(g.target(g.right(g(i, j))) == g(i + 1, j), "Wrong right");      
deba@1680
   182
    }
deba@1680
   183
    check(g.right(g(w - 1, j)) == INVALID, "Wrong right");    
deba@1680
   184
  }
deba@1680
   185
deba@1680
   186
  for (int j = 0; j < h; ++j) {
deba@1680
   187
    for (int i = 1; i < w; ++i) {
deba@1680
   188
      check(g.source(g.left(g(i, j))) == g(i, j), "Wrong left");
deba@1680
   189
      check(g.target(g.left(g(i, j))) == g(i - 1, j), "Wrong left");      
deba@1680
   190
    }
deba@1680
   191
    check(g.left(g(0, j)) == INVALID, "Wrong left");    
deba@1680
   192
  }
klao@1054
   193
}
klao@1054
   194
klao@1054
   195
int main() {
klao@1054
   196
  check_concepts();
klao@1054
   197
klao@1909
   198
  check_graph<ListUGraph>();
klao@1909
   199
  check_graph<SmartUGraph>();
klao@1054
   200
deba@1568
   201
  {
klao@1909
   202
    FullUGraph g(5);
deba@1568
   203
    check_item_counts(g, 5, 10);
deba@1568
   204
  }
deba@1568
   205
deba@1680
   206
  {
deba@1979
   207
    GridUGraph g(5, 6);
deba@1680
   208
    check_item_counts(g, 30, 49);
deba@1979
   209
    checkGridUGraph(g, 5, 6);
deba@1680
   210
  }
deba@1680
   211
deba@1680
   212
  std::cout << __FILE__ ": All tests passed.\n";
deba@1680
   213
klao@962
   214
  return 0;
klao@962
   215
}