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>
168 lines
4.6 KiB
C++
168 lines
4.6 KiB
C++
#include <Void/Histogram/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});
|
|
}
|