raz / commit c9ee56dd

commit
c9ee56dde74b7baf99b0572c079fe54d99f1de4b
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
3a69bc7e
Report 2026-10.36: Rand, Check and Prop in the standard library

Volume 2 gains a section on the three, for testing a program, each
with its interface and a whole example that make check runs. They are
standard library, not core: appendix A says so, and conform/ is
untouched. The language is unchanged and no program breaks.
raz-lib(3) carries them; STANDARDS.md says how a module tests itself;
raztest(1) says --fuzz's timeout is for one property. Step 7 of
TESTING-PLAN.md, which marks steps 1 to 7 done.

make check: 1045 passed, 0 failed.

patch · log

9 files changed, +190 -7

Makefile (history) modified +1 -1

@@ -96,7 +96,7 @@
9696 # raz-lib(3), the library's manual page, made by tools/Doc.raz from
9797 # its sources, in the order of doc/raz-lib.md
9898 LIBDOC = Rt Mem Heap Arena Lock Thread.linux-x86_64 Out Sys.linux-x86_64 \
99- Str Bytes.x86_64 Buf Fmt List Map Vec Sort Math Files Opt Width C
99+ Str Bytes.x86_64 Buf Fmt List Map Vec Sort Math Files Opt Width Rand Check Prop C
100100
101101 man: build/raz-lib.3
102102

STANDARDS.md (history) modified +10 -0

@@ -51,3 +51,13 @@
5151
5252 Anything else that takes `^T` and neither keeps it nor needs one of the
5353 above is a mistake, and its callers' `ADR`s are noise.
54+
55+## A module tests itself
56+
57+A library or support module has its own `proc main*` as its test, and
58+`raztest` runs it. It states what it expects with `Check` (`ok`, `str`,
59+`int`), which reports every failure and ends the run with status 1, and
60+tests a property of its code, one that must hold for any input, with
61+`Prop`, drawing bytes or numbers from its `Source`. A test that passes
62+says nothing. The core modules, and those the compiler imports, keep
63+their own checks, since they can't import a standard-library module.

TESTING-PLAN.md modified +7 -0

@@ -68,17 +68,24 @@
6868 ## Steps
6969
7070 1. `Rand`, with its self-test.
71+ Done: `lib/Rand.raz`, splitmix64.
7172 2. `Check`; the library's self-tests use it.
73+ Done: `lib/Check.raz`.
7274 3. `raztest`: finding, building, running, `-j`, timeouts, colour,
7375 summary; `test/check.sh`'s library loop uses it. `TOOLS` gains
7476 `Test`; `make install` puts `raztest` on PATH; `doc/raztest.1`.
77+ Done: `tools/Test.raz`.
7578 4. `raztest --tap`, `--junit`.
79+ Done.
7680 5. `raztest -w`.
81+ Done.
7782 6. `Prop`, with its self-test.
83+ Done: `lib/Prop.raz`.
7884 7. Volume 2: `Rand`, `Check` and `Prop` go into `doc/raz-lib.md` as
7985 standard library (not core: `conform/` is untouched), with whole
8086 examples, once the first users have settled them; one report
8187 version. `STANDARDS.md` says how a module tests itself with them.
88+ Done: report 2026-10.36, `doc/raz-lib.md`, `STANDARDS.md`.
8289 8. robbie: `robbie -w`, in its own repository.
8390
8491 After the release that brings them: rjson, pict and sauce take `Rand`

doc/raz-lib.md (history) modified +159 -1

@@ -4,7 +4,7 @@
44 the language; this one defines the modules that come with it, and
55 refers to the first's sections by number, as "section 6.8". Only the
66 core binds every implementation (appendix A of the first volume); the
7-rest is the standard library.
7+rest is the standard library, `Rand`, `Check` and `Prop` among it.
88
99 Modules written in raz, in the compiler's `lib` directory:
1010
@@ -31,6 +31,9 @@
3131 | `Files` | an argument as a string, and a whole file read or written |
3232 | `Opt` | a command line read through one table, by `getopt`'s rules with long options; its help and its mistakes |
3333 | `Width` | how many columns of a terminal a character, or a string, takes: 0, 1 or 2, as `wcwidth` says |
34+| `Rand` | a seeded random number generator, splitmix64: the same seed, the same numbers; for tests and simulations, not secrets |
35+| `Check` | checks for a test: `ok`, `str` and `int` say what failed on standard error, and the program ends with status 1 if any did |
36+| `Prop` | property tests and fuzzing: a property draws values, a failing case is shrunk and saved, and run first next time |
3437
3538 Lengths, counts, file descriptors, process ids and statuses are
3639 `INT`s, and a character is an `I32`. What may pass an `INT` on a small
@@ -1037,3 +1040,158 @@
10371040 made again by `tools/Widths.raz` for a new version of Unicode; a
10381041 program that imports `Width` and calls neither procedure carries none
10391042 of them.
1043+
1044+**Rand, Check and Prop** are for testing a program, and are standard
1045+library, not core: a target may leave them out. `raztest(1)` builds
1046+and runs the tests that use them.
1047+
1048+`Rand` is a seeded generator of numbers, splitmix64: one `U64` of
1049+state. The same seed gives the same numbers on every machine, so a
1050+run can be replayed. It is not for secrets.
1051+
1052+```
1053+module Rand;
1054+type Rand* = record ... end;
1055+proc init*(var r: Rand; seed: U64); (* starts r at seed; any seed, 0 too *)
1056+proc next*(var r: Rand): U64; (* the next number, all 64 bits of it *)
1057+proc below*(var r: Rand; n: U64): U64; (* 0 to n - 1, each as likely as another; n = 0 stops the program *)
1058+proc range*(var r: Rand; lo, hi: I64): I64; (* lo to hi, both included; lo must not exceed hi *)
1059+proc seed*(): U64; (* a fresh seed from the kernel, to print and give back to init *)
1060+```
1061+
1062+```
1063+module Dice;
1064+import Rand, Check;
1065+
1066+(* Five rolls of a die from seed, as the digits of a number. *)
1067+proc rolls(seed: U64): INT;
1068+var r: Rand.Rand; i, v: INT;
1069+begin
1070+ Rand.init(r, seed);
1071+ for i := 1 to 5 do v := v * 10 + INT(Rand.range(r, 1, 6)); end;
1072+ return v;
1073+end;
1074+
1075+proc main*;
1076+begin
1077+ Check.int("the same seed, the same rolls", rolls(7), rolls(7));
1078+ Check.ok(rolls(7) >= 11111, "every die shows 1 or more");
1079+ Check.ok(rolls(7) <= 66666, "every die shows 6 or less");
1080+end;
1081+
1082+end Dice.
1083+```
1084+
1085+`Check` says what a test expects. A check that fails writes `FAIL`
1086+and its words on standard error, with what it got and what it wanted
1087+where they differ; the program goes on. At its end, if any check
1088+failed, it says how many and exits with status 1, and a run with none
1089+failed says nothing and keeps its own status. No call is needed at the
1090+end: the first check asks `Rt.atExit` to make it. A program that stops
1091+after a check failed, by `Rt.stop` or a failed check of the language,
1092+ends with status 1 too. `mute` is for `Prop`, which searches with the
1093+checks silenced.
1094+
1095+```
1096+module Check;
1097+proc ok*(cond: BOOL; what: []U8); (* fails, saying what, when cond is false *)
1098+proc str*(what, got, want: []U8); (* fails when got is not want; shows both, escaped, and the byte where they differ *)
1099+proc int*(what: []U8; got, want: I64); (* fails when got is not want; shows both *)
1100+proc mute*(on: BOOL); (* while on, checks say nothing and are not counted *)
1101+proc mutedFailures*(): INT; (* how many failed since mute was turned on *)
1102+```
1103+
1104+```
1105+module Digits;
1106+import Check;
1107+
1108+(* How many decimal digits n, not negative, takes. *)
1109+proc count(n: INT): INT;
1110+var d: INT;
1111+begin
1112+ d := 1;
1113+ while n >= 10 do n := n div 10; INC(d); end;
1114+ return d;
1115+end;
1116+
1117+proc main*;
1118+begin
1119+ Check.int("0 is one digit", count(0), 1);
1120+ Check.int("9 is one digit", count(9), 1);
1121+ Check.int("10 is two digits", count(10), 2);
1122+ Check.ok(count(1000000) = 7, "a million is seven digits");
1123+end;
1124+
1125+end Digits.
1126+```
1127+
1128+`Prop` tests a property: a procedure that draws values and checks
1129+them with `Check`. A case is the bytes the property drew, and
1130+`Source` gives them as a number, a choice, a flag or a string of
1131+bytes; fewer and smaller bytes give simpler values. When a case
1132+fails, `Prop` shrinks it by changing its bytes and running the
1133+property again, so no type needs a shrinker of its own, and it
1134+saves the case. A property that draws only bytes is a fuzzer.
1135+
1136+```
1137+module Prop;
1138+type Source* = record ... end;
1139+type Property* = proc(var s: Source); (* draws from s, checks with Check *)
1140+proc bits*(var s: Source): U64; (* 64 bits, from 8 bytes, the first the highest *)
1141+proc int*(var s: Source; lo, hi: I64): I64; (* lo to hi, both included; zeros give lo *)
1142+proc pick*(var s: Source; n: INT): INT; (* 0 to n - 1; n at least 1 *)
1143+proc bool*(var s: Source): BOOL;
1144+proc bytes*(var s: Source; max: INT; var b: Buf.Buf); (* b, emptied, then given at most max bytes *)
1145+proc check*(name: []U8; p: Property); (* runs p on the saved cases, then on fresh ones *)
1146+```
1147+
1148+`check` runs `p` on each case saved in the directory `$PROP_DIR/name`
1149+(`test/seeds/name` if `PROP_DIR` is unset), then on `$PROP_CASES`
1150+fresh cases (100 if unset), from the seed `$PROP_SEED` or a new one,
1151+or for as many seconds as `$PROP_TIME` says, if it is set. Each is a
1152+whole number. A property that holds says nothing and writes no file.
1153+The first case that fails is shrunk and saved in a file named by its
1154+hash; standard error says the seed and the file; the shrunk case runs
1155+once more with `Check` heard, and the program ends with status 1. A
1156+saved case is run first the next time, so a failure stays found until
1157+it is mended; keep the directory with the tests. A case that stops
1158+the program, by `Rt.stop`, a failed check of the language or a signal,
1159+can't be shrunk; it is saved as it was, when the program ends.
1160+`raztest --seed` and `--fuzz` set `PROP_SEED` and `PROP_TIME`.
1161+
1162+```
1163+module Reverse;
1164+import Prop, Check, Buf, Heap;
1165+
1166+var
1167+ b, was: Buf.Buf; (* the bytes drawn, and a copy of them *)
1168+
1169+(* Turns s round, in place. *)
1170+proc reverse(s: []U8);
1171+var i: INT; c: U8;
1172+begin
1173+ for i := 0 to LEN(s) div 2 - 1 do
1174+ c := s[i]; s[i] := s[LEN(s) - 1 - i]; s[LEN(s) - 1 - i] := c;
1175+ end;
1176+end;
1177+
1178+(* Reversing any string twice gives it back. *)
1179+proc twice(var s: Prop.Source);
1180+begin
1181+ Prop.bytes(s, 64, b);
1182+ Buf.reset(was);
1183+ Buf.str(was, Buf.bytes(b));
1184+ reverse(Buf.bytes(b));
1185+ reverse(Buf.bytes(b));
1186+ Check.str("reversed twice", Buf.bytes(b), Buf.bytes(was));
1187+end;
1188+
1189+proc main*;
1190+begin
1191+ Buf.init(b, Heap.heap);
1192+ Buf.init(was, Heap.heap);
1193+ Prop.check("reverse", twice);
1194+end;
1195+
1196+end Reverse.
1197+```

doc/raz.md (history) modified +3 -3

@@ -13,7 +13,7 @@
1313 This report defines the language. It is short on purpose. Whatever it
1414 doesn't allow, raz doesn't have.
1515
16-This is version 2026-10.35 of the report; section 11 says where each
16+This is version 2026-10.36 of the report; section 11 says where each
1717 version's changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose,
1818 for a language anyone can build again. They break programs written
1919 for 2026-10.10 and before: for 2026-10.9, say, the report of compiler
@@ -1794,8 +1794,8 @@
17941794 language, reals aside, is the core of the library: `Rt`, `Out`, `Mem`,
17951795 `Arena`, `Lock`, `Str` and `Buf`, and of `Sys` what they need. A program
17961796 that keeps to the core runs on every target. The rest of the library
1797-(`Heap`, `Fmt`, `List`, `Map`, `Vec`, `Sort`, `Math`, `Files`, `Opt`, `Width`
1798-and the rest of `Sys`) is the standard library: a target may leave a module
1797+(`Heap`, `Fmt`, `List`, `Map`, `Vec`, `Sort`, `Math`, `Files`, `Opt`, `Width`,
1798+`Rand`, `Check`, `Prop` and the rest of `Sys`) is the standard library: a target may leave a module
17991799 out, and one that has it gives what `doc/raz-lib.md` says. `conform/` tests
18001800 the core only.
18011801

doc/raztest.1 (history) modified +4 -0

@@ -190,6 +190,10 @@
190190 + 60, unless
191191 .Fl t
192192 is given.
193+That is enough for one property in a test; a test with several
194+properties needs
195+.Fl t
196+to say more.
193197 .It Fl h , Fl \-help
194198 Say the options, and exit.
195199 .El

doc/report-history.md (history) modified +4 -0

@@ -230,3 +230,7 @@
230230 Then `Str` gains `base64Size`, `base64` and `unbase64`, RFC 4648's
231231 base64 with padding, decoded strictly (volume 2). It breaks no
232232 program.
233+- **2026-10.36.** Three modules join the standard library: `Rand`, a
234+ seeded random number generator; `Check`, checks for a test; and
235+ `Prop`, property tests and fuzzing with shrinking (volume 2). They are
236+ not core. The language is unchanged, and no program breaks.

src/Raz.raz (history) modified +1 -1

@@ -32,7 +32,7 @@
3232 import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf;
3333
3434 const
35- Report = "2026-10.35"; (* the version of doc/raz.md it follows *)
35+ Report = "2026-10.36"; (* the version of doc/raz.md it follows *)
3636 MemfdCreate = 319; (* the system call *)
3737 MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *)
3838 Execve = 59;

tools/Doc.raz (history) modified +1 -1

@@ -19,7 +19,7 @@
1919 module Doc;
2020 import Out, Rt, Str, Buf, Heap, Scan, Files;
2121
22-const Date = "October 14, 2026"; (* raz-lib(3)'s, when the library last changed *)
22+const Date = "October 22, 2026"; (* raz-lib(3)'s, when the library last changed *)
2323
2424 var
2525 sc: Scan.Scanner; (* the text being read, at the current symbol *)