1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 #include <lemon/concepts/digraph.h>
20 #include <lemon/list_graph.h>
21 #include <lemon/smart_graph.h>
22 #include <lemon/full_graph.h>
24 #include "test_tools.h"
25 #include "graph_test.h"
27 using namespace lemon;
28 using namespace lemon::concepts;
30 template <class Digraph>
31 void checkDigraphBuild() {
32 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
35 checkGraphNodeList(G, 0);
36 checkGraphArcList(G, 0);
45 checkGraphNodeList(G, 3);
46 checkGraphArcList(G, 0);
48 Arc a1 = G.addArc(n1, n2);
49 check(G.source(a1) == n1 && G.target(a1) == n2, "Wrong arc");
50 checkGraphNodeList(G, 3);
51 checkGraphArcList(G, 1);
53 checkGraphOutArcList(G, n1, 1);
54 checkGraphOutArcList(G, n2, 0);
55 checkGraphOutArcList(G, n3, 0);
57 checkGraphInArcList(G, n1, 0);
58 checkGraphInArcList(G, n2, 1);
59 checkGraphInArcList(G, n3, 0);
61 checkGraphConArcList(G, 1);
63 Arc a2 = G.addArc(n2, n1),
64 a3 = G.addArc(n2, n3),
65 a4 = G.addArc(n2, n3);
67 checkGraphNodeList(G, 3);
68 checkGraphArcList(G, 4);
70 checkGraphOutArcList(G, n1, 1);
71 checkGraphOutArcList(G, n2, 3);
72 checkGraphOutArcList(G, n3, 0);
74 checkGraphInArcList(G, n1, 1);
75 checkGraphInArcList(G, n2, 1);
76 checkGraphInArcList(G, n3, 2);
78 checkGraphConArcList(G, 4);
86 template <class Digraph>
87 void checkDigraphSplit() {
88 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
91 Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode();
92 Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n2, n1),
93 a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3);
95 Node n4 = G.split(n2);
97 check(G.target(OutArcIt(G, n2)) == n4 &&
98 G.source(InArcIt(G, n4)) == n2,
101 checkGraphNodeList(G, 4);
102 checkGraphArcList(G, 5);
104 checkGraphOutArcList(G, n1, 1);
105 checkGraphOutArcList(G, n2, 1);
106 checkGraphOutArcList(G, n3, 0);
107 checkGraphOutArcList(G, n4, 3);
109 checkGraphInArcList(G, n1, 1);
110 checkGraphInArcList(G, n2, 1);
111 checkGraphInArcList(G, n3, 2);
112 checkGraphInArcList(G, n4, 1);
114 checkGraphConArcList(G, 5);
117 template <class Digraph>
118 void checkDigraphAlter() {
119 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
122 Node n1 = G.addNode(), n2 = G.addNode(),
123 n3 = G.addNode(), n4 = G.addNode();
124 Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n4, n1),
125 a3 = G.addArc(n4, n3), a4 = G.addArc(n4, n3),
126 a5 = G.addArc(n2, n4);
128 checkGraphNodeList(G, 4);
129 checkGraphArcList(G, 5);
131 // Check changeSource() and changeTarget()
132 G.changeTarget(a4, n1);
134 checkGraphNodeList(G, 4);
135 checkGraphArcList(G, 5);
137 checkGraphOutArcList(G, n1, 1);
138 checkGraphOutArcList(G, n2, 1);
139 checkGraphOutArcList(G, n3, 0);
140 checkGraphOutArcList(G, n4, 3);
142 checkGraphInArcList(G, n1, 2);
143 checkGraphInArcList(G, n2, 1);
144 checkGraphInArcList(G, n3, 1);
145 checkGraphInArcList(G, n4, 1);
147 checkGraphConArcList(G, 5);
149 G.changeSource(a4, n3);
151 checkGraphNodeList(G, 4);
152 checkGraphArcList(G, 5);
154 checkGraphOutArcList(G, n1, 1);
155 checkGraphOutArcList(G, n2, 1);
156 checkGraphOutArcList(G, n3, 1);
157 checkGraphOutArcList(G, n4, 2);
159 checkGraphInArcList(G, n1, 2);
160 checkGraphInArcList(G, n2, 1);
161 checkGraphInArcList(G, n3, 1);
162 checkGraphInArcList(G, n4, 1);
164 checkGraphConArcList(G, 5);
167 G.contract(n2, n4, false);
169 checkGraphNodeList(G, 3);
170 checkGraphArcList(G, 5);
172 checkGraphOutArcList(G, n1, 1);
173 checkGraphOutArcList(G, n2, 3);
174 checkGraphOutArcList(G, n3, 1);
176 checkGraphInArcList(G, n1, 2);
177 checkGraphInArcList(G, n2, 2);
178 checkGraphInArcList(G, n3, 1);
180 checkGraphConArcList(G, 5);
184 checkGraphNodeList(G, 2);
185 checkGraphArcList(G, 3);
187 checkGraphOutArcList(G, n2, 2);
188 checkGraphOutArcList(G, n3, 1);
190 checkGraphInArcList(G, n2, 2);
191 checkGraphInArcList(G, n3, 1);
193 checkGraphConArcList(G, 3);
196 template <class Digraph>
197 void checkDigraphErase() {
198 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
201 Node n1 = G.addNode(), n2 = G.addNode(),
202 n3 = G.addNode(), n4 = G.addNode();
203 Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n4, n1),
204 a3 = G.addArc(n4, n3), a4 = G.addArc(n3, n1),
205 a5 = G.addArc(n2, n4);
207 // Check arc deletion
210 checkGraphNodeList(G, 4);
211 checkGraphArcList(G, 4);
213 checkGraphOutArcList(G, n1, 0);
214 checkGraphOutArcList(G, n2, 1);
215 checkGraphOutArcList(G, n3, 1);
216 checkGraphOutArcList(G, n4, 2);
218 checkGraphInArcList(G, n1, 2);
219 checkGraphInArcList(G, n2, 0);
220 checkGraphInArcList(G, n3, 1);
221 checkGraphInArcList(G, n4, 1);
223 checkGraphConArcList(G, 4);
225 // Check node deletion
228 checkGraphNodeList(G, 3);
229 checkGraphArcList(G, 1);
231 checkGraphOutArcList(G, n1, 0);
232 checkGraphOutArcList(G, n2, 0);
233 checkGraphOutArcList(G, n3, 1);
234 checkGraphOutArcList(G, n4, 0);
236 checkGraphInArcList(G, n1, 1);
237 checkGraphInArcList(G, n2, 0);
238 checkGraphInArcList(G, n3, 0);
239 checkGraphInArcList(G, n4, 0);
241 checkGraphConArcList(G, 1);
245 template <class Digraph>
246 void checkDigraphSnapshot() {
247 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
250 Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode();
251 Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n2, n1),
252 a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3);
254 typename Digraph::Snapshot snapshot(G);
256 Node n = G.addNode();
260 checkGraphNodeList(G, 4);
261 checkGraphArcList(G, 6);
265 checkGraphNodeList(G, 3);
266 checkGraphArcList(G, 4);
268 checkGraphOutArcList(G, n1, 1);
269 checkGraphOutArcList(G, n2, 3);
270 checkGraphOutArcList(G, n3, 0);
272 checkGraphInArcList(G, n1, 1);
273 checkGraphInArcList(G, n2, 1);
274 checkGraphInArcList(G, n3, 2);
276 checkGraphConArcList(G, 4);
280 checkGraphNodeMap(G);
286 G.addArc(G.addNode(), G.addNode());
291 checkGraphNodeList(G, 4);
292 checkGraphArcList(G, 4);
294 G.addArc(G.addNode(), G.addNode());
298 checkGraphNodeList(G, 4);
299 checkGraphArcList(G, 4);
302 void checkConcepts() {
303 { // Checking digraph components
304 checkConcept<BaseDigraphComponent, BaseDigraphComponent >();
306 checkConcept<IDableDigraphComponent<>,
307 IDableDigraphComponent<> >();
309 checkConcept<IterableDigraphComponent<>,
310 IterableDigraphComponent<> >();
312 checkConcept<MappableDigraphComponent<>,
313 MappableDigraphComponent<> >();
315 { // Checking skeleton digraph
316 checkConcept<Digraph, Digraph>();
318 { // Checking ListDigraph
319 checkConcept<Digraph, ListDigraph>();
320 checkConcept<AlterableDigraphComponent<>, ListDigraph>();
321 checkConcept<ExtendableDigraphComponent<>, ListDigraph>();
322 checkConcept<ClearableDigraphComponent<>, ListDigraph>();
323 checkConcept<ErasableDigraphComponent<>, ListDigraph>();
325 { // Checking SmartDigraph
326 checkConcept<Digraph, SmartDigraph>();
327 checkConcept<AlterableDigraphComponent<>, SmartDigraph>();
328 checkConcept<ExtendableDigraphComponent<>, SmartDigraph>();
329 checkConcept<ClearableDigraphComponent<>, SmartDigraph>();
331 { // Checking FullDigraph
332 checkConcept<Digraph, FullDigraph>();
336 template <typename Digraph>
337 void checkDigraphValidity() {
338 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
347 e1 = g.addArc(n1, n2),
348 e2 = g.addArc(n2, n3);
350 check(g.valid(n1), "Wrong validity check");
351 check(g.valid(e1), "Wrong validity check");
353 check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
354 check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
357 template <typename Digraph>
358 void checkDigraphValidityErase() {
359 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
368 e1 = g.addArc(n1, n2),
369 e2 = g.addArc(n2, n3);
371 check(g.valid(n1), "Wrong validity check");
372 check(g.valid(e1), "Wrong validity check");
376 check(!g.valid(n1), "Wrong validity check");
377 check(g.valid(n2), "Wrong validity check");
378 check(g.valid(n3), "Wrong validity check");
379 check(!g.valid(e1), "Wrong validity check");
380 check(g.valid(e2), "Wrong validity check");
382 check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
383 check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
386 void checkFullDigraph(int num) {
387 typedef FullDigraph Digraph;
388 DIGRAPH_TYPEDEFS(Digraph);
391 check(G.nodeNum() == num && G.arcNum() == num * num, "Wrong size");
394 check(G.nodeNum() == num && G.arcNum() == num * num, "Wrong size");
396 checkGraphNodeList(G, num);
397 checkGraphArcList(G, num * num);
399 for (NodeIt n(G); n != INVALID; ++n) {
400 checkGraphOutArcList(G, n, num);
401 checkGraphInArcList(G, n, num);
404 checkGraphConArcList(G, num * num);
408 checkGraphNodeMap(G);
411 for (int i = 0; i < G.nodeNum(); ++i) {
412 check(G.index(G(i)) == i, "Wrong index");
415 for (NodeIt s(G); s != INVALID; ++s) {
416 for (NodeIt t(G); t != INVALID; ++t) {
418 check(G.source(a) == s && G.target(a) == t, "Wrong arc lookup");
423 void checkDigraphs() {
424 { // Checking ListDigraph
425 checkDigraphBuild<ListDigraph>();
426 checkDigraphSplit<ListDigraph>();
427 checkDigraphAlter<ListDigraph>();
428 checkDigraphErase<ListDigraph>();
429 checkDigraphSnapshot<ListDigraph>();
430 checkDigraphValidityErase<ListDigraph>();
432 { // Checking SmartDigraph
433 checkDigraphBuild<SmartDigraph>();
434 checkDigraphSplit<SmartDigraph>();
435 checkDigraphSnapshot<SmartDigraph>();
436 checkDigraphValidity<SmartDigraph>();
438 { // Checking FullDigraph