cheatah
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 the
4// per-function compile-run tests (tests/purrc/random_cr_test.cpp), this drives a
5// single cohesive program through EVERY purr-callable random function and asserts
6// its exact stdout, so the functions are exercised together.
7//
8// RNG is non-deterministic until seeded, so the program seeds first and then
9// asserts DETERMINISTIC properties (same-seed reproducibility, in-bounds
10// 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"
17TEST(StdlibE2E, Random) {
18 e2e::expect_e2e("random_sys", R"PURR(import io
19import random
21# seed + random: same seed reproduces the same draw, which lies in [0, 1).
22random.seed(123)
23let r1 = random.random()
24random.seed(123)
25let r2 = random.random()
26let random_ok = r1 == r2 and 0.0 <= r1 and r1 < 1.0
28# uniform: reproducible under a fixed seed and within the requested bounds.
29random.seed(123)
30let u1 = random.uniform(-5.0, 5.0)
31random.seed(123)
32let u2 = random.uniform(-5.0, 5.0)
33let uniform_ok = u1 == u2 and -5.0 <= u1 and u1 <= 5.0
35# randint: reproducible over a wide range; a degenerate range pins the value.
36random.seed(123)
37let i1 = random.randint(1, 1000000)
38random.seed(123)
39let i2 = random.randint(1, 1000000)
40let randint_ok = i1 == i2 and random.randint(5, 5) == 5
42# gauss: same seed reproduces the same deviate.
43random.seed(123)
44let g1 = random.gauss(0.0, 1.0)
45random.seed(123)
46let g2 = random.gauss(0.0, 1.0)
47let gauss_ok = g1 == g2
49# choice: picking from an all-equal sequence is deterministic.
50random.seed(123)
51let xs = [7, 7, 7, 7]
52let choice_ok = random.choice(xs) == 7
54io.print(random_ok, uniform_ok, randint_ok, gauss_ok, choice_ok)
55)PURR",
56 "True True True True True\n");