]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/graph/test/relaxed_heap_test.cpp
1 // Copyright 2004 The Trustees of Indiana University.
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // Authors: Douglas Gregor
9 #ifndef BOOST_RELAXED_HEAP_DEBUG
10 # define BOOST_RELAXED_HEAP_DEBUG 0
13 #include <boost/pending/relaxed_heap.hpp>
14 #include <boost/test/minimal.hpp>
18 #include <boost/optional.hpp>
19 #include <boost/random.hpp>
20 #include <boost/lexical_cast.hpp>
21 #include <boost/progress.hpp>
23 typedef std::vector
<boost::optional
<double> > values_type
;
28 typedef bool result_type
;
30 bool operator()(unsigned x
, unsigned y
) const
32 assert(values
[x
] && values
[y
]);
33 return values
[x
] < values
[y
];
39 boost::optional
<double> get_min_value()
41 boost::optional
<double> min_value
;
42 for (unsigned i
= 0; i
< values
.size(); ++i
) {
44 if (!min_value
|| *values
[i
] < *min_value
) min_value
= values
[i
];
50 void interactive_test()
53 cout
<< "Enter max number of values in the heap> ";
56 values
.resize(max_values
);
57 boost::relaxed_heap
<unsigned, less_extvalue
> heap(max_values
);
61 cout
<< "Enter command> ";
68 if (cin
>> index
>> value
) {
69 if (index
>= values
.size()) cout
<< "Index out of range.\n";
70 else if (values
[index
]) cout
<< "Already in queue.\n";
72 values
[index
] = value
;
84 if (cin
>> index
>> value
) {
85 if (index
>= values
.size()) cout
<< "Index out of range.\n";
86 else if (!values
[index
]) cout
<< "Not in queue.\n";
88 values
[index
] = value
;
100 if (index
>= values
.size()) cout
<< "Index out of range.\n";
101 else if (!values
[index
]) cout
<< "Not in queue.\n";
112 if (boost::optional
<double> min_value
= get_min_value()) {
113 cout
<< "Top value is (" << heap
.top() << ", "
114 << *values
[heap
.top()] << ").\n";
115 BOOST_CHECK(*min_value
== *values
[heap
.top()]);
117 cout
<< "Queue is empty.\n";
118 BOOST_CHECK(heap
.empty());
125 if (boost::optional
<double> min_value
= get_min_value()) {
126 unsigned victim
= heap
.top();
127 double value
= *values
[victim
];
128 cout
<< "Removed top value (" << victim
<< ", " << value
<< ")\n";
129 BOOST_CHECK(*min_value
== value
);
133 values
[victim
].reset();
135 cout
<< "Queue is empty.\n";
136 BOOST_CHECK(heap
.empty());
145 cout
<< "Unknown command '" << option
<< "'.\n";
148 } while (cin
&& option
!= 'q' && option
!= 'Q');
151 void random_test(int n
, int iterations
, int seed
)
154 boost::relaxed_heap
<unsigned, less_extvalue
> heap(n
);
155 boost::minstd_rand
gen(seed
);
156 boost::uniform_int
<unsigned> rand_index(0, n
-1);
157 boost::uniform_real
<> rand_value(-1000.0, 1000.0);
158 boost::uniform_int
<unsigned> which_option(0, 3);
160 cout
<< n
<< std::endl
;
162 #if BOOST_RELAXED_HEAP_DEBUG > 1
166 BOOST_REQUIRE(heap
.valid());
168 #if BOOST_RELAXED_HEAP_DEBUG == 0
169 boost::progress_display
progress(iterations
);
172 for (int iteration
= 0; iteration
< iterations
; ++iteration
) {
173 #if BOOST_RELAXED_HEAP_DEBUG > 1
174 std::cout
<< "Iteration #" << iteration
<< std::endl
;
176 unsigned victim
= rand_index(gen
);
177 if (values
[victim
]) {
178 switch (which_option(gen
)) {
181 // Update with a smaller weight
182 boost::uniform_real
<> rand_smaller((rand_value
.min
)(), *values
[victim
]);
183 values
[victim
] = rand_smaller(gen
);
184 assert(*values
[victim
] >= (rand_smaller
.min
)());
185 assert(*values
[victim
] <= (rand_smaller
.max
)());
187 #if BOOST_RELAXED_HEAP_DEBUG > 0
188 cout
<< "u " << victim
<< " " << *values
[victim
] << std::endl
;
197 // Delete minimum value in the queue.
199 double top_value
= *values
[victim
];
200 BOOST_CHECK(*get_min_value() == top_value
);
201 if (*get_min_value() != top_value
) return;
202 #if BOOST_RELAXED_HEAP_DEBUG > 0
203 cout
<< "d" << std::endl
;
207 values
[victim
].reset();
208 #if BOOST_RELAXED_HEAP_DEBUG > 1
209 cout
<< "(Removed " << victim
<< ")\n";
210 #endif // BOOST_RELAXED_HEAP_DEBUG > 1
216 // Just remove this value from the queue completely
217 values
[victim
].reset();
218 #if BOOST_RELAXED_HEAP_DEBUG > 0
219 cout
<< "r " << victim
<< std::endl
;
227 cout
<< "Random number generator failed." << endl
;
233 values
[victim
] = rand_value(gen
);
234 assert(*values
[victim
] >= (rand_value
.min
)());
235 assert(*values
[victim
] <= (rand_value
.max
)());
237 #if BOOST_RELAXED_HEAP_DEBUG > 0
238 cout
<< "i " << victim
<< " " << *values
[victim
] << std::endl
;
244 #if BOOST_RELAXED_HEAP_DEBUG > 1
246 #endif // BOOST_RELAXED_HEAP_DEBUG > 1
248 BOOST_REQUIRE(heap
.valid());
250 #if BOOST_RELAXED_HEAP_DEBUG == 0
256 int test_main(int argc
, char* argv
[])
259 int n
= boost::lexical_cast
<int>(argv
[1]);
260 int iterations
= boost::lexical_cast
<int>(argv
[2]);
261 int seed
= (argc
>= 4? boost::lexical_cast
<int>(argv
[3]) : 1);
262 random_test(n
, iterations
, seed
);
263 } else interactive_test();