test/ugraph_test.cc
author deba
Fri, 24 Feb 2006 11:13:09 +0000
changeset 1984 d4cbd10e1256
parent 1956 a055123339d5
child 2111 ea1fa1bc3f6d
permissions -rw-r--r--
Bug fix
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>
klao@962
    21
#include <lemon/list_graph.h>
klao@962
    22
#include <lemon/smart_graph.h>
klao@962
    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@1909
    35
  checkConcept<UGraph, ListUGraph>();
klao@1909
    36
  checkConcept<ErasableUGraph, ListUGraph>();
klao@1034
    37
klao@1909
    38
  checkConcept<UGraph, SmartUGraph>();
klao@1909
    39
  checkConcept<ExtendableUGraph, SmartUGraph>();
klao@1034
    40
klao@1909
    41
  checkConcept<UGraph, FullUGraph>();
deba@1568
    42
klao@1909
    43
  checkConcept<UGraph, UGraph>();
deba@1680
    44
deba@1979
    45
  checkConcept<UGraph, GridUGraph>();
klao@1053
    46
}
klao@1053
    47
klao@1054
    48
template <typename Graph>
klao@1053
    49
void check_item_counts(Graph &g, int n, int e) {
deba@1680
    50
  int nn = 0;
deba@1680
    51
  for (typename Graph::NodeIt it(g); it != INVALID; ++it) {
deba@1680
    52
    ++nn;
deba@1680
    53
  }
deba@1680
    54
deba@1680
    55
  check(nn == n, "Wrong node number.");
deba@1680
    56
  check(countNodes(g) == n, "Wrong node number.");
deba@1680
    57
deba@1680
    58
  int ee = 0;
deba@1680
    59
  for (typename Graph::EdgeIt it(g); it != INVALID; ++it) {
deba@1680
    60
    ++ee;
deba@1680
    61
  }
deba@1680
    62
deba@1680
    63
  check(ee == 2*e, "Wrong edge number.");
deba@1680
    64
  check(countEdges(g) == 2*e, "Wrong edge number.");
deba@1680
    65
deba@1680
    66
  int uee = 0;
klao@1909
    67
  for (typename Graph::UEdgeIt it(g); it != INVALID; ++it) {
deba@1680
    68
    ++uee;
deba@1680
    69
  }
deba@1680
    70
klao@1909
    71
  check(uee == e, "Wrong uedge number.");
klao@1909
    72
  check(countUEdges(g) == e, "Wrong uedge number.");
klao@1053
    73
}
klao@1053
    74
klao@1054
    75
template <typename Graph>
klao@1053
    76
void print_items(Graph &g) {
klao@1054
    77
klao@1054
    78
  typedef typename Graph::NodeIt NodeIt;
klao@1909
    79
  typedef typename Graph::UEdgeIt UEdgeIt;
klao@1054
    80
  typedef typename Graph::EdgeIt EdgeIt;
klao@1054
    81
alpar@1367
    82
  std::cout << "Nodes" << std::endl;
klao@1053
    83
  int i=0;
klao@1053
    84
  for(NodeIt it(g); it!=INVALID; ++it, ++i) {
alpar@1367
    85
    std::cout << "  " << i << ": " << g.id(it) << std::endl;
klao@1053
    86
  }
klao@1053
    87
klao@1909
    88
  std::cout << "UEdge" << std::endl;
klao@1053
    89
  i=0;
klao@1053
    90
  for(UEdgeIt it(g); it!=INVALID; ++it, ++i) {
alpar@1367
    91
    std::cout << "  " << i << ": " << g.id(it) 
klao@1053
    92
	 << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it)) 
alpar@1367
    93
	 << ")" << std::endl;
klao@1053
    94
  }
klao@1053
    95
alpar@1367
    96
  std::cout << "Edge" << std::endl;
klao@1053
    97
  i=0;
klao@1053
    98
  for(EdgeIt it(g); it!=INVALID; ++it, ++i) {
alpar@1367
    99
    std::cout << "  " << i << ": " << g.id(it)
klao@1053
   100
	 << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it)) 
alpar@1367
   101
	 << ")" << std::endl;
klao@1053
   102
  }
klao@1053
   103
klao@1053
   104
}
klao@1053
   105
klao@1054
   106
template <typename Graph>
klao@1054
   107
void check_graph() {
klao@1053
   108
klao@1054
   109
  typedef typename Graph::Node Node;
klao@1909
   110
  typedef typename Graph::UEdge UEdge;
klao@1054
   111
  typedef typename Graph::Edge Edge;
klao@1054
   112
  typedef typename Graph::NodeIt NodeIt;
klao@1909
   113
  typedef typename Graph::UEdgeIt UEdgeIt;
klao@1054
   114
  typedef typename Graph::EdgeIt EdgeIt;
klao@1053
   115
klao@1053
   116
  Graph g;
klao@1053
   117
klao@1053
   118
  check_item_counts(g,0,0);
klao@1053
   119
klao@1053
   120
  Node
klao@1053
   121
    n1 = g.addNode(),
klao@1053
   122
    n2 = g.addNode(),
klao@1053
   123
    n3 = g.addNode();
klao@1053
   124
klao@1053
   125
  UEdge
klao@1053
   126
    e1 = g.addEdge(n1, n2),
klao@1053
   127
    e2 = g.addEdge(n2, n3);
klao@1053
   128
klao@1053
   129
  // print_items(g);
klao@1053
   130
klao@1053
   131
  check_item_counts(g,3,2);
deba@1680
   132
}
klao@1030
   133
deba@1979
   134
void checkGridUGraph(const GridUGraph& g, int w, int h) {
deba@1680
   135
  check(g.width() == w, "Wrong width");
deba@1680
   136
  check(g.height() == h, "Wrong height");
klao@1054
   137
deba@1680
   138
  for (int i = 0; i < w; ++i) {
deba@1680
   139
    for (int j = 0; j < h; ++j) {
deba@1680
   140
      check(g.col(g(i, j)) == i, "Wrong col");
deba@1680
   141
      check(g.row(g(i, j)) == j, "Wrong row");
deba@1680
   142
    }
deba@1680
   143
  }
deba@1680
   144
  
deba@1680
   145
  for (int i = 0; i < w; ++i) {
deba@1680
   146
    for (int j = 0; j < h - 1; ++j) {
deba@1680
   147
      check(g.source(g.down(g(i, j))) == g(i, j), "Wrong down");
deba@1680
   148
      check(g.target(g.down(g(i, j))) == g(i, j + 1), "Wrong down");
deba@1680
   149
    }
deba@1680
   150
    check(g.down(g(i, h - 1)) == INVALID, "Wrong down");
deba@1680
   151
  }
deba@1680
   152
deba@1680
   153
  for (int i = 0; i < w; ++i) {
deba@1680
   154
    for (int j = 1; j < h; ++j) {
deba@1680
   155
      check(g.source(g.up(g(i, j))) == g(i, j), "Wrong up");
deba@1680
   156
      check(g.target(g.up(g(i, j))) == g(i, j - 1), "Wrong up");
deba@1680
   157
    }
deba@1680
   158
    check(g.up(g(i, 0)) == INVALID, "Wrong up");
deba@1680
   159
  }
deba@1680
   160
deba@1680
   161
  for (int j = 0; j < h; ++j) {
deba@1680
   162
    for (int i = 0; i < w - 1; ++i) {
deba@1680
   163
      check(g.source(g.right(g(i, j))) == g(i, j), "Wrong right");
deba@1680
   164
      check(g.target(g.right(g(i, j))) == g(i + 1, j), "Wrong right");      
deba@1680
   165
    }
deba@1680
   166
    check(g.right(g(w - 1, j)) == INVALID, "Wrong right");    
deba@1680
   167
  }
deba@1680
   168
deba@1680
   169
  for (int j = 0; j < h; ++j) {
deba@1680
   170
    for (int i = 1; i < w; ++i) {
deba@1680
   171
      check(g.source(g.left(g(i, j))) == g(i, j), "Wrong left");
deba@1680
   172
      check(g.target(g.left(g(i, j))) == g(i - 1, j), "Wrong left");      
deba@1680
   173
    }
deba@1680
   174
    check(g.left(g(0, j)) == INVALID, "Wrong left");    
deba@1680
   175
  }
klao@1054
   176
}
klao@1054
   177
klao@1054
   178
int main() {
klao@1054
   179
  check_concepts();
klao@1054
   180
klao@1909
   181
  check_graph<ListUGraph>();
klao@1909
   182
  check_graph<SmartUGraph>();
klao@1054
   183
deba@1568
   184
  {
klao@1909
   185
    FullUGraph g(5);
deba@1568
   186
    check_item_counts(g, 5, 10);
deba@1568
   187
  }
deba@1568
   188
deba@1680
   189
  {
deba@1979
   190
    GridUGraph g(5, 6);
deba@1680
   191
    check_item_counts(g, 30, 49);
deba@1979
   192
    checkGridUGraph(g, 5, 6);
deba@1680
   193
  }
deba@1680
   194
deba@1680
   195
  std::cout << __FILE__ ": All tests passed.\n";
deba@1680
   196
klao@962
   197
  return 0;
klao@962
   198
}