gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Port dijkstra_test.cc from SVN -r3499
0 2 1
default
3 files changed with 143 insertions and 0 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2008
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
///\file
20
///\brief Test cases for Dijkstra algorithm.
21

	
22
#include <lemon/concepts/digraph.h>
23
#include <lemon/smart_graph.h>
24
#include <lemon/list_graph.h>
25
#include <lemon/graph_utils.h>
26
#include <lemon/dijkstra.h>
27
#include <lemon/path.h>
28

	
29
#include "test_tools.h"
30

	
31
using namespace lemon;
32

	
33
void checkDijkstraCompile() 
34
{
35
  typedef int VType;
36
  typedef concepts::Digraph Digraph;
37
  typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
38
  typedef Dijkstra<Digraph, LengthMap> DType;
39
  
40
  Digraph G;
41
  Digraph::Node n;
42
  Digraph::Arc e;
43
  VType l;
44
  bool b;
45
  DType::DistMap d(G);
46
  DType::PredMap p(G);
47
  //  DType::PredNodeMap pn(G);
48
  LengthMap length;
49

	
50
  DType dijkstra_test(G,length);
51

	
52
  dijkstra_test.run(n);
53

	
54
  l  = dijkstra_test.dist(n);
55
  e  = dijkstra_test.predArc(n);
56
  n  = dijkstra_test.predNode(n);
57
  d  = dijkstra_test.distMap();
58
  p  = dijkstra_test.predMap();
59
  //  pn = dijkstra_test.predNodeMap();
60
  b  = dijkstra_test.reached(n);
61

	
62
  Path<Digraph> pp = dijkstra_test.path(n);
63
}
64

	
65
void checkDijkstraFunctionCompile() 
66
{
67
  typedef int VType;
68
  typedef concepts::Digraph Digraph;
69
  typedef Digraph::Arc Arc;
70
  typedef Digraph::Node Node;
71
  typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
72
   
73
  Digraph g;
74
  dijkstra(g,LengthMap(),Node()).run();
75
  dijkstra(g,LengthMap()).source(Node()).run();
76
  dijkstra(g,LengthMap())
77
    .predMap(concepts::WriteMap<Node,Arc>())
78
    .distMap(concepts::WriteMap<Node,VType>())
79
    .run(Node());
80
}
81

	
82
template <class Digraph>
83
void checkDijkstra() {    
84
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
85
  typedef typename Digraph::template ArcMap<int> LengthMap;
86

	
87
  Digraph G;
88
  Node s, t;
89
  LengthMap length(G);
90
  PetStruct<Digraph> ps = addPetersen(G, 5);
91
   
92
  for(int i=0;i<5;i++) {
93
    length[ps.outcir[i]]=4;
94
    length[ps.incir[i]]=1;
95
    length[ps.chords[i]]=10;
96
  }
97
  s=ps.outer[0];
98
  t=ps.inner[1];
99
  
100
  Dijkstra<Digraph, LengthMap> 
101
	dijkstra_test(G, length);
102
  dijkstra_test.run(s);
103
  
104
  check(dijkstra_test.dist(t)==13,"Dijkstra found a wrong path.");
105

	
106
  Path<Digraph> p = dijkstra_test.path(t);
107
  check(p.length()==4,"getPath() found a wrong path.");
108
  check(checkPath(G, p),"path() found a wrong path.");
109
  check(pathSource(G, p) == s,"path() found a wrong path.");
110
  check(pathTarget(G, p) == t,"path() found a wrong path.");
111
  
112
  for(ArcIt e(G); e!=INVALID; ++e) {
113
    Node u=G.source(e);
114
    Node v=G.target(e);
115
    check( !dijkstra_test.reached(u) || (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]),
116
	   "dist(target)-dist(source)-arc_length= " << dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]);
117
  }
118

	
119
  for(NodeIt v(G); v!=INVALID; ++v){
120
    check(dijkstra_test.reached(v),"Each node should be reached.");
121
    if ( dijkstra_test.predArc(v)!=INVALID ) {
122
      Arc e=dijkstra_test.predArc(v);
123
      Node u=G.source(e);
124
      check(u==dijkstra_test.predNode(v),"Wrong tree.");
125
      check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e],
126
	    "Wrong distance! Difference: " << std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]));
127
    }
128
  }
129
  
130
  {
131
    NullMap<Node,Arc> myPredMap;
132
    dijkstra(G,length).predMap(myPredMap).run(s);
133
  }
134
}
135

	
136
int main() {
137
  checkDijkstra<ListDigraph>();
138
  checkDijkstra<SmartDigraph>();
139
  return 0;
140
}
Ignore white space 12 line context
... ...
@@ -4,12 +4,13 @@
4 4

	
5 5
set (TESTS
6 6
  bfs_test
7 7
  counter_test
8 8
  dfs_test
9 9
  digraph_test
10
  dijkstra_test
10 11
  dim_test
11 12
  error_test
12 13
  graph_test
13 14
  kruskal_test
14 15
  maps_test
15 16
  random_test
Ignore white space 6 line context
... ...
@@ -10,12 +10,13 @@
10 10

	
11 11
check_PROGRAMS += \
12 12
	test/bfs_test \
13 13
        test/counter_test \
14 14
	test/dfs_test \
15 15
	test/digraph_test \
16
	test/dijkstra_test \
16 17
        test/dim_test \
17 18
	test/error_test \
18 19
	test/graph_test \
19 20
	test/graph_utils_test \
20 21
	test/kruskal_test \
21 22
        test/maps_test \
... ...
@@ -30,12 +31,13 @@
30 31
XFAIL_TESTS += test/test_tools_fail$(EXEEXT)
31 32

	
32 33
test_bfs_test_SOURCES = test/bfs_test.cc
33 34
test_counter_test_SOURCES = test/counter_test.cc
34 35
test_dfs_test_SOURCES = test/dfs_test.cc
35 36
test_digraph_test_SOURCES = test/digraph_test.cc
37
test_dijkstra_test_SOURCES = test/dijkstra_test.cc
36 38
test_dim_test_SOURCES = test/dim_test.cc
37 39
test_error_test_SOURCES = test/error_test.cc
38 40
test_graph_test_SOURCES = test/graph_test.cc
39 41
test_graph_utils_test_SOURCES = test/graph_utils_test.cc
40 42
# test_heap_test_SOURCES = test/heap_test.cc
41 43
test_kruskal_test_SOURCES = test/kruskal_test.cc
0 comments (0 inline)