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>
168 lines
4.6 KiB
C++
168 lines
4.6 KiB
C++
#include <Void/Math/Histogram.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace Void::Histogram;
|
|
|
|
// ── Histogram<K> ──────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test histogram_add_and_count()
|
|
{
|
|
Histogram<std::string> h;
|
|
h.add("a");
|
|
h.add("a");
|
|
h.add("b");
|
|
Void::Testing::expect_eq(h.count("a"), std::size_t{2});
|
|
Void::Testing::expect_eq(h.count("b"), std::size_t{1});
|
|
Void::Testing::expect_eq(h.count("c"), std::size_t{0});
|
|
}
|
|
|
|
Void::Testing::Test histogram_total_and_distinct()
|
|
{
|
|
Histogram<int> h;
|
|
h.add(1); h.add(2); h.add(1); h.add(3);
|
|
Void::Testing::expect_eq(h.total(), std::size_t{4});
|
|
Void::Testing::expect_eq(h.distinct(), std::size_t{3});
|
|
}
|
|
|
|
Void::Testing::Test histogram_add_with_count()
|
|
{
|
|
Histogram<std::string> h;
|
|
h.add("x", 5);
|
|
Void::Testing::expect_eq(h.count("x"), std::size_t{5});
|
|
Void::Testing::expect_eq(h.total(), std::size_t{5});
|
|
}
|
|
|
|
Void::Testing::Test histogram_frequency()
|
|
{
|
|
Histogram<int> h;
|
|
h.add(1, 3);
|
|
h.add(2, 1);
|
|
const double f = h.frequency(1);
|
|
Void::Testing::expect(f > 0.74 && f < 0.76); // 3/4
|
|
}
|
|
|
|
Void::Testing::Test histogram_mode()
|
|
{
|
|
Histogram<std::string> h;
|
|
h.add("a", 3);
|
|
h.add("b", 5);
|
|
h.add("c", 1);
|
|
const auto m = h.mode();
|
|
Void::Testing::expect(m.has_value());
|
|
Void::Testing::expect_eq(*m, std::string{"b"});
|
|
}
|
|
|
|
Void::Testing::Test histogram_least_common()
|
|
{
|
|
Histogram<int> h;
|
|
h.add(1, 10);
|
|
h.add(2, 1);
|
|
h.add(3, 5);
|
|
const auto lc = h.least_common();
|
|
Void::Testing::expect(lc.has_value());
|
|
Void::Testing::expect_eq(*lc, 2);
|
|
}
|
|
|
|
Void::Testing::Test histogram_mode_empty_returns_nullopt()
|
|
{
|
|
Histogram<int> h;
|
|
Void::Testing::expect(!h.mode().has_value());
|
|
}
|
|
|
|
Void::Testing::Test histogram_top_k()
|
|
{
|
|
Histogram<int> h;
|
|
h.add(1, 3);
|
|
h.add(2, 10);
|
|
h.add(3, 5);
|
|
h.add(4, 1);
|
|
const auto top = h.top_k(2);
|
|
Void::Testing::expect_eq(top.size(), std::size_t{2});
|
|
Void::Testing::expect_eq(top[0].first, 2); // highest count first
|
|
Void::Testing::expect_eq(top[0].second, std::size_t{10});
|
|
}
|
|
|
|
Void::Testing::Test histogram_erase()
|
|
{
|
|
Histogram<int> h;
|
|
h.add(1, 5);
|
|
h.add(2, 3);
|
|
Void::Testing::expect(h.erase(1));
|
|
Void::Testing::expect(!h.contains(1));
|
|
Void::Testing::expect_eq(h.total(), std::size_t{3});
|
|
Void::Testing::expect(!h.erase(99));
|
|
}
|
|
|
|
Void::Testing::Test histogram_clear()
|
|
{
|
|
Histogram<int> h;
|
|
h.add(1); h.add(2);
|
|
h.clear();
|
|
Void::Testing::expect(h.empty());
|
|
Void::Testing::expect_eq(h.total(), std::size_t{0});
|
|
}
|
|
|
|
Void::Testing::Test histogram_merge()
|
|
{
|
|
Histogram<std::string> a, b;
|
|
a.add("x", 3);
|
|
b.add("x", 2);
|
|
b.add("y", 1);
|
|
a.merge(b);
|
|
Void::Testing::expect_eq(a.count("x"), std::size_t{5});
|
|
Void::Testing::expect_eq(a.count("y"), std::size_t{1});
|
|
Void::Testing::expect_eq(a.total(), std::size_t{6});
|
|
}
|
|
|
|
// ── FixedBinHistogram ─────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test fixed_bin_basic_add()
|
|
{
|
|
FixedBinHistogram h{0.0, 10.0, 10};
|
|
h.add(0.5); // bin 0
|
|
h.add(1.5); // bin 1
|
|
h.add(9.9); // bin 9
|
|
Void::Testing::expect_eq(h.bin_count(0), std::size_t{1});
|
|
Void::Testing::expect_eq(h.bin_count(1), std::size_t{1});
|
|
Void::Testing::expect_eq(h.bin_count(9), std::size_t{1});
|
|
Void::Testing::expect_eq(h.total(), std::size_t{3});
|
|
}
|
|
|
|
Void::Testing::Test fixed_bin_clamping()
|
|
{
|
|
FixedBinHistogram h{0.0, 5.0, 5};
|
|
h.add(-1.0); // clamp to bin 0
|
|
h.add(10.0); // clamp to last bin
|
|
Void::Testing::expect_eq(h.bin_count(0), std::size_t{1});
|
|
Void::Testing::expect_eq(h.bin_count(4), std::size_t{1});
|
|
}
|
|
|
|
Void::Testing::Test fixed_bin_width()
|
|
{
|
|
FixedBinHistogram h{0.0, 10.0, 5};
|
|
Void::Testing::expect(h.width() > 1.99 && h.width() < 2.01);
|
|
}
|
|
|
|
Void::Testing::Test fixed_bin_invalid_args()
|
|
{
|
|
bool threw = false;
|
|
try { FixedBinHistogram h{5.0, 3.0, 4}; } catch (...) { threw = true; }
|
|
Void::Testing::expect(threw);
|
|
threw = false;
|
|
try { FixedBinHistogram h{0.0, 10.0, 0}; } catch (...) { threw = true; }
|
|
Void::Testing::expect(threw);
|
|
}
|
|
|
|
Void::Testing::Test fixed_bin_clear()
|
|
{
|
|
FixedBinHistogram h{0.0, 10.0, 5};
|
|
h.add(1.0); h.add(5.0);
|
|
h.clear();
|
|
Void::Testing::expect_eq(h.total(), std::size_t{0});
|
|
Void::Testing::expect_eq(h.bin_count(0), std::size_t{0});
|
|
}
|