src/test/dijkstra_test.cc
author alpar
Wed, 16 Mar 2005 16:40:21 +0000
changeset 1220 20b26ee5812b
parent 1218 5331168bbb18
child 1283 fc20371677b9
permissions -rw-r--r--
- Add compilation tests for the function type interface of BFS/DFS/Dijkstra
- Fix the bugs covered up by these tests
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/test/dijkstra_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@578
    17
#include "test_tools.h"
alpar@921
    18
#include <lemon/smart_graph.h>
alpar@921
    19
#include <lemon/dijkstra.h>
alpar@1148
    20
#include <lemon/maps.h>
klao@959
    21
#include <lemon/concept/graph.h>
klao@959
    22
#include <lemon/concept/maps.h>
alpar@921
    23
using namespace lemon;
alpar@568
    24
alpar@568
    25
const int PET_SIZE =5;
alpar@568
    26
alpar@570
    27
alpar@793
    28
void check_Dijkstra_BinHeap_Compile() 
alpar@570
    29
{
alpar@570
    30
  typedef int VType;
klao@959
    31
  typedef concept::StaticGraph Graph;
alpar@570
    32
alpar@570
    33
  typedef Graph::Edge Edge;
alpar@570
    34
  typedef Graph::Node Node;
alpar@570
    35
  typedef Graph::EdgeIt EdgeIt;
alpar@570
    36
  typedef Graph::NodeIt NodeIt;
klao@959
    37
  typedef concept::ReadMap<Edge,VType> LengthMap;
alpar@570
    38
 
alpar@570
    39
  typedef Dijkstra<Graph, LengthMap> DType;
alpar@570
    40
  
alpar@570
    41
  Graph G;
alpar@570
    42
  Node n;
alpar@570
    43
  Edge e;
alpar@570
    44
  VType l;
alpar@570
    45
  bool b;
alpar@570
    46
  DType::DistMap d(G);
alpar@570
    47
  DType::PredMap p(G);
alpar@1148
    48
  //  DType::PredNodeMap pn(G);
alpar@793
    49
  LengthMap cap;
alpar@570
    50
alpar@570
    51
  DType dijkstra_test(G,cap);
alpar@570
    52
alpar@570
    53
  dijkstra_test.run(n);
alpar@570
    54
alpar@570
    55
  l  = dijkstra_test.dist(n);
alpar@570
    56
  e  = dijkstra_test.pred(n);
alpar@570
    57
  n  = dijkstra_test.predNode(n);
alpar@570
    58
  d  = dijkstra_test.distMap();
alpar@570
    59
  p  = dijkstra_test.predMap();
alpar@1148
    60
  //  pn = dijkstra_test.predNodeMap();
alpar@570
    61
  b  = dijkstra_test.reached(n);
alpar@1148
    62
  
alpar@570
    63
}
alpar@570
    64
alpar@1220
    65
void check_Dijkstra_Function_Compile() 
alpar@1220
    66
{
alpar@1220
    67
  typedef int VType;
alpar@1220
    68
  typedef concept::StaticGraph Graph;
alpar@1220
    69
alpar@1220
    70
  typedef Graph::Edge Edge;
alpar@1220
    71
  typedef Graph::Node Node;
alpar@1220
    72
  typedef Graph::EdgeIt EdgeIt;
alpar@1220
    73
  typedef Graph::NodeIt NodeIt;
alpar@1220
    74
  typedef concept::ReadMap<Edge,VType> LengthMap;
alpar@1220
    75
   
alpar@1220
    76
  dijkstra(Graph(),LengthMap(),Node()).run();
alpar@1220
    77
  dijkstra(Graph(),LengthMap()).source(Node()).run();
alpar@1220
    78
  dijkstra(Graph(),LengthMap())
alpar@1220
    79
    .predMap(concept::WriteMap<Node,Edge>())
alpar@1220
    80
    .distMap(concept::WriteMap<Node,VType>())
alpar@1220
    81
    .run(Node());
alpar@1220
    82
  
alpar@1220
    83
}
alpar@1220
    84
alpar@1220
    85
alpar@568
    86
int main()
alpar@568
    87
{
alpar@568
    88
    
alpar@568
    89
  typedef SmartGraph Graph;
alpar@568
    90
alpar@568
    91
  typedef Graph::Edge Edge;
alpar@568
    92
  typedef Graph::Node Node;
alpar@568
    93
  typedef Graph::EdgeIt EdgeIt;
alpar@568
    94
  typedef Graph::NodeIt NodeIt;
alpar@568
    95
  typedef Graph::EdgeMap<int> LengthMap;
alpar@568
    96
alpar@568
    97
  Graph G;
alpar@568
    98
  Node s, t;
alpar@568
    99
  LengthMap cap(G);
alpar@568
   100
  PetStruct<Graph> ps = addPetersen(G,PET_SIZE);
alpar@578
   101
   
alpar@568
   102
  for(int i=0;i<PET_SIZE;i++) {
alpar@568
   103
    cap[ps.outcir[i]]=4;
alpar@568
   104
    cap[ps.incir[i]]=1;
alpar@568
   105
    cap[ps.chords[i]]=10;
alpar@568
   106
  }
alpar@568
   107
  s=ps.outer[0];
alpar@568
   108
  t=ps.inner[1];
alpar@568
   109
  
alpar@568
   110
  Dijkstra<Graph, LengthMap> 
alpar@568
   111
	dijkstra_test(G, cap);
alpar@568
   112
  dijkstra_test.run(s);
alpar@568
   113
  
alpar@568
   114
  check(dijkstra_test.dist(t)==13,"Dijkstra found a wrong path.");
alpar@585
   115
alpar@585
   116
hegyi@776
   117
  for(EdgeIt e(G); e!=INVALID; ++e) {
alpar@986
   118
    Node u=G.source(e);
alpar@986
   119
    Node v=G.target(e);
alpar@585
   120
    check( !dijkstra_test.reached(u) ||
alpar@585
   121
	   (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= cap[e]),
alpar@986
   122
	   "dist(target)-dist(source)- edge_length= " 
alpar@585
   123
	   << dijkstra_test.dist(v) - dijkstra_test.dist(u) 
alpar@585
   124
	   - cap[e]);
alpar@585
   125
  }
alpar@585
   126
alpar@585
   127
  ///\bug This works only for integer lengths
alpar@780
   128
  for(NodeIt v(G); v!=INVALID; ++v){
alpar@780
   129
    check(dijkstra_test.reached(v),"Each node should be reached.");
alpar@780
   130
    if ( dijkstra_test.pred(v)!=INVALID ) {
alpar@585
   131
      Edge e=dijkstra_test.pred(v);
alpar@986
   132
      Node u=G.source(e);
alpar@780
   133
      check(u==dijkstra_test.predNode(v),"Wrong tree.");
alpar@585
   134
      check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == cap[e],
alpar@780
   135
	    "Wrong distance! Difference: " 
alpar@585
   136
	    << std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u) 
alpar@585
   137
			    - cap[e]));
alpar@585
   138
    }
alpar@780
   139
  }
alpar@1148
   140
alpar@1148
   141
  
alpar@1148
   142
  {
alpar@1218
   143
    NullMap<Node,Edge> myPredMap;
alpar@1218
   144
    dijkstra(G,cap).predMap(myPredMap).run(s);
alpar@1148
   145
  }
alpar@1148
   146
  return 0;
alpar@568
   147
}