Files
Library/tests/standalone/Math/Random.cpp
T
faumarayandClaude Opus 4.8 fb0506581f Add Url/Html encoding, ISO-8601 interval parse, TempFile, random_string charset
Universal additions (all generic, header-only, tested; full suite 1127 pass):
- Void::Encoding::Url — RFC 3986 percent encode/decode + query_encode.
- Void::Text::html_unescape — numeric + common named HTML entities -> UTF-8.
- Void::Time::parse_iso8601_interval — parse PT#H#M#S / P#DT#H#M#S durations
  (distinct from the unit-suffix parse_duration).
- Void::FileSystem::TempFile — RAII unique temp file (alongside TempDir),
  with keep() to persist.
- Void::Random::random_string(n, charset) overload.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-22 16:15:01 +03:00

149 lines
5.0 KiB
C++

#include <Void/Math/Random.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <algorithm>
#include <vector>
// ── random_string charset ────────────────────────────────────────────────────
Void::Testing::Test random_string_length_and_charset()
{
const std::string charset = "ABC";
const auto s = Void::Random::random_string(64, charset);
Void::Testing::expect_eq(s.size(), std::size_t{64});
for (char c : s) {
Void::Testing::expect(charset.find(c) != std::string::npos);
}
}
Void::Testing::Test random_string_default_alphanumeric()
{
const auto s = Void::Random::random_string(32);
Void::Testing::expect_eq(s.size(), std::size_t{32});
for (char c : s) {
const bool alnum = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9');
Void::Testing::expect(alnum);
}
}
Void::Testing::Test random_string_empty_charset_is_empty()
{
Void::Testing::expect(Void::Random::random_string(10, "").empty());
}
// ── Xoshiro256 ───────────────────────────────────────────────────────────────
Void::Testing::Test xoshiro_produces_output()
{
Void::Random::Xoshiro256 rng{42};
const auto a = rng();
const auto b = rng();
Void::Testing::expect(a != b);
}
Void::Testing::Test xoshiro_same_seed_same_sequence()
{
Void::Random::Xoshiro256 r1{12345};
Void::Random::Xoshiro256 r2{12345};
for (int i = 0; i < 10; ++i) {
Void::Testing::expect_eq(r1(), r2());
}
}
Void::Testing::Test xoshiro_different_seeds_differ()
{
Void::Random::Xoshiro256 r1{1};
Void::Random::Xoshiro256 r2{2};
Void::Testing::expect(r1() != r2());
}
// ── uniform_int ──────────────────────────────────────────────────────────────
Void::Testing::Test uniform_int_in_range()
{
for (int i = 0; i < 100; ++i) {
const auto v = Void::Random::uniform_int(1, 6);
Void::Testing::expect(v >= 1);
Void::Testing::expect(v <= 6);
}
}
Void::Testing::Test uniform_int_single_value()
{
Void::Testing::expect_eq(Void::Random::uniform_int(7, 7), 7);
}
// ── uniform_real ─────────────────────────────────────────────────────────────
Void::Testing::Test uniform_real_in_range()
{
for (int i = 0; i < 50; ++i) {
const double v = Void::Random::uniform_real(0.0, 1.0);
Void::Testing::expect(v >= 0.0);
Void::Testing::expect(v < 1.0);
}
}
// ── coin_flip ────────────────────────────────────────────────────────────────
Void::Testing::Test coin_flip_always_true()
{
Void::Testing::expect(Void::Random::coin_flip(1.0));
}
Void::Testing::Test coin_flip_always_false()
{
Void::Testing::expect(!Void::Random::coin_flip(0.0));
}
// ── shuffle ──────────────────────────────────────────────────────────────────
Void::Testing::Test shuffle_preserves_elements()
{
std::vector<int> v{1, 2, 3, 4, 5};
const auto orig = v;
Void::Random::shuffle(v);
auto sorted = v;
std::sort(sorted.begin(), sorted.end());
Void::Testing::expect_eq(sorted, orig);
}
// ── random_string ────────────────────────────────────────────────────────────
Void::Testing::Test random_string_correct_length()
{
Void::Testing::expect_eq(Void::Random::random_string(10).size(),
std::size_t{10});
}
Void::Testing::Test random_string_empty()
{
Void::Testing::expect(Void::Random::random_string(0).empty());
}
Void::Testing::Test random_string_varies()
{
// Extremely unlikely to produce identical 16-char strings twice
const auto a = Void::Random::random_string(16);
const auto b = Void::Random::random_string(16);
Void::Testing::expect(a != b);
}
// ── sample_indices ───────────────────────────────────────────────────────────
Void::Testing::Test sample_indices_correct_count()
{
const auto idxs = Void::Random::sample_indices(20, 5);
Void::Testing::expect_eq(idxs.size(), std::size_t{5});
}
Void::Testing::Test sample_indices_in_range()
{
const auto idxs = Void::Random::sample_indices(10, 10);
for (auto i : idxs) {
Void::Testing::expect(i < std::size_t{10});
}
}