From abca7b2fc2eb06acb85bb42dedca03b57a442d62 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 22 Oct 2026 10:17:34 +0100 Subject: [PATCH] Rand: a seeded generator, splitmix64; the testing plan Rand gives init, next, below (no bias, by rejection), range (the whole I64 span) and seed (fresh, from Sys.random), with its self-test. It is step 1 of TESTING-PLAN.md: Rand, Check and Prop in the library, and raztest in tools/. Not yet in raz-lib.md; that waits for step 7. make check: 1021 passed, 1 failed, not this step's: raz --version says report 2026-10.34 while doc/raz.md is at 10.35 since d3ddbe6. --- diff --git a/TESTING-PLAN.md b/TESTING-PLAN.md new file mode 100644 index 0000000..2f6f23b --- /dev/null +++ b/TESTING-PLAN.md @@ -0,0 +1,91 @@ +# Testing: Rand, Check, Prop and raztest + +A plan for testing raz programs: three library modules and a tool, all +shipped with raz, so that a public project needs nothing past raz, make +and sh to use them. Each step is a commit of its own, useful alone; +after each, stop for review. + +## Why here + +- Every program has the library, and a program's make already builds + `tools/Pretty.raz` for `fmt`; `raztest` is found the same way. +- Today ~180 `check`/`expect` helpers are written by hand across ~/r, + four projects carry a PRNG of their own (rjson, pict, sauce, tasta), + and each Makefile hand-rolls its test loop and summary. + +## The pieces + +| Piece | Where | Size | What | +|---|---|---|---| +| `Rand` | `lib/Rand.raz` | ~40 | a seeded generator, splitmix64 | +| `Check` | `lib/Check.raz` | ~80 | assertions, failures on stderr, status by `Rt.atExit` | +| `Prop` | `lib/Prop.raz` | ~300 | properties and fuzzing over recorded choices, with shrinking | +| `raztest` | `tools/Test.raz` | ~700 | finds, builds and runs tests; colour, reports, watch | + +**Rand.** `init(r, seed)`, `next(r): U64`, `below(r, n)` (no bias), +`range(r, lo, hi)`, `seed(): U64` (fresh, from `Sys.random`, to print +and replay). No reals until a user needs them. + +**Check.** `ok(cond, what)`, `str(what, got, want)` (escaped), +`int(what, got, want)`, `mute`/`unmute`. A failure is a block on +standard error: + + FAIL what + got: "..." + want: "..." + +and a program with failures ends with `Check: F of N failed` and status +1, by a procedure registered with `Rt.atExit` (which may call +`Rt.exit` itself), so there is no `done` to forget. A passing run says +nothing. The status and this text are the whole contract with +`raztest`; a bare `./Mod` or a make loop works as before. + +**Prop.** A property is `proc(var s: Prop.Source)` that draws values +(`int(s, lo, hi)`, `bool(s)`, `bytes(s, max, buf)`, `pick(s, n)`) and +checks them with `Check`. A case is the bytes it drew (as Hypothesis +does), so shrinking works on those bytes (cut chunks, zero, lower) and +replays the property: no shrinker per type, no closures. A property +reading only `bytes` is a byte fuzzer. While searching, `Check` is +muted; the smallest failing case is run again aloud and saved to +`test/seeds/`; saved seeds are replayed first, every run. +`PROP_SEED` replays one case, `PROP_TIME=60` fuzzes for a minute. + +**raztest.** Finds modules with `proc main*` in `src/` and `test/*.raz` +(`-I`, default `src`); builds with `-Werror`; runs them, `-j n`, a +timeout each, `-r pattern`; times each; shows a test's output only when +it fails; colour on a terminal unless `CI` or `NO_COLOR`; a summary and +the status. `--tap`, `--junit file` (a module is a case, its `FAIL` +blocks the failure). `-w` reruns what a changed `.raz` reaches, by +polling file times (Sys has no inotify: a gap). `--seed`, `--fuzz 60s` +set `PROP_*`. Left out: `.cn`, sh and pty tests, which stay in make. A +Makefile uses `raztest` when it's there and its own loop when not. + +## Steps + +1. `Rand`, with its self-test. +2. `Check`; the library's self-tests use it. +3. `raztest`: finding, building, running, `-j`, timeouts, colour, + summary; `test/check.sh`'s library loop uses it. `TOOLS` gains + `Test`; `make install` puts `raztest` on PATH; `doc/raztest.1`. +4. `raztest --tap`, `--junit`. +5. `raztest -w`. +6. `Prop`, with its self-test. +7. Volume 2: `Rand`, `Check` and `Prop` go into `doc/raz-lib.md` as + standard library (not core: `conform/` is untouched), with whole + examples, once the first users have settled them; one report + version. `STANDARDS.md` says how a module tests itself with them. +8. robbie: `robbie -w`, in its own repository. + +After the release that brings them: rjson, pict and sauce take `Rand` +and `Prop` (rjson's `Random.raz`, JSONTestSuite as seeds; pict's fuzz; +sauce's `Fuzz.raz` draws), and projects take `Check` and `raztest` as +their files are touched, with no sweep. `desktop.7` names `raztest` +once it is installed. + +## Left out + +Coverage-guided fuzzing (the compiler would have to count branches); +tests in doc comments (no library comment holds a whole example; the +report's and the tutorial's are already run); a shared pty harness +(belongs with rtui or rterm); `Check` naming the failing line (`Rt` +knows places but exports none: a gap). diff --git a/lib/Rand.raz b/lib/Rand.raz new file mode 100644 index 0000000..5baa4c8 --- /dev/null +++ b/lib/Rand.raz @@ -0,0 +1,98 @@ +(* A seeded random number generator, splitmix64: one U64 of state, a + few steps to a number. The same seed gives the same numbers, on + every machine, so a failing run can be replayed. It is fast and + passes the usual statistical tests, but it is not for secrets. + Needs Sys (for a fresh seed) and Rt. Leaves out reals, shuffles and + other distributions. *) +module Rand; +import Sys, Rt; + +type + Rand* = record + state: U64; (* advances by a constant at each draw *) + end; + +(* Starts r at seed; any seed, 0 too, will do. *) +proc init*(var r: Rand; seed: U64); +begin + r.state := seed; +end; + +(* The next number, all 64 bits of it. *) +proc next*(var r: Rand): U64; +var z: U64; +begin + INC(r.state, 09E3779B97F4A7C15H); + z := r.state; + z := (z xor (z shr 30)) * 0BF58476D1CE4E5B9H; + z := (z xor (z shr 27)) * 094D049BB133111EBH; + return z xor (z shr 31); +end; + +(* A number from 0 to n - 1, each as likely as another. It draws again + when it falls in the short last run of the 64-bit range, which n + does not divide, so no number is favoured. n = 0 stops the program. *) +proc below*(var r: Rand; n: U64): U64; +var x, short: U64; +begin + if n = 0 then Rt.stop("Rand.below: n is 0"); end; + short := (0 - n) mod n; (* 2^64 mod n numbers are left over *) + x := next(r); + while x < short do x := next(r); end; + return x mod n; +end; + +(* A number from lo to hi, both included; lo must not exceed hi. The + span is taken as a U64, so the whole I64 range works. *) +proc range*(var r: Rand; lo, hi: I64): I64; +var span: U64; +begin + if lo > hi then Rt.stop("Rand.range: lo > hi"); end; + span := U64(hi) - U64(lo); + if span = 0FFFFFFFFFFFFFFFFH then return I64(next(r)); end; + return I64(U64(lo) + below(r, span + 1)); +end; + +(* A fresh seed from the kernel, for a program to print and, to replay + a run, to be given back to init. *) +proc seed*(): U64; +var b: [8]U8; i: INT; s: U64; +begin + Sys.random(b); + s := 0; + for i := 0 to 7 do s := s shl 8 | U64(b[i]); end; + return s; +end; + +(* The module's test. *) +proc main*; +var r, q: Rand; i: INT; x: U64; v: I64; lo, hi: BOOL; +begin + init(r, 0); + if (next(r) # 0E220A8397B1DCDAFH) or (next(r) # 06E789E6AA1B965F4H) + or (next(r) # 006C45D188009454FH) then + Rt.stop("Rand.next: splitmix64 from 0"); + end; + init(r, 42); init(q, 42); + for i := 1 to 100 do + if next(r) # next(q) then Rt.stop("Rand: same seed, different numbers"); end; + end; + for i := 1 to 1000 do + if below(r, 1) # 0 then Rt.stop("Rand.below 1"); end; + if below(r, 3) >= 3 then Rt.stop("Rand.below 3"); end; + x := below(r, 08000000000000001H); + if x >= 08000000000000001H then Rt.stop("Rand.below 2^63+1"); end; + end; + lo := false; hi := false; + for i := 1 to 1000 do + v := range(r, -2, 2); + if (v < -2) or (v > 2) then Rt.stop("Rand.range -2..2"); end; + lo := lo or (v = -2); hi := hi or (v = 2); + end; + if not (lo and hi) then Rt.stop("Rand.range: ends not reached"); end; + for i := 1 to 100 do v := range(r, -9223372036854775807 - 1, 9223372036854775807); end; + if range(r, 7, 7) # 7 then Rt.stop("Rand.range 7..7"); end; + x := seed(); +end; + +end Rand.