COIN-OR::LEMON - Graph Library

source: lemon-0.x/test/ugraph_test.cc @ 1978:ef2d00e46897

Last change on this file since 1978:ef2d00e46897 was 1956:a055123339d5, checked in by Alpar Juttner, 18 years ago

Unified copyright notices

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