| | @@ -4,7 +4,7 @@ |
| 4 | 4 | the language; this one defines the modules that come with it, and |
| 5 | 5 | refers to the first's sections by number, as "section 6.8". Only the |
| 6 | 6 | 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. |
| 8 | 8 | |
| 9 | 9 | Modules written in raz, in the compiler's `lib` directory: |
| 10 | 10 | |
| | @@ -31,6 +31,9 @@ |
| 31 | 31 | | `Files` | an argument as a string, and a whole file read or written | |
| 32 | 32 | | `Opt` | a command line read through one table, by `getopt`'s rules with long options; its help and its mistakes | |
| 33 | 33 | | `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 | |
| 34 | 37 | |
| 35 | 38 | Lengths, counts, file descriptors, process ids and statuses are |
| 36 | 39 | `INT`s, and a character is an `I32`. What may pass an `INT` on a small |
| | @@ -1037,3 +1040,158 @@ |
| 1037 | 1040 | made again by `tools/Widths.raz` for a new version of Unicode; a |
| 1038 | 1041 | program that imports `Width` and calls neither procedure carries none |
| 1039 | 1042 | 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 | +``` |