COIN-OR::LEMON - Graph Library

source: lemon-main/test/graph_test.cc @ 297:92b193385702

Last change on this file since 297:92b193385702 was 228:b6732e0d38c5, checked in by Balazs Dezso <deba@…>, 16 years ago

Reworking graph testing

  • The graph tests check more graph functionality.
  • The petersen graph is too regular, therefore special graphs are used.
  • The graph_test.h contains just general tools to test graphs.
File size: 6.9 KB
RevLine 
[209]1/* -*- mode: C++; indent-tabs-mode: nil; -*-
[57]2 *
[209]3 * This file is a part of LEMON, a generic C++ optimization library.
[57]4 *
[107]5 * Copyright (C) 2003-2008
[57]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/concepts/graph.h>
20#include <lemon/list_graph.h>
[109]21#include <lemon/smart_graph.h>
[57]22// #include <lemon/full_graph.h>
23// #include <lemon/grid_graph.h>
24
25#include "test_tools.h"
[171]26#include "graph_test.h"
[57]27
28using namespace lemon;
29using namespace lemon::concepts;
30
[228]31template <class Graph>
32void checkGraph() {
33  TEMPLATE_GRAPH_TYPEDEFS(Graph);
34
35  Graph G;
36  checkGraphNodeList(G, 0);
37  checkGraphEdgeList(G, 0);
38
39  Node
40    n1 = G.addNode(),
41    n2 = G.addNode(),
42    n3 = G.addNode();
43  checkGraphNodeList(G, 3);
44  checkGraphEdgeList(G, 0);
45
46  Edge e1 = G.addEdge(n1, n2);
47  check((G.u(e1) == n1 && G.v(e1) == n2) || (G.u(e1) == n2 && G.v(e1) == n1),
48        "Wrong edge");
49  checkGraphNodeList(G, 3);
50  checkGraphArcList(G, 2);
51  checkGraphEdgeList(G, 1);
52
53  checkGraphOutArcList(G, n1, 1);
54  checkGraphOutArcList(G, n2, 1);
55  checkGraphOutArcList(G, n3, 0);
56
57  checkGraphInArcList(G, n1, 1);
58  checkGraphInArcList(G, n2, 1);
59  checkGraphInArcList(G, n3, 0);
60
61  checkGraphIncEdgeList(G, n1, 1);
62  checkGraphIncEdgeList(G, n2, 1);
63  checkGraphIncEdgeList(G, n3, 0);
64
65  checkGraphConArcList(G, 2);
66  checkGraphConEdgeList(G, 1);
67
68  Edge e2 = G.addEdge(n2, n1), e3 = G.addEdge(n2, n3);
69  checkGraphNodeList(G, 3);
70  checkGraphArcList(G, 6);
71  checkGraphEdgeList(G, 3);
72
73  checkGraphOutArcList(G, n1, 2);
74  checkGraphOutArcList(G, n2, 3);
75  checkGraphOutArcList(G, n3, 1);
76
77  checkGraphInArcList(G, n1, 2);
78  checkGraphInArcList(G, n2, 3);
79  checkGraphInArcList(G, n3, 1);
80
81  checkGraphIncEdgeList(G, n1, 2);
82  checkGraphIncEdgeList(G, n2, 3);
83  checkGraphIncEdgeList(G, n3, 1);
84
85  checkGraphConArcList(G, 6);
86  checkGraphConEdgeList(G, 3);
87
88  checkArcDirections(G);
89
90  checkNodeIds(G);
91  checkArcIds(G);
92  checkEdgeIds(G);
93  checkGraphNodeMap(G);
94  checkGraphArcMap(G);
95  checkGraphEdgeMap(G);
96}
97
98void checkConcepts() {
[171]99  { // Checking graph components
[57]100    checkConcept<BaseGraphComponent, BaseGraphComponent >();
101
[209]102    checkConcept<IDableGraphComponent<>,
[57]103      IDableGraphComponent<> >();
104
[209]105    checkConcept<IterableGraphComponent<>,
[57]106      IterableGraphComponent<> >();
107
[209]108    checkConcept<MappableGraphComponent<>,
[57]109      MappableGraphComponent<> >();
110  }
[171]111  { // Checking skeleton graph
112    checkConcept<Graph, Graph>();
[57]113  }
[171]114  { // Checking ListGraph
115    checkConcept<Graph, ListGraph>();
116    checkConcept<AlterableGraphComponent<>, ListGraph>();
117    checkConcept<ExtendableGraphComponent<>, ListGraph>();
118    checkConcept<ClearableGraphComponent<>, ListGraph>();
119    checkConcept<ErasableGraphComponent<>, ListGraph>();
120  }
121  { // Checking SmartGraph
122    checkConcept<Graph, SmartGraph>();
123    checkConcept<AlterableGraphComponent<>, SmartGraph>();
124    checkConcept<ExtendableGraphComponent<>, SmartGraph>();
125    checkConcept<ClearableGraphComponent<>, SmartGraph>();
126  }
127//  { // Checking FullGraph
128//    checkConcept<Graph, FullGraph>();
129//    checkGraphIterators<FullGraph>();
130//  }
131//  { // Checking GridGraph
132//    checkConcept<Graph, GridGraph>();
133//    checkGraphIterators<GridGraph>();
134//  }
[57]135}
136
137template <typename Graph>
[228]138void checkGraphValidity() {
[149]139  TEMPLATE_GRAPH_TYPEDEFS(Graph);
[57]140  Graph g;
141
142  Node
143    n1 = g.addNode(),
144    n2 = g.addNode(),
145    n3 = g.addNode();
146
147  Edge
148    e1 = g.addEdge(n1, n2),
149    e2 = g.addEdge(n2, n3);
150
[171]151  check(g.valid(n1), "Wrong validity check");
152  check(g.valid(e1), "Wrong validity check");
153  check(g.valid(g.direct(e1, true)), "Wrong validity check");
154
155  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
156  check(!g.valid(g.edgeFromId(-1)), "Wrong validity check");
157  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
[57]158}
159
[149]160template <typename Graph>
[228]161void checkGraphValidityErase() {
[149]162  TEMPLATE_GRAPH_TYPEDEFS(Graph);
163  Graph g;
164
165  Node
166    n1 = g.addNode(),
167    n2 = g.addNode(),
168    n3 = g.addNode();
169
170  Edge
171    e1 = g.addEdge(n1, n2),
172    e2 = g.addEdge(n2, n3);
173
[171]174  check(g.valid(n1), "Wrong validity check");
175  check(g.valid(e1), "Wrong validity check");
176  check(g.valid(g.direct(e1, true)), "Wrong validity check");
[149]177
178  g.erase(n1);
179
[171]180  check(!g.valid(n1), "Wrong validity check");
181  check(g.valid(n2), "Wrong validity check");
182  check(g.valid(n3), "Wrong validity check");
183  check(!g.valid(e1), "Wrong validity check");
184  check(g.valid(e2), "Wrong validity check");
[149]185
[171]186  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
187  check(!g.valid(g.edgeFromId(-1)), "Wrong validity check");
188  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
[149]189}
190
[57]191// void checkGridGraph(const GridGraph& g, int w, int h) {
192//   check(g.width() == w, "Wrong width");
193//   check(g.height() == h, "Wrong height");
194
195//   for (int i = 0; i < w; ++i) {
196//     for (int j = 0; j < h; ++j) {
197//       check(g.col(g(i, j)) == i, "Wrong col");
198//       check(g.row(g(i, j)) == j, "Wrong row");
199//     }
200//   }
[209]201
[57]202//   for (int i = 0; i < w; ++i) {
203//     for (int j = 0; j < h - 1; ++j) {
204//       check(g.source(g.down(g(i, j))) == g(i, j), "Wrong down");
205//       check(g.target(g.down(g(i, j))) == g(i, j + 1), "Wrong down");
206//     }
207//     check(g.down(g(i, h - 1)) == INVALID, "Wrong down");
208//   }
209
210//   for (int i = 0; i < w; ++i) {
211//     for (int j = 1; j < h; ++j) {
212//       check(g.source(g.up(g(i, j))) == g(i, j), "Wrong up");
213//       check(g.target(g.up(g(i, j))) == g(i, j - 1), "Wrong up");
214//     }
215//     check(g.up(g(i, 0)) == INVALID, "Wrong up");
216//   }
217
218//   for (int j = 0; j < h; ++j) {
219//     for (int i = 0; i < w - 1; ++i) {
220//       check(g.source(g.right(g(i, j))) == g(i, j), "Wrong right");
[209]221//       check(g.target(g.right(g(i, j))) == g(i + 1, j), "Wrong right");
[57]222//     }
[209]223//     check(g.right(g(w - 1, j)) == INVALID, "Wrong right");
[57]224//   }
225
226//   for (int j = 0; j < h; ++j) {
227//     for (int i = 1; i < w; ++i) {
228//       check(g.source(g.left(g(i, j))) == g(i, j), "Wrong left");
[209]229//       check(g.target(g.left(g(i, j))) == g(i - 1, j), "Wrong left");
[57]230//     }
[209]231//     check(g.left(g(0, j)) == INVALID, "Wrong left");
[57]232//   }
233// }
234
[228]235void checkGraphs() {
[171]236  { // Checking ListGraph
237    checkGraph<ListGraph>();
[228]238    checkGraphValidityErase<ListGraph>();
[171]239  }
240  { // Checking SmartGraph
241    checkGraph<SmartGraph>();
[228]242    checkGraphValidity<SmartGraph>();
[171]243  }
244//   { // Checking FullGraph
245//     FullGraph g(5);
246//     checkGraphNodeList(g, 5);
247//     checkGraphEdgeList(g, 10);
248//   }
249//   { // Checking GridGraph
250//     GridGraph g(5, 6);
251//     checkGraphNodeList(g, 30);
252//     checkGraphEdgeList(g, 49);
253//     checkGridGraph(g, 5, 6);
254//   }
255}
256
[57]257int main() {
[228]258  checkConcepts();
259  checkGraphs();
[57]260  return 0;
261}
Note: See TracBrowser for help on using the repository browser.