Backport relevant parts of bugfixes [ad22262328b3], [61fdd06833a6] and [4add05447ca0] to branch 1.2 (#623)
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
22 #include <lemon/random.h>
23 #include <lemon/list_graph.h>
24 #include <lemon/smart_graph.h>
25 #include <lemon/maps.h>
27 #include "graph_test.h"
28 #include "test_tools.h"
30 using namespace lemon;
32 template <typename Digraph>
33 void checkFindArcs() {
34 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
38 for (int i = 0; i < 10; ++i) {
41 RangeIdMap<Digraph, Node> nodes(digraph);
42 typename RangeIdMap<Digraph, Node>::InverseMap invNodes(nodes);
43 for (int i = 0; i < 100; ++i) {
44 int src = rnd[invNodes.size()];
45 int trg = rnd[invNodes.size()];
46 digraph.addArc(invNodes[src], invNodes[trg]);
48 typename Digraph::template ArcMap<bool> found(digraph, false);
49 RangeIdMap<Digraph, Arc> arcs(digraph);
50 for (NodeIt src(digraph); src != INVALID; ++src) {
51 for (NodeIt trg(digraph); trg != INVALID; ++trg) {
52 for (ConArcIt<Digraph> con(digraph, src, trg); con != INVALID; ++con) {
53 check(digraph.source(con) == src, "Wrong source.");
54 check(digraph.target(con) == trg, "Wrong target.");
55 check(found[con] == false, "The arc found already.");
60 for (ArcIt it(digraph); it != INVALID; ++it) {
61 check(found[it] == true, "The arc is not found.");
68 std::vector<Node> nodes;
69 for (int i = 0; i < num; ++i) {
70 nodes.push_back(fg.addNode());
72 for (int i = 0; i < num * num; ++i) {
73 fg.addArc(nodes[i / num], nodes[i % num]);
75 check(countNodes(fg) == num, "Wrong node number.");
76 check(countArcs(fg) == num*num, "Wrong arc number.");
77 for (NodeIt src(fg); src != INVALID; ++src) {
78 for (NodeIt trg(fg); trg != INVALID; ++trg) {
79 ConArcIt<Digraph> con(fg, src, trg);
80 check(con != INVALID, "There is no connecting arc.");
81 check(fg.source(con) == src, "Wrong source.");
82 check(fg.target(con) == trg, "Wrong target.");
83 check(++con == INVALID, "There is more connecting arc.");
86 ArcLookUp<Digraph> al1(fg);
87 DynArcLookUp<Digraph> al2(fg);
88 AllArcLookUp<Digraph> al3(fg);
89 for (NodeIt src(fg); src != INVALID; ++src) {
90 for (NodeIt trg(fg); trg != INVALID; ++trg) {
91 Arc con1 = al1(src, trg);
92 Arc con2 = al2(src, trg);
93 Arc con3 = al3(src, trg);
94 Arc con4 = findArc(fg, src, trg);
95 check(con1 == con2 && con2 == con3 && con3 == con4,
97 check(con1 != INVALID, "There is no connecting arc.");
98 check(fg.source(con1) == src, "Wrong source.");
99 check(fg.target(con1) == trg, "Wrong target.");
100 check(al3(src, trg, con3) == INVALID,
101 "There is more connecting arc.");
102 check(findArc(fg, src, trg, con4) == INVALID,
103 "There is more connecting arc.");
109 template <typename Graph>
110 void checkFindEdges() {
111 TEMPLATE_GRAPH_TYPEDEFS(Graph);
113 for (int i = 0; i < 10; ++i) {
116 RangeIdMap<Graph, Node> nodes(graph);
117 typename RangeIdMap<Graph, Node>::InverseMap invNodes(nodes);
118 for (int i = 0; i < 100; ++i) {
119 int src = rnd[invNodes.size()];
120 int trg = rnd[invNodes.size()];
121 graph.addEdge(invNodes[src], invNodes[trg]);
123 typename Graph::template EdgeMap<int> found(graph, 0);
124 RangeIdMap<Graph, Edge> edges(graph);
125 for (NodeIt src(graph); src != INVALID; ++src) {
126 for (NodeIt trg(graph); trg != INVALID; ++trg) {
127 for (ConEdgeIt<Graph> con(graph, src, trg); con != INVALID; ++con) {
128 check( (graph.u(con) == src && graph.v(con) == trg) ||
129 (graph.v(con) == src && graph.u(con) == trg),
132 check(found[con] <= 2, "The edge found more than twice.");
136 for (EdgeIt it(graph); it != INVALID; ++it) {
137 check( (graph.u(it) != graph.v(it) && found[it] == 2) ||
138 (graph.u(it) == graph.v(it) && found[it] == 1),
139 "The edge is not found correctly.");
143 template <class Digraph>
146 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
148 const int nodeNum = 10;
149 const int arcNum = 100;
151 InDegMap<Digraph> inDeg(digraph);
152 OutDegMap<Digraph> outDeg(digraph);
153 std::vector<Node> nodes(nodeNum);
154 for (int i = 0; i < nodeNum; ++i) {
155 nodes[i] = digraph.addNode();
157 std::vector<Arc> arcs(arcNum);
158 for (int i = 0; i < arcNum; ++i) {
159 arcs[i] = digraph.addArc(nodes[rnd[nodeNum]], nodes[rnd[nodeNum]]);
161 for (int i = 0; i < nodeNum; ++i) {
162 check(inDeg[nodes[i]] == countInArcs(digraph, nodes[i]),
163 "Wrong in degree map");
165 for (int i = 0; i < nodeNum; ++i) {
166 check(outDeg[nodes[i]] == countOutArcs(digraph, nodes[i]),
167 "Wrong out degree map");
171 template <class Digraph>
174 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
180 InDegMap<Digraph> ind(g);
184 typename Digraph::Snapshot snap(g);
186 OutDegMap<Digraph> outd(g);
188 check(ind[n1]==0 && ind[n2]==1, "Wrong InDegMap value.");
189 check(outd[n1]==1 && outd[n2]==0, "Wrong OutDegMap value.");
194 check(ind[n1]==1 && ind[n2]==2, "Wrong InDegMap value.");
195 check(outd[n1]==2 && outd[n2]==1, "Wrong OutDegMap value.");
199 check(ind[n1]==0 && ind[n2]==1, "Wrong InDegMap value.");
200 check(outd[n1]==1 && outd[n2]==0, "Wrong OutDegMap value.");
204 // Checking ConArcIt, ConEdgeIt, ArcLookUp, AllArcLookUp, and DynArcLookUp
205 checkFindArcs<ListDigraph>();
206 checkFindArcs<SmartDigraph>();
207 checkFindEdges<ListGraph>();
208 checkFindEdges<SmartGraph>();
210 // Checking In/OutDegMap (and Snapshot feature)
211 checkDeg<ListDigraph>();
212 checkDeg<SmartDigraph>();
213 checkSnapDeg<ListDigraph>();
214 checkSnapDeg<SmartDigraph>();