src/test/heap_test.h
author alpar
Wed, 30 Mar 2005 13:01:58 +0000
changeset 1275 16980bf77bd3
parent 1187 04e5825000c5
permissions -rw-r--r--
Minor improvements
deba@1187
     1
// -+- c++ -+-
deba@1206
     2
deba@1187
     3
#include <vector>
deba@1187
     4
#include <algorithm>
deba@1187
     5
deba@1187
     6
#include <lemon/dijkstra.h>
deba@1187
     7
deba@1187
     8
class IntIntMap : public std::vector<int> {
deba@1187
     9
public:
deba@1187
    10
  typedef std::vector<int> Parent;
deba@1187
    11
deba@1187
    12
  IntIntMap() : Parent() {}
deba@1187
    13
  IntIntMap(int n) : Parent(n) {}
deba@1187
    14
  IntIntMap(int n, int v) : Parent(n, v) {}
deba@1187
    15
deba@1187
    16
  void set(int key, int value) {
deba@1187
    17
    Parent::operator[](key) = value;
deba@1187
    18
  }
deba@1187
    19
};
deba@1187
    20
deba@1187
    21
deba@1187
    22
template <typename _Heap>
deba@1187
    23
void heapSortTest(int n) {
deba@1187
    24
  typedef _Heap Heap;
deba@1187
    25
  IntIntMap map(n, -1);
deba@1187
    26
deba@1187
    27
  Heap heap(map);
deba@1187
    28
  
deba@1187
    29
  std::vector<int> v(n);
deba@1187
    30
deba@1187
    31
  for (int i = 0; i < n; ++i) {
deba@1187
    32
    v[i] = rand() % 1000;
deba@1187
    33
    heap.push(i, v[i]);
deba@1187
    34
  }
deba@1187
    35
  std::sort(v.begin(), v.end());
deba@1187
    36
  for (int i = 0; i < n; ++i) {
deba@1187
    37
    check(v[i] == heap.prio() ,"Wrong order in heap sort.");
deba@1187
    38
    heap.pop();
deba@1187
    39
  }
deba@1187
    40
}
deba@1187
    41
deba@1206
    42
template <typename _Heap>
deba@1206
    43
void heapIncreaseTest(int n) {
deba@1206
    44
  typedef _Heap Heap;
deba@1206
    45
  IntIntMap map(n, -1);
deba@1206
    46
deba@1206
    47
  Heap heap(map);
deba@1206
    48
  
deba@1206
    49
  std::vector<int> v(n);
deba@1206
    50
deba@1206
    51
  for (int i = 0; i < n; ++i) {
deba@1206
    52
    v[i] = rand() % 1000;
deba@1206
    53
    heap.push(i, v[i]);
deba@1206
    54
  }
deba@1206
    55
  for (int i = 0; i < n; ++i) {
deba@1206
    56
    v[i] += rand() % 1000;
deba@1206
    57
    heap.increase(i, v[i]);
deba@1206
    58
  }
deba@1206
    59
  std::sort(v.begin(), v.end());
deba@1206
    60
  for (int i = 0; i < n; ++i) {
deba@1206
    61
    check(v[i] == heap.prio() ,"Wrong order in heap increase test.");
deba@1206
    62
    heap.pop();
deba@1206
    63
  }
deba@1206
    64
}
deba@1206
    65
deba@1206
    66
deba@1206
    67
deba@1187
    68
template <typename _Traits, typename _Heap>
deba@1187
    69
struct DefHeapTraits : public _Traits {
deba@1187
    70
  typedef _Heap Heap;
deba@1187
    71
};
deba@1187
    72
deba@1187
    73
template <typename _Graph, typename _LengthMap, typename _Heap>
deba@1187
    74
void dijkstraHeapTest(_Graph& graph, _LengthMap& length,
deba@1187
    75
		      typename _Graph::Node& start) {
deba@1187
    76
deba@1187
    77
  typedef _Heap Heap;
deba@1187
    78
  typedef _Graph Graph;
deba@1187
    79
  typedef _LengthMap LengthMap;
deba@1187
    80
deba@1187
    81
  typedef typename Graph::Node Node;
deba@1187
    82
  typedef typename Graph::Edge Edge;
deba@1187
    83
  typedef typename Graph::NodeIt NodeIt;
deba@1187
    84
  typedef typename Graph::EdgeIt EdgeIt;
deba@1187
    85
deba@1187
    86
  Dijkstra<Graph, LengthMap, 
deba@1187
    87
    DefHeapTraits<DijkstraDefaultTraits<Graph, LengthMap>, Heap> > 
deba@1187
    88
    dijkstra(graph, length);
deba@1187
    89
deba@1187
    90
  dijkstra.run(start);
deba@1187
    91
deba@1187
    92
  for(EdgeIt e(graph); e!=INVALID; ++e) {
deba@1187
    93
    Node u=graph.source(e); 
deba@1187
    94
    Node v=graph.target(e);
deba@1187
    95
    if (dijkstra.reached(u)) {
deba@1187
    96
      check( dijkstra.dist(v) - dijkstra.dist(u) <= length[e],
deba@1187
    97
	     "Error in a shortest path tree edge!");
deba@1187
    98
    }
deba@1187
    99
  }
deba@1187
   100
deba@1187
   101
  for(NodeIt v(graph); v!=INVALID; ++v) {
deba@1206
   102
    if ( dijkstra.reached(v) && dijkstra.pred(v) != INVALID ) {
deba@1187
   103
      Edge e=dijkstra.pred(v);
deba@1187
   104
      Node u=graph.source(e);
deba@1187
   105
      check( dijkstra.dist(v) - dijkstra .dist(u) == length[e],
deba@1187
   106
	     "Error in a shortest path tree edge!");
deba@1187
   107
    }
deba@1187
   108
  }
deba@1187
   109
deba@1187
   110
}