test/path_test.cc
changeset 1044 15d7c5eadaca
parent 990 7440937d154b
child 1092 dceba191c00d
     1.1 --- a/test/path_test.cc	Sat Mar 20 11:03:12 2010 +0100
     1.2 +++ b/test/path_test.cc	Fri Mar 08 10:47:38 2013 +0100
     1.3 @@ -21,6 +21,7 @@
     1.4  
     1.5  #include <lemon/concepts/path.h>
     1.6  #include <lemon/concepts/digraph.h>
     1.7 +#include <lemon/concept_check.h>
     1.8  
     1.9  #include <lemon/path.h>
    1.10  #include <lemon/list_graph.h>
    1.11 @@ -30,44 +31,309 @@
    1.12  using namespace std;
    1.13  using namespace lemon;
    1.14  
    1.15 -void check_concepts() {
    1.16 -  checkConcept<concepts::Path<ListDigraph>, concepts::Path<ListDigraph> >();
    1.17 -  checkConcept<concepts::Path<ListDigraph>, Path<ListDigraph> >();
    1.18 -  checkConcept<concepts::Path<ListDigraph>, SimplePath<ListDigraph> >();
    1.19 -  checkConcept<concepts::Path<ListDigraph>, StaticPath<ListDigraph> >();
    1.20 -  checkConcept<concepts::Path<ListDigraph>, ListPath<ListDigraph> >();
    1.21 +template <typename GR>
    1.22 +void checkConcepts() {
    1.23 +  checkConcept<concepts::Path<GR>, concepts::Path<GR> >();
    1.24 +  checkConcept<concepts::Path<GR>, Path<GR> >();
    1.25 +  checkConcept<concepts::Path<GR>, SimplePath<GR> >();
    1.26 +  checkConcept<concepts::Path<GR>, StaticPath<GR> >();
    1.27 +  checkConcept<concepts::Path<GR>, ListPath<GR> >();
    1.28 +}
    1.29 +
    1.30 +// Conecpt checking for path structures
    1.31 +void checkPathConcepts() {
    1.32 +  checkConcepts<concepts::Digraph>();
    1.33 +  checkConcepts<ListDigraph>();
    1.34  }
    1.35  
    1.36  // Check if proper copy consructor is called (use valgrind for testing)
    1.37 -template<class _Path>
    1.38 -void checkCopy()
    1.39 -{
    1.40 +template <typename GR, typename P1, typename P2>
    1.41 +void checkCopy(typename GR::Arc a) {
    1.42 +  P1 p;
    1.43 +  p.addBack(a);
    1.44 +  P1 q;
    1.45 +  q = p;
    1.46 +  P1 r(p);
    1.47 +  P2 q2;
    1.48 +  q2 = p;
    1.49 +  P2 r2(p);
    1.50 +}
    1.51 +
    1.52 +// Tests for copy constructors and assignment operators of paths
    1.53 +void checkPathCopy() {
    1.54    ListDigraph g;
    1.55 -  ListDigraph::Arc a  = g.addArc(g.addNode(), g.addNode());
    1.56 -  
    1.57 -  _Path p,q;
    1.58 -  p.addBack(a);
    1.59 -  q=p;
    1.60 -  _Path r(p);
    1.61 -  StaticPath<ListDigraph> s(r);
    1.62 +  ListDigraph::Arc a = g.addArc(g.addNode(), g.addNode());
    1.63 +
    1.64 +  typedef Path<ListDigraph> Path1;
    1.65 +  typedef SimplePath<ListDigraph> Path2;
    1.66 +  typedef ListPath<ListDigraph> Path3;
    1.67 +  typedef StaticPath<ListDigraph> Path4;
    1.68 +  checkCopy<ListDigraph, Path1, Path2>(a);
    1.69 +  checkCopy<ListDigraph, Path1, Path3>(a);
    1.70 +  checkCopy<ListDigraph, Path1, Path4>(a);
    1.71 +  checkCopy<ListDigraph, Path2, Path1>(a);
    1.72 +  checkCopy<ListDigraph, Path2, Path3>(a);
    1.73 +  checkCopy<ListDigraph, Path2, Path4>(a);
    1.74 +  checkCopy<ListDigraph, Path3, Path1>(a);
    1.75 +  checkCopy<ListDigraph, Path3, Path2>(a);
    1.76 +  checkCopy<ListDigraph, Path3, Path4>(a);
    1.77  }
    1.78 -  
    1.79 +
    1.80 +// Class for testing path functions
    1.81 +class CheckPathFunctions {
    1.82 +  typedef ListDigraph GR;
    1.83 +  DIGRAPH_TYPEDEFS(GR);
    1.84 +  GR gr;
    1.85 +  const GR& cgr;
    1.86 +  Node n1, n2, n3, n4;
    1.87 +  Node tmp_n;
    1.88 +  Arc a1, a2, a3, a4;
    1.89 +  Arc tmp_a;
    1.90 +
    1.91 +public:
    1.92 +
    1.93 +  CheckPathFunctions() : cgr(gr) {
    1.94 +    n1 = gr.addNode();
    1.95 +    n2 = gr.addNode();
    1.96 +    n3 = gr.addNode();
    1.97 +    n4 = gr.addNode();
    1.98 +    a1 = gr.addArc(n1, n2);
    1.99 +    a2 = gr.addArc(n2, n3);
   1.100 +    a3 = gr.addArc(n3, n4);
   1.101 +    a4 = gr.addArc(n4, n1);
   1.102 +  }
   1.103 +
   1.104 +  void run() {
   1.105 +    checkBackAndFrontInsertablePath<Path<GR> >();
   1.106 +    checkBackAndFrontInsertablePath<ListPath<GR> >();
   1.107 +    checkBackInsertablePath<SimplePath<GR> >();
   1.108 +
   1.109 +    checkListPathSplitAndSplice();
   1.110 +  }
   1.111 +
   1.112 +private:
   1.113 +
   1.114 +  template <typename P>
   1.115 +  void checkBackInsertablePath() {
   1.116 +
   1.117 +    // Create and check empty path
   1.118 +    P p;
   1.119 +    const P& cp = p;
   1.120 +    check(cp.empty(), "The path is not empty");
   1.121 +    check(cp.length() == 0, "The path is not empty");
   1.122 +//    check(cp.front() == INVALID, "Wrong front()");
   1.123 +//    check(cp.back() == INVALID, "Wrong back()");
   1.124 +    typename P::ArcIt ai(cp);
   1.125 +    check(ai == INVALID, "Wrong ArcIt");
   1.126 +    check(pathSource(cgr, cp) == INVALID, "Wrong pathSource()");
   1.127 +    check(pathTarget(cgr, cp) == INVALID, "Wrong pathTarget()");
   1.128 +    check(checkPath(cgr, cp), "Wrong checkPath()");
   1.129 +    PathNodeIt<P> ni(cgr, cp);
   1.130 +    check(ni == INVALID, "Wrong PathNodeIt");
   1.131 +
   1.132 +    // Check single-arc path
   1.133 +    p.addBack(a1);
   1.134 +    check(!cp.empty(), "Wrong empty()");
   1.135 +    check(cp.length() == 1, "Wrong length");
   1.136 +    check(cp.front() == a1, "Wrong front()");
   1.137 +    check(cp.back() == a1, "Wrong back()");
   1.138 +    check(cp.nth(0) == a1, "Wrong nth()");
   1.139 +    ai = cp.nthIt(0);
   1.140 +    check((tmp_a = ai) == a1, "Wrong nthIt()");
   1.141 +    check(++ai == INVALID, "Wrong nthIt()");
   1.142 +    typename P::ArcIt ai2(cp);
   1.143 +    check((tmp_a = ai2) == a1, "Wrong ArcIt");
   1.144 +    check(++ai2 == INVALID, "Wrong ArcIt");
   1.145 +    check(pathSource(cgr, cp) == n1, "Wrong pathSource()");
   1.146 +    check(pathTarget(cgr, cp) == n2, "Wrong pathTarget()");
   1.147 +    check(checkPath(cgr, cp), "Wrong checkPath()");
   1.148 +    PathNodeIt<P> ni2(cgr, cp);
   1.149 +    check((tmp_n = ni2) == n1, "Wrong PathNodeIt");
   1.150 +    check((tmp_n = ++ni2) == n2, "Wrong PathNodeIt");
   1.151 +    check(++ni2 == INVALID, "Wrong PathNodeIt");
   1.152 +
   1.153 +    // Check adding more arcs
   1.154 +    p.addBack(a2);
   1.155 +    p.addBack(a3);
   1.156 +    check(!cp.empty(), "Wrong empty()");
   1.157 +    check(cp.length() == 3, "Wrong length");
   1.158 +    check(cp.front() == a1, "Wrong front()");
   1.159 +    check(cp.back() == a3, "Wrong back()");
   1.160 +    check(cp.nth(0) == a1, "Wrong nth()");
   1.161 +    check(cp.nth(1) == a2, "Wrong nth()");
   1.162 +    check(cp.nth(2) == a3, "Wrong nth()");
   1.163 +    typename P::ArcIt ai3(cp);
   1.164 +    check((tmp_a = ai3) == a1, "Wrong ArcIt");
   1.165 +    check((tmp_a = ++ai3) == a2, "Wrong nthIt()");
   1.166 +    check((tmp_a = ++ai3) == a3, "Wrong nthIt()");
   1.167 +    check(++ai3 == INVALID, "Wrong nthIt()");
   1.168 +    ai = cp.nthIt(0);
   1.169 +    check((tmp_a = ai) == a1, "Wrong nthIt()");
   1.170 +    check((tmp_a = ++ai) == a2, "Wrong nthIt()");
   1.171 +    ai = cp.nthIt(2);
   1.172 +    check((tmp_a = ai) == a3, "Wrong nthIt()");
   1.173 +    check(++ai == INVALID, "Wrong nthIt()");
   1.174 +    check(pathSource(cgr, cp) == n1, "Wrong pathSource()");
   1.175 +    check(pathTarget(cgr, cp) == n4, "Wrong pathTarget()");
   1.176 +    check(checkPath(cgr, cp), "Wrong checkPath()");
   1.177 +    PathNodeIt<P> ni3(cgr, cp);
   1.178 +    check((tmp_n = ni3) == n1, "Wrong PathNodeIt");
   1.179 +    check((tmp_n = ++ni3) == n2, "Wrong PathNodeIt");
   1.180 +    check((tmp_n = ++ni3) == n3, "Wrong PathNodeIt");
   1.181 +    check((tmp_n = ++ni3) == n4, "Wrong PathNodeIt");
   1.182 +    check(++ni3 == INVALID, "Wrong PathNodeIt");
   1.183 +
   1.184 +    // Check arc removal and addition
   1.185 +    p.eraseBack();
   1.186 +    p.eraseBack();
   1.187 +    p.addBack(a2);
   1.188 +    check(!cp.empty(), "Wrong empty()");
   1.189 +    check(cp.length() == 2, "Wrong length");
   1.190 +    check(cp.front() == a1, "Wrong front()");
   1.191 +    check(cp.back() == a2, "Wrong back()");
   1.192 +    check(pathSource(cgr, cp) == n1, "Wrong pathSource()");
   1.193 +    check(pathTarget(cgr, cp) == n3, "Wrong pathTarget()");
   1.194 +    check(checkPath(cgr, cp), "Wrong checkPath()");
   1.195 +
   1.196 +    // Check clear()
   1.197 +    p.clear();
   1.198 +    check(cp.empty(), "The path is not empty");
   1.199 +    check(cp.length() == 0, "The path is not empty");
   1.200 +
   1.201 +    // Check inconsistent path
   1.202 +    p.addBack(a4);
   1.203 +    p.addBack(a2);
   1.204 +    p.addBack(a1);
   1.205 +    check(!cp.empty(), "Wrong empty()");
   1.206 +    check(cp.length() == 3, "Wrong length");
   1.207 +    check(cp.front() == a4, "Wrong front()");
   1.208 +    check(cp.back() == a1, "Wrong back()");
   1.209 +    check(pathSource(cgr, cp) == n4, "Wrong pathSource()");
   1.210 +    check(pathTarget(cgr, cp) == n2, "Wrong pathTarget()");
   1.211 +    check(!checkPath(cgr, cp), "Wrong checkPath()");
   1.212 +  }
   1.213 +
   1.214 +  template <typename P>
   1.215 +  void checkBackAndFrontInsertablePath() {
   1.216 +
   1.217 +    // Include back insertable test cases
   1.218 +    checkBackInsertablePath<P>();
   1.219 +
   1.220 +    // Check front and back insertion
   1.221 +    P p;
   1.222 +    const P& cp = p;
   1.223 +    p.addFront(a4);
   1.224 +    p.addBack(a1);
   1.225 +    p.addFront(a3);
   1.226 +    check(!cp.empty(), "Wrong empty()");
   1.227 +    check(cp.length() == 3, "Wrong length");
   1.228 +    check(cp.front() == a3, "Wrong front()");
   1.229 +    check(cp.back() == a1, "Wrong back()");
   1.230 +    check(cp.nth(0) == a3, "Wrong nth()");
   1.231 +    check(cp.nth(1) == a4, "Wrong nth()");
   1.232 +    check(cp.nth(2) == a1, "Wrong nth()");
   1.233 +    typename P::ArcIt ai(cp);
   1.234 +    check((tmp_a = ai) == a3, "Wrong ArcIt");
   1.235 +    check((tmp_a = ++ai) == a4, "Wrong nthIt()");
   1.236 +    check((tmp_a = ++ai) == a1, "Wrong nthIt()");
   1.237 +    check(++ai == INVALID, "Wrong nthIt()");
   1.238 +    ai = cp.nthIt(0);
   1.239 +    check((tmp_a = ai) == a3, "Wrong nthIt()");
   1.240 +    check((tmp_a = ++ai) == a4, "Wrong nthIt()");
   1.241 +    ai = cp.nthIt(2);
   1.242 +    check((tmp_a = ai) == a1, "Wrong nthIt()");
   1.243 +    check(++ai == INVALID, "Wrong nthIt()");
   1.244 +    check(pathSource(cgr, cp) == n3, "Wrong pathSource()");
   1.245 +    check(pathTarget(cgr, cp) == n2, "Wrong pathTarget()");
   1.246 +    check(checkPath(cgr, cp), "Wrong checkPath()");
   1.247 +
   1.248 +    // Check eraseFront()
   1.249 +    p.eraseFront();
   1.250 +    p.addBack(a2);
   1.251 +    check(!cp.empty(), "Wrong empty()");
   1.252 +    check(cp.length() == 3, "Wrong length");
   1.253 +    check(cp.front() == a4, "Wrong front()");
   1.254 +    check(cp.back() == a2, "Wrong back()");
   1.255 +    check(cp.nth(0) == a4, "Wrong nth()");
   1.256 +    check(cp.nth(1) == a1, "Wrong nth()");
   1.257 +    check(cp.nth(2) == a2, "Wrong nth()");
   1.258 +    typename P::ArcIt ai2(cp);
   1.259 +    check((tmp_a = ai2) == a4, "Wrong ArcIt");
   1.260 +    check((tmp_a = ++ai2) == a1, "Wrong nthIt()");
   1.261 +    check((tmp_a = ++ai2) == a2, "Wrong nthIt()");
   1.262 +    check(++ai2 == INVALID, "Wrong nthIt()");
   1.263 +    ai = cp.nthIt(0);
   1.264 +    check((tmp_a = ai) == a4, "Wrong nthIt()");
   1.265 +    check((tmp_a = ++ai) == a1, "Wrong nthIt()");
   1.266 +    ai = cp.nthIt(2);
   1.267 +    check((tmp_a = ai) == a2, "Wrong nthIt()");
   1.268 +    check(++ai == INVALID, "Wrong nthIt()");
   1.269 +    check(pathSource(cgr, cp) == n4, "Wrong pathSource()");
   1.270 +    check(pathTarget(cgr, cp) == n3, "Wrong pathTarget()");
   1.271 +    check(checkPath(cgr, cp), "Wrong checkPath()");
   1.272 +  }
   1.273 +
   1.274 +  void checkListPathSplitAndSplice() {
   1.275 +
   1.276 +    // Build a path with spliceFront() and spliceBack()
   1.277 +    ListPath<GR> p1, p2, p3, p4;
   1.278 +    p1.addBack(a3);
   1.279 +    p1.addFront(a2);
   1.280 +    p2.addBack(a1);
   1.281 +    p1.spliceFront(p2);
   1.282 +    p3.addFront(a4);
   1.283 +    p1.spliceBack(p3);
   1.284 +    check(p1.length() == 4, "Wrong length");
   1.285 +    check(p1.front() == a1, "Wrong front()");
   1.286 +    check(p1.back() == a4, "Wrong back()");
   1.287 +    ListPath<GR>::ArcIt ai(p1);
   1.288 +    check((tmp_a = ai) == a1, "Wrong ArcIt");
   1.289 +    check((tmp_a = ++ai) == a2, "Wrong nthIt()");
   1.290 +    check((tmp_a = ++ai) == a3, "Wrong nthIt()");
   1.291 +    check((tmp_a = ++ai) == a4, "Wrong nthIt()");
   1.292 +    check(++ai == INVALID, "Wrong nthIt()");
   1.293 +    check(checkPath(cgr, p1), "Wrong checkPath()");
   1.294 +
   1.295 +    // Check split()
   1.296 +    p1.split(p1.nthIt(2), p2);
   1.297 +    check(p1.length() == 2, "Wrong length");
   1.298 +    ai = p1.nthIt(0);
   1.299 +    check((tmp_a = ai) == a1, "Wrong ArcIt");
   1.300 +    check((tmp_a = ++ai) == a2, "Wrong nthIt()");
   1.301 +    check(++ai == INVALID, "Wrong nthIt()");
   1.302 +    check(checkPath(cgr, p1), "Wrong checkPath()");
   1.303 +    check(p2.length() == 2, "Wrong length");
   1.304 +    ai = p2.nthIt(0);
   1.305 +    check((tmp_a = ai) == a3, "Wrong ArcIt");
   1.306 +    check((tmp_a = ++ai) == a4, "Wrong nthIt()");
   1.307 +    check(++ai == INVALID, "Wrong nthIt()");
   1.308 +    check(checkPath(cgr, p2), "Wrong checkPath()");
   1.309 +
   1.310 +    // Check split() and splice()
   1.311 +    p1.spliceFront(p2);
   1.312 +    p1.split(p1.nthIt(2), p2);
   1.313 +    p2.split(p2.nthIt(1), p3);
   1.314 +    p2.spliceBack(p1);
   1.315 +    p2.splice(p2.nthIt(1), p3);
   1.316 +    check(p2.length() == 4, "Wrong length");
   1.317 +    check(p2.front() == a1, "Wrong front()");
   1.318 +    check(p2.back() == a4, "Wrong back()");
   1.319 +    ai = p2.nthIt(0);
   1.320 +    check((tmp_a = ai) == a1, "Wrong ArcIt");
   1.321 +    check((tmp_a = ++ai) == a2, "Wrong nthIt()");
   1.322 +    check((tmp_a = ++ai) == a3, "Wrong nthIt()");
   1.323 +    check((tmp_a = ++ai) == a4, "Wrong nthIt()");
   1.324 +    check(++ai == INVALID, "Wrong nthIt()");
   1.325 +    check(checkPath(cgr, p2), "Wrong checkPath()");
   1.326 +  }
   1.327 +
   1.328 +};
   1.329 +
   1.330  int main() {
   1.331 -  check_concepts();
   1.332 -
   1.333 -  checkCopy<Path<ListDigraph> >();
   1.334 -  checkCopy<SimplePath<ListDigraph> >();
   1.335 -  checkCopy<ListPath<ListDigraph> >();
   1.336 -
   1.337 -  ListDigraph g;
   1.338 -  ListDigraph::Arc a  = g.addArc(g.addNode(), g.addNode());
   1.339 -  
   1.340 -  Path<ListDigraph> p;
   1.341 -  StaticPath<ListDigraph> q,r;
   1.342 -  p.addBack(a);
   1.343 -  q=p;
   1.344 -  r=q;
   1.345 -  StaticPath<ListDigraph> s(q);
   1.346 +  checkPathConcepts();
   1.347 +  checkPathCopy();
   1.348 +  CheckPathFunctions cpf;
   1.349 +  cpf.run();
   1.350  
   1.351    return 0;
   1.352  }