1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
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 #ifndef LEMON_TEST_GRAPH_TEST_H
20 #define LEMON_TEST_GRAPH_TEST_H
24 #include <lemon/core.h>
25 #include <lemon/maps.h>
27 #include "test_tools.h"
32 void checkGraphNodeList(const Graph &G, int cnt)
34 typename Graph::NodeIt n(G);
35 for(int i=0;i<cnt;i++) {
36 check(n!=INVALID,"Wrong Node list linking.");
39 check(n==INVALID,"Wrong Node list linking.");
40 check(countNodes(G)==cnt,"Wrong Node number.");
44 typename Graph::NodeIt n(G);
45 for(auto u: G.nodes())
47 check(n==u,"Wrong STL Node iterator.");
50 check(n==INVALID,"Wrong STL Node iterator.");
53 typename Graph::NodeIt n(G);
54 for(typename Graph::Node u: G.nodes())
56 check(n==u,"Wrong STL Node iterator.");
59 check(n==INVALID,"Wrong STL Node iterator.");
65 void checkGraphRedNodeList(const Graph &G, int cnt)
67 typename Graph::RedNodeIt n(G);
68 for(int i=0;i<cnt;i++) {
69 check(n!=INVALID,"Wrong red Node list linking.");
70 check(G.red(n),"Wrong node set check.");
71 check(!G.blue(n),"Wrong node set check.");
72 typename Graph::Node nn = n;
73 check(G.asRedNodeUnsafe(nn) == n,"Wrong node conversion.");
74 check(G.asRedNode(nn) == n,"Wrong node conversion.");
75 check(G.asBlueNode(nn) == INVALID,"Wrong node conversion.");
78 check(n==INVALID,"Wrong red Node list linking.");
79 check(countRedNodes(G)==cnt,"Wrong red Node number.");
82 typename Graph::RedNodeIt n(G);
83 for(auto u: G.redNodes())
85 check(n==u,"Wrong STL RedNode iterator.");
88 check(n==INVALID,"Wrong STL RedNode iterator.");
91 typename Graph::RedNodeIt n(G);
92 for(typename Graph::RedNode u: G.redNodes())
94 check(n==u,"Wrong STL RedNode iterator.");
97 check(n==INVALID,"Wrong STL RedNode iterator.");
102 template<class Graph>
103 void checkGraphBlueNodeList(const Graph &G, int cnt)
105 typename Graph::BlueNodeIt n(G);
106 for(int i=0;i<cnt;i++) {
107 check(n!=INVALID,"Wrong blue Node list linking.");
108 check(G.blue(n),"Wrong node set check.");
109 check(!G.red(n),"Wrong node set check.");
110 typename Graph::Node nn = n;
111 check(G.asBlueNodeUnsafe(nn) == n,"Wrong node conversion.");
112 check(G.asBlueNode(nn) == n,"Wrong node conversion.");
113 check(G.asRedNode(nn) == INVALID,"Wrong node conversion.");
116 check(n==INVALID,"Wrong blue Node list linking.");
117 check(countBlueNodes(G)==cnt,"Wrong blue Node number.");
120 typename Graph::BlueNodeIt n(G);
121 for(auto u: G.blueNodes())
123 check(n==u,"Wrong STL BlueNode iterator.");
126 check(n==INVALID,"Wrong STL BlueNode iterator.");
129 typename Graph::BlueNodeIt n(G);
130 for(typename Graph::BlueNode u: G.blueNodes())
132 check(n==u,"Wrong STL BlueNode iterator.");
135 check(n==INVALID,"Wrong STL BlueNode iterator.");
141 template<class Graph>
142 void checkGraphArcList(const Graph &G, int cnt)
144 typename Graph::ArcIt e(G);
145 for(int i=0;i<cnt;i++) {
146 check(e!=INVALID,"Wrong Arc list linking.");
147 check(G.oppositeNode(G.source(e), e) == G.target(e),
148 "Wrong opposite node");
149 check(G.oppositeNode(G.target(e), e) == G.source(e),
150 "Wrong opposite node");
153 check(e==INVALID,"Wrong Arc list linking.");
154 check(countArcs(G)==cnt,"Wrong Arc number.");
157 typename Graph::ArcIt a(G);
158 for(auto e: G.arcs())
160 check(a==e,"Wrong STL Arc iterator.");
163 check(a==INVALID,"Wrong STL Arc iterator.");
166 typename Graph::ArcIt a(G);
167 for(typename Graph::Arc e: G.arcs())
169 check(a==e,"Wrong STL Arc iterator.");
172 check(a==INVALID,"Wrong STL Arc iterator.");
178 template<class Graph>
179 void checkGraphOutArcList(const Graph &G, typename Graph::Node n, int cnt)
181 typename Graph::OutArcIt e(G,n);
182 for(int i=0;i<cnt;i++) {
183 check(e!=INVALID,"Wrong OutArc list linking.");
184 check(n==G.source(e),"Wrong OutArc list linking.");
185 check(n==G.baseNode(e),"Wrong OutArc list linking.");
186 check(G.target(e)==G.runningNode(e),"Wrong OutArc list linking.");
189 check(e==INVALID,"Wrong OutArc list linking.");
190 check(countOutArcs(G,n)==cnt,"Wrong OutArc number.");
193 typename Graph::OutArcIt a(G,n);
194 for(auto e: G.outArcs(n))
196 check(a==e,"Wrong STL OutArc iterator.");
199 check(a==INVALID,"Wrong STL OutArc iterator.");
202 typename Graph::OutArcIt a(G,n);
203 for(typename Graph::Arc e: G.outArcs(n))
205 check(a==e,"Wrong STL OutArc iterator.");
208 check(a==INVALID,"Wrong STL OutArc iterator.");
214 template<class Graph>
215 void checkGraphInArcList(const Graph &G, typename Graph::Node n, int cnt)
217 typename Graph::InArcIt e(G,n);
218 for(int i=0;i<cnt;i++) {
219 check(e!=INVALID,"Wrong InArc list linking.");
220 check(n==G.target(e),"Wrong InArc list linking.");
221 check(n==G.baseNode(e),"Wrong OutArc list linking.");
222 check(G.source(e)==G.runningNode(e),"Wrong OutArc list linking.");
225 check(e==INVALID,"Wrong InArc list linking.");
226 check(countInArcs(G,n)==cnt,"Wrong InArc number.");
229 typename Graph::InArcIt a(G,n);
230 for(auto e: G.inArcs(n))
232 check(a==e,"Wrong STL InArc iterator.");
235 check(a==INVALID,"Wrong STL InArc iterator.");
238 typename Graph::InArcIt a(G,n);
239 for(typename Graph::Arc e: G.inArcs(n))
241 check(a==e,"Wrong STL InArc iterator.");
244 check(a==INVALID,"Wrong STL InArc iterator.");
249 template<class Graph>
250 void checkGraphEdgeList(const Graph &G, int cnt)
252 typename Graph::EdgeIt e(G);
253 for(int i=0;i<cnt;i++) {
254 check(e!=INVALID,"Wrong Edge list linking.");
255 check(G.oppositeNode(G.u(e), e) == G.v(e), "Wrong opposite node");
256 check(G.oppositeNode(G.v(e), e) == G.u(e), "Wrong opposite node");
259 check(e==INVALID,"Wrong Edge list linking.");
260 check(countEdges(G)==cnt,"Wrong Edge number.");
263 typename Graph::EdgeIt a(G);
264 for(auto e: G.edges())
266 check(a==e,"Wrong STL Edge iterator.");
269 check(a==INVALID,"Wrong STL Edge iterator.");
272 typename Graph::EdgeIt a(G);
273 for(typename Graph::Edge e: G.edges())
275 check(a==e,"Wrong STL Edge iterator.");
278 check(a==INVALID,"Wrong STL Edge iterator.");
284 template<class Graph>
285 void checkGraphIncEdgeList(const Graph &G, typename Graph::Node n, int cnt)
287 typename Graph::IncEdgeIt e(G,n);
288 for(int i=0;i<cnt;i++) {
289 check(e!=INVALID,"Wrong IncEdge list linking.");
290 check(n==G.u(e) || n==G.v(e),"Wrong IncEdge list linking.");
291 check(n==G.baseNode(e),"Wrong OutArc list linking.");
292 check(G.u(e)==G.runningNode(e) || G.v(e)==G.runningNode(e),
293 "Wrong OutArc list linking.");
296 check(e==INVALID,"Wrong IncEdge list linking.");
297 check(countIncEdges(G,n)==cnt,"Wrong IncEdge number.");
300 typename Graph::IncEdgeIt a(G,n);
301 for(auto e: G.incEdges(n))
303 check(a==e,"Wrong STL IncEdge iterator.");
306 check(a==INVALID,"Wrong STL IncEdge iterator.");
309 typename Graph::IncEdgeIt a(G,n);
310 for(typename Graph::Edge e: G.incEdges(n))
312 check(a==e,"Wrong STL IncEdge iterator.");
315 check(a==INVALID,"Wrong STL IncEdge iterator.");
321 template <class Graph>
322 void checkGraphIncEdgeArcLists(const Graph &G, typename Graph::Node n,
325 checkGraphIncEdgeList(G, n, cnt);
326 checkGraphOutArcList(G, n, cnt);
327 checkGraphInArcList(G, n, cnt);
330 template <class Graph>
331 void checkGraphConArcList(const Graph &G, int cnt) {
333 for (typename Graph::NodeIt u(G); u != INVALID; ++u) {
334 for (typename Graph::NodeIt v(G); v != INVALID; ++v) {
335 for (ConArcIt<Graph> a(G, u, v); a != INVALID; ++a) {
336 check(G.source(a) == u, "Wrong iterator.");
337 check(G.target(a) == v, "Wrong iterator.");
342 check(cnt == i, "Wrong iterator.");
345 template <class Graph>
346 void checkGraphConEdgeList(const Graph &G, int cnt) {
348 for (typename Graph::NodeIt u(G); u != INVALID; ++u) {
349 for (typename Graph::NodeIt v(G); v != INVALID; ++v) {
350 for (ConEdgeIt<Graph> e(G, u, v); e != INVALID; ++e) {
351 check((G.u(e) == u && G.v(e) == v) ||
352 (G.u(e) == v && G.v(e) == u), "Wrong iterator.");
357 check(2 * cnt == i, "Wrong iterator.");
360 template <typename Graph>
361 void checkArcDirections(const Graph& G) {
362 for (typename Graph::ArcIt a(G); a != INVALID; ++a) {
363 check(G.source(a) == G.target(G.oppositeArc(a)), "Wrong direction");
364 check(G.target(a) == G.source(G.oppositeArc(a)), "Wrong direction");
365 check(G.direct(a, G.direction(a)) == a, "Wrong direction");
369 template <typename Graph>
370 void checkNodeIds(const Graph& G) {
371 typedef typename Graph::Node Node;
372 std::set<int> values;
373 for (typename Graph::NodeIt n(G); n != INVALID; ++n) {
374 check(G.nodeFromId(G.id(n)) == n, "Wrong id");
375 check(values.find(G.id(n)) == values.end(), "Wrong id");
376 check(G.id(n) <= G.maxNodeId(), "Wrong maximum id");
377 values.insert(G.id(n));
379 check(G.maxId(Node()) <= G.maxNodeId(), "Wrong maximum id");
382 template <typename Graph>
383 void checkRedNodeIds(const Graph& G) {
384 typedef typename Graph::RedNode RedNode;
385 std::set<int> values;
386 for (typename Graph::RedNodeIt n(G); n != INVALID; ++n) {
387 check(G.red(n), "Wrong partition");
388 check(values.find(G.id(n)) == values.end(), "Wrong id");
389 check(G.id(n) <= G.maxRedId(), "Wrong maximum id");
390 values.insert(G.id(n));
392 check(G.maxId(RedNode()) == G.maxRedId(), "Wrong maximum id");
395 template <typename Graph>
396 void checkBlueNodeIds(const Graph& G) {
397 typedef typename Graph::BlueNode BlueNode;
398 std::set<int> values;
399 for (typename Graph::BlueNodeIt n(G); n != INVALID; ++n) {
400 check(G.blue(n), "Wrong partition");
401 check(values.find(G.id(n)) == values.end(), "Wrong id");
402 check(G.id(n) <= G.maxBlueId(), "Wrong maximum id");
403 values.insert(G.id(n));
405 check(G.maxId(BlueNode()) == G.maxBlueId(), "Wrong maximum id");
408 template <typename Graph>
409 void checkArcIds(const Graph& G) {
410 typedef typename Graph::Arc Arc;
411 std::set<int> values;
412 for (typename Graph::ArcIt a(G); a != INVALID; ++a) {
413 check(G.arcFromId(G.id(a)) == a, "Wrong id");
414 check(values.find(G.id(a)) == values.end(), "Wrong id");
415 check(G.id(a) <= G.maxArcId(), "Wrong maximum id");
416 values.insert(G.id(a));
418 check(G.maxId(Arc()) <= G.maxArcId(), "Wrong maximum id");
421 template <typename Graph>
422 void checkEdgeIds(const Graph& G) {
423 typedef typename Graph::Edge Edge;
424 std::set<int> values;
425 for (typename Graph::EdgeIt e(G); e != INVALID; ++e) {
426 check(G.edgeFromId(G.id(e)) == e, "Wrong id");
427 check(values.find(G.id(e)) == values.end(), "Wrong id");
428 check(G.id(e) <= G.maxEdgeId(), "Wrong maximum id");
429 values.insert(G.id(e));
431 check(G.maxId(Edge()) <= G.maxEdgeId(), "Wrong maximum id");
434 template <typename Graph>
435 void checkGraphNodeMap(const Graph& G) {
436 typedef typename Graph::Node Node;
437 typedef typename Graph::NodeIt NodeIt;
439 typedef typename Graph::template NodeMap<int> IntNodeMap;
440 IntNodeMap map(G, 42);
441 for (NodeIt it(G); it != INVALID; ++it) {
442 check(map[it] == 42, "Wrong map constructor.");
445 for (NodeIt it(G); it != INVALID; ++it) {
447 check(map[it] == 0, "Wrong operator[].");
449 check(map[it] == s, "Wrong set.");
453 for (NodeIt it(G); it != INVALID; ++it) {
456 check(s == 0, "Wrong sum.");
458 // map = constMap<Node>(12);
459 // for (NodeIt it(G); it != INVALID; ++it) {
460 // check(map[it] == 12, "Wrong operator[].");
464 template <typename Graph>
465 void checkGraphRedNodeMap(const Graph& G) {
466 typedef typename Graph::Node Node;
467 typedef typename Graph::RedNodeIt RedNodeIt;
469 typedef typename Graph::template RedNodeMap<int> IntRedNodeMap;
470 IntRedNodeMap map(G, 42);
471 for (RedNodeIt it(G); it != INVALID; ++it) {
472 check(map[it] == 42, "Wrong map constructor.");
475 for (RedNodeIt it(G); it != INVALID; ++it) {
477 check(map[it] == 0, "Wrong operator[].");
479 check(map[it] == s, "Wrong set.");
483 for (RedNodeIt it(G); it != INVALID; ++it) {
486 check(s == 0, "Wrong sum.");
488 // map = constMap<Node>(12);
489 // for (NodeIt it(G); it != INVALID; ++it) {
490 // check(map[it] == 12, "Wrong operator[].");
494 template <typename Graph>
495 void checkGraphBlueNodeMap(const Graph& G) {
496 typedef typename Graph::Node Node;
497 typedef typename Graph::BlueNodeIt BlueNodeIt;
499 typedef typename Graph::template BlueNodeMap<int> IntBlueNodeMap;
500 IntBlueNodeMap map(G, 42);
501 for (BlueNodeIt it(G); it != INVALID; ++it) {
502 check(map[it] == 42, "Wrong map constructor.");
505 for (BlueNodeIt it(G); it != INVALID; ++it) {
507 check(map[it] == 0, "Wrong operator[].");
509 check(map[it] == s, "Wrong set.");
513 for (BlueNodeIt it(G); it != INVALID; ++it) {
516 check(s == 0, "Wrong sum.");
518 // map = constMap<Node>(12);
519 // for (NodeIt it(G); it != INVALID; ++it) {
520 // check(map[it] == 12, "Wrong operator[].");
524 template <typename Graph>
525 void checkGraphArcMap(const Graph& G) {
526 typedef typename Graph::Arc Arc;
527 typedef typename Graph::ArcIt ArcIt;
529 typedef typename Graph::template ArcMap<int> IntArcMap;
530 IntArcMap map(G, 42);
531 for (ArcIt it(G); it != INVALID; ++it) {
532 check(map[it] == 42, "Wrong map constructor.");
535 for (ArcIt it(G); it != INVALID; ++it) {
537 check(map[it] == 0, "Wrong operator[].");
539 check(map[it] == s, "Wrong set.");
543 for (ArcIt it(G); it != INVALID; ++it) {
546 check(s == 0, "Wrong sum.");
548 // map = constMap<Arc>(12);
549 // for (ArcIt it(G); it != INVALID; ++it) {
550 // check(map[it] == 12, "Wrong operator[].");
554 template <typename Graph>
555 void checkGraphEdgeMap(const Graph& G) {
556 typedef typename Graph::Edge Edge;
557 typedef typename Graph::EdgeIt EdgeIt;
559 typedef typename Graph::template EdgeMap<int> IntEdgeMap;
560 IntEdgeMap map(G, 42);
561 for (EdgeIt it(G); it != INVALID; ++it) {
562 check(map[it] == 42, "Wrong map constructor.");
565 for (EdgeIt it(G); it != INVALID; ++it) {
567 check(map[it] == 0, "Wrong operator[].");
569 check(map[it] == s, "Wrong set.");
573 for (EdgeIt it(G); it != INVALID; ++it) {
576 check(s == 0, "Wrong sum.");
578 // map = constMap<Edge>(12);
579 // for (EdgeIt it(G); it != INVALID; ++it) {
580 // check(map[it] == 12, "Wrong operator[].");