4 #error A C++ compiler is required.
24 template <
typename pq_t>
27 clock_t bench_begin, bench_mid, bench_end;
28 std::cout <<
"Benchmark results: ";
30 for (
unsigned n = benchmark_elements / 2; n--;)
33 bench_begin = clock();
34 for (
unsigned n = benchmark_elements / 2; n--;)
37 std::cout <<
"Push: " << (bench_mid - bench_begin) <<
" (" << static_cast<double>(bench_mid - bench_begin) / CLOCKS_PER_SEC <<
"s)";
41 for (
unsigned n = benchmark_elements / 2; n--;)
44 std::cout <<
", Pop: " << (bench_end - bench_mid) <<
" (" << static_cast<double>(bench_end - bench_mid) / CLOCKS_PER_SEC <<
"s)\n";
48 std::cout <<
"__cplusplus = " << __cplusplus <<
"\n";
50 std::cout <<
"Debug mode (asserts active).\n";
56 using std::priority_queue;
60 priority_deque<int> test_deque;
62 for (
int i = 0; i < 256; ++i) {
65 if (std::find(test_deque.begin(), test_deque.end(), i) == test_deque.end()) {
66 std::cerr <<
"Element lost <push>.\n";
69 if (!
is_interval_heap(test_deque.begin(), test_deque.end(), std::less<int>())) {
70 std::cerr <<
"Heap corrupted <push>.\n";
73 if (test_deque.maximum() != i)
74 std::cerr <<
"Incorrect maximum. i = " << i <<
"\n";
76 std::cerr <<
"Exception thrown <push>.\n";
80 std::vector<int> test_vector;
81 for (
int i = 0; i < 256; ++i) {
82 test_vector.push_back(i);
83 std::random_shuffle(test_vector.begin(), test_vector.end());
86 test_deque.merge(test_vector);
87 if (test_deque.size() != test_vector.size())
88 std::cerr <<
"Merge changed number of elements.\n";
89 for (
int j = 0; j <= i; ++j) {
90 if (std::find(test_deque.begin(), test_deque.end(), j) == test_deque.end()) {
91 std::cerr <<
"Element removed <merge>.\n";
95 if (!
is_interval_heap(test_deque.begin(), test_deque.end(), std::less<int>())) {
96 std::cerr <<
"Heap corrupted <merge>.\n";
100 std::cerr <<
"Exception thrown <merge>.\n";
108 typedef intmax_t benchmark_type;
109 const unsigned benchmark_elements = 20000000;
111 benchmark_priority_queue<priority_deque<benchmark_type> >(benchmark_elements);
113 benchmark_priority_queue<priority_queue<benchmark_type> >(benchmark_elements);
118 std::cout <<
"\n\nTesting heap integrity after:\n";
119 const int element_total = 3001;
120 priority_deque<uint32_t> pd;
122 std::cout <<
"'push' (" << element_total <<
"x)\n";
123 for (
int n = 1; n <= element_total; ++n) {
126 std::cout <<
"Failed push (error in replace_leaf)\n";
132 std::vector<uint32_t> v;
133 for (
int n = 1; n <= 5; ++n)
135 for (
int n = 1; n <= 1; ++n) {
136 std::cout <<
"'merge' (" << v.size() <<
" elements)\n";
143 std::cout <<
"Failed\n";
144 for (
auto it = v.begin(); it != v.end(); ++it)
145 std::cout << *it <<
", ";
148 std::random_shuffle(v.begin(), v.end());
160 std::cout <<
"'set' (" << element_total <<
"x)\n";
161 for (
int t = 0; t < element_total; ++t) {
162 pd.update(pd.begin() + rand() % pd.size(), rand());
164 std::cout <<
"Failed random-access set (error in replace)\n";
169 std::cout <<
"'pop_minimum' (" << pd.size() <<
"x)\n";
170 while (!pd.empty()) {
173 std::cout <<
"Failed pop_minimum (error in replace)\n";
179 std::cout <<
"'push', 'pop_maximum', 'pop_minimum', 'erase', 'set' (indefinitely)\n";
181 for (
int n = 0; n < 13; ++n) {
182 test_pd.
push(rand());
184 std::cout <<
"Failed push (error in replace_leaf)\n";
188 if (test_pd.
size() < 3000) {
190 test_pd.
push(rand());
192 last_rand = rand() % 8;
196 case 2: test_pd.
erase(test_pd.
begin() + rand() % test_pd.
size());
break;
197 default: test_pd.
update(test_pd.
begin() + rand() % test_pd.
size(), rand());
201 std::cout <<
"An error has occurred! (code " << last_rand <<
").\n";