From c9ee56dde74b7baf99b0572c079fe54d99f1de4b Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 22 Oct 2026 14:03:59 +0100 Subject: [PATCH] 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. --- diff --git a/Makefile b/Makefile index fa2cbdc..6dae1ea 100644 --- a/Makefile +++ b/Makefile @@ -96,7 +96,7 @@ # raz-lib(3), the library's manual page, made by tools/Doc.raz from # its sources, in the order of doc/raz-lib.md LIBDOC = Rt Mem Heap Arena Lock Thread.linux-x86_64 Out Sys.linux-x86_64 \ - Str Bytes.x86_64 Buf Fmt List Map Vec Sort Math Files Opt Width C + Str Bytes.x86_64 Buf Fmt List Map Vec Sort Math Files Opt Width Rand Check Prop C man: build/raz-lib.3 diff --git a/STANDARDS.md b/STANDARDS.md index 4d6100f..6b5f3ac 100644 --- a/STANDARDS.md +++ b/STANDARDS.md @@ -51,3 +51,13 @@ Anything else that takes `^T` and neither keeps it nor needs one of the above is a mistake, and its callers' `ADR`s are noise. + +## A module tests itself + +A library or support module has its own `proc main*` as its test, and +`raztest` runs it. It states what it expects with `Check` (`ok`, `str`, +`int`), which reports every failure and ends the run with status 1, and +tests a property of its code, one that must hold for any input, with +`Prop`, drawing bytes or numbers from its `Source`. A test that passes +says nothing. The core modules, and those the compiler imports, keep +their own checks, since they can't import a standard-library module. diff --git a/TESTING-PLAN.md b/TESTING-PLAN.md index cf29f0a..c282c08 100644 --- a/TESTING-PLAN.md +++ b/TESTING-PLAN.md @@ -68,17 +68,24 @@ ## Steps 1. `Rand`, with its self-test. + Done: `lib/Rand.raz`, splitmix64. 2. `Check`; the library's self-tests use it. + Done: `lib/Check.raz`. 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`. + Done: `tools/Test.raz`. 4. `raztest --tap`, `--junit`. + Done. 5. `raztest -w`. + Done. 6. `Prop`, with its self-test. + Done: `lib/Prop.raz`. 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. + Done: report 2026-10.36, `doc/raz-lib.md`, `STANDARDS.md`. 8. robbie: `robbie -w`, in its own repository. After the release that brings them: rjson, pict and sauce take `Rand` diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 4b2f3ac..f9c5b53 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -4,7 +4,7 @@ the language; this one defines the modules that come with it, and refers to the first's sections by number, as "section 6.8". Only the core binds every implementation (appendix A of the first volume); the -rest is the standard library. +rest is the standard library, `Rand`, `Check` and `Prop` among it. Modules written in raz, in the compiler's `lib` directory: @@ -31,6 +31,9 @@ | `Files` | an argument as a string, and a whole file read or written | | `Opt` | a command line read through one table, by `getopt`'s rules with long options; its help and its mistakes | | `Width` | how many columns of a terminal a character, or a string, takes: 0, 1 or 2, as `wcwidth` says | +| `Rand` | a seeded random number generator, splitmix64: the same seed, the same numbers; for tests and simulations, not secrets | +| `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 | +| `Prop` | property tests and fuzzing: a property draws values, a failing case is shrunk and saved, and run first next time | Lengths, counts, file descriptors, process ids and statuses are `INT`s, and a character is an `I32`. What may pass an `INT` on a small @@ -1037,3 +1040,158 @@ made again by `tools/Widths.raz` for a new version of Unicode; a program that imports `Width` and calls neither procedure carries none of them. + +**Rand, Check and Prop** are for testing a program, and are standard +library, not core: a target may leave them out. `raztest(1)` builds +and runs the tests that use them. + +`Rand` is a seeded generator of numbers, splitmix64: one `U64` of +state. The same seed gives the same numbers on every machine, so a +run can be replayed. It is not for secrets. + +``` +module Rand; +type Rand* = record ... end; +proc init*(var r: Rand; seed: U64); (* starts r at seed; any seed, 0 too *) +proc next*(var r: Rand): U64; (* the next number, all 64 bits of it *) +proc below*(var r: Rand; n: U64): U64; (* 0 to n - 1, each as likely as another; n = 0 stops the program *) +proc range*(var r: Rand; lo, hi: I64): I64; (* lo to hi, both included; lo must not exceed hi *) +proc seed*(): U64; (* a fresh seed from the kernel, to print and give back to init *) +``` + +``` +module Dice; +import Rand, Check; + +(* Five rolls of a die from seed, as the digits of a number. *) +proc rolls(seed: U64): INT; +var r: Rand.Rand; i, v: INT; +begin + Rand.init(r, seed); + for i := 1 to 5 do v := v * 10 + INT(Rand.range(r, 1, 6)); end; + return v; +end; + +proc main*; +begin + Check.int("the same seed, the same rolls", rolls(7), rolls(7)); + Check.ok(rolls(7) >= 11111, "every die shows 1 or more"); + Check.ok(rolls(7) <= 66666, "every die shows 6 or less"); +end; + +end Dice. +``` + +`Check` says what a test expects. A check that fails writes `FAIL` +and its words on standard error, with what it got and what it wanted +where they differ; the program goes on. At its end, if any check +failed, it says how many and exits with status 1, and a run with none +failed says nothing and keeps its own status. No call is needed at the +end: the first check asks `Rt.atExit` to make it. A program that stops +after a check failed, by `Rt.stop` or a failed check of the language, +ends with status 1 too. `mute` is for `Prop`, which searches with the +checks silenced. + +``` +module Check; +proc ok*(cond: BOOL; what: []U8); (* fails, saying what, when cond is false *) +proc str*(what, got, want: []U8); (* fails when got is not want; shows both, escaped, and the byte where they differ *) +proc int*(what: []U8; got, want: I64); (* fails when got is not want; shows both *) +proc mute*(on: BOOL); (* while on, checks say nothing and are not counted *) +proc mutedFailures*(): INT; (* how many failed since mute was turned on *) +``` + +``` +module Digits; +import Check; + +(* How many decimal digits n, not negative, takes. *) +proc count(n: INT): INT; +var d: INT; +begin + d := 1; + while n >= 10 do n := n div 10; INC(d); end; + return d; +end; + +proc main*; +begin + Check.int("0 is one digit", count(0), 1); + Check.int("9 is one digit", count(9), 1); + Check.int("10 is two digits", count(10), 2); + Check.ok(count(1000000) = 7, "a million is seven digits"); +end; + +end Digits. +``` + +`Prop` tests a property: a procedure that draws values and checks +them with `Check`. A case is the bytes the property drew, and +`Source` gives them as a number, a choice, a flag or a string of +bytes; fewer and smaller bytes give simpler values. When a case +fails, `Prop` shrinks it by changing its bytes and running the +property again, so no type needs a shrinker of its own, and it +saves the case. A property that draws only bytes is a fuzzer. + +``` +module Prop; +type Source* = record ... end; +type Property* = proc(var s: Source); (* draws from s, checks with Check *) +proc bits*(var s: Source): U64; (* 64 bits, from 8 bytes, the first the highest *) +proc int*(var s: Source; lo, hi: I64): I64; (* lo to hi, both included; zeros give lo *) +proc pick*(var s: Source; n: INT): INT; (* 0 to n - 1; n at least 1 *) +proc bool*(var s: Source): BOOL; +proc bytes*(var s: Source; max: INT; var b: Buf.Buf); (* b, emptied, then given at most max bytes *) +proc check*(name: []U8; p: Property); (* runs p on the saved cases, then on fresh ones *) +``` + +`check` runs `p` on each case saved in the directory `$PROP_DIR/name` +(`test/seeds/name` if `PROP_DIR` is unset), then on `$PROP_CASES` +fresh cases (100 if unset), from the seed `$PROP_SEED` or a new one, +or for as many seconds as `$PROP_TIME` says, if it is set. Each is a +whole number. A property that holds says nothing and writes no file. +The first case that fails is shrunk and saved in a file named by its +hash; standard error says the seed and the file; the shrunk case runs +once more with `Check` heard, and the program ends with status 1. A +saved case is run first the next time, so a failure stays found until +it is mended; keep the directory with the tests. A case that stops +the program, by `Rt.stop`, a failed check of the language or a signal, +can't be shrunk; it is saved as it was, when the program ends. +`raztest --seed` and `--fuzz` set `PROP_SEED` and `PROP_TIME`. + +``` +module Reverse; +import Prop, Check, Buf, Heap; + +var + b, was: Buf.Buf; (* the bytes drawn, and a copy of them *) + +(* Turns s round, in place. *) +proc reverse(s: []U8); +var i: INT; c: U8; +begin + for i := 0 to LEN(s) div 2 - 1 do + c := s[i]; s[i] := s[LEN(s) - 1 - i]; s[LEN(s) - 1 - i] := c; + end; +end; + +(* Reversing any string twice gives it back. *) +proc twice(var s: Prop.Source); +begin + Prop.bytes(s, 64, b); + Buf.reset(was); + Buf.str(was, Buf.bytes(b)); + reverse(Buf.bytes(b)); + reverse(Buf.bytes(b)); + Check.str("reversed twice", Buf.bytes(b), Buf.bytes(was)); +end; + +proc main*; +begin + Buf.init(b, Heap.heap); + Buf.init(was, Heap.heap); + Prop.check("reverse", twice); +end; + +end Reverse. +``` diff --git a/doc/raz.md b/doc/raz.md index f30d792..43c7c48 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -13,7 +13,7 @@ This report defines the language. It is short on purpose. Whatever it doesn't allow, raz doesn't have. -This is version 2026-10.35 of the report; section 11 says where each +This is version 2026-10.36 of the report; section 11 says where each version's changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler @@ -1794,8 +1794,8 @@ language, reals aside, is the core of the library: `Rt`, `Out`, `Mem`, `Arena`, `Lock`, `Str` and `Buf`, and of `Sys` what they need. A program that keeps to the core runs on every target. The rest of the library -(`Heap`, `Fmt`, `List`, `Map`, `Vec`, `Sort`, `Math`, `Files`, `Opt`, `Width` -and the rest of `Sys`) is the standard library: a target may leave a module +(`Heap`, `Fmt`, `List`, `Map`, `Vec`, `Sort`, `Math`, `Files`, `Opt`, `Width`, +`Rand`, `Check`, `Prop` and the rest of `Sys`) is the standard library: a target may leave a module out, and one that has it gives what `doc/raz-lib.md` says. `conform/` tests the core only. diff --git a/doc/raztest.1 b/doc/raztest.1 index 1b0912e..f598476 100644 --- a/doc/raztest.1 +++ b/doc/raztest.1 @@ -190,6 +190,10 @@ + 60, unless .Fl t is given. +That is enough for one property in a test; a test with several +properties needs +.Fl t +to say more. .It Fl h , Fl \-help Say the options, and exit. .El diff --git a/doc/report-history.md b/doc/report-history.md index 608d4c3..77c74bc 100644 --- a/doc/report-history.md +++ b/doc/report-history.md @@ -230,3 +230,7 @@ Then `Str` gains `base64Size`, `base64` and `unbase64`, RFC 4648's base64 with padding, decoded strictly (volume 2). It breaks no program. +- **2026-10.36.** Three modules join the standard library: `Rand`, a + seeded random number generator; `Check`, checks for a test; and + `Prop`, property tests and fuzzing with shrinking (volume 2). They are + not core. The language is unchanged, and no program breaks. diff --git a/src/Raz.raz b/src/Raz.raz index 2c2324a..faafc38 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -32,7 +32,7 @@ import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf; const - Report = "2026-10.35"; (* the version of doc/raz.md it follows *) + Report = "2026-10.36"; (* the version of doc/raz.md it follows *) MemfdCreate = 319; (* the system call *) MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *) Execve = 59; diff --git a/tools/Doc.raz b/tools/Doc.raz index 3cbdffa..f316034 100644 --- a/tools/Doc.raz +++ b/tools/Doc.raz @@ -19,7 +19,7 @@ module Doc; import Out, Rt, Str, Buf, Heap, Scan, Files; -const Date = "October 14, 2026"; (* raz-lib(3)'s, when the library last changed *) +const Date = "October 22, 2026"; (* raz-lib(3)'s, when the library last changed *) var sc: Scan.Scanner; (* the text being read, at the current symbol *)