src/test/bfs_test.cc
author athos
Fri, 25 Mar 2005 15:32:05 +0000
changeset 1262 61f989e3e525
parent 1218 5331168bbb18
child 1283 fc20371677b9
permissions -rw-r--r--
This was a bug, I guess
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/test/bfs_test.cc - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
alpar@774
    17
#include "test_tools.h"
alpar@921
    18
#include <lemon/smart_graph.h>
alpar@921
    19
#include <lemon/bfs.h>
klao@959
    20
#include<lemon/concept/graph.h>
alpar@774
    21
alpar@921
    22
using namespace lemon;
alpar@774
    23
alpar@774
    24
const int PET_SIZE =5;
alpar@774
    25
alpar@774
    26
alpar@793
    27
void check_Bfs_Compile() 
alpar@774
    28
{
klao@959
    29
  typedef concept::StaticGraph Graph;
alpar@774
    30
alpar@774
    31
  typedef Graph::Edge Edge;
alpar@774
    32
  typedef Graph::Node Node;
alpar@774
    33
  typedef Graph::EdgeIt EdgeIt;
alpar@774
    34
  typedef Graph::NodeIt NodeIt;
alpar@774
    35
 
alpar@774
    36
  typedef Bfs<Graph> BType;
alpar@774
    37
  
alpar@774
    38
  Graph G;
alpar@774
    39
  Node n;
alpar@774
    40
  Edge e;
alpar@793
    41
  int l;
alpar@774
    42
  bool b;
alpar@774
    43
  BType::DistMap d(G);
alpar@774
    44
  BType::PredMap p(G);
alpar@1218
    45
  //  BType::PredNodeMap pn(G);
alpar@774
    46
  
alpar@774
    47
  BType bfs_test(G);
alpar@774
    48
  
alpar@774
    49
  bfs_test.run(n);
alpar@774
    50
  
alpar@774
    51
  l  = bfs_test.dist(n);
alpar@774
    52
  e  = bfs_test.pred(n);
alpar@774
    53
  n  = bfs_test.predNode(n);
alpar@774
    54
  d  = bfs_test.distMap();
alpar@774
    55
  p  = bfs_test.predMap();
alpar@1218
    56
  //  pn = bfs_test.predNodeMap();
alpar@774
    57
  b  = bfs_test.reached(n);
alpar@774
    58
alpar@774
    59
}
alpar@774
    60
alpar@1220
    61
void check_Bfs_Function_Compile() 
alpar@1220
    62
{
alpar@1220
    63
  typedef int VType;
alpar@1220
    64
  typedef concept::StaticGraph Graph;
alpar@1220
    65
alpar@1220
    66
  typedef Graph::Edge Edge;
alpar@1220
    67
  typedef Graph::Node Node;
alpar@1220
    68
  typedef Graph::EdgeIt EdgeIt;
alpar@1220
    69
  typedef Graph::NodeIt NodeIt;
alpar@1220
    70
  typedef concept::ReadMap<Edge,VType> LengthMap;
alpar@1220
    71
   
alpar@1220
    72
  bfs(Graph(),Node()).run();
alpar@1220
    73
  bfs(Graph()).source(Node()).run();
alpar@1220
    74
  bfs(Graph())
alpar@1220
    75
    .predMap(concept::WriteMap<Node,Edge>())
alpar@1220
    76
    .distMap(concept::WriteMap<Node,VType>())
alpar@1220
    77
    .reachedMap(concept::ReadWriteMap<Node,bool>())
alpar@1220
    78
    .processedMap(concept::WriteMap<Node,bool>())
alpar@1220
    79
    .run(Node());
alpar@1220
    80
  
alpar@1220
    81
}
alpar@1220
    82
alpar@774
    83
int main()
alpar@774
    84
{
alpar@774
    85
    
alpar@774
    86
  typedef SmartGraph Graph;
alpar@774
    87
alpar@774
    88
  typedef Graph::Edge Edge;
alpar@774
    89
  typedef Graph::Node Node;
alpar@774
    90
  typedef Graph::EdgeIt EdgeIt;
alpar@774
    91
  typedef Graph::NodeIt NodeIt;
alpar@774
    92
  typedef Graph::EdgeMap<int> LengthMap;
alpar@774
    93
alpar@774
    94
  Graph G;
alpar@774
    95
  Node s, t;
alpar@774
    96
  PetStruct<Graph> ps = addPetersen(G,PET_SIZE);
alpar@774
    97
   
alpar@774
    98
  s=ps.outer[2];
alpar@774
    99
  t=ps.inner[0];
alpar@774
   100
  
alpar@774
   101
  Bfs<Graph> bfs_test(G);
alpar@774
   102
  bfs_test.run(s);
alpar@774
   103
  
alpar@774
   104
  check(bfs_test.dist(t)==3,"Bfs found a wrong path. " << bfs_test.dist(t));
alpar@774
   105
alpar@774
   106
alpar@774
   107
  for(EdgeIt e(G); e==INVALID; ++e) {
alpar@986
   108
    Node u=G.source(e);
alpar@986
   109
    Node v=G.target(e);
alpar@774
   110
    check( !bfs_test.reached(u) ||
alpar@774
   111
	   (bfs_test.dist(v) > bfs_test.dist(u)+1),
alpar@774
   112
	   "Wrong output.");
alpar@774
   113
  }
alpar@774
   114
alpar@780
   115
  for(NodeIt v(G); v==INVALID; ++v) {
alpar@780
   116
    check(bfs_test.reached(v),"Each node should be reached.");
alpar@780
   117
    if ( bfs_test.pred(v)!=INVALID ) {
alpar@774
   118
      Edge e=bfs_test.pred(v);
alpar@986
   119
      Node u=G.source(e);
alpar@780
   120
      check(u==bfs_test.predNode(v),"Wrong tree.");
alpar@774
   121
      check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
alpar@780
   122
	    "Wrong distance. Difference: " 
alpar@774
   123
	    << std::abs(bfs_test.dist(v) - bfs_test.dist(u) 
alpar@774
   124
			- 1));
alpar@774
   125
    }
alpar@780
   126
  }
alpar@774
   127
}
alpar@780
   128