alpar@209: /* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@100:  *
alpar@209:  * This file is a part of LEMON, a generic C++ optimization library.
alpar@100:  *
alpar@100:  * Copyright (C) 2003-2008
alpar@100:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@100:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@100:  *
alpar@100:  * Permission to use, modify and distribute this software is granted
alpar@100:  * provided that this copyright notice appears in all copies. For
alpar@100:  * precise terms see the accompanying LICENSE file.
alpar@100:  *
alpar@100:  * This software is provided "AS IS" with no warranty of any kind,
alpar@100:  * express or implied, and with no claim as to its suitability for any
alpar@100:  * purpose.
alpar@100:  *
alpar@100:  */
alpar@100: 
kpeter@171: #include <lemon/concepts/digraph.h>
kpeter@171: #include <lemon/smart_graph.h>
alpar@100: #include <lemon/list_graph.h>
deba@228: #include <lemon/lgf_reader.h>
deba@228: 
alpar@100: #include <lemon/dfs.h>
alpar@100: #include <lemon/path.h>
kpeter@171: 
kpeter@171: #include "graph_test.h"
kpeter@171: #include "test_tools.h"
alpar@100: 
alpar@100: using namespace lemon;
alpar@100: 
deba@228: char test_lgf[] =
deba@228:   "@nodes\n"
deba@228:   "label\n"
deba@228:   "0\n"
deba@228:   "1\n"
deba@228:   "2\n"
deba@228:   "3\n"
deba@228:   "4\n"
deba@228:   "5\n"
deba@228:   "6\n"
deba@228:   "@arcs\n"
deba@228:   "     label\n"
deba@228:   "0 1  0\n"
deba@228:   "1 2  1\n"
deba@228:   "2 3  2\n"
deba@228:   "1 4  3\n"
deba@228:   "4 2  4\n"
deba@228:   "4 5  5\n"
deba@228:   "5 0  6\n"
deba@228:   "6 3  7\n"
deba@228:   "@attributes\n"
deba@228:   "source 0\n"
deba@228:   "target 5\n";
deba@228: 
alpar@209: void checkDfsCompile()
alpar@100: {
alpar@100:   typedef concepts::Digraph Digraph;
alpar@100:   typedef Dfs<Digraph> DType;
alpar@209: 
alpar@100:   Digraph G;
kpeter@171:   Digraph::Node n;
kpeter@171:   Digraph::Arc e;
alpar@100:   int l;
alpar@100:   bool b;
alpar@100:   DType::DistMap d(G);
alpar@100:   DType::PredMap p(G);
alpar@209: 
alpar@100:   DType dfs_test(G);
alpar@209: 
alpar@100:   dfs_test.run(n);
alpar@209: 
alpar@100:   l  = dfs_test.dist(n);
alpar@100:   e  = dfs_test.predArc(n);
alpar@100:   n  = dfs_test.predNode(n);
alpar@100:   d  = dfs_test.distMap();
alpar@100:   p  = dfs_test.predMap();
alpar@100:   b  = dfs_test.reached(n);
alpar@100: 
alpar@100:   Path<Digraph> pp = dfs_test.path(n);
alpar@100: }
alpar@100: 
alpar@209: void checkDfsFunctionCompile()
alpar@100: {
alpar@100:   typedef int VType;
alpar@100:   typedef concepts::Digraph Digraph;
alpar@100:   typedef Digraph::Arc Arc;
alpar@100:   typedef Digraph::Node Node;
alpar@209: 
alpar@100:   Digraph g;
alpar@100:   dfs(g,Node()).run();
alpar@100:   dfs(g).source(Node()).run();
alpar@100:   dfs(g)
alpar@100:     .predMap(concepts::WriteMap<Node,Arc>())
alpar@100:     .distMap(concepts::WriteMap<Node,VType>())
alpar@100:     .reachedMap(concepts::ReadWriteMap<Node,bool>())
alpar@100:     .processedMap(concepts::WriteMap<Node,bool>())
alpar@209:     .run(Node());
alpar@100: }
alpar@100: 
kpeter@171: template <class Digraph>
kpeter@171: void checkDfs() {
kpeter@171:   TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
alpar@100: 
alpar@100:   Digraph G;
alpar@100:   Node s, t;
alpar@209: 
deba@228:   std::istringstream input(test_lgf);
deba@228:   digraphReader(input, G).
deba@228:     node("source", s).
deba@228:     node("target", t).
deba@228:     run();
alpar@209: 
alpar@100:   Dfs<Digraph> dfs_test(G);
alpar@209:   dfs_test.run(s);
alpar@209: 
alpar@100:   Path<Digraph> p = dfs_test.path(t);
kpeter@171:   check(p.length() == dfs_test.dist(t),"path() found a wrong path.");
alpar@100:   check(checkPath(G, p),"path() found a wrong path.");
alpar@100:   check(pathSource(G, p) == s,"path() found a wrong path.");
alpar@100:   check(pathTarget(G, p) == t,"path() found a wrong path.");
alpar@209: 
alpar@100:   for(NodeIt v(G); v!=INVALID; ++v) {
deba@228:     if (dfs_test.reached(v)) {
deba@228:       check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree.");
deba@228:       if (dfs_test.predArc(v)!=INVALID ) {
deba@228:         Arc e=dfs_test.predArc(v);
deba@228:         Node u=G.source(e);
deba@228:         check(u==dfs_test.predNode(v),"Wrong tree.");
deba@228:         check(dfs_test.dist(v) - dfs_test.dist(u) == 1,
deba@228:               "Wrong distance. (" << dfs_test.dist(u) << "->"
deba@228:               <<dfs_test.dist(v) << ')');
deba@228:       }
alpar@100:     }
alpar@100:   }
alpar@100: }
alpar@100: 
kpeter@171: int main()
kpeter@171: {
kpeter@171:   checkDfs<ListDigraph>();
kpeter@171:   checkDfs<SmartDigraph>();
kpeter@171:   return 0;
kpeter@171: }