test/bfs_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Tue, 29 Jul 2008 14:41:55 +0200
changeset 242 dbe3fc9c875d
parent 222 f9a18c21dba8
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>
alpar@100
    23
#include <lemon/bfs.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
  "@arcs\n"
deba@228
    41
  "     label\n"
deba@228
    42
  "0 1  0\n"
deba@228
    43
  "1 2  1\n"
deba@228
    44
  "2 3  2\n"
deba@228
    45
  "3 4  3\n"
deba@228
    46
  "0 3  4\n"
deba@228
    47
  "0 3  5\n"
deba@228
    48
  "5 2  6\n"
deba@228
    49
  "@attributes\n"
deba@228
    50
  "source 0\n"
deba@228
    51
  "target 4\n";
deba@228
    52
alpar@209
    53
void checkBfsCompile()
alpar@100
    54
{
alpar@100
    55
  typedef concepts::Digraph Digraph;
alpar@100
    56
  typedef Bfs<Digraph> BType;
alpar@209
    57
alpar@100
    58
  Digraph G;
kpeter@171
    59
  Digraph::Node n;
kpeter@171
    60
  Digraph::Arc e;
alpar@100
    61
  int l;
alpar@100
    62
  bool b;
alpar@100
    63
  BType::DistMap d(G);
alpar@100
    64
  BType::PredMap p(G);
alpar@100
    65
  //  BType::PredNodeMap pn(G);
alpar@209
    66
alpar@100
    67
  BType bfs_test(G);
alpar@209
    68
alpar@100
    69
  bfs_test.run(n);
alpar@209
    70
alpar@100
    71
  l  = bfs_test.dist(n);
alpar@100
    72
  e  = bfs_test.predArc(n);
alpar@100
    73
  n  = bfs_test.predNode(n);
alpar@100
    74
  d  = bfs_test.distMap();
deba@228
    75
alpar@100
    76
  p  = bfs_test.predMap();
alpar@100
    77
  //  pn = bfs_test.predNodeMap();
alpar@100
    78
  b  = bfs_test.reached(n);
alpar@100
    79
alpar@100
    80
  Path<Digraph> pp = bfs_test.path(n);
alpar@100
    81
}
alpar@100
    82
alpar@209
    83
void checkBfsFunctionCompile()
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
  bfs(g,Node()).run();
alpar@100
    92
  bfs(g).source(Node()).run();
alpar@100
    93
  bfs(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@100
    98
    .run(Node());
alpar@100
    99
}
alpar@100
   100
kpeter@171
   101
template <class Digraph>
kpeter@171
   102
void checkBfs() {
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
  Bfs<Digraph> bfs_test(G);
alpar@100
   115
  bfs_test.run(s);
alpar@209
   116
deba@228
   117
  check(bfs_test.dist(t)==2,"Bfs found a wrong path." << bfs_test.dist(t));
alpar@100
   118
alpar@100
   119
  Path<Digraph> p = bfs_test.path(t);
deba@228
   120
  check(p.length()==2,"path() found a wrong path.");
alpar@100
   121
  check(checkPath(G, p),"path() found a wrong path.");
alpar@100
   122
  check(pathSource(G, p) == s,"path() found a wrong path.");
alpar@100
   123
  check(pathTarget(G, p) == t,"path() found a wrong path.");
alpar@209
   124
alpar@100
   125
deba@228
   126
  for(ArcIt a(G); a!=INVALID; ++a) {
deba@228
   127
    Node u=G.source(a);
deba@228
   128
    Node v=G.target(a);
alpar@100
   129
    check( !bfs_test.reached(u) ||
deba@222
   130
           (bfs_test.dist(v) <= bfs_test.dist(u)+1),
deba@228
   131
           "Wrong output." << G.id(v) << ' ' << G.id(u));
alpar@100
   132
  }
alpar@100
   133
deba@222
   134
  for(NodeIt v(G); v!=INVALID; ++v) {
deba@228
   135
    if (bfs_test.reached(v)) {
deba@228
   136
      check(v==s || bfs_test.predArc(v)!=INVALID, "Wrong tree.");
deba@228
   137
      if (bfs_test.predArc(v)!=INVALID ) {
deba@228
   138
        Arc a=bfs_test.predArc(v);
deba@228
   139
        Node u=G.source(a);
deba@228
   140
        check(u==bfs_test.predNode(v),"Wrong tree.");
deba@228
   141
        check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
deba@228
   142
              "Wrong distance. Difference: "
deba@228
   143
              << std::abs(bfs_test.dist(v) - bfs_test.dist(u)
deba@228
   144
                          - 1));
deba@228
   145
      }
alpar@100
   146
    }
alpar@100
   147
  }
alpar@100
   148
}
alpar@100
   149
kpeter@171
   150
int main()
kpeter@171
   151
{
kpeter@171
   152
  checkBfs<ListDigraph>();
kpeter@171
   153
  checkBfs<SmartDigraph>();
kpeter@171
   154
  return 0;
kpeter@171
   155
}