| 1 | /* Copyright (c) 2014, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <algorithm> |
| 26 | #include <functional> |
| 27 | #include <iostream> |
| 28 | #include <iterator> |
| 29 | #include <random> |
| 30 | #include <sstream> |
| 31 | #include <vector> |
| 32 | |
| 33 | #include "priority_queue.h" |
| 34 | |
| 35 | namespace priority_queue_unittest { |
| 36 | |
| 37 | // random integer generator -- start |
| 38 | template <typename IntegerType> |
| 39 | class random_integer_generator { |
| 40 | public: |
| 41 | random_integer_generator(unsigned int seed = 0) { srand(seed); } |
| 42 | |
| 43 | IntegerType operator()(IntegerType const &low, IntegerType const &high) { |
| 44 | IntegerType i = rand() % (high - low + 1); |
| 45 | i += low; |
| 46 | return i; |
| 47 | } |
| 48 | }; |
| 49 | // random integer generator -- end |
| 50 | |
| 51 | //-------------------------------------------------------- |
| 52 | |
| 53 | // handle support -- start |
| 54 | template <typename T> |
| 55 | struct handle { |
| 56 | T *ptr; |
| 57 | |
| 58 | handle() : ptr(NULL) {} |
| 59 | handle(T &t) : ptr(&t) {} |
| 60 | }; |
| 61 | |
| 62 | template <typename T> |
| 63 | inline std::ostream &operator<<(std::ostream &os, handle<T> const &h) { |
| 64 | os << *h.ptr; |
| 65 | return os; |
| 66 | } |
| 67 | |
| 68 | template <typename Handle, typename Value, typename Less = std::less<Value>> |
| 69 | struct handle_less { |
| 70 | inline bool operator()(Handle const &p1, Handle const &p2) const { |
| 71 | return Less()(*p1.ptr, *p2.ptr); |
| 72 | } |
| 73 | }; |
| 74 | // handle support -- end |
| 75 | |
| 76 | //-------------------------------------------------------- |
| 77 | |
| 78 | // dummy stream that "eats" all input |
| 79 | struct null_stream : public std::ostream { |
| 80 | // Visual Studio needs a default constructor. |
| 81 | null_stream() : std::ostream(nullptr) {} |
| 82 | }; |
| 83 | |
| 84 | template <typename T> |
| 85 | inline null_stream &operator<<(null_stream &ns, T const &) { |
| 86 | return ns; |
| 87 | } |
| 88 | |
| 89 | //-------------------------------------------------------- |
| 90 | |
| 91 | // i/o support -- start |
| 92 | template <typename InputIterator> |
| 93 | inline void print_range(std::ostream &os, InputIterator first, |
| 94 | InputIterator last) { |
| 95 | for (InputIterator it = first; it != last; ++it) { |
| 96 | os << " " << *it; |
| 97 | } |
| 98 | os << std::endl; |
| 99 | } |
| 100 | |
| 101 | template <typename InputIterator> |
| 102 | inline void print_range(std::ostream &os, std::string const &header, |
| 103 | InputIterator first, InputIterator last) { |
| 104 | os << header; |
| 105 | print_range(os, first, last); |
| 106 | } |
| 107 | // i/o support -- end |
| 108 | |
| 109 | //-------------------------------------------------------- |
| 110 | |
| 111 | // Some template functions below do not play well with gtest internals |
| 112 | static void assert_true_helper(bool val) { ASSERT_TRUE(val); } |
| 113 | |
| 114 | // k min elements algorithm -- start |
| 115 | template <bool UseSorting> |
| 116 | struct min_k_elements { |
| 117 | template <typename InputIterator, typename OutputIterator> |
| 118 | static inline OutputIterator apply(InputIterator first, InputIterator last, |
| 119 | size_t k, OutputIterator oit) { |
| 120 | SCOPED_TRACE(""); |
| 121 | assert_true_helper(static_cast<size_t>(std::distance(first, last)) > k); |
| 122 | |
| 123 | typedef typename std::iterator_traits<InputIterator>::value_type value_type; |
| 124 | |
| 125 | std::vector<value_type> values; |
| 126 | |
| 127 | // copy locally |
| 128 | std::copy(first, last, std::back_inserter(values)); |
| 129 | |
| 130 | // sort |
| 131 | std::sort(values.begin(), values.end()); |
| 132 | |
| 133 | // output k smallest values |
| 134 | std::copy(values.begin(), values.begin() + k, oit); |
| 135 | return oit; |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | template <> |
| 140 | struct min_k_elements<false> { |
| 141 | template <typename RandomAccessIterator, typename OutputIterator> |
| 142 | static inline OutputIterator apply(RandomAccessIterator first, |
| 143 | RandomAccessIterator last, size_t k, |
| 144 | OutputIterator oit) { |
| 145 | SCOPED_TRACE(""); |
| 146 | assert_true_helper(static_cast<size_t>(std::distance(first, last)) > k); |
| 147 | |
| 148 | typedef typename std::iterator_traits<RandomAccessIterator>::value_type |
| 149 | value_type; |
| 150 | |
| 151 | typedef handle<value_type> handle_type; |
| 152 | |
| 153 | typedef handle_less<handle_type, value_type, std::less<value_type>> |
| 154 | handle_less_type; |
| 155 | |
| 156 | typedef Priority_queue<handle_type, std::vector<handle_type>, |
| 157 | handle_less_type> |
| 158 | queue_type; |
| 159 | |
| 160 | typedef typename queue_type::const_iterator queue_iterator; |
| 161 | |
| 162 | // copy k + 1 values locally |
| 163 | std::vector<value_type> values; |
| 164 | values.reserve(k + 1); |
| 165 | |
| 166 | std::copy(first, first + k + 1, std::back_inserter(values)); |
| 167 | |
| 168 | // create a queue of k + 1 handles to the local values |
| 169 | queue_type queue; |
| 170 | assert_true_helper(!queue.reserve(k + 1)); |
| 171 | |
| 172 | for (size_t i = 0; i < k + 1; ++i) { |
| 173 | EXPECT_FALSE(queue.push(handle_type(values[i]))); |
| 174 | } |
| 175 | SCOPED_TRACE(""); |
| 176 | assert_true_helper(queue.is_valid()); |
| 177 | |
| 178 | // for the remaining values update the queue's root node |
| 179 | // and rebuild the heap |
| 180 | for (RandomAccessIterator it = first + k + 1; it != last; ++it) { |
| 181 | *queue[0].ptr = *it; |
| 182 | queue.update(0); |
| 183 | } |
| 184 | |
| 185 | // output the k minimum values (all but the root) |
| 186 | for (queue_iterator it = ++queue.begin(); it != queue.end(); ++it) { |
| 187 | *oit++ = *it->ptr; |
| 188 | } |
| 189 | |
| 190 | return oit; |
| 191 | } |
| 192 | }; |
| 193 | // k min elements algorithm -- end |
| 194 | |
| 195 | //-------------------------------------------------------- |
| 196 | |
| 197 | template <typename RandomAccessIterator> |
| 198 | inline void test_min_k_elements(RandomAccessIterator first, |
| 199 | RandomAccessIterator last, size_t k) { |
| 200 | std::vector<int> keys_copy(first, last); |
| 201 | std::sort(keys_copy.begin(), keys_copy.end()); |
| 202 | |
| 203 | #ifdef PRIORITY_QUEUE_TEST_DEBUG |
| 204 | std::ostream &os = std::cout; |
| 205 | #else |
| 206 | null_stream os; |
| 207 | #endif |
| 208 | |
| 209 | print_range(os, "elements: ", first, last); |
| 210 | |
| 211 | print_range(os, "sorted elements: ", keys_copy.begin(), keys_copy.end()); |
| 212 | os << std::endl; |
| 213 | |
| 214 | std::vector<int> min_elements_sort, min_elements_heap; |
| 215 | |
| 216 | // using the heap |
| 217 | min_k_elements<false>::apply(first, last, k, |
| 218 | std::back_inserter(min_elements_heap)); |
| 219 | |
| 220 | os << "min " << k << " elements (heap): "; |
| 221 | print_range(os, min_elements_heap.begin(), min_elements_heap.end()); |
| 222 | |
| 223 | std::sort(min_elements_heap.begin(), min_elements_heap.end()); |
| 224 | os << "min " << k << " elements (hp/s): "; |
| 225 | print_range(os, min_elements_heap.begin(), min_elements_heap.end()); |
| 226 | os << std::endl; |
| 227 | |
| 228 | // using sorting |
| 229 | min_k_elements<true>::apply(first, last, k, |
| 230 | std::back_inserter(min_elements_sort)); |
| 231 | |
| 232 | os << "min " << k << " elements (sort): "; |
| 233 | print_range(os, min_elements_sort.begin(), min_elements_sort.end()); |
| 234 | os << std::endl; |
| 235 | |
| 236 | ASSERT_TRUE(std::equal(min_elements_sort.begin(), min_elements_sort.end(), |
| 237 | min_elements_heap.begin())); |
| 238 | } |
| 239 | |
| 240 | //-------------------------------------------------------- |
| 241 | |
| 242 | template <typename Queue> |
| 243 | inline void print_update_msg(std::ostream &os, std::string header, |
| 244 | Queue const &q, typename Queue::size_type pos, |
| 245 | typename Queue::value_type const &old_value, |
| 246 | typename Queue::value_type const &new_value) { |
| 247 | os << header << " priority of element " << old_value << " at position " << pos |
| 248 | << " to new value " << new_value << "; new_queue: " << q << std::endl; |
| 249 | } |
| 250 | |
| 251 | template <typename RandomAccessIterator> |
| 252 | inline void test_heap(RandomAccessIterator first, RandomAccessIterator last) { |
| 253 | typedef typename std::iterator_traits<RandomAccessIterator>::value_type |
| 254 | value_type; |
| 255 | |
| 256 | typedef Priority_queue<value_type> queue; |
| 257 | typedef typename queue::size_type size_type; |
| 258 | typedef typename queue::iterator iterator; |
| 259 | typedef typename queue::const_iterator const_iterator; |
| 260 | |
| 261 | #ifdef PRIORITY_QUEUE_TEST_DEBUG |
| 262 | std::ostream &os = std::cout; |
| 263 | #else |
| 264 | null_stream os; |
| 265 | #endif |
| 266 | |
| 267 | queue pq; |
| 268 | |
| 269 | for (RandomAccessIterator it = first; it != last; ++it) { |
| 270 | EXPECT_FALSE(pq.push(*it)); |
| 271 | ASSERT_TRUE(pq.is_valid()); |
| 272 | os << "queue: " << pq << std::endl; |
| 273 | } |
| 274 | |
| 275 | // test constructor with iterators |
| 276 | { |
| 277 | queue pq2(first, last); |
| 278 | ASSERT_TRUE(pq2.is_valid()); |
| 279 | os << "queue (constructor with iterators): " << pq2 << std::endl; |
| 280 | } |
| 281 | |
| 282 | // test top |
| 283 | os << "top element: " << pq.top() << std::endl; |
| 284 | ASSERT_TRUE(pq.top() == pq[0]); |
| 285 | |
| 286 | // test push |
| 287 | value_type new_value = *std::min_element(pq.begin(), pq.end()) - 1; |
| 288 | EXPECT_FALSE(pq.push(new_value)); |
| 289 | ASSERT_TRUE(pq.is_valid()); |
| 290 | os << "pushed " << new_value << "; new queue: " << pq << std::endl; |
| 291 | |
| 292 | // test pop |
| 293 | pq.pop(); |
| 294 | ASSERT_TRUE(pq.is_valid()); |
| 295 | os << "popped top element; new queue: " << pq << std::endl; |
| 296 | |
| 297 | // test decrease |
| 298 | // decrease priority of element at position 3 |
| 299 | { |
| 300 | size_type pos = 3; |
| 301 | value_type old_priority = pq[pos]; |
| 302 | value_type new_priority = pq[pos]; |
| 303 | pq.decrease(pos, new_priority); |
| 304 | ASSERT_TRUE(pq.is_valid()); |
| 305 | print_update_msg(os, "decreasing (2)", pq, pos, old_priority, new_priority); |
| 306 | |
| 307 | old_priority = pq[pos]; |
| 308 | new_priority = pq[pos]; |
| 309 | *(pq.begin() + pos) = new_priority; |
| 310 | pq.decrease(pos); |
| 311 | ASSERT_TRUE(pq.is_valid()); |
| 312 | print_update_msg(os, "decreasing (1)", pq, pos, old_priority, new_priority); |
| 313 | |
| 314 | old_priority = pq[pos]; |
| 315 | new_priority = pq[pos] - 10; |
| 316 | pq.decrease(pos, new_priority); |
| 317 | ASSERT_TRUE(pq.is_valid()); |
| 318 | print_update_msg(os, "decreasing (2)", pq, pos, old_priority, new_priority); |
| 319 | |
| 320 | old_priority = pq[pos]; |
| 321 | new_priority = pq[pos] - 20; |
| 322 | pq[pos] = new_priority; |
| 323 | pq.decrease(pos); |
| 324 | ASSERT_TRUE(pq.is_valid()); |
| 325 | print_update_msg(os, "decreasing (1)", pq, pos, old_priority, new_priority); |
| 326 | } |
| 327 | |
| 328 | // test increase |
| 329 | // increase priority of element at position 4 |
| 330 | { |
| 331 | size_type pos = 4; |
| 332 | value_type old_priority = pq[pos]; |
| 333 | value_type new_priority = pq[pos]; |
| 334 | pq.increase(pos, new_priority); |
| 335 | ASSERT_TRUE(pq.is_valid()); |
| 336 | print_update_msg(os, "increasing (2)", pq, pos, old_priority, new_priority); |
| 337 | |
| 338 | old_priority = pq[pos]; |
| 339 | new_priority = pq[pos]; |
| 340 | *(pq.begin() + pos) = new_priority; |
| 341 | pq.increase(pos); |
| 342 | ASSERT_TRUE(pq.is_valid()); |
| 343 | print_update_msg(os, "increasing (1)", pq, pos, old_priority, new_priority); |
| 344 | |
| 345 | old_priority = pq[pos]; |
| 346 | new_priority = pq[pos] + 30; |
| 347 | pq.increase(pos, new_priority); |
| 348 | ASSERT_TRUE(pq.is_valid()); |
| 349 | print_update_msg(os, "increasing (2)", pq, pos, old_priority, new_priority); |
| 350 | |
| 351 | old_priority = pq[pos]; |
| 352 | new_priority = pq[pos] + 50; |
| 353 | pq[pos] = new_priority; |
| 354 | pq.increase(pos); |
| 355 | ASSERT_TRUE(pq.is_valid()); |
| 356 | print_update_msg(os, "increasing (1)", pq, pos, old_priority, new_priority); |
| 357 | } |
| 358 | |
| 359 | // test update |
| 360 | // update priority of element at position 2 |
| 361 | { |
| 362 | size_type pos = 2; |
| 363 | value_type old_priority = pq[pos]; |
| 364 | value_type new_priority = pq[pos]; |
| 365 | pq.update(pos, new_priority); |
| 366 | ASSERT_TRUE(pq.is_valid()); |
| 367 | print_update_msg(os, "updating (2)", pq, pos, old_priority, new_priority); |
| 368 | |
| 369 | old_priority = pq[pos]; |
| 370 | new_priority = pq[pos]; |
| 371 | *(pq.begin() + pos) = new_priority; |
| 372 | pq.update(pos); |
| 373 | ASSERT_TRUE(pq.is_valid()); |
| 374 | print_update_msg(os, "updating (1)", pq, pos, old_priority, new_priority); |
| 375 | |
| 376 | old_priority = pq[pos]; |
| 377 | new_priority = pq[pos] - 20; |
| 378 | *(pq.begin() + pos) = new_priority; |
| 379 | pq.update(pos); |
| 380 | ASSERT_TRUE(pq.is_valid()); |
| 381 | print_update_msg(os, "updating (2)", pq, pos, old_priority, new_priority); |
| 382 | |
| 383 | old_priority = pq[pos]; |
| 384 | new_priority = pq[pos] + 100; |
| 385 | pq[pos] = new_priority; |
| 386 | pq.update(pos); |
| 387 | ASSERT_TRUE(pq.is_valid()); |
| 388 | print_update_msg(os, "updating (1)", pq, pos, old_priority, new_priority); |
| 389 | } |
| 390 | |
| 391 | // test size |
| 392 | ASSERT_TRUE(pq.size() == static_cast<size_t>(std::distance(first, last))); |
| 393 | ASSERT_TRUE(pq.is_valid()); |
| 394 | os << "size: " << pq.size() << std::endl; |
| 395 | |
| 396 | // test empty |
| 397 | ASSERT_TRUE(!pq.empty()); |
| 398 | ASSERT_TRUE(pq.is_valid()); |
| 399 | os << "empty? " << std::boolalpha << pq.empty() << std::noboolalpha |
| 400 | << std::endl; |
| 401 | |
| 402 | // print all elements in queue using operator[] |
| 403 | os << "queue (using operator[]):"; |
| 404 | for (size_type i = 0; i < pq.size(); ++i) { |
| 405 | os << " " << pq[i]; |
| 406 | } |
| 407 | os << std::endl; |
| 408 | |
| 409 | // print all elements in queue using const/non-const iterators |
| 410 | os << "queue (using const iterators):"; |
| 411 | for (const_iterator it = pq.begin(); it != pq.end(); ++it) { |
| 412 | os << " " << *it; |
| 413 | } |
| 414 | os << std::endl; |
| 415 | |
| 416 | os << "queue (using non-const iterators):"; |
| 417 | for (iterator it = pq.begin(); it != pq.end(); ++it) { |
| 418 | os << " " << *it; |
| 419 | } |
| 420 | os << std::endl; |
| 421 | |
| 422 | // testing swap |
| 423 | queue other; |
| 424 | |
| 425 | ASSERT_TRUE(pq.is_valid() && !pq.empty()); |
| 426 | ASSERT_TRUE(other.is_valid() && other.empty()); |
| 427 | os << "before swap: " << pq << "; " << other << std::endl; |
| 428 | pq.swap(other); |
| 429 | ASSERT_TRUE(pq.is_valid() && pq.empty()); |
| 430 | ASSERT_TRUE(other.is_valid() && !other.empty()); |
| 431 | os << "after swap: " << pq << "; " << other << std::endl; |
| 432 | |
| 433 | // clear the heap |
| 434 | pq.clear(); |
| 435 | ASSERT_TRUE(pq.is_valid()); |
| 436 | ASSERT_TRUE(pq.empty()); |
| 437 | os << "clearing the queue" << std::endl; |
| 438 | |
| 439 | // reserve 10 heap elements and push 10 elements in the heap |
| 440 | os << "testing reserve" << std::endl; |
| 441 | ASSERT_TRUE(pq.empty()); |
| 442 | ASSERT_FALSE(pq.reserve(2 * pq.capacity())); |
| 443 | ASSERT_TRUE(pq.empty()); |
| 444 | for (size_type i = 0; i < static_cast<size_t>(std::distance(first, last)); |
| 445 | ++i) { |
| 446 | ASSERT_TRUE(pq.size() == i); |
| 447 | EXPECT_FALSE(pq.push(*(last - 1 - i))); |
| 448 | ASSERT_TRUE(pq.is_valid()); |
| 449 | os << "queue: " << pq << std::endl; |
| 450 | } |
| 451 | |
| 452 | // now use the non-const top() method to update the root element |
| 453 | os << "testing non-const top()" << std::endl; |
| 454 | { |
| 455 | value_type old_priority = pq.top(); |
| 456 | value_type new_priority = pq.top() - 40; |
| 457 | pq.top() = pq.top() - 40; |
| 458 | pq.update(0); |
| 459 | ASSERT_TRUE(pq.is_valid()); |
| 460 | print_update_msg(os, "updating (using top)", pq, 0, old_priority, |
| 461 | new_priority); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | //-------------------------------------------------------- |
| 466 | |
| 467 | template <typename RandomAccessIterator> |
| 468 | inline void test_heap_of_handles(RandomAccessIterator first, |
| 469 | RandomAccessIterator last) { |
| 470 | typedef typename std::iterator_traits<RandomAccessIterator>::value_type |
| 471 | value_type; |
| 472 | |
| 473 | typedef handle<value_type> handle_type; |
| 474 | |
| 475 | typedef handle_less<handle_type, value_type, std::greater<value_type>> |
| 476 | handle_greater_type; |
| 477 | |
| 478 | typedef Priority_queue<handle_type, std::vector<handle_type>, |
| 479 | handle_less<handle_type, value_type>> |
| 480 | queue; |
| 481 | |
| 482 | typedef typename queue::const_iterator const_iterator; |
| 483 | |
| 484 | #ifdef PRIORITY_QUEUE_TEST_DEBUG |
| 485 | std::ostream &os = std::cout; |
| 486 | #else |
| 487 | null_stream os; |
| 488 | #endif |
| 489 | |
| 490 | size_t const N = static_cast<size_t>(std::distance(first, last)); |
| 491 | |
| 492 | std::vector<handle_type> handles; |
| 493 | for (size_t i = 0; i < N; ++i) { |
| 494 | handles.push_back(handle_type(*(first + i))); |
| 495 | } |
| 496 | for (size_t i = 0; i < N; ++i) { |
| 497 | os << handles[i] << std::endl; |
| 498 | } |
| 499 | |
| 500 | queue pq; |
| 501 | for (size_t i = 0; i < N; ++i) { |
| 502 | EXPECT_FALSE(pq.push(handles[i])); |
| 503 | ASSERT_TRUE(pq.is_valid()); |
| 504 | os << "queue: " << pq << std::endl; |
| 505 | } |
| 506 | |
| 507 | { |
| 508 | queue pq2(handles.begin(), handles.end()); |
| 509 | ASSERT_TRUE(pq2.is_valid()); |
| 510 | os << "queue: " << pq2 << std::endl; |
| 511 | } |
| 512 | |
| 513 | const_iterator it_max = |
| 514 | std::max_element(pq.begin(), pq.end(), handle_greater_type()); |
| 515 | *pq[0].ptr = *it_max->ptr + 1000; |
| 516 | os << "queue: " << pq << std::endl; |
| 517 | pq.update_top(); |
| 518 | ASSERT_TRUE(pq.is_valid()); |
| 519 | os << "queue: " << pq << std::endl; |
| 520 | |
| 521 | it_max = std::max_element(pq.begin(), pq.end(), handle_greater_type()); |
| 522 | *pq[0].ptr = *it_max->ptr + 500; |
| 523 | os << "queue: " << pq << std::endl; |
| 524 | pq.update_top(); |
| 525 | ASSERT_TRUE(pq.is_valid()); |
| 526 | os << "queue: " << pq << std::endl; |
| 527 | } |
| 528 | |
| 529 | //-------------------------------------------------------- |
| 530 | |
| 531 | class PriorityQueueTest : public ::testing::Test { |
| 532 | protected: |
| 533 | void SetUp() override { |
| 534 | int xkeys[10] = {10, 4, 7, 8, 21, -5, 6, 10, 7, 9}; |
| 535 | memcpy(keys, xkeys, sizeof(xkeys)); |
| 536 | pq = Priority_queue<int>(xkeys, xkeys + 10); |
| 537 | keys2.assign(5, 50); |
| 538 | } |
| 539 | |
| 540 | size_t parent(size_t i) { return Priority_queue<int>::parent(i); } |
| 541 | size_t left(size_t i) { return Priority_queue<int>::left(i); } |
| 542 | size_t right(size_t i) { return Priority_queue<int>::right(i); } |
| 543 | |
| 544 | int keys[10]; |
| 545 | Priority_queue<int> pq; |
| 546 | std::vector<unsigned> keys2; |
| 547 | }; |
| 548 | |
| 549 | TEST_F(PriorityQueueTest, ParentLeftRight) { |
| 550 | EXPECT_EQ(0U, parent(1)); |
| 551 | EXPECT_EQ(0U, parent(2)); |
| 552 | EXPECT_EQ(1U, left(0)); |
| 553 | EXPECT_EQ(2U, right(0)); |
| 554 | for (size_t ix = 0; ix < 42; ++ix) { |
| 555 | EXPECT_EQ(ix, parent(left(ix))); |
| 556 | EXPECT_EQ(ix, parent(right(ix))); |
| 557 | EXPECT_EQ(1 + left(ix), right(ix)); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | struct My_less { |
| 562 | bool operator()(int a, int b) const { return a < b; } |
| 563 | }; |
| 564 | |
| 565 | struct My_greater { |
| 566 | bool operator()(int a, int b) const { return a > b; } |
| 567 | }; |
| 568 | |
| 569 | TEST_F(PriorityQueueTest, MaxVsMinHeap) { |
| 570 | Priority_queue<int, std::vector<int>, My_less> pql; |
| 571 | Priority_queue<int, std::vector<int>, My_greater> pqg; |
| 572 | for (int ix = 0; ix < 10; ++ix) { |
| 573 | EXPECT_FALSE(pql.push(ix)); |
| 574 | EXPECT_FALSE(pqg.push(ix)); |
| 575 | EXPECT_EQ(ix, pql.top()); |
| 576 | EXPECT_EQ(0, pqg.top()); |
| 577 | EXPECT_TRUE(pql.is_valid()); |
| 578 | EXPECT_TRUE(pqg.is_valid()); |
| 579 | } |
| 580 | std::stringstream ss1, ss2; |
| 581 | ss1 << pql; |
| 582 | EXPECT_STREQ("9 8 5 6 7 1 4 0 3 2 ", ss1.str().c_str()); |
| 583 | ss2 << pqg; |
| 584 | EXPECT_STREQ("0 1 2 3 4 5 6 7 8 9 ", ss2.str().c_str()); |
| 585 | } |
| 586 | |
| 587 | TEST_F(PriorityQueueTest, DifferentCtors) { |
| 588 | Priority_queue<int, std::vector<int>, My_less> pql; |
| 589 | std::vector<int> intvec; |
| 590 | for (int ix = 0; ix < 10; ++ix) { |
| 591 | Priority_queue<int> pq_ix(intvec.begin(), intvec.end()); |
| 592 | EXPECT_TRUE(pq_ix.is_valid()); |
| 593 | EXPECT_FALSE(pql.push(ix)); |
| 594 | EXPECT_EQ(ix, pql.top()); |
| 595 | intvec.push_back(ix); |
| 596 | } |
| 597 | Priority_queue<int, std::vector<int>, My_less> pql_range(intvec.begin(), |
| 598 | intvec.end()); |
| 599 | |
| 600 | EXPECT_EQ(10U, pql.size()); |
| 601 | EXPECT_FALSE(pql.empty()); |
| 602 | EXPECT_TRUE(pql.is_valid()); |
| 603 | EXPECT_EQ(10U, pql_range.size()); |
| 604 | EXPECT_FALSE(pql_range.empty()); |
| 605 | EXPECT_TRUE(pql_range.is_valid()); |
| 606 | |
| 607 | std::stringstream ss1, ss2; |
| 608 | ss1 << pql; |
| 609 | ss2 << pql_range; |
| 610 | // Different heaps, both are valid: |
| 611 | EXPECT_STREQ("9 8 5 6 7 1 4 0 3 2 ", ss1.str().c_str()); |
| 612 | EXPECT_STREQ("9 8 6 7 4 5 2 0 3 1 ", ss2.str().c_str()); |
| 613 | } |
| 614 | |
| 615 | TEST_F(PriorityQueueTest, Swap) { |
| 616 | std::random_device rng; |
| 617 | std::mt19937 urng(rng()); |
| 618 | std::shuffle(keys, keys + 10, urng); |
| 619 | Priority_queue<int> pq(keys, keys + 10); |
| 620 | std::stringstream ss1, ss2; |
| 621 | ss1 << pq; |
| 622 | Priority_queue<int> pq_swap; |
| 623 | pq_swap.swap(pq); |
| 624 | ss2 << pq_swap; |
| 625 | EXPECT_STREQ(ss1.str().c_str(), ss2.str().c_str()); |
| 626 | EXPECT_EQ(0U, pq.size()); |
| 627 | } |
| 628 | |
| 629 | TEST_F(PriorityQueueTest, DecreaseNoop) { |
| 630 | std::stringstream ss1, ss2; |
| 631 | ss1 << pq; |
| 632 | for (size_t ix = 0; ix < 10; ++ix) { |
| 633 | pq.decrease(ix); |
| 634 | int val = pq[ix]; |
| 635 | pq.decrease(ix, val); |
| 636 | *(pq.begin() + ix) = val; |
| 637 | pq.decrease(ix); |
| 638 | EXPECT_TRUE(pq.is_valid()); |
| 639 | } |
| 640 | ss2 << pq; |
| 641 | EXPECT_STREQ(ss1.str().c_str(), ss2.str().c_str()); |
| 642 | } |
| 643 | |
| 644 | TEST_F(PriorityQueueTest, IncreaseNoop) { |
| 645 | std::stringstream ss1, ss2; |
| 646 | ss1 << pq; |
| 647 | for (size_t ix = 0; ix < 10; ++ix) { |
| 648 | pq.increase(ix); |
| 649 | int val = pq[ix]; |
| 650 | pq.increase(ix, val); |
| 651 | *(pq.begin() + ix) = val; |
| 652 | pq.increase(ix); |
| 653 | EXPECT_TRUE(pq.is_valid()); |
| 654 | } |
| 655 | ss2 << pq; |
| 656 | EXPECT_STREQ(ss1.str().c_str(), ss2.str().c_str()); |
| 657 | } |
| 658 | |
| 659 | TEST_F(PriorityQueueTest, UpdateNoop) { |
| 660 | std::stringstream ss1, ss2; |
| 661 | ss1 << pq; |
| 662 | for (size_t ix = 0; ix < 10; ++ix) { |
| 663 | pq.update(ix); |
| 664 | int val = pq[ix]; |
| 665 | pq.update(ix, val); |
| 666 | *(pq.begin() + ix) = val; |
| 667 | pq.update(ix); |
| 668 | EXPECT_TRUE(pq.is_valid()); |
| 669 | } |
| 670 | ss2 << pq; |
| 671 | EXPECT_STREQ(ss1.str().c_str(), ss2.str().c_str()); |
| 672 | } |
| 673 | |
| 674 | TEST_F(PriorityQueueTest, Decrease3) { |
| 675 | Priority_queue<int> pqcopy = pq; |
| 676 | const int old_priority = pq[3]; |
| 677 | const int new_priority = old_priority - rand() / 2; |
| 678 | |
| 679 | pq.decrease(3, new_priority); |
| 680 | pqcopy[3] = new_priority; |
| 681 | pqcopy.decrease(3); |
| 682 | |
| 683 | std::stringstream ss1, ss2; |
| 684 | ss1 << pq; |
| 685 | ss2 << pqcopy; |
| 686 | EXPECT_TRUE(pq.is_valid()); |
| 687 | EXPECT_TRUE(pqcopy.is_valid()); |
| 688 | EXPECT_STREQ(ss1.str().c_str(), ss2.str().c_str()); |
| 689 | } |
| 690 | |
| 691 | TEST_F(PriorityQueueTest, Increase4) { |
| 692 | Priority_queue<int> pqcopy = pq; |
| 693 | const int old_priority = pq[4]; |
| 694 | const int new_priority = old_priority + rand() / 2; |
| 695 | |
| 696 | pq.increase(4, new_priority); |
| 697 | pqcopy[4] = new_priority; |
| 698 | pqcopy.increase(4); |
| 699 | |
| 700 | std::stringstream ss1, ss2; |
| 701 | ss1 << pq; |
| 702 | ss2 << pqcopy; |
| 703 | EXPECT_TRUE(pq.is_valid()); |
| 704 | EXPECT_TRUE(pqcopy.is_valid()); |
| 705 | EXPECT_STREQ(ss1.str().c_str(), ss2.str().c_str()); |
| 706 | } |
| 707 | |
| 708 | TEST_F(PriorityQueueTest, Update2) { |
| 709 | Priority_queue<int> pqcopy = pq; |
| 710 | |
| 711 | for (int i = -10; i <= 10; ++i) { |
| 712 | const int old_priority = pq[2]; |
| 713 | const int new_priority = old_priority + i; |
| 714 | |
| 715 | pq.update(2, new_priority); |
| 716 | pqcopy[2] = new_priority; |
| 717 | pqcopy.update(2); |
| 718 | |
| 719 | std::stringstream ss1, ss2; |
| 720 | ss1 << pq; |
| 721 | ss2 << pqcopy; |
| 722 | EXPECT_TRUE(pq.is_valid()) << "i:" << i << " pq:" << pq; |
| 723 | EXPECT_TRUE(pqcopy.is_valid()); |
| 724 | EXPECT_STREQ(ss1.str().c_str(), ss2.str().c_str()); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | TEST_F(PriorityQueueTest, UpdateTop) { |
| 729 | Priority_queue<int> pqcopy = pq; |
| 730 | const int old_priority = pq.top(); |
| 731 | const int new_priority = old_priority + 10; |
| 732 | |
| 733 | pq.top() = new_priority; |
| 734 | pq.update_top(); |
| 735 | pqcopy.update(0, new_priority); |
| 736 | std::stringstream ss1, ss2; |
| 737 | ss1 << pq; |
| 738 | ss2 << pqcopy; |
| 739 | EXPECT_TRUE(pq.is_valid()); |
| 740 | EXPECT_TRUE(pqcopy.is_valid()); |
| 741 | EXPECT_STREQ(ss1.str().c_str(), ss2.str().c_str()); |
| 742 | } |
| 743 | |
| 744 | TEST_F(PriorityQueueTest, PopAndRemove) { |
| 745 | Priority_queue<int> pqcopy = pq; |
| 746 | |
| 747 | EXPECT_TRUE(pqcopy.size() == 10U); |
| 748 | pqcopy.pop(); |
| 749 | EXPECT_TRUE(pqcopy.is_valid()); |
| 750 | EXPECT_TRUE(pqcopy.size() == 9U); |
| 751 | pqcopy.remove(3); |
| 752 | EXPECT_TRUE(pqcopy.is_valid()); |
| 753 | EXPECT_TRUE(pqcopy.size() == 8U); |
| 754 | pqcopy.remove(pqcopy.size() - 1); |
| 755 | EXPECT_TRUE(pqcopy.is_valid()); |
| 756 | EXPECT_TRUE(pqcopy.size() == 7U); |
| 757 | |
| 758 | Priority_queue<int> singleton; |
| 759 | EXPECT_FALSE(singleton.push(10)); |
| 760 | singleton.pop(); |
| 761 | EXPECT_TRUE(singleton.empty()); |
| 762 | EXPECT_TRUE(singleton.is_valid()); |
| 763 | } |
| 764 | |
| 765 | TEST_F(PriorityQueueTest, Iterators) { |
| 766 | Priority_queue<int>::iterator it; |
| 767 | Priority_queue<int>::const_iterator cit; |
| 768 | std::stringstream ss1, ss2; |
| 769 | for (it = pq.begin(); it != pq.end(); ++it) ss1 << *it << " "; |
| 770 | for (cit = pq.begin(); cit != pq.end(); ++cit) ss2 << *cit << " "; |
| 771 | EXPECT_STREQ(ss1.str().c_str(), ss2.str().c_str()); |
| 772 | } |
| 773 | |
| 774 | TEST_F(PriorityQueueTest, Clear) { |
| 775 | EXPECT_EQ(10U, pq.size()); |
| 776 | EXPECT_TRUE(pq.is_valid()); |
| 777 | pq.clear(); |
| 778 | EXPECT_EQ(0U, pq.size()); |
| 779 | EXPECT_TRUE(pq.is_valid()); |
| 780 | } |
| 781 | |
| 782 | TEST_F(PriorityQueueTest, Reserve) { |
| 783 | EXPECT_EQ(10U, pq.capacity()); |
| 784 | EXPECT_EQ(10U, pq.size()); |
| 785 | EXPECT_FALSE(pq.reserve(10)); |
| 786 | EXPECT_EQ(10U, pq.capacity()); |
| 787 | EXPECT_EQ(10U, pq.size()); |
| 788 | EXPECT_FALSE(pq.reserve(5)); |
| 789 | EXPECT_EQ(10U, pq.capacity()); |
| 790 | EXPECT_EQ(10U, pq.size()); |
| 791 | EXPECT_FALSE(pq.reserve(20)); |
| 792 | EXPECT_EQ(20U, pq.capacity()); |
| 793 | EXPECT_EQ(10U, pq.size()); |
| 794 | } |
| 795 | |
| 796 | TEST_F(PriorityQueueTest, Sort) { |
| 797 | Priority_queue<int> pqcopy = pq; |
| 798 | std::vector<int> keyscopy(keys, keys + 10); |
| 799 | |
| 800 | pqcopy.sort(); |
| 801 | std::sort(keyscopy.begin(), keyscopy.end()); |
| 802 | |
| 803 | std::stringstream ss1, ss2; |
| 804 | ss1 << pqcopy; |
| 805 | for (size_t i = 0; i < keyscopy.size(); ++i) { |
| 806 | ss2 << keyscopy[i] << " "; |
| 807 | } |
| 808 | EXPECT_STREQ(ss1.str().c_str(), ss2.str().c_str()); |
| 809 | } |
| 810 | |
| 811 | TEST_F(PriorityQueueTest, TestHeapKeys) { |
| 812 | SCOPED_TRACE(""); |
| 813 | test_heap(keys, keys + 10); |
| 814 | } |
| 815 | |
| 816 | TEST_F(PriorityQueueTest, TestHeapKeys2) { |
| 817 | SCOPED_TRACE(""); |
| 818 | test_heap(keys2.begin(), keys2.end()); |
| 819 | } |
| 820 | |
| 821 | TEST_F(PriorityQueueTest, TestHeapOfHandles) { |
| 822 | SCOPED_TRACE(""); |
| 823 | test_heap_of_handles(keys, keys + 10); |
| 824 | } |
| 825 | |
| 826 | TEST_F(PriorityQueueTest, TestHeapOfHandles2) { |
| 827 | SCOPED_TRACE(""); |
| 828 | test_heap_of_handles(keys2.begin(), keys2.end()); |
| 829 | } |
| 830 | |
| 831 | TEST_F(PriorityQueueTest, TestMinKElements) { |
| 832 | SCOPED_TRACE(""); |
| 833 | test_min_k_elements(keys, keys + 10, 7); |
| 834 | } |
| 835 | |
| 836 | TEST_F(PriorityQueueTest, TestMinKElements2) { |
| 837 | SCOPED_TRACE(""); |
| 838 | test_min_k_elements(keys2.begin(), keys2.end(), 4); |
| 839 | } |
| 840 | |
| 841 | TEST_F(PriorityQueueTest, RandomIntegerGenerator) { |
| 842 | random_integer_generator<int> g; |
| 843 | std::vector<int> many_keys; |
| 844 | |
| 845 | for (int i = 0; i < 200; ++i) { |
| 846 | int value = g(0, 300); |
| 847 | many_keys.push_back(value); |
| 848 | } |
| 849 | |
| 850 | SCOPED_TRACE(""); |
| 851 | test_min_k_elements(many_keys.begin(), many_keys.end(), 20); |
| 852 | } |
| 853 | |
| 854 | /** |
| 855 | Bug#30301356 - SOME EVENTS ARE DELAYED AFTER DROPPING EVENT |
| 856 | |
| 857 | Test that ensures heap property is not violated if we remove an |
| 858 | element from an interior node. In the below test, we remove the |
| 859 | element 90 at index 6 in the array. After 90 is removed, the |
| 860 | parent node's of the deleted node violates the heap property. |
| 861 | In order to restore the heap property, we need to move up the |
| 862 | heap until we reach a node which satisfies the heap property or |
| 863 | the root. Without the fix, we adjust the heap downwards. |
| 864 | */ |
| 865 | |
| 866 | TEST_F(PriorityQueueTest, TestElementRemove) { |
| 867 | Priority_queue<int, std::vector<int>, My_greater> pq; |
| 868 | |
| 869 | int keys[11] = {60, 65, 84, 75, 80, 85, 90, 95, 100, 105, 82}; |
| 870 | pq = Priority_queue<int, std::vector<int>, My_greater>(keys, keys + 11); |
| 871 | pq.remove(6); |
| 872 | EXPECT_TRUE(pq.is_valid()); |
| 873 | } |
| 874 | |
| 875 | struct IntWithMark { |
| 876 | int value; |
| 877 | int index; |
| 878 | }; |
| 879 | std::stringstream &operator<<(std::stringstream &stream, const IntWithMark *a) { |
| 880 | stream << a->value; |
| 881 | return stream; |
| 882 | } |
| 883 | struct Pointer_less { |
| 884 | bool operator()(const IntWithMark *a, const IntWithMark *b) const { |
| 885 | return a->value < b->value; |
| 886 | } |
| 887 | }; |
| 888 | struct IntMarker { |
| 889 | void operator()(size_t index, IntWithMark **value) { |
| 890 | (*value)->index = index; |
| 891 | } |
| 892 | }; |
| 893 | |
| 894 | TEST_F(PriorityQueueTest, Mark) { |
| 895 | Priority_queue<IntWithMark *, std::vector<IntWithMark *>, Pointer_less, |
| 896 | IntMarker> |
| 897 | pq; |
| 898 | |
| 899 | IntWithMark a, b, c, d; |
| 900 | a.value = 10; |
| 901 | b.value = 5; |
| 902 | c.value = 15; |
| 903 | d.value = 25; |
| 904 | |
| 905 | pq.push(&a); |
| 906 | pq.push(&b); |
| 907 | pq.push(&c); |
| 908 | pq.push(&d); |
| 909 | |
| 910 | std::stringstream ss1; |
| 911 | ss1 << pq; |
| 912 | EXPECT_STREQ("25 15 10 5 ", ss1.str().c_str()); |
| 913 | EXPECT_EQ(2, a.index); |
| 914 | EXPECT_EQ(3, b.index); |
| 915 | EXPECT_EQ(1, c.index); |
| 916 | EXPECT_EQ(0, d.index); |
| 917 | |
| 918 | c.value = 1; |
| 919 | pq.update(c.index); |
| 920 | EXPECT_EQ(3, c.index); |
| 921 | |
| 922 | d.value = 100; |
| 923 | pq.update(d.index); |
| 924 | EXPECT_EQ(0, d.index); |
| 925 | EXPECT_EQ(100, pq.top()->value); |
| 926 | |
| 927 | pq.pop(); |
| 928 | std::stringstream ss2; |
| 929 | ss2 << pq; |
| 930 | EXPECT_STREQ("10 5 1 ", ss2.str().c_str()); |
| 931 | EXPECT_EQ(0, a.index); |
| 932 | EXPECT_EQ(1, b.index); |
| 933 | EXPECT_EQ(2, c.index); |
| 934 | } |
| 935 | |
| 936 | } // namespace priority_queue_unittest |