test/dfs_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 18 Aug 2008 20:33:11 +0200
changeset 249 f0b89f242745
parent 209 765619b7cbb2
child 278 931190050520
permissions -rw-r--r--
Ignore the downloaded tag files
     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/lgf_reader.h>
    23 
    24 #include <lemon/dfs.h>
    25 #include <lemon/path.h>
    26 
    27 #include "graph_test.h"
    28 #include "test_tools.h"
    29 
    30 using namespace lemon;
    31 
    32 char test_lgf[] =
    33   "@nodes\n"
    34   "label\n"
    35   "0\n"
    36   "1\n"
    37   "2\n"
    38   "3\n"
    39   "4\n"
    40   "5\n"
    41   "6\n"
    42   "@arcs\n"
    43   "     label\n"
    44   "0 1  0\n"
    45   "1 2  1\n"
    46   "2 3  2\n"
    47   "1 4  3\n"
    48   "4 2  4\n"
    49   "4 5  5\n"
    50   "5 0  6\n"
    51   "6 3  7\n"
    52   "@attributes\n"
    53   "source 0\n"
    54   "target 5\n";
    55 
    56 void checkDfsCompile()
    57 {
    58   typedef concepts::Digraph Digraph;
    59   typedef Dfs<Digraph> DType;
    60 
    61   Digraph G;
    62   Digraph::Node n;
    63   Digraph::Arc e;
    64   int l;
    65   bool b;
    66   DType::DistMap d(G);
    67   DType::PredMap p(G);
    68 
    69   DType dfs_test(G);
    70 
    71   dfs_test.run(n);
    72 
    73   l  = dfs_test.dist(n);
    74   e  = dfs_test.predArc(n);
    75   n  = dfs_test.predNode(n);
    76   d  = dfs_test.distMap();
    77   p  = dfs_test.predMap();
    78   b  = dfs_test.reached(n);
    79 
    80   Path<Digraph> pp = dfs_test.path(n);
    81 }
    82 
    83 void checkDfsFunctionCompile()
    84 {
    85   typedef int VType;
    86   typedef concepts::Digraph Digraph;
    87   typedef Digraph::Arc Arc;
    88   typedef Digraph::Node Node;
    89 
    90   Digraph g;
    91   dfs(g,Node()).run();
    92   dfs(g).source(Node()).run();
    93   dfs(g)
    94     .predMap(concepts::WriteMap<Node,Arc>())
    95     .distMap(concepts::WriteMap<Node,VType>())
    96     .reachedMap(concepts::ReadWriteMap<Node,bool>())
    97     .processedMap(concepts::WriteMap<Node,bool>())
    98     .run(Node());
    99 }
   100 
   101 template <class Digraph>
   102 void checkDfs() {
   103   TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
   104 
   105   Digraph G;
   106   Node s, t;
   107 
   108   std::istringstream input(test_lgf);
   109   digraphReader(input, G).
   110     node("source", s).
   111     node("target", t).
   112     run();
   113 
   114   Dfs<Digraph> dfs_test(G);
   115   dfs_test.run(s);
   116 
   117   Path<Digraph> p = dfs_test.path(t);
   118   check(p.length() == dfs_test.dist(t),"path() found a wrong path.");
   119   check(checkPath(G, p),"path() found a wrong path.");
   120   check(pathSource(G, p) == s,"path() found a wrong path.");
   121   check(pathTarget(G, p) == t,"path() found a wrong path.");
   122 
   123   for(NodeIt v(G); v!=INVALID; ++v) {
   124     if (dfs_test.reached(v)) {
   125       check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree.");
   126       if (dfs_test.predArc(v)!=INVALID ) {
   127         Arc e=dfs_test.predArc(v);
   128         Node u=G.source(e);
   129         check(u==dfs_test.predNode(v),"Wrong tree.");
   130         check(dfs_test.dist(v) - dfs_test.dist(u) == 1,
   131               "Wrong distance. (" << dfs_test.dist(u) << "->"
   132               <<dfs_test.dist(v) << ')');
   133       }
   134     }
   135   }
   136 }
   137 
   138 int main()
   139 {
   140   checkDfs<ListDigraph>();
   141   checkDfs<SmartDigraph>();
   142   return 0;
   143 }