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>
29 #include <lemon/lgf_reader.h>
30 #include <lemon/dijkstra.h>
31 #include <lemon/maps.h>
33 #include <lemon/bin_heap.h>
34 #include <lemon/fib_heap.h>
35 #include <lemon/radix_heap.h>
36 #include <lemon/bucket_heap.h>
38 #include "test_tools.h"
40 using namespace lemon;
41 using namespace lemon::concepts;
43 typedef ListDigraph Digraph;
44 DIGRAPH_TYPEDEFS(Digraph);
84 int test_seq[] = { 2, 28, 19, 27, 33, 25, 13, 41, 10, 26, 1, 9, 4, 34};
85 int test_inc[] = {20, 28, 34, 16, 0, 46, 44, 0, 42, 32, 14, 8, 6, 37};
87 int test_len = sizeof(test_seq) / sizeof(test_seq[0]);
89 template <typename Heap>
91 RangeMap<int> map(test_len, -1);
95 std::vector<int> v(test_len);
97 for (int i = 0; i < test_len; ++i) {
101 std::sort(v.begin(), v.end());
102 for (int i = 0; i < test_len; ++i) {
103 check(v[i] == heap.prio() ,"Wrong order in heap sort.");
108 template <typename Heap>
109 void heapIncreaseTest() {
110 RangeMap<int> map(test_len, -1);
114 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.");
133 template <typename Heap>
134 void dijkstraHeapTest(const Digraph& digraph, const IntArcMap& length,
137 typename Dijkstra<Digraph, IntArcMap>::template SetStandardHeap<Heap>::
138 Create dijkstra(digraph, length);
140 dijkstra.run(source);
142 for(ArcIt a(digraph); a != INVALID; ++a) {
143 Node s = digraph.source(a);
144 Node t = digraph.target(a);
145 if (dijkstra.reached(s)) {
146 check( dijkstra.dist(t) - dijkstra.dist(s) <= length[a],
147 "Error in a shortest path tree!");
151 for(NodeIt n(digraph); n != INVALID; ++n) {
152 if ( dijkstra.reached(n) && dijkstra.predArc(n) != INVALID ) {
153 Arc a = dijkstra.predArc(n);
154 Node s = digraph.source(a);
155 check( dijkstra.dist(n) - dijkstra.dist(s) == length[a],
156 "Error in a shortest path tree!");
166 typedef RangeMap<int> ItemIntMap;
169 IntArcMap length(digraph);
172 std::istringstream input(test_lgf);
173 digraphReader(digraph, input).
174 arcMap("capacity", length).
175 node("source", source).
179 typedef BinHeap<Prio, ItemIntMap> IntHeap;
180 checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
181 heapSortTest<IntHeap>();
182 heapIncreaseTest<IntHeap>();
184 typedef BinHeap<Prio, IntNodeMap > NodeHeap;
185 checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
186 dijkstraHeapTest<NodeHeap>(digraph, length, source);
190 typedef FibHeap<Prio, ItemIntMap> IntHeap;
191 checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
192 heapSortTest<IntHeap>();
193 heapIncreaseTest<IntHeap>();
195 typedef FibHeap<Prio, IntNodeMap > NodeHeap;
196 checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
197 dijkstraHeapTest<NodeHeap>(digraph, length, source);
201 typedef RadixHeap<ItemIntMap> IntHeap;
202 checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
203 heapSortTest<IntHeap>();
204 heapIncreaseTest<IntHeap>();
206 typedef RadixHeap<IntNodeMap > NodeHeap;
207 checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
208 dijkstraHeapTest<NodeHeap>(digraph, length, source);
212 typedef BucketHeap<ItemIntMap> IntHeap;
213 checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
214 heapSortTest<IntHeap>();
215 heapIncreaseTest<IntHeap>();
217 typedef BucketHeap<IntNodeMap > NodeHeap;
218 checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>();
219 dijkstraHeapTest<NodeHeap>(digraph, length, source);