test/dfs_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 26 Sep 2008 09:52:28 +0200
changeset 281 e9b4fbe163f5
parent 228 b6732e0d38c5
child 286 da414906fe21
permissions -rw-r--r--
Merge
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@100
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@100
     4
 *
alpar@100
     5
 * Copyright (C) 2003-2008
alpar@100
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@100
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@100
     8
 *
alpar@100
     9
 * Permission to use, modify and distribute this software is granted
alpar@100
    10
 * provided that this copyright notice appears in all copies. For
alpar@100
    11
 * precise terms see the accompanying LICENSE file.
alpar@100
    12
 *
alpar@100
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@100
    14
 * express or implied, and with no claim as to its suitability for any
alpar@100
    15
 * purpose.
alpar@100
    16
 *
alpar@100
    17
 */
alpar@100
    18
kpeter@171
    19
#include <lemon/concepts/digraph.h>
kpeter@171
    20
#include <lemon/smart_graph.h>
alpar@100
    21
#include <lemon/list_graph.h>
deba@228
    22
#include <lemon/lgf_reader.h>
alpar@100
    23
#include <lemon/dfs.h>
alpar@100
    24
#include <lemon/path.h>
kpeter@171
    25
kpeter@171
    26
#include "graph_test.h"
kpeter@171
    27
#include "test_tools.h"
alpar@100
    28
alpar@100
    29
using namespace lemon;
alpar@100
    30
deba@228
    31
char test_lgf[] =
deba@228
    32
  "@nodes\n"
deba@228
    33
  "label\n"
deba@228
    34
  "0\n"
deba@228
    35
  "1\n"
deba@228
    36
  "2\n"
deba@228
    37
  "3\n"
deba@228
    38
  "4\n"
deba@228
    39
  "5\n"
deba@228
    40
  "6\n"
deba@228
    41
  "@arcs\n"
deba@228
    42
  "     label\n"
deba@228
    43
  "0 1  0\n"
deba@228
    44
  "1 2  1\n"
deba@228
    45
  "2 3  2\n"
deba@228
    46
  "1 4  3\n"
deba@228
    47
  "4 2  4\n"
deba@228
    48
  "4 5  5\n"
deba@228
    49
  "5 0  6\n"
deba@228
    50
  "6 3  7\n"
deba@228
    51
  "@attributes\n"
deba@228
    52
  "source 0\n"
deba@228
    53
  "target 5\n";
deba@228
    54
alpar@209
    55
void checkDfsCompile()
alpar@100
    56
{
alpar@100
    57
  typedef concepts::Digraph Digraph;
alpar@100
    58
  typedef Dfs<Digraph> DType;
alpar@209
    59
alpar@100
    60
  Digraph G;
kpeter@171
    61
  Digraph::Node n;
kpeter@171
    62
  Digraph::Arc e;
alpar@100
    63
  int l;
alpar@100
    64
  bool b;
alpar@100
    65
  DType::DistMap d(G);
alpar@100
    66
  DType::PredMap p(G);
alpar@209
    67
alpar@100
    68
  DType dfs_test(G);
alpar@209
    69
alpar@100
    70
  dfs_test.run(n);
alpar@209
    71
alpar@100
    72
  l  = dfs_test.dist(n);
alpar@100
    73
  e  = dfs_test.predArc(n);
alpar@100
    74
  n  = dfs_test.predNode(n);
alpar@100
    75
  d  = dfs_test.distMap();
alpar@100
    76
  p  = dfs_test.predMap();
alpar@100
    77
  b  = dfs_test.reached(n);
alpar@100
    78
alpar@100
    79
  Path<Digraph> pp = dfs_test.path(n);
alpar@100
    80
}
alpar@100
    81
alpar@209
    82
void checkDfsFunctionCompile()
alpar@100
    83
{
alpar@100
    84
  typedef int VType;
alpar@100
    85
  typedef concepts::Digraph Digraph;
alpar@100
    86
  typedef Digraph::Arc Arc;
alpar@100
    87
  typedef Digraph::Node Node;
alpar@209
    88
alpar@100
    89
  Digraph g;
kpeter@278
    90
  bool b;
kpeter@278
    91
  dfs(g).run(Node());
kpeter@278
    92
  b=dfs(g).run(Node(),Node());
kpeter@278
    93
  dfs(g).run();
alpar@100
    94
  dfs(g)
kpeter@278
    95
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@278
    96
    .distMap(concepts::ReadWriteMap<Node,VType>())
alpar@100
    97
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
alpar@100
    98
    .processedMap(concepts::WriteMap<Node,bool>())
alpar@209
    99
    .run(Node());
kpeter@278
   100
  b=dfs(g)
kpeter@278
   101
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@278
   102
    .distMap(concepts::ReadWriteMap<Node,VType>())
kpeter@278
   103
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
kpeter@278
   104
    .processedMap(concepts::WriteMap<Node,bool>())
kpeter@278
   105
    .path(concepts::Path<Digraph>())
kpeter@278
   106
    .dist(VType())
kpeter@278
   107
    .run(Node(),Node());
kpeter@278
   108
  dfs(g)
kpeter@278
   109
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@278
   110
    .distMap(concepts::ReadWriteMap<Node,VType>())
kpeter@278
   111
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
kpeter@278
   112
    .processedMap(concepts::WriteMap<Node,bool>())
kpeter@278
   113
    .run();
alpar@100
   114
}
alpar@100
   115
kpeter@171
   116
template <class Digraph>
kpeter@171
   117
void checkDfs() {
kpeter@171
   118
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
alpar@100
   119
alpar@100
   120
  Digraph G;
alpar@100
   121
  Node s, t;
alpar@209
   122
deba@228
   123
  std::istringstream input(test_lgf);
deba@228
   124
  digraphReader(input, G).
deba@228
   125
    node("source", s).
deba@228
   126
    node("target", t).
deba@228
   127
    run();
alpar@209
   128
alpar@100
   129
  Dfs<Digraph> dfs_test(G);
alpar@209
   130
  dfs_test.run(s);
alpar@209
   131
alpar@100
   132
  Path<Digraph> p = dfs_test.path(t);
kpeter@171
   133
  check(p.length() == dfs_test.dist(t),"path() found a wrong path.");
alpar@100
   134
  check(checkPath(G, p),"path() found a wrong path.");
alpar@100
   135
  check(pathSource(G, p) == s,"path() found a wrong path.");
alpar@100
   136
  check(pathTarget(G, p) == t,"path() found a wrong path.");
alpar@209
   137
alpar@100
   138
  for(NodeIt v(G); v!=INVALID; ++v) {
deba@228
   139
    if (dfs_test.reached(v)) {
deba@228
   140
      check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree.");
deba@228
   141
      if (dfs_test.predArc(v)!=INVALID ) {
deba@228
   142
        Arc e=dfs_test.predArc(v);
deba@228
   143
        Node u=G.source(e);
deba@228
   144
        check(u==dfs_test.predNode(v),"Wrong tree.");
deba@228
   145
        check(dfs_test.dist(v) - dfs_test.dist(u) == 1,
deba@228
   146
              "Wrong distance. (" << dfs_test.dist(u) << "->"
kpeter@278
   147
              << dfs_test.dist(v) << ")");
deba@228
   148
      }
alpar@100
   149
    }
alpar@100
   150
  }
kpeter@278
   151
kpeter@278
   152
  {
kpeter@278
   153
    NullMap<Node,Arc> myPredMap;
kpeter@278
   154
    dfs(G).predMap(myPredMap).run(s);
kpeter@278
   155
  }
alpar@100
   156
}
alpar@100
   157
kpeter@171
   158
int main()
kpeter@171
   159
{
kpeter@171
   160
  checkDfs<ListDigraph>();
kpeter@171
   161
  checkDfs<SmartDigraph>();
kpeter@171
   162
  return 0;
kpeter@171
   163
}