1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
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/static_graph.h>
23 #include <lemon/full_graph.h>
25 #include "test_tools.h"
26 #include "graph_test.h"
28 using namespace lemon;
29 using namespace lemon::concepts;
31 template <class Digraph>
32 void checkDigraphBuild() {
33 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
36 checkGraphNodeList(G, 0);
37 checkGraphArcList(G, 0);
46 checkGraphNodeList(G, 3);
47 checkGraphArcList(G, 0);
49 Arc a1 = G.addArc(n1, n2);
50 check(G.source(a1) == n1 && G.target(a1) == n2, "Wrong arc");
51 checkGraphNodeList(G, 3);
52 checkGraphArcList(G, 1);
54 checkGraphOutArcList(G, n1, 1);
55 checkGraphOutArcList(G, n2, 0);
56 checkGraphOutArcList(G, n3, 0);
58 checkGraphInArcList(G, n1, 0);
59 checkGraphInArcList(G, n2, 1);
60 checkGraphInArcList(G, n3, 0);
62 checkGraphConArcList(G, 1);
64 Arc a2 = G.addArc(n2, n1),
65 a3 = G.addArc(n2, n3),
66 a4 = G.addArc(n2, n3);
68 checkGraphNodeList(G, 3);
69 checkGraphArcList(G, 4);
71 checkGraphOutArcList(G, n1, 1);
72 checkGraphOutArcList(G, n2, 3);
73 checkGraphOutArcList(G, n3, 0);
75 checkGraphInArcList(G, n1, 1);
76 checkGraphInArcList(G, n2, 1);
77 checkGraphInArcList(G, n3, 2);
79 checkGraphConArcList(G, 4);
87 template <class Digraph>
88 void checkDigraphSplit() {
89 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
92 Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode();
93 Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n2, n1),
94 a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3);
96 Node n4 = G.split(n2);
98 check(G.target(OutArcIt(G, n2)) == n4 &&
99 G.source(InArcIt(G, n4)) == n2,
102 checkGraphNodeList(G, 4);
103 checkGraphArcList(G, 5);
105 checkGraphOutArcList(G, n1, 1);
106 checkGraphOutArcList(G, n2, 1);
107 checkGraphOutArcList(G, n3, 0);
108 checkGraphOutArcList(G, n4, 3);
110 checkGraphInArcList(G, n1, 1);
111 checkGraphInArcList(G, n2, 1);
112 checkGraphInArcList(G, n3, 2);
113 checkGraphInArcList(G, n4, 1);
115 checkGraphConArcList(G, 5);
118 template <class Digraph>
119 void checkDigraphAlter() {
120 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
123 Node n1 = G.addNode(), n2 = G.addNode(),
124 n3 = G.addNode(), n4 = G.addNode();
125 Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n4, n1),
126 a3 = G.addArc(n4, n3), a4 = G.addArc(n4, n3),
127 a5 = G.addArc(n2, n4);
129 checkGraphNodeList(G, 4);
130 checkGraphArcList(G, 5);
132 // Check changeSource() and changeTarget()
133 G.changeTarget(a4, n1);
135 checkGraphNodeList(G, 4);
136 checkGraphArcList(G, 5);
138 checkGraphOutArcList(G, n1, 1);
139 checkGraphOutArcList(G, n2, 1);
140 checkGraphOutArcList(G, n3, 0);
141 checkGraphOutArcList(G, n4, 3);
143 checkGraphInArcList(G, n1, 2);
144 checkGraphInArcList(G, n2, 1);
145 checkGraphInArcList(G, n3, 1);
146 checkGraphInArcList(G, n4, 1);
148 checkGraphConArcList(G, 5);
150 G.changeSource(a4, n3);
152 checkGraphNodeList(G, 4);
153 checkGraphArcList(G, 5);
155 checkGraphOutArcList(G, n1, 1);
156 checkGraphOutArcList(G, n2, 1);
157 checkGraphOutArcList(G, n3, 1);
158 checkGraphOutArcList(G, n4, 2);
160 checkGraphInArcList(G, n1, 2);
161 checkGraphInArcList(G, n2, 1);
162 checkGraphInArcList(G, n3, 1);
163 checkGraphInArcList(G, n4, 1);
165 checkGraphConArcList(G, 5);
168 G.contract(n2, n4, false);
170 checkGraphNodeList(G, 3);
171 checkGraphArcList(G, 5);
173 checkGraphOutArcList(G, n1, 1);
174 checkGraphOutArcList(G, n2, 3);
175 checkGraphOutArcList(G, n3, 1);
177 checkGraphInArcList(G, n1, 2);
178 checkGraphInArcList(G, n2, 2);
179 checkGraphInArcList(G, n3, 1);
181 checkGraphConArcList(G, 5);
185 checkGraphNodeList(G, 2);
186 checkGraphArcList(G, 3);
188 checkGraphOutArcList(G, n2, 2);
189 checkGraphOutArcList(G, n3, 1);
191 checkGraphInArcList(G, n2, 2);
192 checkGraphInArcList(G, n3, 1);
194 checkGraphConArcList(G, 3);
197 template <class Digraph>
198 void checkDigraphErase() {
199 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
202 Node n1 = G.addNode(), n2 = G.addNode(),
203 n3 = G.addNode(), n4 = G.addNode();
204 Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n4, n1),
205 a3 = G.addArc(n4, n3), a4 = G.addArc(n3, n1),
206 a5 = G.addArc(n2, n4);
208 // Check arc deletion
211 checkGraphNodeList(G, 4);
212 checkGraphArcList(G, 4);
214 checkGraphOutArcList(G, n1, 0);
215 checkGraphOutArcList(G, n2, 1);
216 checkGraphOutArcList(G, n3, 1);
217 checkGraphOutArcList(G, n4, 2);
219 checkGraphInArcList(G, n1, 2);
220 checkGraphInArcList(G, n2, 0);
221 checkGraphInArcList(G, n3, 1);
222 checkGraphInArcList(G, n4, 1);
224 checkGraphConArcList(G, 4);
226 // Check node deletion
229 checkGraphNodeList(G, 3);
230 checkGraphArcList(G, 1);
232 checkGraphOutArcList(G, n1, 0);
233 checkGraphOutArcList(G, n2, 0);
234 checkGraphOutArcList(G, n3, 1);
235 checkGraphOutArcList(G, n4, 0);
237 checkGraphInArcList(G, n1, 1);
238 checkGraphInArcList(G, n2, 0);
239 checkGraphInArcList(G, n3, 0);
240 checkGraphInArcList(G, n4, 0);
242 checkGraphConArcList(G, 1);
246 template <class Digraph>
247 void checkDigraphSnapshot() {
248 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
251 Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode();
252 Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n2, n1),
253 a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3);
255 typename Digraph::Snapshot snapshot(G);
257 Node n = G.addNode();
261 checkGraphNodeList(G, 4);
262 checkGraphArcList(G, 6);
266 checkGraphNodeList(G, 3);
267 checkGraphArcList(G, 4);
269 checkGraphOutArcList(G, n1, 1);
270 checkGraphOutArcList(G, n2, 3);
271 checkGraphOutArcList(G, n3, 0);
273 checkGraphInArcList(G, n1, 1);
274 checkGraphInArcList(G, n2, 1);
275 checkGraphInArcList(G, n3, 2);
277 checkGraphConArcList(G, 4);
281 checkGraphNodeMap(G);
287 G.addArc(G.addNode(), G.addNode());
292 checkGraphNodeList(G, 4);
293 checkGraphArcList(G, 4);
295 G.addArc(G.addNode(), G.addNode());
299 checkGraphNodeList(G, 4);
300 checkGraphArcList(G, 4);
303 void checkConcepts() {
304 { // Checking digraph components
305 checkConcept<BaseDigraphComponent, BaseDigraphComponent >();
307 checkConcept<IDableDigraphComponent<>,
308 IDableDigraphComponent<> >();
310 checkConcept<IterableDigraphComponent<>,
311 IterableDigraphComponent<> >();
313 checkConcept<MappableDigraphComponent<>,
314 MappableDigraphComponent<> >();
316 { // Checking skeleton digraph
317 checkConcept<Digraph, Digraph>();
319 { // Checking ListDigraph
320 checkConcept<Digraph, ListDigraph>();
321 checkConcept<AlterableDigraphComponent<>, ListDigraph>();
322 checkConcept<ExtendableDigraphComponent<>, ListDigraph>();
323 checkConcept<ClearableDigraphComponent<>, ListDigraph>();
324 checkConcept<ErasableDigraphComponent<>, ListDigraph>();
326 { // Checking SmartDigraph
327 checkConcept<Digraph, SmartDigraph>();
328 checkConcept<AlterableDigraphComponent<>, SmartDigraph>();
329 checkConcept<ExtendableDigraphComponent<>, SmartDigraph>();
330 checkConcept<ClearableDigraphComponent<>, SmartDigraph>();
332 { // Checking StaticDigraph
333 checkConcept<Digraph, StaticDigraph>();
334 checkConcept<ClearableDigraphComponent<>, StaticDigraph>();
336 { // Checking FullDigraph
337 checkConcept<Digraph, FullDigraph>();
341 template <typename Digraph>
342 void checkDigraphValidity() {
343 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
352 e1 = g.addArc(n1, n2),
353 e2 = g.addArc(n2, n3);
355 check(g.valid(n1), "Wrong validity check");
356 check(g.valid(e1), "Wrong validity check");
358 check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
359 check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
362 template <typename Digraph>
363 void checkDigraphValidityErase() {
364 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
373 e1 = g.addArc(n1, n2),
374 e2 = g.addArc(n2, n3);
376 check(g.valid(n1), "Wrong validity check");
377 check(g.valid(e1), "Wrong validity check");
381 check(!g.valid(n1), "Wrong validity check");
382 check(g.valid(n2), "Wrong validity check");
383 check(g.valid(n3), "Wrong validity check");
384 check(!g.valid(e1), "Wrong validity check");
385 check(g.valid(e2), "Wrong validity check");
387 check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
388 check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
391 void checkStaticDigraph() {
393 SmartDigraph::NodeMap<StaticDigraph::Node> nref(g);
394 SmartDigraph::ArcMap<StaticDigraph::Arc> aref(g);
398 checkGraphNodeList(G, 0);
399 checkGraphArcList(G, 0);
401 G.build(g, nref, aref);
403 checkGraphNodeList(G, 0);
404 checkGraphArcList(G, 0);
411 G.build(g, nref, aref);
413 checkGraphNodeList(G, 3);
414 checkGraphArcList(G, 0);
416 SmartDigraph::Arc a1 = g.addArc(n1, n2);
418 G.build(g, nref, aref);
420 check(G.source(aref[a1]) == nref[n1] && G.target(aref[a1]) == nref[n2],
421 "Wrong arc or wrong references");
422 checkGraphNodeList(G, 3);
423 checkGraphArcList(G, 1);
425 checkGraphOutArcList(G, nref[n1], 1);
426 checkGraphOutArcList(G, nref[n2], 0);
427 checkGraphOutArcList(G, nref[n3], 0);
429 checkGraphInArcList(G, nref[n1], 0);
430 checkGraphInArcList(G, nref[n2], 1);
431 checkGraphInArcList(G, nref[n3], 0);
433 checkGraphConArcList(G, 1);
436 a2 = g.addArc(n2, n1),
437 a3 = g.addArc(n2, n3),
438 a4 = g.addArc(n2, n3);
440 digraphCopy(g, G).nodeRef(nref).run();
442 checkGraphNodeList(G, 3);
443 checkGraphArcList(G, 4);
445 checkGraphOutArcList(G, nref[n1], 1);
446 checkGraphOutArcList(G, nref[n2], 3);
447 checkGraphOutArcList(G, nref[n3], 0);
449 checkGraphInArcList(G, nref[n1], 1);
450 checkGraphInArcList(G, nref[n2], 1);
451 checkGraphInArcList(G, nref[n3], 2);
453 checkGraphConArcList(G, 4);
455 std::vector<std::pair<int,int> > arcs;
456 arcs.push_back(std::make_pair(0,1));
457 arcs.push_back(std::make_pair(0,2));
458 arcs.push_back(std::make_pair(1,3));
459 arcs.push_back(std::make_pair(1,2));
460 arcs.push_back(std::make_pair(3,0));
461 arcs.push_back(std::make_pair(3,3));
462 arcs.push_back(std::make_pair(4,2));
463 arcs.push_back(std::make_pair(4,3));
464 arcs.push_back(std::make_pair(4,1));
466 G.build(6, arcs.begin(), arcs.end());
468 checkGraphNodeList(G, 6);
469 checkGraphArcList(G, 9);
471 checkGraphOutArcList(G, G.node(0), 2);
472 checkGraphOutArcList(G, G.node(1), 2);
473 checkGraphOutArcList(G, G.node(2), 0);
474 checkGraphOutArcList(G, G.node(3), 2);
475 checkGraphOutArcList(G, G.node(4), 3);
476 checkGraphOutArcList(G, G.node(5), 0);
478 checkGraphInArcList(G, G.node(0), 1);
479 checkGraphInArcList(G, G.node(1), 2);
480 checkGraphInArcList(G, G.node(2), 3);
481 checkGraphInArcList(G, G.node(3), 3);
482 checkGraphInArcList(G, G.node(4), 0);
483 checkGraphInArcList(G, G.node(5), 0);
485 checkGraphConArcList(G, 9);
489 checkGraphNodeMap(G);
494 check(G.index(G.node(n-1)) == n-1, "Wrong index.");
495 check(G.index(G.arc(m-1)) == m-1, "Wrong index.");
498 void checkFullDigraph(int num) {
499 typedef FullDigraph Digraph;
500 DIGRAPH_TYPEDEFS(Digraph);
503 check(G.nodeNum() == num && G.arcNum() == num * num, "Wrong size");
506 check(G.nodeNum() == num && G.arcNum() == num * num, "Wrong size");
508 checkGraphNodeList(G, num);
509 checkGraphArcList(G, num * num);
511 for (NodeIt n(G); n != INVALID; ++n) {
512 checkGraphOutArcList(G, n, num);
513 checkGraphInArcList(G, n, num);
516 checkGraphConArcList(G, num * num);
520 checkGraphNodeMap(G);
523 for (int i = 0; i < G.nodeNum(); ++i) {
524 check(G.index(G(i)) == i, "Wrong index");
527 for (NodeIt s(G); s != INVALID; ++s) {
528 for (NodeIt t(G); t != INVALID; ++t) {
530 check(G.source(a) == s && G.target(a) == t, "Wrong arc lookup");
535 void checkDigraphs() {
536 { // Checking ListDigraph
537 checkDigraphBuild<ListDigraph>();
538 checkDigraphSplit<ListDigraph>();
539 checkDigraphAlter<ListDigraph>();
540 checkDigraphErase<ListDigraph>();
541 checkDigraphSnapshot<ListDigraph>();
542 checkDigraphValidityErase<ListDigraph>();
544 { // Checking SmartDigraph
545 checkDigraphBuild<SmartDigraph>();
546 checkDigraphSplit<SmartDigraph>();
547 checkDigraphSnapshot<SmartDigraph>();
548 checkDigraphValidity<SmartDigraph>();
550 { // Checking StaticDigraph
551 checkStaticDigraph();
553 { // Checking FullDigraph