COIN-OR::LEMON - Graph Library

source: lemon-main/test/dijkstra_test.cc @ 189:a63ed81c57ba

Last change on this file since 189:a63ed81c57ba was 171:02f4d5d9bfd7, checked in by Peter Kovacs <kpeter@…>, 16 years ago

Improve and redesign test programs + unify their output (ticket #25)

  • Move graph related utilities form test_tools.h to graph_test.h.
  • Move the contents of graph_utils_test.h to graph_utils_test.cc.
  • Rename map_test.h -> graph_maps_test.h.
  • Rename digraph_test.h -> graph_test.h.
  • Many improvements in the following files:
    • digraph_test.cc
    • graph_test.cc
    • graph_test.h
    • graph_maps_test.h
    • graph_utils_test.cc
    • bfs_test.cc
    • dfs_test.cc
    • counter_test.cc
  • Test programs print messages only if it really seems necessary.
  • Remove \file commands form .cc test files.
File size: 3.7 KB
Line 
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#include <lemon/concepts/digraph.h>
20#include <lemon/smart_graph.h>
21#include <lemon/list_graph.h>
22#include <lemon/graph_utils.h>
23#include <lemon/dijkstra.h>
24#include <lemon/path.h>
25
26#include "graph_test.h"
27#include "test_tools.h"
28
29using namespace lemon;
30
31void checkDijkstraCompile()
32{
33  typedef int VType;
34  typedef concepts::Digraph Digraph;
35  typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
36  typedef Dijkstra<Digraph, LengthMap> DType;
37 
38  Digraph G;
39  Digraph::Node n;
40  Digraph::Arc e;
41  VType l;
42  bool b;
43  DType::DistMap d(G);
44  DType::PredMap p(G);
45  //  DType::PredNodeMap pn(G);
46  LengthMap length;
47
48  DType dijkstra_test(G,length);
49
50  dijkstra_test.run(n);
51
52  l  = dijkstra_test.dist(n);
53  e  = dijkstra_test.predArc(n);
54  n  = dijkstra_test.predNode(n);
55  d  = dijkstra_test.distMap();
56  p  = dijkstra_test.predMap();
57  //  pn = dijkstra_test.predNodeMap();
58  b  = dijkstra_test.reached(n);
59
60  Path<Digraph> pp = dijkstra_test.path(n);
61}
62
63void checkDijkstraFunctionCompile()
64{
65  typedef int VType;
66  typedef concepts::Digraph Digraph;
67  typedef Digraph::Arc Arc;
68  typedef Digraph::Node Node;
69  typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
70   
71  Digraph g;
72  dijkstra(g,LengthMap(),Node()).run();
73  dijkstra(g,LengthMap()).source(Node()).run();
74  dijkstra(g,LengthMap())
75    .predMap(concepts::WriteMap<Node,Arc>())
76    .distMap(concepts::WriteMap<Node,VType>())
77    .run(Node());
78}
79
80template <class Digraph>
81void checkDijkstra() {   
82  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
83  typedef typename Digraph::template ArcMap<int> LengthMap;
84
85  Digraph G;
86  Node s, t;
87  LengthMap length(G);
88  PetStruct<Digraph> ps = addPetersen(G, 5);
89   
90  for(int i=0;i<5;i++) {
91    length[ps.outcir[i]]=4;
92    length[ps.incir[i]]=1;
93    length[ps.chords[i]]=10;
94  }
95  s=ps.outer[0];
96  t=ps.inner[1];
97 
98  Dijkstra<Digraph, LengthMap>
99        dijkstra_test(G, length);
100  dijkstra_test.run(s);
101 
102  check(dijkstra_test.dist(t)==13,"Dijkstra found a wrong path.");
103
104  Path<Digraph> p = dijkstra_test.path(t);
105  check(p.length()==4,"getPath() found a wrong path.");
106  check(checkPath(G, p),"path() found a wrong path.");
107  check(pathSource(G, p) == s,"path() found a wrong path.");
108  check(pathTarget(G, p) == t,"path() found a wrong path.");
109 
110  for(ArcIt e(G); e!=INVALID; ++e) {
111    Node u=G.source(e);
112    Node v=G.target(e);
113    check( !dijkstra_test.reached(u) || (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]),
114           "dist(target)-dist(source)-arc_length= " << dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]);
115  }
116
117  for(NodeIt v(G); v!=INVALID; ++v){
118    check(dijkstra_test.reached(v),"Each node should be reached.");
119    if ( dijkstra_test.predArc(v)!=INVALID ) {
120      Arc e=dijkstra_test.predArc(v);
121      Node u=G.source(e);
122      check(u==dijkstra_test.predNode(v),"Wrong tree.");
123      check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e],
124            "Wrong distance! Difference: " << std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]));
125    }
126  }
127 
128  {
129    NullMap<Node,Arc> myPredMap;
130    dijkstra(G,length).predMap(myPredMap).run(s);
131  }
132}
133
134int main() {
135  checkDijkstra<ListDigraph>();
136  checkDijkstra<SmartDigraph>();
137  return 0;
138}
Note: See TracBrowser for help on using the repository browser.