#include #include #include #include #include using namespace Void::Histogram; // ── Histogram ────────────────────────────────────────────────────────────── Void::Testing::Test histogram_add_and_count() { Histogram 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 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 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 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 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 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 h; Void::Testing::expect(!h.mode().has_value()); } Void::Testing::Test histogram_top_k() { Histogram 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 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 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 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}); }