COIN-OR::LEMON - Graph Library

source: lemon-0.x/test/ugraph_test.cc @ 1970:bd88ea06ab69

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

Unified copyright notices

File size: 5.3 KB
Line 
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 */
18
19#include <lemon/bits/graph_extender.h>
20#include <lemon/concept/ugraph.h>
21#include <lemon/list_graph.h>
22#include <lemon/smart_graph.h>
23#include <lemon/full_graph.h>
24#include <lemon/grid_graph.h>
25
26#include <lemon/graph_utils.h>
27
28#include "test_tools.h"
29
30
31using namespace lemon;
32using namespace lemon::concept;
33
34void check_concepts() {
35  typedef UGraphExtender<ListGraphBase> ListUGraphBase;
36
37  typedef IterableUGraphExtender<
38    AlterableUGraphExtender<ListUGraphBase> > IterableListUGraph;
39
40  typedef MappableUGraphExtender<IterableListUGraph>
41    MappableListUGraph;
42
43  typedef ErasableUGraphExtender<
44    ClearableUGraphExtender<
45    ExtendableUGraphExtender<MappableListUGraph> > > Graph;
46
47  checkConcept<BaseIterableUGraphConcept, Graph>();
48  checkConcept<IterableUGraphConcept, Graph>();
49  checkConcept<MappableUGraphConcept, Graph>();
50
51  checkConcept<UGraph, Graph>();
52  checkConcept<ErasableUGraph, Graph>();
53
54  checkConcept<UGraph, ListUGraph>();
55  checkConcept<ErasableUGraph, ListUGraph>();
56
57  checkConcept<UGraph, SmartUGraph>();
58  checkConcept<ExtendableUGraph, SmartUGraph>();
59
60  checkConcept<UGraph, FullUGraph>();
61
62  checkConcept<UGraph, UGraph>();
63
64  checkConcept<UGraph, GridGraph>();
65}
66
67template <typename Graph>
68void check_item_counts(Graph &g, int n, int e) {
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;
86  for (typename Graph::UEdgeIt it(g); it != INVALID; ++it) {
87    ++uee;
88  }
89
90  check(uee == e, "Wrong uedge number.");
91  check(countUEdges(g) == e, "Wrong uedge number.");
92}
93
94template <typename Graph>
95void print_items(Graph &g) {
96
97  typedef typename Graph::NodeIt NodeIt;
98  typedef typename Graph::UEdgeIt UEdgeIt;
99  typedef typename Graph::EdgeIt EdgeIt;
100
101  std::cout << "Nodes" << std::endl;
102  int i=0;
103  for(NodeIt it(g); it!=INVALID; ++it, ++i) {
104    std::cout << "  " << i << ": " << g.id(it) << std::endl;
105  }
106
107  std::cout << "UEdge" << std::endl;
108  i=0;
109  for(UEdgeIt it(g); it!=INVALID; ++it, ++i) {
110    std::cout << "  " << i << ": " << g.id(it)
111         << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it))
112         << ")" << std::endl;
113  }
114
115  std::cout << "Edge" << std::endl;
116  i=0;
117  for(EdgeIt it(g); it!=INVALID; ++it, ++i) {
118    std::cout << "  " << i << ": " << g.id(it)
119         << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it))
120         << ")" << std::endl;
121  }
122
123}
124
125template <typename Graph>
126void check_graph() {
127
128  typedef typename Graph::Node Node;
129  typedef typename Graph::UEdge UEdge;
130  typedef typename Graph::Edge Edge;
131  typedef typename Graph::NodeIt NodeIt;
132  typedef typename Graph::UEdgeIt UEdgeIt;
133  typedef typename Graph::EdgeIt EdgeIt;
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);
151}
152
153void checkGridGraph(const GridGraph& g, int w, int h) {
154  check(g.width() == w, "Wrong width");
155  check(g.height() == h, "Wrong height");
156
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  }
195}
196
197int main() {
198  check_concepts();
199
200  check_graph<ListUGraph>();
201  check_graph<SmartUGraph>();
202
203  {
204    FullUGraph g(5);
205    check_item_counts(g, 5, 10);
206  }
207
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
216  return 0;
217}
Note: See TracBrowser for help on using the repository browser.