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@440: * Copyright (C) 2003-2009 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 kpeter@171: #include alpar@100: #include deba@228: #include alpar@100: #include alpar@100: #include 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" alpar@906: "target 5\n" alpar@906: "source1 6\n" alpar@906: "target1 3\n"; alpar@906: deba@228: alpar@209: void checkDfsCompile() alpar@100: { alpar@100: typedef concepts::Digraph Digraph; alpar@100: typedef Dfs DType; kpeter@286: typedef Digraph::Node Node; kpeter@286: typedef Digraph::Arc Arc; alpar@209: alpar@100: Digraph G; kpeter@286: Node s, t; kpeter@286: Arc e; kpeter@585: int l, i; alpar@100: bool b; alpar@100: DType::DistMap d(G); alpar@100: DType::PredMap p(G); kpeter@286: Path pp; kpeter@585: concepts::ReadMap am; alpar@209: kpeter@286: { kpeter@286: DType dfs_test(G); kpeter@585: const DType& const_dfs_test = dfs_test; alpar@209: kpeter@286: dfs_test.run(s); kpeter@286: dfs_test.run(s,t); kpeter@286: dfs_test.run(); alpar@209: kpeter@585: dfs_test.init(); kpeter@585: dfs_test.addSource(s); kpeter@585: e = dfs_test.processNextArc(); kpeter@585: e = const_dfs_test.nextArc(); kpeter@585: b = const_dfs_test.emptyQueue(); kpeter@585: i = const_dfs_test.queueSize(); kpeter@585: kpeter@585: dfs_test.start(); kpeter@585: dfs_test.start(t); kpeter@585: dfs_test.start(am); kpeter@585: kpeter@585: l = const_dfs_test.dist(t); kpeter@585: e = const_dfs_test.predArc(t); kpeter@585: s = const_dfs_test.predNode(t); kpeter@585: b = const_dfs_test.reached(t); kpeter@585: d = const_dfs_test.distMap(); kpeter@585: p = const_dfs_test.predMap(); kpeter@585: pp = const_dfs_test.path(t); kpeter@286: } kpeter@286: { kpeter@286: DType kpeter@286: ::SetPredMap > kpeter@286: ::SetDistMap > kpeter@286: ::SetReachedMap > kpeter@585: ::SetStandardProcessedMap kpeter@286: ::SetProcessedMap > kpeter@286: ::Create dfs_test(G); alpar@100: kpeter@585: concepts::ReadWriteMap pred_map; kpeter@585: concepts::ReadWriteMap dist_map; kpeter@585: concepts::ReadWriteMap reached_map; kpeter@585: concepts::WriteMap processed_map; kpeter@585: kpeter@585: dfs_test kpeter@585: .predMap(pred_map) kpeter@585: .distMap(dist_map) kpeter@585: .reachedMap(reached_map) kpeter@585: .processedMap(processed_map); kpeter@585: kpeter@286: dfs_test.run(s); kpeter@286: dfs_test.run(s,t); kpeter@286: dfs_test.run(); kpeter@585: dfs_test.init(); kpeter@585: kpeter@585: dfs_test.addSource(s); kpeter@585: e = dfs_test.processNextArc(); kpeter@585: e = dfs_test.nextArc(); kpeter@585: b = dfs_test.emptyQueue(); kpeter@585: i = dfs_test.queueSize(); kpeter@585: kpeter@585: dfs_test.start(); kpeter@585: dfs_test.start(t); kpeter@585: dfs_test.start(am); kpeter@286: kpeter@286: l = dfs_test.dist(t); kpeter@286: e = dfs_test.predArc(t); kpeter@286: s = dfs_test.predNode(t); kpeter@286: b = dfs_test.reached(t); kpeter@286: pp = dfs_test.path(t); kpeter@286: } 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; kpeter@278: bool b; kpeter@278: dfs(g).run(Node()); kpeter@278: b=dfs(g).run(Node(),Node()); kpeter@278: dfs(g).run(); alpar@100: dfs(g) kpeter@278: .predMap(concepts::ReadWriteMap()) kpeter@278: .distMap(concepts::ReadWriteMap()) alpar@100: .reachedMap(concepts::ReadWriteMap()) alpar@100: .processedMap(concepts::WriteMap()) alpar@209: .run(Node()); kpeter@278: b=dfs(g) kpeter@278: .predMap(concepts::ReadWriteMap()) kpeter@278: .distMap(concepts::ReadWriteMap()) kpeter@278: .reachedMap(concepts::ReadWriteMap()) kpeter@278: .processedMap(concepts::WriteMap()) kpeter@278: .path(concepts::Path()) kpeter@278: .dist(VType()) kpeter@278: .run(Node(),Node()); kpeter@278: dfs(g) kpeter@278: .predMap(concepts::ReadWriteMap()) kpeter@278: .distMap(concepts::ReadWriteMap()) kpeter@278: .reachedMap(concepts::ReadWriteMap()) kpeter@278: .processedMap(concepts::WriteMap()) kpeter@278: .run(); alpar@100: } alpar@100: kpeter@171: template kpeter@171: void checkDfs() { kpeter@171: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); alpar@100: alpar@100: Digraph G; alpar@100: Node s, t; alpar@906: Node s1, t1; alpar@209: deba@228: std::istringstream input(test_lgf); kpeter@293: digraphReader(G, input). deba@228: node("source", s). deba@228: node("target", t). alpar@906: node("source1", s1). alpar@906: node("target1", t1). deba@228: run(); alpar@209: alpar@100: Dfs dfs_test(G); alpar@209: dfs_test.run(s); alpar@209: alpar@100: Path 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) << "->" kpeter@278: << dfs_test.dist(v) << ")"); deba@228: } alpar@100: } alpar@100: } kpeter@278: kpeter@278: { alpar@906: Dfs dfs(G); alpar@906: check(dfs.run(s1,t1) && dfs.reached(t1),"Node 3 is reachable from Node 6."); alpar@906: } alpar@906: alpar@906: { kpeter@278: NullMap myPredMap; kpeter@278: dfs(G).predMap(myPredMap).run(s); kpeter@278: } alpar@100: } alpar@100: kpeter@171: int main() kpeter@171: { kpeter@171: checkDfs(); kpeter@171: checkDfs(); kpeter@171: return 0; kpeter@171: }