#include #include #include #include #include using namespace Void::PriorityQueue; // ── basic operations ────────────────────────────────────────────────────────── Void::Testing::Test pq_push_pop_min_order() { PriorityQueue pq; pq.push("c", 30); pq.push("a", 10); pq.push("b", 20); Void::Testing::expect_eq(pq.top_key(), std::string{"a"}); Void::Testing::expect_eq(pq.top_priority(), 10); Void::Testing::expect_eq(pq.pop(), std::string{"a"}); Void::Testing::expect_eq(pq.top_key(), std::string{"b"}); Void::Testing::expect_eq(pq.pop(), std::string{"b"}); Void::Testing::expect_eq(pq.pop(), std::string{"c"}); Void::Testing::expect(pq.empty()); } Void::Testing::Test pq_size_and_empty() { PriorityQueue pq; Void::Testing::expect(pq.empty()); Void::Testing::expect_eq(pq.size(), std::size_t{0}); pq.push(1, 10); pq.push(2, 20); Void::Testing::expect_eq(pq.size(), std::size_t{2}); Void::Testing::expect(!pq.empty()); } Void::Testing::Test pq_contains() { PriorityQueue pq; pq.push("x", 1); Void::Testing::expect(pq.contains("x")); Void::Testing::expect(!pq.contains("y")); } Void::Testing::Test pq_priority_of() { PriorityQueue pq; pq.push(1, 3.14); const auto p = pq.priority_of(1); Void::Testing::expect(p.has_value()); Void::Testing::expect(*p > 3.1 && *p < 3.2); Void::Testing::expect(!pq.priority_of(99).has_value()); } // ── update ──────────────────────────────────────────────────────────────────── Void::Testing::Test pq_update_decreases_priority() { PriorityQueue pq; pq.push("a", 10); pq.push("b", 5); // Update "a" to have priority 1 (now minimum) pq.update("a", 1); Void::Testing::expect_eq(pq.top_key(), std::string{"a"}); } Void::Testing::Test pq_update_increases_priority() { PriorityQueue pq; pq.push("a", 1); pq.push("b", 5); pq.update("a", 10); // "a" no longer minimum Void::Testing::expect_eq(pq.top_key(), std::string{"b"}); } Void::Testing::Test pq_push_existing_key_updates() { PriorityQueue pq; pq.push(1, 10); pq.push(1, 2); // update key 1 to priority 2 Void::Testing::expect_eq(pq.size(), std::size_t{1}); Void::Testing::expect_eq(*pq.priority_of(1), 2); } // ── erase ───────────────────────────────────────────────────────────────────── Void::Testing::Test pq_erase_top() { PriorityQueue pq; pq.push(1, 5); pq.push(2, 10); pq.push(3, 15); pq.erase(1); Void::Testing::expect(!pq.contains(1)); Void::Testing::expect_eq(pq.top_key(), 2); } Void::Testing::Test pq_erase_middle() { PriorityQueue pq; pq.push(1, 5); pq.push(2, 10); pq.push(3, 15); pq.erase(2); Void::Testing::expect_eq(pq.size(), std::size_t{2}); Void::Testing::expect(!pq.contains(2)); } Void::Testing::Test pq_erase_nonexistent_is_noop() { PriorityQueue pq; pq.push(1, 5); pq.erase(99); Void::Testing::expect_eq(pq.size(), std::size_t{1}); } // ── clear ───────────────────────────────────────────────────────────────────── Void::Testing::Test pq_clear() { PriorityQueue pq; pq.push(1, 1); pq.push(2, 2); pq.clear(); Void::Testing::expect(pq.empty()); } // ── max-heap variant ────────────────────────────────────────────────────────── Void::Testing::Test pq_max_heap() { MaxPriorityQueue pq; pq.push("a", 1); pq.push("b", 100); pq.push("c", 50); Void::Testing::expect_eq(pq.top_key(), std::string{"b"}); Void::Testing::expect_eq(pq.pop(), std::string{"b"}); Void::Testing::expect_eq(pq.top_key(), std::string{"c"}); } // ── dijkstra-style usage ────────────────────────────────────────────────────── Void::Testing::Test pq_dijkstra_pattern() { // Shortest path from 0 in a simple graph: 0-1 weight 4, 0-2 weight 1, 2-1 weight 2 PriorityQueue pq; std::vector dist(3, std::numeric_limits::max()); dist[0] = 0; pq.push(0, 0); while (!pq.empty()) { const int u = pq.pop(); if (u == 0) { if (4 < dist[1]) { dist[1] = 4; pq.push(1, 4); } if (1 < dist[2]) { dist[2] = 1; pq.push(2, 1); } } else if (u == 2) { if (dist[2] + 2 < dist[1]) { dist[1] = dist[2] + 2; pq.push(1, dist[1]); } } } Void::Testing::expect_eq(dist[1], 3); // via node 2: 1+2 Void::Testing::expect_eq(dist[2], 1); }