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/concepts/path.h>
23 #include <lemon/concepts/digraph.h>
24 #include <lemon/concept_check.h>
26 #include <lemon/path.h>
27 #include <lemon/list_graph.h>
29 #include "test_tools.h"
32 using namespace lemon;
34 template <typename GR>
35 void checkConcepts() {
36 checkConcept<concepts::Path<GR>, concepts::Path<GR> >();
37 checkConcept<concepts::Path<GR>, Path<GR> >();
38 checkConcept<concepts::Path<GR>, SimplePath<GR> >();
39 checkConcept<concepts::Path<GR>, StaticPath<GR> >();
40 checkConcept<concepts::Path<GR>, ListPath<GR> >();
43 // Conecpt checking for path structures
44 void checkPathConcepts() {
45 checkConcepts<concepts::Digraph>();
46 checkConcepts<ListDigraph>();
49 // Check if proper copy consructor is called (use valgrind for testing)
50 template <typename GR, typename P1, typename P2>
51 void checkCopy(typename GR::Arc a) {
62 // Tests for copy constructors and assignment operators of paths
63 void checkPathCopy() {
65 ListDigraph::Arc a = g.addArc(g.addNode(), g.addNode());
67 typedef Path<ListDigraph> Path1;
68 typedef SimplePath<ListDigraph> Path2;
69 typedef ListPath<ListDigraph> Path3;
70 typedef StaticPath<ListDigraph> Path4;
71 checkCopy<ListDigraph, Path1, Path2>(a);
72 checkCopy<ListDigraph, Path1, Path3>(a);
73 checkCopy<ListDigraph, Path1, Path4>(a);
74 checkCopy<ListDigraph, Path2, Path1>(a);
75 checkCopy<ListDigraph, Path2, Path3>(a);
76 checkCopy<ListDigraph, Path2, Path4>(a);
77 checkCopy<ListDigraph, Path3, Path1>(a);
78 checkCopy<ListDigraph, Path3, Path2>(a);
79 checkCopy<ListDigraph, Path3, Path4>(a);
82 // Class for testing path functions
83 class CheckPathFunctions {
84 typedef ListDigraph GR;
95 CheckPathFunctions() : cgr(gr) {
100 a1 = gr.addArc(n1, n2);
101 a2 = gr.addArc(n2, n3);
102 a3 = gr.addArc(n3, n4);
103 a4 = gr.addArc(n4, n1);
107 checkBackAndFrontInsertablePath<Path<GR> >();
108 checkBackAndFrontInsertablePath<ListPath<GR> >();
109 checkBackInsertablePath<SimplePath<GR> >();
111 checkListPathSplitAndSplice();
116 template <typename P>
117 void checkBackInsertablePath() {
119 // Create and check empty path
122 check(cp.empty(), "The path is not empty");
123 check(cp.length() == 0, "The path is not empty");
124 // check(cp.front() == INVALID, "Wrong front()");
125 // check(cp.back() == INVALID, "Wrong back()");
126 typename P::ArcIt ai(cp);
127 check(ai == INVALID, "Wrong ArcIt");
128 check(pathSource(cgr, cp) == INVALID, "Wrong pathSource()");
129 check(pathTarget(cgr, cp) == INVALID, "Wrong pathTarget()");
130 check(checkPath(cgr, cp), "Wrong checkPath()");
131 PathNodeIt<P> ni(cgr, cp);
132 check(ni == INVALID, "Wrong PathNodeIt");
134 // Check single-arc path
136 check(!cp.empty(), "Wrong empty()");
137 check(cp.length() == 1, "Wrong length");
138 check(cp.front() == a1, "Wrong front()");
139 check(cp.back() == a1, "Wrong back()");
140 check(cp.nth(0) == a1, "Wrong nth()");
142 check((tmp_a = ai) == a1, "Wrong nthIt()");
143 check(++ai == INVALID, "Wrong nthIt()");
144 typename P::ArcIt ai2(cp);
145 check((tmp_a = ai2) == a1, "Wrong ArcIt");
146 check(++ai2 == INVALID, "Wrong ArcIt");
147 check(pathSource(cgr, cp) == n1, "Wrong pathSource()");
148 check(pathTarget(cgr, cp) == n2, "Wrong pathTarget()");
149 check(checkPath(cgr, cp), "Wrong checkPath()");
150 PathNodeIt<P> ni2(cgr, cp);
151 check((tmp_n = ni2) == n1, "Wrong PathNodeIt");
152 check((tmp_n = ++ni2) == n2, "Wrong PathNodeIt");
153 check(++ni2 == INVALID, "Wrong PathNodeIt");
155 // Check adding more arcs
158 check(!cp.empty(), "Wrong empty()");
159 check(cp.length() == 3, "Wrong length");
160 check(cp.front() == a1, "Wrong front()");
161 check(cp.back() == a3, "Wrong back()");
162 check(cp.nth(0) == a1, "Wrong nth()");
163 check(cp.nth(1) == a2, "Wrong nth()");
164 check(cp.nth(2) == a3, "Wrong nth()");
165 typename P::ArcIt ai3(cp);
166 check((tmp_a = ai3) == a1, "Wrong ArcIt");
167 check((tmp_a = ++ai3) == a2, "Wrong nthIt()");
168 check((tmp_a = ++ai3) == a3, "Wrong nthIt()");
169 check(++ai3 == INVALID, "Wrong nthIt()");
171 check((tmp_a = ai) == a1, "Wrong nthIt()");
172 check((tmp_a = ++ai) == a2, "Wrong nthIt()");
174 check((tmp_a = ai) == a3, "Wrong nthIt()");
175 check(++ai == INVALID, "Wrong nthIt()");
176 check(pathSource(cgr, cp) == n1, "Wrong pathSource()");
177 check(pathTarget(cgr, cp) == n4, "Wrong pathTarget()");
178 check(checkPath(cgr, cp), "Wrong checkPath()");
179 PathNodeIt<P> ni3(cgr, cp);
180 check((tmp_n = ni3) == n1, "Wrong PathNodeIt");
181 check((tmp_n = ++ni3) == n2, "Wrong PathNodeIt");
182 check((tmp_n = ++ni3) == n3, "Wrong PathNodeIt");
183 check((tmp_n = ++ni3) == n4, "Wrong PathNodeIt");
184 check(++ni3 == INVALID, "Wrong PathNodeIt");
186 // Check arc removal and addition
190 check(!cp.empty(), "Wrong empty()");
191 check(cp.length() == 2, "Wrong length");
192 check(cp.front() == a1, "Wrong front()");
193 check(cp.back() == a2, "Wrong back()");
194 check(pathSource(cgr, cp) == n1, "Wrong pathSource()");
195 check(pathTarget(cgr, cp) == n3, "Wrong pathTarget()");
196 check(checkPath(cgr, cp), "Wrong checkPath()");
200 check(cp.empty(), "The path is not empty");
201 check(cp.length() == 0, "The path is not empty");
203 // Check inconsistent path
207 check(!cp.empty(), "Wrong empty()");
208 check(cp.length() == 3, "Wrong length");
209 check(cp.front() == a4, "Wrong front()");
210 check(cp.back() == a1, "Wrong back()");
211 check(pathSource(cgr, cp) == n4, "Wrong pathSource()");
212 check(pathTarget(cgr, cp) == n2, "Wrong pathTarget()");
213 check(!checkPath(cgr, cp), "Wrong checkPath()");
216 template <typename P>
217 void checkBackAndFrontInsertablePath() {
219 // Include back insertable test cases
220 checkBackInsertablePath<P>();
222 // Check front and back insertion
228 check(!cp.empty(), "Wrong empty()");
229 check(cp.length() == 3, "Wrong length");
230 check(cp.front() == a3, "Wrong front()");
231 check(cp.back() == a1, "Wrong back()");
232 check(cp.nth(0) == a3, "Wrong nth()");
233 check(cp.nth(1) == a4, "Wrong nth()");
234 check(cp.nth(2) == a1, "Wrong nth()");
235 typename P::ArcIt ai(cp);
236 check((tmp_a = ai) == a3, "Wrong ArcIt");
237 check((tmp_a = ++ai) == a4, "Wrong nthIt()");
238 check((tmp_a = ++ai) == a1, "Wrong nthIt()");
239 check(++ai == INVALID, "Wrong nthIt()");
241 check((tmp_a = ai) == a3, "Wrong nthIt()");
242 check((tmp_a = ++ai) == a4, "Wrong nthIt()");
244 check((tmp_a = ai) == a1, "Wrong nthIt()");
245 check(++ai == INVALID, "Wrong nthIt()");
246 check(pathSource(cgr, cp) == n3, "Wrong pathSource()");
247 check(pathTarget(cgr, cp) == n2, "Wrong pathTarget()");
248 check(checkPath(cgr, cp), "Wrong checkPath()");
250 // Check eraseFront()
253 check(!cp.empty(), "Wrong empty()");
254 check(cp.length() == 3, "Wrong length");
255 check(cp.front() == a4, "Wrong front()");
256 check(cp.back() == a2, "Wrong back()");
257 check(cp.nth(0) == a4, "Wrong nth()");
258 check(cp.nth(1) == a1, "Wrong nth()");
259 check(cp.nth(2) == a2, "Wrong nth()");
260 typename P::ArcIt ai2(cp);
261 check((tmp_a = ai2) == a4, "Wrong ArcIt");
262 check((tmp_a = ++ai2) == a1, "Wrong nthIt()");
263 check((tmp_a = ++ai2) == a2, "Wrong nthIt()");
264 check(++ai2 == INVALID, "Wrong nthIt()");
266 check((tmp_a = ai) == a4, "Wrong nthIt()");
267 check((tmp_a = ++ai) == a1, "Wrong nthIt()");
269 check((tmp_a = ai) == a2, "Wrong nthIt()");
270 check(++ai == INVALID, "Wrong nthIt()");
271 check(pathSource(cgr, cp) == n4, "Wrong pathSource()");
272 check(pathTarget(cgr, cp) == n3, "Wrong pathTarget()");
273 check(checkPath(cgr, cp), "Wrong checkPath()");
276 void checkListPathSplitAndSplice() {
278 // Build a path with spliceFront() and spliceBack()
279 ListPath<GR> p1, p2, p3, p4;
286 check(p1.length() == 4, "Wrong length");
287 check(p1.front() == a1, "Wrong front()");
288 check(p1.back() == a4, "Wrong back()");
289 ListPath<GR>::ArcIt ai(p1);
290 check((tmp_a = ai) == a1, "Wrong ArcIt");
291 check((tmp_a = ++ai) == a2, "Wrong nthIt()");
292 check((tmp_a = ++ai) == a3, "Wrong nthIt()");
293 check((tmp_a = ++ai) == a4, "Wrong nthIt()");
294 check(++ai == INVALID, "Wrong nthIt()");
295 check(checkPath(cgr, p1), "Wrong checkPath()");
298 p1.split(p1.nthIt(2), p2);
299 check(p1.length() == 2, "Wrong length");
301 check((tmp_a = ai) == a1, "Wrong ArcIt");
302 check((tmp_a = ++ai) == a2, "Wrong nthIt()");
303 check(++ai == INVALID, "Wrong nthIt()");
304 check(checkPath(cgr, p1), "Wrong checkPath()");
305 check(p2.length() == 2, "Wrong length");
307 check((tmp_a = ai) == a3, "Wrong ArcIt");
308 check((tmp_a = ++ai) == a4, "Wrong nthIt()");
309 check(++ai == INVALID, "Wrong nthIt()");
310 check(checkPath(cgr, p2), "Wrong checkPath()");
312 // Check split() and splice()
314 p1.split(p1.nthIt(2), p2);
315 p2.split(p2.nthIt(1), p3);
317 p2.splice(p2.nthIt(1), p3);
318 check(p2.length() == 4, "Wrong length");
319 check(p2.front() == a1, "Wrong front()");
320 check(p2.back() == a4, "Wrong back()");
322 check((tmp_a = ai) == a1, "Wrong ArcIt");
323 check((tmp_a = ++ai) == a2, "Wrong nthIt()");
324 check((tmp_a = ++ai) == a3, "Wrong nthIt()");
325 check((tmp_a = ++ai) == a4, "Wrong nthIt()");
326 check(++ai == INVALID, "Wrong nthIt()");
327 check(checkPath(cgr, p2), "Wrong checkPath()");
335 CheckPathFunctions cpf;