Files
Library/tests/standalone/Random/Random.cpp
T
faumarayandClaude Sonnet 4.6 a09e000f0f Add Hash, Random, Uuid, Encoding, Bits, Math modules + make Testing deps optional
New header-only modules:
- Void::Hash — FNV-1a 32/64, CRC-32, DJB2, MurmurHash3-32, UDL literals
- Void::Random — Xoshiro256** PRNG, uniform_int/real, coin_flip, shuffle, random_string
- Void::Uuid — v4 (random), v5_lite (name-based), parse, RFC 4122 namespaces
- Void::Encoding — Base64 (standard + URL-safe), Hex encode/decode
- Void::Bits — popcount, bit extract/deposit, rotation, align, byteswap, masks
- Void::Math — Vec2/Vec3/Vec4, Mat4 (identity, translation, scale, rotation)

Testing module: move Stacktrace from INTERNAL_DEPS to OPTIONAL_DEPS. Added
Detail/Color.hpp shim that delegates to Stacktrace::Color when present or
provides inline ANSI fallback otherwise. All Stacktrace calls in SignalHandler,
ConsoleReporter, and Runner are gated on VOID_HAS_STACKTRACE.

Fix Repr.hpp reflect_struct to handle template specializations that lack
std::meta::identifier_of (now guarded by has_identifier check).

33/33 tests pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-14 21:52:22 +03:00

121 lines
4.0 KiB
C++

#include <Void/Random/Random.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <algorithm>
#include <vector>
// ── 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});
}
}