src/test/heap_test.cc
author alpar
Thu, 31 Mar 2005 14:04:13 +0000
changeset 1284 b941d044f87b
parent 1206 9c398137c2cb
child 1325 916ec8699dc3
permissions -rw-r--r--
SmartGraph can also split() a node!
deba@1187
     1
// -*- c++ -*-
deba@1187
     2
deba@1187
     3
#include <iostream>
deba@1187
     4
#include <fstream>
alpar@1215
     5
#include <string>
deba@1187
     6
#include <vector>
deba@1187
     7
deba@1187
     8
#include <lemon/concept_check.h>
deba@1187
     9
deba@1206
    10
#include <lemon/smart_graph.h>
deba@1206
    11
deba@1206
    12
#include <lemon/graph_reader.h>
deba@1206
    13
deba@1187
    14
#include <lemon/bin_heap.h>
deba@1187
    15
#include <lemon/fib_heap.h>
deba@1187
    16
#include <lemon/radix_heap.h>
deba@1187
    17
deba@1206
    18
#include "test_tools.h"
deba@1187
    19
deba@1187
    20
#include "heap_test.h"
deba@1187
    21
deba@1187
    22
deba@1187
    23
using namespace lemon;
deba@1187
    24
deba@1187
    25
template <typename Item, typename Prio, typename ItemIntMap>
deba@1187
    26
class HeapConcept {
deba@1187
    27
public:  
deba@1187
    28
deba@1187
    29
  template <typename _Heap>
deba@1187
    30
  struct Constraints {
deba@1187
    31
  public:
deba@1187
    32
    
deba@1187
    33
    void constraints() {
deba@1187
    34
      Item item;
deba@1187
    35
      Prio prio;
deba@1187
    36
deba@1206
    37
      ignore_unused_variable_warning(item);
deba@1206
    38
      ignore_unused_variable_warning(prio);
deba@1206
    39
deba@1187
    40
      typedef typename _Heap::state_enum state_enum;
deba@1187
    41
      state_enum state;
deba@1206
    42
deba@1206
    43
      ignore_unused_variable_warning(state);
deba@1187
    44
      
deba@1187
    45
      _Heap heap1 = _Heap(map);
deba@1206
    46
deba@1206
    47
      ignore_unused_variable_warning(heap1);
deba@1187
    48
      
deba@1187
    49
      heap.push(item, prio);
deba@1187
    50
deba@1187
    51
      prio = heap.prio();
deba@1187
    52
      item = heap.top();
deba@1187
    53
deba@1187
    54
      heap.pop();
deba@1187
    55
deba@1187
    56
      heap.set(item, prio);
deba@1187
    57
      heap.decrease(item, prio);
deba@1187
    58
      heap.increase(item, prio);
deba@1187
    59
      prio = heap[item];
deba@1187
    60
deba@1187
    61
      heap.erase(item);
deba@1187
    62
deba@1187
    63
      state = heap.state(item);
deba@1187
    64
deba@1187
    65
      state = _Heap::PRE_HEAP;
deba@1187
    66
      state = _Heap::IN_HEAP;
deba@1187
    67
      state = _Heap::POST_HEAP;
deba@1187
    68
    }
deba@1187
    69
    
deba@1187
    70
    _Heap& heap;
deba@1187
    71
    ItemIntMap& map;
deba@1187
    72
  };
deba@1187
    73
};
deba@1187
    74
deba@1187
    75
int main() {
deba@1187
    76
deba@1187
    77
  typedef int Item;
deba@1187
    78
  typedef int Prio;
deba@1187
    79
  typedef IntIntMap ItemIntMap;
deba@1187
    80
deba@1187
    81
  typedef ListGraph Graph;
deba@1187
    82
deba@1187
    83
  typedef Graph::Edge Edge;
deba@1187
    84
  typedef Graph::Node Node;
deba@1187
    85
  typedef Graph::EdgeIt EdgeIt;
deba@1187
    86
  typedef Graph::NodeIt NodeIt;
deba@1187
    87
  typedef Graph::EdgeMap<int> LengthMap;
deba@1187
    88
deba@1187
    89
  Graph graph;
deba@1187
    90
  LengthMap length(graph);
deba@1187
    91
  Node start;
deba@1187
    92
deba@1206
    93
  /// \todo create own test graph
alpar@1215
    94
alpar@1215
    95
  std::string f_name;
alpar@1215
    96
  if( getenv("srcdir") )
alpar@1215
    97
    f_name = std::string(getenv("srcdir"));
alpar@1215
    98
  else f_name = ".";
alpar@1215
    99
  f_name += "/dijkstra_test.lgf";
alpar@1215
   100
  
alpar@1215
   101
  std::ifstream input(f_name.c_str());
alpar@1215
   102
  check(input, "Input file '" << f_name << "' not found.");
deba@1206
   103
  readGraph(input, graph, length, start);  
deba@1187
   104
 
deba@1187
   105
  {
deba@1187
   106
    std::cerr << "Checking Bin Heap" << std::endl;
deba@1187
   107
deba@1187
   108
    typedef BinHeap<Item, Prio, ItemIntMap> IntHeap;
deba@1187
   109
    checkConcept<HeapConcept<Item, Prio, ItemIntMap>, IntHeap>();
deba@1206
   110
    heapSortTest<IntHeap>(100);
deba@1206
   111
    heapIncreaseTest<IntHeap>(100);
deba@1187
   112
    
deba@1187
   113
    typedef FibHeap<Node, Prio, Graph::NodeMap<int> > NodeHeap;
deba@1187
   114
    checkConcept<HeapConcept<Node, Prio, Graph::NodeMap<int> >, NodeHeap>();
deba@1187
   115
    dijkstraHeapTest<Graph, LengthMap, NodeHeap>(graph, length, start);
deba@1187
   116
  }
deba@1187
   117
  {
deba@1187
   118
    std::cerr << "Checking Fib Heap" << std::endl;
deba@1187
   119
deba@1187
   120
    typedef FibHeap<Item, Prio, ItemIntMap> IntHeap;
deba@1187
   121
    checkConcept<HeapConcept<Item, Prio, ItemIntMap>, IntHeap>();
deba@1206
   122
    heapSortTest<IntHeap>(100);
deba@1206
   123
    heapIncreaseTest<IntHeap>(100);
deba@1187
   124
deba@1187
   125
    typedef FibHeap<Node, Prio, Graph::NodeMap<int> > NodeHeap;
deba@1187
   126
    checkConcept<HeapConcept<Node, Prio, Graph::NodeMap<int> >, NodeHeap>();
deba@1187
   127
    dijkstraHeapTest<Graph, LengthMap, NodeHeap>(graph, length, start);
deba@1187
   128
  }
deba@1187
   129
  {
deba@1187
   130
    std::cerr << "Checking Radix Heap" << std::endl;
deba@1187
   131
deba@1187
   132
    typedef RadixHeap<Item, ItemIntMap> IntHeap;
deba@1187
   133
    checkConcept<HeapConcept<Item, Prio, ItemIntMap>, IntHeap>();
deba@1206
   134
    heapSortTest<IntHeap>(100);
deba@1206
   135
    heapIncreaseTest<IntHeap>(100);
deba@1187
   136
deba@1187
   137
    typedef RadixHeap<Node, Graph::NodeMap<int> > NodeHeap;
deba@1187
   138
    checkConcept<HeapConcept<Node, Prio, Graph::NodeMap<int> >, NodeHeap>();
deba@1187
   139
    dijkstraHeapTest<Graph, LengthMap, NodeHeap>(graph, length, start);
deba@1187
   140
  }
deba@1187
   141
deba@1187
   142
  std::cout << __FILE__ ": All tests passed.\n";
deba@1187
   143
deba@1187
   144
  return 0;
deba@1187
   145
}