test/bfs_test.cc
author deba
Thu, 10 Nov 2005 12:40:58 +0000
changeset 1785 606178a14011
parent 1435 8e85e6bbefdf
child 1875 98698b69a902
permissions -rw-r--r--
Assertion with own exception throwing
alpar@906
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * 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@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, 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>
alpar@1283
    20
#include <lemon/path.h>
klao@959
    21
#include<lemon/concept/graph.h>
alpar@774
    22
alpar@921
    23
using namespace lemon;
alpar@774
    24
alpar@774
    25
const int PET_SIZE =5;
alpar@774
    26
alpar@774
    27
alpar@793
    28
void check_Bfs_Compile() 
alpar@774
    29
{
klao@959
    30
  typedef concept::StaticGraph Graph;
alpar@774
    31
alpar@774
    32
  typedef Graph::Edge Edge;
alpar@774
    33
  typedef Graph::Node Node;
alpar@774
    34
  typedef Graph::EdgeIt EdgeIt;
alpar@774
    35
  typedef Graph::NodeIt NodeIt;
alpar@774
    36
 
alpar@774
    37
  typedef Bfs<Graph> BType;
alpar@774
    38
  
alpar@774
    39
  Graph G;
alpar@774
    40
  Node n;
alpar@774
    41
  Edge e;
alpar@793
    42
  int l;
alpar@774
    43
  bool b;
alpar@774
    44
  BType::DistMap d(G);
alpar@774
    45
  BType::PredMap p(G);
alpar@1218
    46
  //  BType::PredNodeMap pn(G);
alpar@774
    47
  
alpar@774
    48
  BType bfs_test(G);
alpar@774
    49
  
alpar@774
    50
  bfs_test.run(n);
alpar@774
    51
  
alpar@774
    52
  l  = bfs_test.dist(n);
deba@1763
    53
  e  = bfs_test.predEdge(n);
alpar@774
    54
  n  = bfs_test.predNode(n);
alpar@774
    55
  d  = bfs_test.distMap();
alpar@774
    56
  p  = bfs_test.predMap();
alpar@1218
    57
  //  pn = bfs_test.predNodeMap();
alpar@774
    58
  b  = bfs_test.reached(n);
alpar@774
    59
alpar@1283
    60
  DirPath<Graph> pp(G);
alpar@1283
    61
  bfs_test.getPath(pp,n);
alpar@774
    62
}
alpar@774
    63
alpar@1220
    64
void check_Bfs_Function_Compile() 
alpar@1220
    65
{
alpar@1220
    66
  typedef int VType;
alpar@1220
    67
  typedef concept::StaticGraph Graph;
alpar@1220
    68
alpar@1220
    69
  typedef Graph::Edge Edge;
alpar@1220
    70
  typedef Graph::Node Node;
alpar@1220
    71
  typedef Graph::EdgeIt EdgeIt;
alpar@1220
    72
  typedef Graph::NodeIt NodeIt;
alpar@1220
    73
  typedef concept::ReadMap<Edge,VType> LengthMap;
alpar@1220
    74
   
alpar@1220
    75
  bfs(Graph(),Node()).run();
alpar@1220
    76
  bfs(Graph()).source(Node()).run();
alpar@1220
    77
  bfs(Graph())
alpar@1220
    78
    .predMap(concept::WriteMap<Node,Edge>())
alpar@1220
    79
    .distMap(concept::WriteMap<Node,VType>())
alpar@1220
    80
    .reachedMap(concept::ReadWriteMap<Node,bool>())
alpar@1220
    81
    .processedMap(concept::WriteMap<Node,bool>())
alpar@1220
    82
    .run(Node());
alpar@1220
    83
  
alpar@1220
    84
}
alpar@1220
    85
alpar@774
    86
int main()
alpar@774
    87
{
alpar@774
    88
    
alpar@774
    89
  typedef SmartGraph Graph;
alpar@774
    90
alpar@774
    91
  typedef Graph::Edge Edge;
alpar@774
    92
  typedef Graph::Node Node;
alpar@774
    93
  typedef Graph::EdgeIt EdgeIt;
alpar@774
    94
  typedef Graph::NodeIt NodeIt;
alpar@774
    95
  typedef Graph::EdgeMap<int> LengthMap;
alpar@774
    96
alpar@774
    97
  Graph G;
alpar@774
    98
  Node s, t;
alpar@774
    99
  PetStruct<Graph> ps = addPetersen(G,PET_SIZE);
alpar@774
   100
   
alpar@774
   101
  s=ps.outer[2];
alpar@774
   102
  t=ps.inner[0];
alpar@774
   103
  
alpar@774
   104
  Bfs<Graph> bfs_test(G);
alpar@774
   105
  bfs_test.run(s);
alpar@774
   106
  
alpar@774
   107
  check(bfs_test.dist(t)==3,"Bfs found a wrong path. " << bfs_test.dist(t));
alpar@774
   108
alpar@1283
   109
  DirPath<Graph> p(G);
alpar@1283
   110
  check(bfs_test.getPath(p,t),"getPath() failed to set the path.");
alpar@1283
   111
  check(p.length()==3,"getPath() found a wrong path.");
alpar@1283
   112
  
alpar@774
   113
alpar@774
   114
  for(EdgeIt e(G); e==INVALID; ++e) {
alpar@986
   115
    Node u=G.source(e);
alpar@986
   116
    Node v=G.target(e);
alpar@774
   117
    check( !bfs_test.reached(u) ||
alpar@774
   118
	   (bfs_test.dist(v) > bfs_test.dist(u)+1),
alpar@774
   119
	   "Wrong output.");
alpar@774
   120
  }
alpar@774
   121
alpar@780
   122
  for(NodeIt v(G); v==INVALID; ++v) {
alpar@780
   123
    check(bfs_test.reached(v),"Each node should be reached.");
deba@1763
   124
    if ( bfs_test.predEdge(v)!=INVALID ) {
deba@1763
   125
      Edge e=bfs_test.predEdge(v);
alpar@986
   126
      Node u=G.source(e);
alpar@780
   127
      check(u==bfs_test.predNode(v),"Wrong tree.");
alpar@774
   128
      check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
alpar@780
   129
	    "Wrong distance. Difference: " 
alpar@774
   130
	    << std::abs(bfs_test.dist(v) - bfs_test.dist(u) 
alpar@774
   131
			- 1));
alpar@774
   132
    }
alpar@780
   133
  }
alpar@774
   134
}
alpar@780
   135