Files
Library/tests/standalone/PriorityQueue/PriorityQueue.cpp
T
faumarayandClaude Sonnet 4.6 58ff7b38a0 Add 20+ new modules with tests: Pipeline, Matrix, Tokenizer, ScopeGuard, Bitset, Optional, Counted, Curry, TypeMap, Bench, DisjointSet, Histogram, StopWatch, and more
Also fixes Diff/Memo/PriorityQueue from previous session: LineDiff is now
an alias for Edit<string> so count_op/apply work on line diffs; Memoized
uses std::apply instead of type-based get to handle duplicate arg types.

874/877 tests pass (3 are platform-gated plugin tests).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-14 23:49:09 +03:00

160 lines
5.4 KiB
C++

#include <Void/PriorityQueue/PriorityQueue.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
#include <vector>
using namespace Void::PriorityQueue;
// ── basic operations ──────────────────────────────────────────────────────────
Void::Testing::Test pq_push_pop_min_order()
{
PriorityQueue<std::string, int> 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<int, int> 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<std::string, int> pq;
pq.push("x", 1);
Void::Testing::expect(pq.contains("x"));
Void::Testing::expect(!pq.contains("y"));
}
Void::Testing::Test pq_priority_of()
{
PriorityQueue<int, double> 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<std::string, int> 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<std::string, int> 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<int, int> 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<int, int> 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<int, int> 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<int, int> 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<int, int> 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<std::string, int> 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<int, int> pq;
std::vector<int> dist(3, std::numeric_limits<int>::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);
}