test/bfs_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 18 Jul 2008 16:36:57 +0100
changeset 224 b246072978e8
parent 171 02f4d5d9bfd7
child 222 f9a18c21dba8
permissions -rw-r--r--
Quote path names.
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     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.
    12  *
    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
    15  * purpose.
    16  *
    17  */
    18 
    19 #include <lemon/concepts/digraph.h>
    20 #include <lemon/smart_graph.h>
    21 #include <lemon/list_graph.h>
    22 #include <lemon/bfs.h>
    23 #include <lemon/path.h>
    24 
    25 #include "graph_test.h"
    26 #include "test_tools.h"
    27 
    28 using namespace lemon;
    29 
    30 void checkBfsCompile()
    31 {
    32   typedef concepts::Digraph Digraph;
    33   typedef Bfs<Digraph> BType;
    34 
    35   Digraph G;
    36   Digraph::Node n;
    37   Digraph::Arc e;
    38   int l;
    39   bool b;
    40   BType::DistMap d(G);
    41   BType::PredMap p(G);
    42   //  BType::PredNodeMap pn(G);
    43 
    44   BType bfs_test(G);
    45 
    46   bfs_test.run(n);
    47 
    48   l  = bfs_test.dist(n);
    49   e  = bfs_test.predArc(n);
    50   n  = bfs_test.predNode(n);
    51   d  = bfs_test.distMap();
    52   p  = bfs_test.predMap();
    53   //  pn = bfs_test.predNodeMap();
    54   b  = bfs_test.reached(n);
    55 
    56   Path<Digraph> pp = bfs_test.path(n);
    57 }
    58 
    59 void checkBfsFunctionCompile()
    60 {
    61   typedef int VType;
    62   typedef concepts::Digraph Digraph;
    63   typedef Digraph::Arc Arc;
    64   typedef Digraph::Node Node;
    65 
    66   Digraph g;
    67   bfs(g,Node()).run();
    68   bfs(g).source(Node()).run();
    69   bfs(g)
    70     .predMap(concepts::WriteMap<Node,Arc>())
    71     .distMap(concepts::WriteMap<Node,VType>())
    72     .reachedMap(concepts::ReadWriteMap<Node,bool>())
    73     .processedMap(concepts::WriteMap<Node,bool>())
    74     .run(Node());
    75 }
    76 
    77 template <class Digraph>
    78 void checkBfs() {
    79   TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
    80 
    81   Digraph G;
    82   Node s, t;
    83   PetStruct<Digraph> ps = addPetersen(G, 5);
    84 
    85   s=ps.outer[2];
    86   t=ps.inner[0];
    87 
    88   Bfs<Digraph> bfs_test(G);
    89   bfs_test.run(s);
    90 
    91   check(bfs_test.dist(t)==3,"Bfs found a wrong path." << bfs_test.dist(t));
    92 
    93   Path<Digraph> p = bfs_test.path(t);
    94   check(p.length()==3,"path() found a wrong path.");
    95   check(checkPath(G, p),"path() found a wrong path.");
    96   check(pathSource(G, p) == s,"path() found a wrong path.");
    97   check(pathTarget(G, p) == t,"path() found a wrong path.");
    98 
    99 
   100   for(ArcIt e(G); e==INVALID; ++e) {
   101     Node u=G.source(e);
   102     Node v=G.target(e);
   103     check( !bfs_test.reached(u) ||
   104            (bfs_test.dist(v) > bfs_test.dist(u)+1),
   105            "Wrong output.");
   106   }
   107 
   108   for(NodeIt v(G); v==INVALID; ++v) {
   109     check(bfs_test.reached(v),"Each node should be reached.");
   110     if ( bfs_test.predArc(v)!=INVALID ) {
   111       Arc e=bfs_test.predArc(v);
   112       Node u=G.source(e);
   113       check(u==bfs_test.predNode(v),"Wrong tree.");
   114       check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
   115             "Wrong distance. Difference: "
   116             << std::abs(bfs_test.dist(v) - bfs_test.dist(u)
   117                         - 1));
   118     }
   119   }
   120 }
   121 
   122 int main()
   123 {
   124   checkBfs<ListDigraph>();
   125   checkBfs<SmartDigraph>();
   126   return 0;
   127 }