1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <lemon/smart_graph.h> |
20 | 20 |
#include <lemon/list_graph.h> |
21 |
#include <lemon/static_graph.h> |
|
21 | 22 |
#include <lemon/lgf_reader.h> |
22 | 23 |
#include <lemon/error.h> |
23 | 24 |
|
24 | 25 |
#include "test_tools.h" |
25 | 26 |
|
26 | 27 |
using namespace std; |
27 | 28 |
using namespace lemon; |
28 | 29 |
|
30 |
template <typename GR> |
|
29 | 31 |
void digraph_copy_test() { |
30 | 32 |
const int nn = 10; |
31 | 33 |
|
32 | 34 |
// Build a digraph |
33 | 35 |
SmartDigraph from; |
34 | 36 |
SmartDigraph::NodeMap<int> fnm(from); |
35 | 37 |
SmartDigraph::ArcMap<int> fam(from); |
36 | 38 |
SmartDigraph::Node fn = INVALID; |
37 | 39 |
SmartDigraph::Arc fa = INVALID; |
38 | 40 |
|
39 | 41 |
std::vector<SmartDigraph::Node> fnv; |
40 | 42 |
for (int i = 0; i < nn; ++i) { |
41 | 43 |
SmartDigraph::Node node = from.addNode(); |
42 | 44 |
fnv.push_back(node); |
43 | 45 |
fnm[node] = i * i; |
44 | 46 |
if (i == 0) fn = node; |
45 | 47 |
} |
46 | 48 |
|
47 | 49 |
for (int i = 0; i < nn; ++i) { |
48 | 50 |
for (int j = 0; j < nn; ++j) { |
49 | 51 |
SmartDigraph::Arc arc = from.addArc(fnv[i], fnv[j]); |
50 | 52 |
fam[arc] = i + j * j; |
51 | 53 |
if (i == 0 && j == 0) fa = arc; |
52 | 54 |
} |
53 | 55 |
} |
54 | 56 |
|
55 | 57 |
// Test digraph copy |
56 |
ListDigraph to; |
|
57 |
ListDigraph::NodeMap<int> tnm(to); |
|
58 |
ListDigraph::ArcMap<int> tam(to); |
|
59 |
ListDigraph::Node tn; |
|
60 |
|
|
58 |
GR to; |
|
59 |
typename GR::template NodeMap<int> tnm(to); |
|
60 |
typename GR::template ArcMap<int> tam(to); |
|
61 |
typename GR::Node tn; |
|
62 |
typename GR::Arc ta; |
|
61 | 63 |
|
62 |
SmartDigraph::NodeMap<ListDigraph::Node> nr(from); |
|
63 |
SmartDigraph::ArcMap<ListDigraph::Arc> er(from); |
|
64 |
SmartDigraph::NodeMap<typename GR::Node> nr(from); |
|
65 |
SmartDigraph::ArcMap<typename GR::Arc> er(from); |
|
64 | 66 |
|
65 |
ListDigraph::NodeMap<SmartDigraph::Node> ncr(to); |
|
66 |
ListDigraph::ArcMap<SmartDigraph::Arc> ecr(to); |
|
67 |
typename GR::template NodeMap<SmartDigraph::Node> ncr(to); |
|
68 |
typename GR::template ArcMap<SmartDigraph::Arc> ecr(to); |
|
67 | 69 |
|
68 | 70 |
digraphCopy(from, to). |
69 | 71 |
nodeMap(fnm, tnm).arcMap(fam, tam). |
70 | 72 |
nodeRef(nr).arcRef(er). |
71 | 73 |
nodeCrossRef(ncr).arcCrossRef(ecr). |
72 | 74 |
node(fn, tn).arc(fa, ta).run(); |
73 | 75 |
|
74 | 76 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
75 | 77 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
76 | 78 |
|
77 | 79 |
for (SmartDigraph::NodeIt it(from); it != INVALID; ++it) { |
78 | 80 |
check(ncr[nr[it]] == it, "Wrong copy."); |
79 | 81 |
check(fnm[it] == tnm[nr[it]], "Wrong copy."); |
80 | 82 |
} |
81 | 83 |
|
82 | 84 |
for (SmartDigraph::ArcIt it(from); it != INVALID; ++it) { |
83 | 85 |
check(ecr[er[it]] == it, "Wrong copy."); |
84 | 86 |
check(fam[it] == tam[er[it]], "Wrong copy."); |
85 | 87 |
check(nr[from.source(it)] == to.source(er[it]), "Wrong copy."); |
86 | 88 |
check(nr[from.target(it)] == to.target(er[it]), "Wrong copy."); |
87 | 89 |
} |
88 | 90 |
|
89 |
for ( |
|
91 |
for (typename GR::NodeIt it(to); it != INVALID; ++it) { |
|
90 | 92 |
check(nr[ncr[it]] == it, "Wrong copy."); |
91 | 93 |
} |
92 | 94 |
|
93 |
for ( |
|
95 |
for (typename GR::ArcIt it(to); it != INVALID; ++it) { |
|
94 | 96 |
check(er[ecr[it]] == it, "Wrong copy."); |
95 | 97 |
} |
96 | 98 |
check(tn == nr[fn], "Wrong copy."); |
97 | 99 |
check(ta == er[fa], "Wrong copy."); |
98 | 100 |
|
99 | 101 |
// Test repeated copy |
100 | 102 |
digraphCopy(from, to).run(); |
101 | 103 |
|
102 | 104 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
103 | 105 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
104 | 106 |
} |
105 | 107 |
|
108 |
template <typename GR> |
|
106 | 109 |
void graph_copy_test() { |
107 | 110 |
const int nn = 10; |
108 | 111 |
|
109 | 112 |
// Build a graph |
110 | 113 |
SmartGraph from; |
111 | 114 |
SmartGraph::NodeMap<int> fnm(from); |
112 | 115 |
SmartGraph::ArcMap<int> fam(from); |
113 | 116 |
SmartGraph::EdgeMap<int> fem(from); |
114 | 117 |
SmartGraph::Node fn = INVALID; |
115 | 118 |
SmartGraph::Arc fa = INVALID; |
116 | 119 |
SmartGraph::Edge fe = INVALID; |
117 | 120 |
|
118 | 121 |
std::vector<SmartGraph::Node> fnv; |
119 | 122 |
for (int i = 0; i < nn; ++i) { |
120 | 123 |
SmartGraph::Node node = from.addNode(); |
121 | 124 |
fnv.push_back(node); |
122 | 125 |
fnm[node] = i * i; |
123 | 126 |
if (i == 0) fn = node; |
124 | 127 |
} |
125 | 128 |
|
126 | 129 |
for (int i = 0; i < nn; ++i) { |
127 | 130 |
for (int j = 0; j < nn; ++j) { |
128 | 131 |
SmartGraph::Edge edge = from.addEdge(fnv[i], fnv[j]); |
129 | 132 |
fem[edge] = i * i + j * j; |
130 | 133 |
fam[from.direct(edge, true)] = i + j * j; |
131 | 134 |
fam[from.direct(edge, false)] = i * i + j; |
132 | 135 |
if (i == 0 && j == 0) fa = from.direct(edge, true); |
133 | 136 |
if (i == 0 && j == 0) fe = edge; |
134 | 137 |
} |
135 | 138 |
} |
136 | 139 |
|
137 | 140 |
// Test graph copy |
138 |
ListGraph to; |
|
139 |
ListGraph::NodeMap<int> tnm(to); |
|
140 |
ListGraph::ArcMap<int> tam(to); |
|
141 |
ListGraph::EdgeMap<int> tem(to); |
|
142 |
ListGraph::Node tn; |
|
143 |
ListGraph::Arc ta; |
|
144 |
|
|
141 |
GR to; |
|
142 |
typename GR::template NodeMap<int> tnm(to); |
|
143 |
typename GR::template ArcMap<int> tam(to); |
|
144 |
typename GR::template EdgeMap<int> tem(to); |
|
145 |
typename GR::Node tn; |
|
146 |
typename GR::Arc ta; |
|
147 |
typename GR::Edge te; |
|
145 | 148 |
|
146 |
SmartGraph::NodeMap<ListGraph::Node> nr(from); |
|
147 |
SmartGraph::ArcMap<ListGraph::Arc> ar(from); |
|
148 |
SmartGraph:: |
|
149 |
SmartGraph::NodeMap<typename GR::Node> nr(from); |
|
150 |
SmartGraph::ArcMap<typename GR::Arc> ar(from); |
|
151 |
SmartGraph::EdgeMap<typename GR::Edge> er(from); |
|
149 | 152 |
|
150 |
ListGraph::NodeMap<SmartGraph::Node> ncr(to); |
|
151 |
ListGraph::ArcMap<SmartGraph::Arc> acr(to); |
|
152 |
|
|
153 |
typename GR::template NodeMap<SmartGraph::Node> ncr(to); |
|
154 |
typename GR::template ArcMap<SmartGraph::Arc> acr(to); |
|
155 |
typename GR::template EdgeMap<SmartGraph::Edge> ecr(to); |
|
153 | 156 |
|
154 | 157 |
graphCopy(from, to). |
155 | 158 |
nodeMap(fnm, tnm).arcMap(fam, tam).edgeMap(fem, tem). |
156 | 159 |
nodeRef(nr).arcRef(ar).edgeRef(er). |
157 | 160 |
nodeCrossRef(ncr).arcCrossRef(acr).edgeCrossRef(ecr). |
158 | 161 |
node(fn, tn).arc(fa, ta).edge(fe, te).run(); |
159 | 162 |
|
160 | 163 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
161 | 164 |
check(countEdges(from) == countEdges(to), "Wrong copy."); |
162 | 165 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
163 | 166 |
|
164 | 167 |
for (SmartGraph::NodeIt it(from); it != INVALID; ++it) { |
165 | 168 |
check(ncr[nr[it]] == it, "Wrong copy."); |
166 | 169 |
check(fnm[it] == tnm[nr[it]], "Wrong copy."); |
167 | 170 |
} |
168 | 171 |
|
169 | 172 |
for (SmartGraph::ArcIt it(from); it != INVALID; ++it) { |
170 | 173 |
check(acr[ar[it]] == it, "Wrong copy."); |
171 | 174 |
check(fam[it] == tam[ar[it]], "Wrong copy."); |
172 | 175 |
check(nr[from.source(it)] == to.source(ar[it]), "Wrong copy."); |
173 | 176 |
check(nr[from.target(it)] == to.target(ar[it]), "Wrong copy."); |
174 | 177 |
} |
175 | 178 |
|
176 | 179 |
for (SmartGraph::EdgeIt it(from); it != INVALID; ++it) { |
177 | 180 |
check(ecr[er[it]] == it, "Wrong copy."); |
178 | 181 |
check(fem[it] == tem[er[it]], "Wrong copy."); |
179 | 182 |
check(nr[from.u(it)] == to.u(er[it]) || nr[from.u(it)] == to.v(er[it]), |
180 | 183 |
"Wrong copy."); |
181 | 184 |
check(nr[from.v(it)] == to.u(er[it]) || nr[from.v(it)] == to.v(er[it]), |
182 | 185 |
"Wrong copy."); |
183 | 186 |
check((from.u(it) != from.v(it)) == (to.u(er[it]) != to.v(er[it])), |
184 | 187 |
"Wrong copy."); |
185 | 188 |
} |
186 | 189 |
|
187 |
for ( |
|
190 |
for (typename GR::NodeIt it(to); it != INVALID; ++it) { |
|
188 | 191 |
check(nr[ncr[it]] == it, "Wrong copy."); |
189 | 192 |
} |
190 | 193 |
|
191 |
for ( |
|
194 |
for (typename GR::ArcIt it(to); it != INVALID; ++it) { |
|
192 | 195 |
check(ar[acr[it]] == it, "Wrong copy."); |
193 | 196 |
} |
194 |
for ( |
|
197 |
for (typename GR::EdgeIt it(to); it != INVALID; ++it) { |
|
195 | 198 |
check(er[ecr[it]] == it, "Wrong copy."); |
196 | 199 |
} |
197 | 200 |
check(tn == nr[fn], "Wrong copy."); |
198 | 201 |
check(ta == ar[fa], "Wrong copy."); |
199 | 202 |
check(te == er[fe], "Wrong copy."); |
200 | 203 |
|
201 | 204 |
// Test repeated copy |
202 | 205 |
graphCopy(from, to).run(); |
203 | 206 |
|
204 | 207 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
205 | 208 |
check(countEdges(from) == countEdges(to), "Wrong copy."); |
206 | 209 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
207 | 210 |
} |
208 | 211 |
|
209 | 212 |
|
210 | 213 |
int main() { |
211 |
digraph_copy_test(); |
|
212 |
graph_copy_test(); |
|
214 |
digraph_copy_test<SmartDigraph>(); |
|
215 |
digraph_copy_test<ListDigraph>(); |
|
216 |
digraph_copy_test<StaticDigraph>(); |
|
217 |
graph_copy_test<SmartGraph>(); |
|
218 |
graph_copy_test<ListGraph>(); |
|
213 | 219 |
|
214 | 220 |
return 0; |
215 | 221 |
} |
0 comments (0 inline)