raz / commit abca7b2f

commit
abca7b2fc2eb06acb85bb42dedca03b57a442d62
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
7342933b
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.

patch · log

2 files changed, +189 -0

TESTING-PLAN.md added +91 -0

@@ -0,0 +1,91 @@
1+# Testing: Rand, Check, Prop and raztest
2+
3+A plan for testing raz programs: three library modules and a tool, all
4+shipped with raz, so that a public project needs nothing past raz, make
5+and sh to use them. Each step is a commit of its own, useful alone;
6+after each, stop for review.
7+
8+## Why here
9+
10+- Every program has the library, and a program's make already builds
11+ `tools/Pretty.raz` for `fmt`; `raztest` is found the same way.
12+- Today ~180 `check`/`expect` helpers are written by hand across ~/r,
13+ four projects carry a PRNG of their own (rjson, pict, sauce, tasta),
14+ and each Makefile hand-rolls its test loop and summary.
15+
16+## The pieces
17+
18+| Piece | Where | Size | What |
19+|---|---|---|---|
20+| `Rand` | `lib/Rand.raz` | ~40 | a seeded generator, splitmix64 |
21+| `Check` | `lib/Check.raz` | ~80 | assertions, failures on stderr, status by `Rt.atExit` |
22+| `Prop` | `lib/Prop.raz` | ~300 | properties and fuzzing over recorded choices, with shrinking |
23+| `raztest` | `tools/Test.raz` | ~700 | finds, builds and runs tests; colour, reports, watch |
24+
25+**Rand.** `init(r, seed)`, `next(r): U64`, `below(r, n)` (no bias),
26+`range(r, lo, hi)`, `seed(): U64` (fresh, from `Sys.random`, to print
27+and replay). No reals until a user needs them.
28+
29+**Check.** `ok(cond, what)`, `str(what, got, want)` (escaped),
30+`int(what, got, want)`, `mute`/`unmute`. A failure is a block on
31+standard error:
32+
33+ FAIL what
34+ got: "..."
35+ want: "..."
36+
37+and a program with failures ends with `Check: F of N failed` and status
38+1, by a procedure registered with `Rt.atExit` (which may call
39+`Rt.exit` itself), so there is no `done` to forget. A passing run says
40+nothing. The status and this text are the whole contract with
41+`raztest`; a bare `./Mod` or a make loop works as before.
42+
43+**Prop.** A property is `proc(var s: Prop.Source)` that draws values
44+(`int(s, lo, hi)`, `bool(s)`, `bytes(s, max, buf)`, `pick(s, n)`) and
45+checks them with `Check`. A case is the bytes it drew (as Hypothesis
46+does), so shrinking works on those bytes (cut chunks, zero, lower) and
47+replays the property: no shrinker per type, no closures. A property
48+reading only `bytes` is a byte fuzzer. While searching, `Check` is
49+muted; the smallest failing case is run again aloud and saved to
50+`test/seeds/<name>`; saved seeds are replayed first, every run.
51+`PROP_SEED` replays one case, `PROP_TIME=60` fuzzes for a minute.
52+
53+**raztest.** Finds modules with `proc main*` in `src/` and `test/*.raz`
54+(`-I`, default `src`); builds with `-Werror`; runs them, `-j n`, a
55+timeout each, `-r pattern`; times each; shows a test's output only when
56+it fails; colour on a terminal unless `CI` or `NO_COLOR`; a summary and
57+the status. `--tap`, `--junit file` (a module is a case, its `FAIL`
58+blocks the failure). `-w` reruns what a changed `.raz` reaches, by
59+polling file times (Sys has no inotify: a gap). `--seed`, `--fuzz 60s`
60+set `PROP_*`. Left out: `.cn`, sh and pty tests, which stay in make. A
61+Makefile uses `raztest` when it's there and its own loop when not.
62+
63+## Steps
64+
65+1. `Rand`, with its self-test.
66+2. `Check`; the library's self-tests use it.
67+3. `raztest`: finding, building, running, `-j`, timeouts, colour,
68+ summary; `test/check.sh`'s library loop uses it. `TOOLS` gains
69+ `Test`; `make install` puts `raztest` on PATH; `doc/raztest.1`.
70+4. `raztest --tap`, `--junit`.
71+5. `raztest -w`.
72+6. `Prop`, with its self-test.
73+7. Volume 2: `Rand`, `Check` and `Prop` go into `doc/raz-lib.md` as
74+ standard library (not core: `conform/` is untouched), with whole
75+ examples, once the first users have settled them; one report
76+ version. `STANDARDS.md` says how a module tests itself with them.
77+8. robbie: `robbie -w`, in its own repository.
78+
79+After the release that brings them: rjson, pict and sauce take `Rand`
80+and `Prop` (rjson's `Random.raz`, JSONTestSuite as seeds; pict's fuzz;
81+sauce's `Fuzz.raz` draws), and projects take `Check` and `raztest` as
82+their files are touched, with no sweep. `desktop.7` names `raztest`
83+once it is installed.
84+
85+## Left out
86+
87+Coverage-guided fuzzing (the compiler would have to count branches);
88+tests in doc comments (no library comment holds a whole example; the
89+report's and the tutorial's are already run); a shared pty harness
90+(belongs with rtui or rterm); `Check` naming the failing line (`Rt`
91+knows places but exports none: a gap).

lib/Rand.raz (history) added +98 -0

@@ -0,0 +1,98 @@
1+(* A seeded random number generator, splitmix64: one U64 of state, a
2+ few steps to a number. The same seed gives the same numbers, on
3+ every machine, so a failing run can be replayed. It is fast and
4+ passes the usual statistical tests, but it is not for secrets.
5+ Needs Sys (for a fresh seed) and Rt. Leaves out reals, shuffles and
6+ other distributions. *)
7+module Rand;
8+import Sys, Rt;
9+
10+type
11+ Rand* = record
12+ state: U64; (* advances by a constant at each draw *)
13+ end;
14+
15+(* Starts r at seed; any seed, 0 too, will do. *)
16+proc init*(var r: Rand; seed: U64);
17+begin
18+ r.state := seed;
19+end;
20+
21+(* The next number, all 64 bits of it. *)
22+proc next*(var r: Rand): U64;
23+var z: U64;
24+begin
25+ INC(r.state, 09E3779B97F4A7C15H);
26+ z := r.state;
27+ z := (z xor (z shr 30)) * 0BF58476D1CE4E5B9H;
28+ z := (z xor (z shr 27)) * 094D049BB133111EBH;
29+ return z xor (z shr 31);
30+end;
31+
32+(* A number from 0 to n - 1, each as likely as another. It draws again
33+ when it falls in the short last run of the 64-bit range, which n
34+ does not divide, so no number is favoured. n = 0 stops the program. *)
35+proc below*(var r: Rand; n: U64): U64;
36+var x, short: U64;
37+begin
38+ if n = 0 then Rt.stop("Rand.below: n is 0"); end;
39+ short := (0 - n) mod n; (* 2^64 mod n numbers are left over *)
40+ x := next(r);
41+ while x < short do x := next(r); end;
42+ return x mod n;
43+end;
44+
45+(* A number from lo to hi, both included; lo must not exceed hi. The
46+ span is taken as a U64, so the whole I64 range works. *)
47+proc range*(var r: Rand; lo, hi: I64): I64;
48+var span: U64;
49+begin
50+ if lo > hi then Rt.stop("Rand.range: lo > hi"); end;
51+ span := U64(hi) - U64(lo);
52+ if span = 0FFFFFFFFFFFFFFFFH then return I64(next(r)); end;
53+ return I64(U64(lo) + below(r, span + 1));
54+end;
55+
56+(* A fresh seed from the kernel, for a program to print and, to replay
57+ a run, to be given back to init. *)
58+proc seed*(): U64;
59+var b: [8]U8; i: INT; s: U64;
60+begin
61+ Sys.random(b);
62+ s := 0;
63+ for i := 0 to 7 do s := s shl 8 | U64(b[i]); end;
64+ return s;
65+end;
66+
67+(* The module's test. *)
68+proc main*;
69+var r, q: Rand; i: INT; x: U64; v: I64; lo, hi: BOOL;
70+begin
71+ init(r, 0);
72+ if (next(r) # 0E220A8397B1DCDAFH) or (next(r) # 06E789E6AA1B965F4H)
73+ or (next(r) # 006C45D188009454FH) then
74+ Rt.stop("Rand.next: splitmix64 from 0");
75+ end;
76+ init(r, 42); init(q, 42);
77+ for i := 1 to 100 do
78+ if next(r) # next(q) then Rt.stop("Rand: same seed, different numbers"); end;
79+ end;
80+ for i := 1 to 1000 do
81+ if below(r, 1) # 0 then Rt.stop("Rand.below 1"); end;
82+ if below(r, 3) >= 3 then Rt.stop("Rand.below 3"); end;
83+ x := below(r, 08000000000000001H);
84+ if x >= 08000000000000001H then Rt.stop("Rand.below 2^63+1"); end;
85+ end;
86+ lo := false; hi := false;
87+ for i := 1 to 1000 do
88+ v := range(r, -2, 2);
89+ if (v < -2) or (v > 2) then Rt.stop("Rand.range -2..2"); end;
90+ lo := lo or (v = -2); hi := hi or (v = 2);
91+ end;
92+ if not (lo and hi) then Rt.stop("Rand.range: ends not reached"); end;
93+ for i := 1 to 100 do v := range(r, -9223372036854775807 - 1, 9223372036854775807); end;
94+ if range(r, 7, 7) # 7 then Rt.stop("Rand.range 7..7"); end;
95+ x := seed();
96+end;
97+
98+end Rand.