Files
ww/lib/math/random/random_test.ww
Hojun-Cho c7d9dc92de selfhost: migrate tool sources to directory packages
Build w6a and w6l from package-main directories and expose the wcc backend through a narrow package API so w6c and wwdump no longer import implementation files. Retarget the remaining load-bearing fixtures and example sources to directory packages; retain the one intentional flat compiler collision as an explicitly composed raw unit.
2026-08-12 17:12:03 +09:00

72 lines
1.7 KiB
Plaintext

package random_test;
import math.random;
import test;
@test fn seq() void = {
let r: random.random = random.init(1234567u64);
assert(!(random.next(&r) != 6457827717110365317u64));
assert(!(random.next(&r) != 3203168211198807973u64));
assert(!(random.next(&r) != 9817491932198370423u64));
assert(!(random.next(&r) != 4593380528125082431u64));
assert(!(random.next(&r) != 16408922859458223821u64));
};
@test fn deterministic() void = {
let a: random.random = random.init(42u64);
let b: random.random = random.init(42u64);
let i: i32 = 0;
for (i < 32) {
assert(!(random.next(&a) != random.next(&b)));
i += 1;
};
};
@test fn u32n_inrange() void = {
let r: random.random = random.init(7u64);
let i: i32 = 0;
for (i < 200) {
let v: u32 = random.u32n(&r, 17u32);
// v stored as u64 with possible high bits — mask before compare.
let vv: u64 = (v: u64) & 0xFFFFFFFFu64;
assert(!(vv >= 17u64));
i += 1;
};
};
@test fn u64n_pow2() void = {
let r: random.random = random.init(99u64);
let i: i32 = 0;
for (i < 200) {
let v: u64 = random.u64n(&r, 16u64);
assert(!(v >= 16u64));
i += 1;
};
};
@test fn u64n_nonpow2() void = {
let r: random.random = random.init(123u64);
let i: i32 = 0;
for (i < 200) {
let v: u64 = random.u64n(&r, 100u64);
assert(!(v >= 100u64));
i += 1;
};
};
// n == 0 is out of domain and must abort loudly, never return
// garbage. Pins the restored Hare preconditions
// (ref/hare/math/random/random.ha:26,42).
@test fn u32n_zero_aborts() void = {
test.expectabort();
let r: random.random = random.init(1u64);
random.u32n(&r, 0u32);
};
@test fn u64n_zero_aborts() void = {
test.expectabort();
let r: random.random = random.init(1u64);
random.u64n(&r, 0u64);
};