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