Source
tests/purrc/random_sys_test.cpp
1
// Copyright (c) 2026 BigBrain LLC. MIT-licensed (see LICENSE).2
// Original work; see ACKNOWLEDGMENTS.md for the open-source ideas we build upon.3
// System-level (whole-program) test for the `random` stdlib module. Unlike the4
// per-function compile-run tests (tests/purrc/random_cr_test.cpp), this drives a5
// single cohesive program through EVERY purr-callable random function and asserts6
// its exact stdout, so the functions are exercised together.7
//8
// RNG is non-deterministic until seeded, so the program seeds first and then9
// asserts DETERMINISTIC properties (same-seed reproducibility, in-bounds10
// membership, degenerate range, all-equal choice) — never a raw random value.11
// Each assertion prints True, confirming every function ran successfully.12
//13
// Coverage — every function in stdlib/random/random.hpp:14
// seed, random, uniform, randint, gauss, choice.15
#include "e2e_harness.hpp"17
TEST(StdlibE2E, Random) {18
e2e::expect_e2e("random_sys", R"PURR(import io19
import random21
# seed + random: same seed reproduces the same draw, which lies in [0, 1).22
random.seed(123)23
let r1 = random.random()24
random.seed(123)25
let r2 = random.random()26
let random_ok = r1 == r2 and 0.0 <= r1 and r1 < 1.028
# uniform: reproducible under a fixed seed and within the requested bounds.29
random.seed(123)30
let u1 = random.uniform(-5.0, 5.0)31
random.seed(123)32
let u2 = random.uniform(-5.0, 5.0)33
let uniform_ok = u1 == u2 and -5.0 <= u1 and u1 <= 5.035
# randint: reproducible over a wide range; a degenerate range pins the value.36
random.seed(123)37
let i1 = random.randint(1, 1000000)38
random.seed(123)39
let i2 = random.randint(1, 1000000)40
let randint_ok = i1 == i2 and random.randint(5, 5) == 542
# gauss: same seed reproduces the same deviate.43
random.seed(123)44
let g1 = random.gauss(0.0, 1.0)45
random.seed(123)46
let g2 = random.gauss(0.0, 1.0)47
let gauss_ok = g1 == g249
# choice: picking from an all-equal sequence is deterministic.50
random.seed(123)51
let xs = [7, 7, 7, 7]52
let choice_ok = random.choice(xs) == 754
io.print(random_ok, uniform_ok, randint_ok, gauss_ok, choice_ok)55
)PURR",56
"True True True True True\n");57
}