Files
Library/tests/standalone/Containers/PriorityQueue.cpp
T
faumarayandClaude Fable 5 28b94883fd Restructure into umbrella modules (~55 -> 16 targets)
Headers move, CMake targets merge, C++ namespaces stay unchanged:
- Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant
- Functional: Pipeline, Either, Result, Retry
- Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval
- Math: +Matrix, Numeric, Random, Histogram
- Text: Str, Format, Tokenizer, Diff, Encoding, Semver
- Data: Json, Csv, Ini
- Async: Coro, Concurrency, EventBus, Observable
- Time: +Bench
- Log: Log facade + Logging framework (facade now header-only, breaking
  the Stacktrace<->Log link cycle the merge would have created)
- System: FileSystem + Process
- Configuration: +Argparse
- Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail

Module system fixes:
- OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no
  longer depends on the alphabetical discovery order of src/ subdirs
  (Testing -> Text would have been silently dropped).
- Every header-only module now has a tracked module.cmake; previously the
  empty src/<Name> marker dirs were untracked, so a fresh clone would have
  silently skipped those modules.
- VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT.

Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files
prefixed: ResultBasic etc.). Test count verified identical (831) before and
after. README/CLAUDE.md module tables updated.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 18:50:02 +03:00

160 lines
5.3 KiB
C++

#include <Void/Containers/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);
}