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