1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
 
     3  * This file is a part of LEMON, a generic C++ optimization library.
 
     5  * Copyright (C) 2003-2009
 
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     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.
 
    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
 
    24 #include <lemon/concept_check.h>
 
    25 #include <lemon/concepts/heap.h>
 
    27 #include <lemon/smart_graph.h>
 
    28 #include <lemon/lgf_reader.h>
 
    29 #include <lemon/dijkstra.h>
 
    30 #include <lemon/maps.h>
 
    32 #include <lemon/bin_heap.h>
 
    33 #include <lemon/fourary_heap.h>
 
    34 #include <lemon/kary_heap.h>
 
    35 #include <lemon/fib_heap.h>
 
    36 #include <lemon/pairing_heap.h>
 
    37 #include <lemon/radix_heap.h>
 
    38 #include <lemon/binom_heap.h>
 
    39 #include <lemon/bucket_heap.h>
 
    41 #include "test_tools.h"
 
    43 using namespace lemon;
 
    44 using namespace lemon::concepts;
 
    46 typedef ListDigraph Digraph;
 
    47 DIGRAPH_TYPEDEFS(Digraph);
 
    87 int test_seq[] = { 2, 28, 19, 27, 33, 25, 13, 41, 10, 26,  1,  9,  4, 34};
 
    88 int test_inc[] = {20, 28, 34, 16,  0, 46, 44,  0, 42, 32, 14,  8,  6, 37};
 
    90 int test_len = sizeof(test_seq) / sizeof(test_seq[0]);
 
    92 template <typename Heap>
 
    94   RangeMap<int> map(test_len, -1);
 
    97   std::vector<int> v(test_len);
 
    98   for (int i = 0; i < test_len; ++i) {
 
   102   std::sort(v.begin(), v.end());
 
   103   for (int i = 0; i < test_len; ++i) {
 
   104     check(v[i] == heap.prio(), "Wrong order in heap sort.");
 
   109 template <typename Heap>
 
   110 void heapIncreaseTest() {
 
   111   RangeMap<int> map(test_len, -1);
 
   115   std::vector<int> v(test_len);
 
   116   for (int i = 0; i < test_len; ++i) {
 
   120   for (int i = 0; i < test_len; ++i) {
 
   122     heap.increase(i, v[i]);
 
   124   std::sort(v.begin(), v.end());
 
   125   for (int i = 0; i < test_len; ++i) {
 
   126     check(v[i] == heap.prio(), "Wrong order in heap increase test.");
 
   131 template <typename Heap>
 
   132 void dijkstraHeapTest(const Digraph& digraph, const IntArcMap& length,
 
   135   typename Dijkstra<Digraph, IntArcMap>::template SetStandardHeap<Heap>::
 
   136     Create dijkstra(digraph, length);
 
   138   dijkstra.run(source);
 
   140   for(ArcIt a(digraph); a != INVALID; ++a) {
 
   141     Node s = digraph.source(a);
 
   142     Node t = digraph.target(a);
 
   143     if (dijkstra.reached(s)) {
 
   144       check( dijkstra.dist(t) - dijkstra.dist(s) <= length[a],
 
   145              "Error in shortest path tree.");
 
   149   for(NodeIt n(digraph); n != INVALID; ++n) {
 
   150     if ( dijkstra.reached(n) && dijkstra.predArc(n) != INVALID ) {
 
   151       Arc a = dijkstra.predArc(n);
 
   152       Node s = digraph.source(a);
 
   153       check( dijkstra.dist(n) - dijkstra.dist(s) == length[a],
 
   154              "Error in shortest path tree.");
 
   164   typedef RangeMap<int> ItemIntMap;
 
   167   IntArcMap length(digraph);
 
   170   std::istringstream input(test_lgf);
 
   171   digraphReader(digraph, input).
 
   172     arcMap("capacity", length).
 
   173     node("source", source).
 
   178     typedef BinHeap<Prio, ItemIntMap> IntHeap;
 
   179     checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
 
   180     heapSortTest<IntHeap>();
 
   181     heapIncreaseTest<IntHeap>();
 
   183     typedef BinHeap<Prio, IntNodeMap > NodeHeap;
 
   184     checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
 
   185     dijkstraHeapTest<NodeHeap>(digraph, length, source);
 
   190     typedef FouraryHeap<Prio, ItemIntMap> IntHeap;
 
   191     checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
 
   192     heapSortTest<IntHeap>();
 
   193     heapIncreaseTest<IntHeap>();
 
   195     typedef FouraryHeap<Prio, IntNodeMap > NodeHeap;
 
   196     checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
 
   197     dijkstraHeapTest<NodeHeap>(digraph, length, source);
 
   202     typedef KaryHeap<Prio, ItemIntMap> IntHeap;
 
   203     checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
 
   204     heapSortTest<IntHeap>();
 
   205     heapIncreaseTest<IntHeap>();
 
   207     typedef KaryHeap<Prio, IntNodeMap > NodeHeap;
 
   208     checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
 
   209     dijkstraHeapTest<NodeHeap>(digraph, length, source);
 
   214     typedef FibHeap<Prio, ItemIntMap> IntHeap;
 
   215     checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
 
   216     heapSortTest<IntHeap>();
 
   217     heapIncreaseTest<IntHeap>();
 
   219     typedef FibHeap<Prio, IntNodeMap > NodeHeap;
 
   220     checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
 
   221     dijkstraHeapTest<NodeHeap>(digraph, length, source);
 
   226     typedef PairingHeap<Prio, ItemIntMap> IntHeap;
 
   227     checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
 
   228     heapSortTest<IntHeap>();
 
   229     heapIncreaseTest<IntHeap>();
 
   231     typedef PairingHeap<Prio, IntNodeMap > NodeHeap;
 
   232     checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
 
   233     dijkstraHeapTest<NodeHeap>(digraph, length, source);
 
   238     typedef RadixHeap<ItemIntMap> IntHeap;
 
   239     checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
 
   240     heapSortTest<IntHeap>();
 
   241     heapIncreaseTest<IntHeap>();
 
   243     typedef RadixHeap<IntNodeMap > NodeHeap;
 
   244     checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
 
   245     dijkstraHeapTest<NodeHeap>(digraph, length, source);
 
   250     typedef BinomHeap<Prio, ItemIntMap> IntHeap;
 
   251     checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
 
   252     heapSortTest<IntHeap>();
 
   253     heapIncreaseTest<IntHeap>();
 
   255     typedef BinomHeap<Prio, IntNodeMap > NodeHeap;
 
   256     checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
 
   257     dijkstraHeapTest<NodeHeap>(digraph, length, source);
 
   260   // BucketHeap, SimpleBucketHeap
 
   262     typedef BucketHeap<ItemIntMap> IntHeap;
 
   263     checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
 
   264     heapSortTest<IntHeap>();
 
   265     heapIncreaseTest<IntHeap>();
 
   267     typedef BucketHeap<IntNodeMap > NodeHeap;
 
   268     checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
 
   269     dijkstraHeapTest<NodeHeap>(digraph, length, source);
 
   271     typedef SimpleBucketHeap<ItemIntMap> SimpleIntHeap;
 
   272     heapSortTest<SimpleIntHeap>();