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());
290 checkGraphNodeList(G, 4);
291 checkGraphArcList(G, 4);
294 void checkConcepts() {
295 { // Checking digraph components
296 checkConcept<BaseDigraphComponent, BaseDigraphComponent >();
298 checkConcept<IDableDigraphComponent<>,
299 IDableDigraphComponent<> >();
301 checkConcept<IterableDigraphComponent<>,
302 IterableDigraphComponent<> >();
304 checkConcept<MappableDigraphComponent<>,
305 MappableDigraphComponent<> >();
307 { // Checking skeleton digraph
308 checkConcept<Digraph, Digraph>();
310 { // Checking ListDigraph
311 checkConcept<Digraph, ListDigraph>();
312 checkConcept<AlterableDigraphComponent<>, ListDigraph>();
313 checkConcept<ExtendableDigraphComponent<>, ListDigraph>();
314 checkConcept<ClearableDigraphComponent<>, ListDigraph>();
315 checkConcept<ErasableDigraphComponent<>, ListDigraph>();
317 { // Checking SmartDigraph
318 checkConcept<Digraph, SmartDigraph>();
319 checkConcept<AlterableDigraphComponent<>, SmartDigraph>();
320 checkConcept<ExtendableDigraphComponent<>, SmartDigraph>();
321 checkConcept<ClearableDigraphComponent<>, SmartDigraph>();
323 { // Checking FullDigraph
324 checkConcept<Digraph, FullDigraph>();
328 template <typename Digraph>
329 void checkDigraphValidity() {
330 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
339 e1 = g.addArc(n1, n2),
340 e2 = g.addArc(n2, n3);
342 check(g.valid(n1), "Wrong validity check");
343 check(g.valid(e1), "Wrong validity check");
345 check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
346 check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
349 template <typename Digraph>
350 void checkDigraphValidityErase() {
351 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
360 e1 = g.addArc(n1, n2),
361 e2 = g.addArc(n2, n3);
363 check(g.valid(n1), "Wrong validity check");
364 check(g.valid(e1), "Wrong validity check");
368 check(!g.valid(n1), "Wrong validity check");
369 check(g.valid(n2), "Wrong validity check");
370 check(g.valid(n3), "Wrong validity check");
371 check(!g.valid(e1), "Wrong validity check");
372 check(g.valid(e2), "Wrong validity check");
374 check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
375 check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
378 void checkFullDigraph(int num) {
379 typedef FullDigraph Digraph;
380 DIGRAPH_TYPEDEFS(Digraph);
383 check(G.nodeNum() == num && G.arcNum() == num * num, "Wrong size");
386 check(G.nodeNum() == num && G.arcNum() == num * num, "Wrong size");
388 checkGraphNodeList(G, num);
389 checkGraphArcList(G, num * num);
391 for (NodeIt n(G); n != INVALID; ++n) {
392 checkGraphOutArcList(G, n, num);
393 checkGraphInArcList(G, n, num);
396 checkGraphConArcList(G, num * num);
400 checkGraphNodeMap(G);
403 for (int i = 0; i < G.nodeNum(); ++i) {
404 check(G.index(G(i)) == i, "Wrong index");
407 for (NodeIt s(G); s != INVALID; ++s) {
408 for (NodeIt t(G); t != INVALID; ++t) {
410 check(G.source(a) == s && G.target(a) == t, "Wrong arc lookup");
415 void checkDigraphs() {
416 { // Checking ListDigraph
417 checkDigraphBuild<ListDigraph>();
418 checkDigraphSplit<ListDigraph>();
419 checkDigraphAlter<ListDigraph>();
420 checkDigraphErase<ListDigraph>();
421 checkDigraphSnapshot<ListDigraph>();
422 checkDigraphValidityErase<ListDigraph>();
424 { // Checking SmartDigraph
425 checkDigraphBuild<SmartDigraph>();
426 checkDigraphSplit<SmartDigraph>();
427 checkDigraphSnapshot<SmartDigraph>();
428 checkDigraphValidity<SmartDigraph>();
430 { // Checking FullDigraph