Location: LEMON/LEMON-official/test/heap_test.cc - annotation
Load file history
Merge bugfix #330 to branch 1.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | r209:765619b7cbb2 r100:4f754b4cf82b r209:765619b7cbb2 r100:4f754b4cf82b r463:88ed40ad0d4f r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r203:215bfc30b14f r100:4f754b4cf82b r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r100:4f754b4cf82b r100:4f754b4cf82b r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r209:765619b7cbb2 r209:765619b7cbb2 r209:765619b7cbb2 r209:765619b7cbb2 r209:765619b7cbb2 r209:765619b7cbb2 r209:765619b7cbb2 r209:765619b7cbb2 r209:765619b7cbb2 r209:765619b7cbb2 r209:765619b7cbb2 r209:765619b7cbb2 r209:765619b7cbb2 r209:765619b7cbb2 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r212:1ae84dea7d09 r209:765619b7cbb2 r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r209:765619b7cbb2 r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r209:765619b7cbb2 r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r209:765619b7cbb2 r209:765619b7cbb2 r209:765619b7cbb2 r257:8d76a7bf9961 r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r209:765619b7cbb2 r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r212:1ae84dea7d09 r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r209:765619b7cbb2 r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r203:215bfc30b14f r209:765619b7cbb2 r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r100:4f754b4cf82b r203:215bfc30b14f r293:47fbc814aa31 r203:215bfc30b14f r203:215bfc30b14f r209:765619b7cbb2 r209:765619b7cbb2 r100:4f754b4cf82b r100:4f754b4cf82b r100:4f754b4cf82b r203:215bfc30b14f r203:215bfc30b14f r209:765619b7cbb2 r203:215bfc30b14f r203:215bfc30b14f r203:215bfc30b14f r100:4f754b4cf82b r100:4f754b4cf82b r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r728:532697c9fa53 r100:4f754b4cf82b r100:4f754b4cf82b | /* -*- mode: C++; indent-tabs-mode: nil; -*-
*
* This file is a part of LEMON, a generic C++ optimization library.
*
* Copyright (C) 2003-2009
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
*
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
*
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
* purpose.
*
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <lemon/concept_check.h>
#include <lemon/concepts/heap.h>
#include <lemon/smart_graph.h>
#include <lemon/lgf_reader.h>
#include <lemon/dijkstra.h>
#include <lemon/maps.h>
#include <lemon/bin_heap.h>
#include <lemon/fib_heap.h>
#include <lemon/radix_heap.h>
#include <lemon/bucket_heap.h>
#include "test_tools.h"
using namespace lemon;
using namespace lemon::concepts;
typedef ListDigraph Digraph;
DIGRAPH_TYPEDEFS(Digraph);
char test_lgf[] =
"@nodes\n"
"label\n"
"0\n"
"1\n"
"2\n"
"3\n"
"4\n"
"5\n"
"6\n"
"7\n"
"8\n"
"9\n"
"@arcs\n"
" label capacity\n"
"0 5 0 94\n"
"3 9 1 11\n"
"8 7 2 83\n"
"1 2 3 94\n"
"5 7 4 35\n"
"7 4 5 84\n"
"9 5 6 38\n"
"0 4 7 96\n"
"6 7 8 6\n"
"3 1 9 27\n"
"5 2 10 77\n"
"5 6 11 69\n"
"6 5 12 41\n"
"4 6 13 70\n"
"3 2 14 45\n"
"7 9 15 93\n"
"5 9 16 50\n"
"9 0 17 94\n"
"9 6 18 67\n"
"0 9 19 86\n"
"@attributes\n"
"source 3\n";
int test_seq[] = { 2, 28, 19, 27, 33, 25, 13, 41, 10, 26, 1, 9, 4, 34};
int test_inc[] = {20, 28, 34, 16, 0, 46, 44, 0, 42, 32, 14, 8, 6, 37};
int test_len = sizeof(test_seq) / sizeof(test_seq[0]);
template <typename Heap>
void heapSortTest() {
RangeMap<int> map(test_len, -1);
Heap heap(map);
std::vector<int> v(test_len);
for (int i = 0; i < test_len; ++i) {
v[i] = test_seq[i];
heap.push(i, v[i]);
}
std::sort(v.begin(), v.end());
for (int i = 0; i < test_len; ++i) {
check(v[i] == heap.prio() ,"Wrong order in heap sort.");
heap.pop();
}
}
template <typename Heap>
void heapIncreaseTest() {
RangeMap<int> map(test_len, -1);
Heap heap(map);
std::vector<int> v(test_len);
for (int i = 0; i < test_len; ++i) {
v[i] = test_seq[i];
heap.push(i, v[i]);
}
for (int i = 0; i < test_len; ++i) {
v[i] += test_inc[i];
heap.increase(i, v[i]);
}
std::sort(v.begin(), v.end());
for (int i = 0; i < test_len; ++i) {
check(v[i] == heap.prio() ,"Wrong order in heap increase test.");
heap.pop();
}
}
template <typename Heap>
void dijkstraHeapTest(const Digraph& digraph, const IntArcMap& length,
Node source) {
typename Dijkstra<Digraph, IntArcMap>::template SetStandardHeap<Heap>::
Create dijkstra(digraph, length);
dijkstra.run(source);
for(ArcIt a(digraph); a != INVALID; ++a) {
Node s = digraph.source(a);
Node t = digraph.target(a);
if (dijkstra.reached(s)) {
check( dijkstra.dist(t) - dijkstra.dist(s) <= length[a],
"Error in a shortest path tree!");
}
}
for(NodeIt n(digraph); n != INVALID; ++n) {
if ( dijkstra.reached(n) && dijkstra.predArc(n) != INVALID ) {
Arc a = dijkstra.predArc(n);
Node s = digraph.source(a);
check( dijkstra.dist(n) - dijkstra.dist(s) == length[a],
"Error in a shortest path tree!");
}
}
}
int main() {
typedef int Item;
typedef int Prio;
typedef RangeMap<int> ItemIntMap;
Digraph digraph;
IntArcMap length(digraph);
Node source;
std::istringstream input(test_lgf);
digraphReader(digraph, input).
arcMap("capacity", length).
node("source", source).
run();
{
typedef BinHeap<Prio, ItemIntMap> IntHeap;
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
heapSortTest<IntHeap>();
heapIncreaseTest<IntHeap>();
typedef BinHeap<Prio, IntNodeMap > NodeHeap;
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
dijkstraHeapTest<NodeHeap>(digraph, length, source);
}
{
typedef FibHeap<Prio, ItemIntMap> IntHeap;
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
heapSortTest<IntHeap>();
heapIncreaseTest<IntHeap>();
typedef FibHeap<Prio, IntNodeMap > NodeHeap;
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
dijkstraHeapTest<NodeHeap>(digraph, length, source);
}
{
typedef RadixHeap<ItemIntMap> IntHeap;
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
heapSortTest<IntHeap>();
heapIncreaseTest<IntHeap>();
typedef RadixHeap<IntNodeMap > NodeHeap;
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
dijkstraHeapTest<NodeHeap>(digraph, length, source);
}
{
typedef BucketHeap<ItemIntMap> IntHeap;
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
heapSortTest<IntHeap>();
heapIncreaseTest<IntHeap>();
typedef BucketHeap<IntNodeMap > NodeHeap;
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
dijkstraHeapTest<NodeHeap>(digraph, length, source);
}
return 0;
}
|