raz / commit 280d4d6f

commit
280d4d6fd91d8bbd9b9d2c50f1ce9b80ea3bdf53
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
6603c911
Check: a test's checks, said on standard error, its status at exit

Check gives ok, str (escaped, with the first byte that differs) and
int. A failure is reported and counted and the program goes on; at
exit, by Rt.atExit, it says how many failed and ends with status 1.
It needs only Rt, Sys and Out, so any module may use it.
test/run/CheckFail shows the report and the status when main returns.

The tests of Math, Opt, Width, Thread and Rand use it. The core and
the modules the compiler imports keep their own checks. Step 2 of
TESTING-PLAN.md.

make check: 1026 passed, 1 failed, not this step's: raz --version
says report 2026-10.34 while doc/raz.md is at 10.35.

patch · log

12 files changed, +326 -80

lib/Check.raz (history) added +213 -0

@@ -0,0 +1,213 @@
1+(* Checks for a test: a program says what it expects, and a check that
2+ fails writes what was got and what was wanted on standard error.
3+ The program goes on, and at its end, if any check failed, it says
4+ how many and exits with status 1, by a procedure registered with
5+ Rt.atExit at the first check; so there is no call to remember at the
6+ end, and a run with no failures says nothing. A program that stops
7+ otherwise after a check failed, by Rt.stop or a failed check of the
8+ language, ends with status 1 too.
9+
10+ FAIL what
11+ got: "a\nb"
12+ want: "a\tb"
13+ at byte 1
14+
15+ Needs only Rt, Sys and Out, so that any library module may use it
16+ without a cycle. Leaves out threads (the counts are not safe to
17+ change from several at once), reals, and naming the failing line. *)
18+module Check;
19+import Rt, Sys, Out;
20+
21+const
22+ Hex = "0123456789ABCDEF"; (* the digits of base 16 *)
23+
24+var
25+ checks: INT; (* checks made *)
26+ failures: INT; (* of them, failed *)
27+ registered: BOOL; (* the exit procedure is on Rt's list *)
28+ sink: proc(s: []U8); (* where text goes, or nil for standard error; a test sets it *)
29+
30+(* Writes s to standard error, trying again after a short or interrupted write. *)
31+proc put(s: []U8);
32+var n: INT;
33+begin
34+ if sink # nil then sink(s); return; end;
35+ while LEN(s) > 0 do
36+ n := Sys.write(Sys.stderr, s);
37+ if n > 0 then s := s[n..LEN(s)];
38+ elsif n # Sys.errIntr then break; (* closed or failed: give up *)
39+ end;
40+ end;
41+end;
42+
43+(* Writes the decimal digits of v, with a minus sign if it is below 0. *)
44+proc putInt(v: I64);
45+var d: [20]U8; i: INT; u: U64;
46+begin
47+ u := U64(v);
48+ if v < 0 then u := 0 - u; end; (* the magnitude, right for the smallest I64 too *)
49+ i := LEN(d);
50+ repeat
51+ DEC(i);
52+ d[i] := U8('0' + INT(u mod 10));
53+ u := u div 10;
54+ until u = 0;
55+ if v < 0 then put("-"); end;
56+ put(d[i..LEN(d)]);
57+end;
58+
59+(* Writes the two uppercase hex digits of c, after the backslash and x. *)
60+proc putHex(c: U8);
61+var d: [4]U8;
62+begin
63+ d[0] := '\\'; d[1] := 'x';
64+ d[2] := Hex[INT(c shr 4)];
65+ d[3] := Hex[INT(c & 15)];
66+ put(d);
67+end;
68+
69+(* Writes s between quotes, with the quote, backslash and bytes below
70+ 32 or 127 as escapes; the bytes from 128 are written as they are,
71+ so UTF-8 reads. *)
72+proc putQuoted(s: []U8);
73+var i, from: INT; c: U8;
74+begin
75+ put("\"");
76+ from := 0; (* the start of the run not yet written *)
77+ for i := 0 to LEN(s) - 1 do
78+ c := s[i];
79+ if (c >= 32) and (c # 127) and (c # '"') and (c # '\\') then continue; end;
80+ put(s[from..i]);
81+ from := i + 1;
82+ if c = '"' then put("\\\"");
83+ elsif c = '\\' then put("\\\\");
84+ elsif c = 10 then put("\\n");
85+ elsif c = 9 then put("\\t");
86+ elsif c = 13 then put("\\r");
87+ else putHex(c);
88+ end;
89+ end;
90+ put(s[from..LEN(s)]);
91+ put("\"");
92+end;
93+
94+(* The index of the first byte where a and b differ, or the shorter
95+ length if one is a prefix of the other. *)
96+proc firstDiff(a, b: []U8): INT;
97+var i, n: INT;
98+begin
99+ n := LEN(a);
100+ if LEN(b) < n then n := LEN(b); end;
101+ i := 0;
102+ while (i < n) and (a[i] = b[i]) do INC(i); end;
103+ return i;
104+end;
105+
106+(* The procedure Rt runs at the end: says how many checks failed, if
107+ any did, and ends the program with status 1. *)
108+proc report;
109+begin
110+ if failures = 0 then return; end;
111+ put("Check: "); putInt(failures); put(" of "); putInt(checks); put(" failed\n");
112+ Rt.exit(1);
113+end;
114+
115+(* Counts a check, and the first time registers report to run at exit. *)
116+proc count;
117+begin
118+ if not registered then
119+ if Rt.atExit(report) # nil then Rt.stop("Check: no room for the exit procedure"); end;
120+ registered := true;
121+ end;
122+ INC(checks);
123+end;
124+
125+(* Counts a failure and begins its report with the FAIL line. Standard
126+ output is flushed first, so the two come in order. *)
127+proc fail(what: []U8);
128+begin
129+ INC(failures);
130+ Out.flush;
131+ put("FAIL "); put(what); put("\n");
132+end;
133+
134+(* Fails, saying what, when cond is false. *)
135+proc ok*(cond: BOOL; what: []U8);
136+begin
137+ count;
138+ if not cond then fail(what); end;
139+end;
140+
141+(* Fails, saying what, when got is not want; shows both, escaped, and
142+ where they first differ. *)
143+proc str*(what, got, want: []U8);
144+var at: INT;
145+begin
146+ count;
147+ at := firstDiff(got, want);
148+ if (at = LEN(got)) and (at = LEN(want)) then return; end;
149+ fail(what);
150+ put(" got: "); putQuoted(got); put("\n");
151+ put(" want: "); putQuoted(want); put("\n");
152+ put(" at byte "); putInt(at); put("\n");
153+end;
154+
155+(* Fails, saying what, when got is not want; shows both. *)
156+proc int*(what: []U8; got, want: I64);
157+begin
158+ count;
159+ if got = want then return; end;
160+ fail(what);
161+ put(" got: "); putInt(got); put("\n");
162+ put(" want: "); putInt(want); put("\n");
163+end;
164+
165+var
166+ cap: [512]U8; (* the test's capture of what put was given *)
167+ capN: INT; (* bytes in cap *)
168+
169+(* The test's sink: appends s to cap. *)
170+proc capture(s: []U8);
171+var i: INT;
172+begin
173+ for i := 0 to LEN(s) - 1 do cap[capN] := s[i]; INC(capN); end;
174+end;
175+
176+(* Runs f with its output captured, and checks that it is want. *)
177+proc expect(what: []U8; f: proc; want: []U8);
178+begin
179+ capN := 0;
180+ f;
181+ str(what, cap[0..capN], want);
182+end;
183+
184+proc tInt0; begin putInt(0); end;
185+proc tIntMin; begin putInt(-9223372036854775807 - 1); end;
186+proc tIntMax; begin putInt(9223372036854775807); end;
187+proc tIntNeg; begin putInt(-420); end;
188+proc tPlain; begin putQuoted("héllo"); end;
189+proc tEmpty; begin putQuoted(""); end;
190+proc tEsc; begin putQuoted("a\"b\\c\nd\te\rf\x01g\x7Fh\x1Fi"); end;
191+
192+(* The module's test: private formatting through a captured sink, then
193+ the public checks, all passing, as a failing one would end the run. *)
194+proc main*;
195+begin
196+ sink := capture;
197+ expect("putInt 0", tInt0, "0");
198+ expect("putInt smallest", tIntMin, "-9223372036854775808");
199+ expect("putInt largest", tIntMax, "9223372036854775807");
200+ expect("putInt negative", tIntNeg, "-420");
201+ expect("quote plain and UTF-8", tPlain, "\"héllo\"");
202+ expect("quote empty", tEmpty, "\"\"");
203+ expect("quote escapes", tEsc, "\"a\\\"b\\\\c\\nd\\te\\rf\\x01g\\x7Fh\\x1Fi\"");
204+ ok(firstDiff("abc", "abd") = 2, "firstDiff differs");
205+ ok(firstDiff("ab", "abc") = 2, "firstDiff prefix");
206+ ok(firstDiff("", "") = 0, "firstDiff empty");
207+ sink := nil;
208+ ok(true, "ok passes");
209+ str("str passes", "same", "same");
210+ int("int passes", -5, -5);
211+ if (checks # 10 + 3) or (failures # 0) then Rt.stop("Check: counts are wrong"); end;
212+end;
213+end Check.

lib/Math.raz (history) modified +34 -38

@@ -8,7 +8,7 @@
88 Left out: the hyperbolic functions, fmod and hypot, until a program
99 needs them. *)
1010 module Math;
11-import Rt;
11+import Check;
1212
1313 const
1414 pi* = 3.141592653589793; (* the F64 nearest to pi *)
@@ -925,25 +925,22 @@
925925 proc testToReal;
926926 var b: [900]U8; i: INT;
927927 begin
928- if not reads("2", 2.0, true) or not reads("-0.5", -0.5, true) or not reads("1.5e-3", 1.5E-3, true)
929- or not reads("+1.25E+2", 125.0, true) or not reads("0.1", 0.1, true)
930- or not reads("1.7976931348623157E308", 1.7976931348623157E308, true)
931- or not reads("4.9e-324", fromBits(1), true) or not reads("1e-400", 0.0, true)
932- or not reads("-0", -0.0, true) or not reads("9007199254740993", 9007199254740992.0, true)
933- or not reads("1e309", 0.0, false) or not reads("1.8E308", 0.0, false)
934- then Rt.stop("Math.toReal"); end;
935- if not reads("", 0.0, false) or not reads("-", 0.0, false) or not reads("1.", 0.0, false)
936- or not reads(".5", 0.0, false) or not reads("1e", 0.0, false) or not reads("1.5.2", 0.0, false)
937- or not reads(" 1", 0.0, false) or not reads("1x", 0.0, false) or not reads("0x10", 0.0, false)
938- then Rt.stop("Math.toReal: not a real"); end;
928+ Check.ok(reads("2", 2.0, true) and reads("-0.5", -0.5, true) and reads("1.5e-3", 1.5E-3, true)
929+ and reads("+1.25E+2", 125.0, true) and reads("0.1", 0.1, true)
930+ and reads("1.7976931348623157E308", 1.7976931348623157E308, true)
931+ and reads("4.9e-324", fromBits(1), true) and reads("1e-400", 0.0, true)
932+ and reads("-0", -0.0, true) and reads("9007199254740993", 9007199254740992.0, true)
933+ and reads("1e309", 0.0, false) and reads("1.8E308", 0.0, false), "Math.toReal");
934+ Check.ok(reads("", 0.0, false) and reads("-", 0.0, false) and reads("1.", 0.0, false)
935+ and reads(".5", 0.0, false) and reads("1e", 0.0, false) and reads("1.5.2", 0.0, false)
936+ and reads(" 1", 0.0, false) and reads("1x", 0.0, false) and reads("0x10", 0.0, false), "Math.toReal: not a real");
939937 (* 2^53 + 1, then 801 zeros and a 1: just above the half between two
940938 F64s, though the 1 is past the digits kept, so it rounds up *)
941939 COPY(b[0..16], "9007199254740993");
942940 for i := 16 to 816 do b[i] := '0'; end;
943941 b[817] := '1';
944942 COPY(b[818..823], "e-802");
945- if not reads(b[0..823], 9007199254740994.0, true) or not reads(b[0..817], 0.0, false)
946- then Rt.stop("Math.toReal: many digits"); end;
943+ Check.ok(reads(b[0..823], 9007199254740994.0, true) and reads(b[0..817], 0.0, false), "Math.toReal: many digits");
947944 end;
948945
949946 proc near(got, want: F64): BOOL;
@@ -962,47 +959,46 @@
962959 inf := 1.0 / 0.0;
963960 nan := inf - inf;
964961 hard := scale(6381956970095103.0, 797); (* of all F64s, the nearest a multiple of pi/2 *)
965- if (sqrt(2.0) # 1.4142135623730951) or (sqrt(0.0) # 0.0) or (sqrt(-1.0) = sqrt(-1.0)) then Rt.stop("Math.sqrt"); end;
966- if (floor(2.5) # 2.0) or (floor(-2.5) # -3.0) or (floor(-2.0) # -2.0) or (floor(1.0E300) # 1.0E300)
967- or not negative(floor(-0.0)) or (floor(nan) = floor(nan)) then Rt.stop("Math.floor"); end;
968- if not near(exp(1.0), 2.718281828459045) or not near(exp(-3.5), 0.0301973834223185)
962+ Check.ok(not ((sqrt(2.0) # 1.4142135623730951) or (sqrt(0.0) # 0.0) or (sqrt(-1.0) = sqrt(-1.0))), "Math.sqrt");
963+ Check.ok(not ((floor(2.5) # 2.0) or (floor(-2.5) # -3.0) or (floor(-2.0) # -2.0) or (floor(1.0E300) # 1.0E300)
964+ or not negative(floor(-0.0)) or (floor(nan) = floor(nan))), "Math.floor");
965+ Check.ok(not (not near(exp(1.0), 2.718281828459045) or not near(exp(-3.5), 0.0301973834223185)
969966 or not near(exp(700.0), 1.0142320547350045E304) or not near(exp(-740.0), 4.2E-322)
970- or (exp(0.0) # 1.0) or (exp(710.0) # inf) or (exp(-746.0) # 0.0) or (exp(-inf) # 0.0) then Rt.stop("Math.exp"); end;
971- if not near(ln(10.0), 2.302585092994046) or not near(ln(0.3), -1.2039728043259361)
967+ or (exp(0.0) # 1.0) or (exp(710.0) # inf) or (exp(-746.0) # 0.0) or (exp(-inf) # 0.0)), "Math.exp");
968+ Check.ok(not (not near(ln(10.0), 2.302585092994046) or not near(ln(0.3), -1.2039728043259361)
972969 or not near(ln(1.0E-310), -713.8013788281542) or (ln(1.0) # 0.0) or (ln(0.0) # -inf)
973- or (ln(inf) # inf) or (ln(-1.0) = ln(-1.0)) then Rt.stop("Math.ln"); end;
974- if not near(sin(1.0), 0.8414709848078965) or not near(sin(-100.0), 0.5063656411097588)
970+ or (ln(inf) # inf) or (ln(-1.0) = ln(-1.0))), "Math.ln");
971+ Check.ok(not (not near(sin(1.0), 0.8414709848078965) or not near(sin(-100.0), 0.5063656411097588)
975972 or not near(sin(pi), 1.2246467991473532E-16) or not near(sin(1.0E6), -0.34999350217129294)
976973 or not near(sin(1.0E22), -0.8522008497671888) or not negative(sin(-0.0))
977- or (sin(inf) = sin(inf)) then Rt.stop("Math.sin"); end;
978- if not near(cos(1.0), 0.5403023058681398) or not near(cos(-100.0), 0.8623188722876839)
974+ or (sin(inf) = sin(inf))), "Math.sin");
975+ Check.ok(not (not near(cos(1.0), 0.5403023058681398) or not near(cos(-100.0), 0.8623188722876839)
979976 or not near(cos(pi / 2.0), 6.123233995736766E-17) or not near(cos(1.0E6), 0.9367521275331447)
980977 or not near(cos(1.0E300), -0.5753861119575491) or not near(cos(hard), -4.687165924254628E-19)
981- or (cos(0.0) # 1.0) or (cos(nan) = cos(nan)) then Rt.stop("Math.cos"); end;
982- if not near(atan2(1.0, 2.0), 0.4636476090008061) or not near(atan2(-3.0, -4.0), -2.498091544796509)
978+ or (cos(0.0) # 1.0) or (cos(nan) = cos(nan))), "Math.cos");
979+ Check.ok(not (not near(atan2(1.0, 2.0), 0.4636476090008061) or not near(atan2(-3.0, -4.0), -2.498091544796509)
983980 or not near(atan2(5.0, 0.1), 1.550798992821746) or not near(atan2(1.0, -1.0E-20), 1.5707963267948966)
984981 or (atan2(0.0, -1.0) # pi) or not negative(atan2(-0.0, 1.0)) or (atan2(-inf, inf) # -pi / 4.0)
985- or not near(atan2(inf, -inf), 2.356194490192345) or (atan2(1.0, 0.0) # pi / 2.0) then Rt.stop("Math.atan2"); end;
986- if (ceil(2.5) # 3.0) or (ceil(-2.5) # -2.0) or not negative(ceil(-0.5)) or (ceil(1.0E300) # 1.0E300)
982+ or not near(atan2(inf, -inf), 2.356194490192345) or (atan2(1.0, 0.0) # pi / 2.0)), "Math.atan2");
983+ Check.ok(not ((ceil(2.5) # 3.0) or (ceil(-2.5) # -2.0) or not negative(ceil(-0.5)) or (ceil(1.0E300) # 1.0E300)
987984 or (round(2.5) # 3.0) or (round(-2.5) # -3.0) or (round(0.49999999999999994) # 0.0)
988- or not negative(round(-0.3)) then Rt.stop("Math.ceil, round"); end;
989- if not near(pow(2.5, 3.7), 29.67413253642086) or not near(pow(0.3, -12.5), 3435455.4096325925)
985+ or not negative(round(-0.3))), "Math.ceil, round");
986+ Check.ok(not (not near(pow(2.5, 3.7), 29.67413253642086) or not near(pow(0.3, -12.5), 3435455.4096325925)
990987 or not near(pow(1.0000001, 1.0E9), 2.6881038582144647E43) or (pow(-2.0, -3.0) # -0.125)
991988 or (pow(10.0, 22.0) # 1.0E22) or (pow(2.0, -1074.0) # scale(1.0, -1074)) or (pow(2.0, 1024.0) # inf)
992989 or (pow(nan, 0.0) # 1.0) or (pow(1.0, nan) # 1.0) or (pow(-1.0, inf) # 1.0) or (pow(0.0, -1.0) # inf)
993- or (pow(-0.0, -1.0) # -inf) or (pow(-2.0, 0.5) = pow(-2.0, 0.5)) then Rt.stop("Math.pow"); end;
994- if (log2(8.0) # 3.0) or (log2(0.5) # -1.0) or not near(log2(10.0), 3.321928094887362)
990+ or (pow(-0.0, -1.0) # -inf) or (pow(-2.0, 0.5) = pow(-2.0, 0.5))), "Math.pow");
991+ Check.ok(not ((log2(8.0) # 3.0) or (log2(0.5) # -1.0) or not near(log2(10.0), 3.321928094887362)
995992 or not near(log2(3.0E-310), -1028.212746914361) or (log10(1000.0) # 3.0) or (log10(1.0E22) # 22.0)
996993 or not near(log10(2.0), 0.3010299956639812) or not near(log10(7.0E-300), -299.15490195998575)
997- or (log2(0.0) # -inf) or (log10(-1.0) = log10(-1.0)) then Rt.stop("Math.log2, log10"); end;
998- if not near(tan(1.0), 1.5574077246549023) or not near(tan(-1.5707963267948966), -1.633123935319537E16)
999- or not near(tan(1.0E22), -1.6287782256068988) or not negative(tan(-0.0)) or (tan(inf) = tan(inf))
1000- then Rt.stop("Math.tan"); end;
1001- if not near(asin(0.3), 0.3046926540153975) or not near(asin(-0.99), -1.4292568534704693)
994+ or (log2(0.0) # -inf) or (log10(-1.0) = log10(-1.0))), "Math.log2, log10");
995+ Check.ok(not (not near(tan(1.0), 1.5574077246549023) or not near(tan(-1.5707963267948966), -1.633123935319537E16)
996+ or not near(tan(1.0E22), -1.6287782256068988) or not negative(tan(-0.0)) or (tan(inf) = tan(inf))), "Math.tan");
997+ Check.ok(not (not near(asin(0.3), 0.3046926540153975) or not near(asin(-0.99), -1.4292568534704693)
1002998 or (asin(1.0) # pi / 2.0) or (asin(1.5) = asin(1.5)) or not near(acos(0.7), 0.7953988301841436)
1003999 or not near(acos(-0.2), 1.7721542475852274) or (acos(1.0) # 0.0) or (acos(-1.0) # pi)
10041000 or not near(atan(0.5), 0.4636476090008061) or not near(atan(-1.0E10), -1.5707963266948965)
1005- or (atan(inf) # pi / 2.0) or not negative(atan(-0.0)) then Rt.stop("Math.asin, acos, atan"); end;
1001+ or (atan(inf) # pi / 2.0) or not negative(atan(-0.0))), "Math.asin, acos, atan");
10061002 testToReal;
10071003 end;
10081004

lib/Opt.raz (history) modified +11 -8

@@ -5,11 +5,11 @@
55 from it, so they can't drift from it.
66
77 Depends on Files for the words, and writes the help and the
8- messages itself, then ends the program.
8+ messages itself, then ends the program. Its test uses Check.
99 Leaves out: options after operands (GNU's reordering), and
1010 completion for concha. *)
1111 module Opt;
12-import Files, Str, Buf, Fmt, Heap, Out, Sys, Rt;
12+import Files, Str, Buf, Fmt, Heap, Sys, Rt, Check;
1313
1414 type
1515 Row* = variant
@@ -534,8 +534,9 @@
534534 return n;
535535 end;
536536
537+(* Runs one case in a child and checks how it ended and what it wrote. *)
537538 proc run(c: Case; n: INT);
538-var outR, outW, errR, errW, pid, status, no, ne: INT; st: Sys.Status; out, err: [4096]U8;
539+var outR, outW, errR, errW, pid, status, no, ne: INT; st: Sys.Status; out, err: [4096]U8; what: Buf.Buf;
539540 begin
540541 outR, outW := Sys.pipe();
541542 errR, errW := Sys.pipe();
@@ -546,11 +547,13 @@
546547 pid, st, status := Sys.wait(pid, true);
547548 no := readAll(outR, out);
548549 ne := readAll(errR, err);
549- if (st # Sys.exited) or (status # c.status) or (out[0..no] # c.out) or (err[0..ne] # c.err) then
550- Fmt.print("case {}: status {}, out:\n{}err:\n{}", [Fmt.int(n), Fmt.int(status), Fmt.str(out[0..no]), Fmt.str(err[0..ne])]);
551- Out.flush;
552- Rt.stop("Opt: a case went wrong");
553- end;
550+ Buf.init(what, Heap.heap);
551+ Fmt.write(what, "Opt case {}", [Fmt.int(n)]);
552+ Check.ok(st = Sys.exited, "Opt: the child did not exit");
553+ Check.int(Buf.bytes(what), status, c.status);
554+ Check.str(Buf.bytes(what), out[0..no], c.out);
555+ Check.str(Buf.bytes(what), err[0..ne], c.err);
556+ Buf.done(what);
554557 end;
555558
556559 proc main*;

lib/Rand.raz (history) modified +13 -14

@@ -2,10 +2,10 @@
22 few steps to a number. The same seed gives the same numbers, on
33 every machine, so a failing run can be replayed. It is fast and
44 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. *)
5+ Needs Sys (for a fresh seed) and Rt; its test uses Check.
6+ Leaves out reals, shuffles and other distributions. *)
77 module Rand;
8-import Sys, Rt;
8+import Sys, Rt, Check;
99
1010 type
1111 Rand* = record
@@ -69,29 +69,28 @@
6969 var r, q: Rand; i: INT; x: U64; v: I64; lo, hi: BOOL;
7070 begin
7171 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;
72+ Check.ok(next(r) = 0E220A8397B1DCDAFH, "Rand.next: splitmix64 from 0, first");
73+ Check.ok(next(r) = 06E789E6AA1B965F4H, "Rand.next: splitmix64 from 0, second");
74+ Check.ok(next(r) = 006C45D188009454FH, "Rand.next: splitmix64 from 0, third");
7675 init(r, 42); init(q, 42);
7776 for i := 1 to 100 do
78- if next(r) # next(q) then Rt.stop("Rand: same seed, different numbers"); end;
77+ Check.ok(next(r) = next(q), "Rand: same seed, different numbers");
7978 end;
8079 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;
80+ Check.int("Rand.below 1", I64(below(r, 1)), 0);
81+ Check.ok(below(r, 3) < 3, "Rand.below 3");
8382 x := below(r, 08000000000000001H);
84- if x >= 08000000000000001H then Rt.stop("Rand.below 2^63+1"); end;
83+ Check.ok(x < 08000000000000001H, "Rand.below 2^63+1");
8584 end;
8685 lo := false; hi := false;
8786 for i := 1 to 1000 do
8887 v := range(r, -2, 2);
89- if (v < -2) or (v > 2) then Rt.stop("Rand.range -2..2"); end;
88+ Check.ok((v >= -2) and (v <= 2), "Rand.range -2..2");
9089 lo := lo or (v = -2); hi := hi or (v = 2);
9190 end;
92- if not (lo and hi) then Rt.stop("Rand.range: ends not reached"); end;
91+ Check.ok(lo and hi, "Rand.range: ends not reached");
9392 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;
93+ Check.int("Rand.range 7..7", range(r, 7, 7), 7);
9594 x := seed();
9695 end;
9796

lib/Thread.linux-x86_64.raz (history) modified +5 -4

@@ -13,9 +13,9 @@
1313 stores C's exit in Rt.cExit; so this module uses no C itself, and
1414 serves both kinds of program. C's thread has a stack and a guard
1515 page of C's; the mapping here holds only the handlers' stack and
16- the Thread. *)
16+ the Thread. Its test uses Check. *)
1717 module Thread;
18-import Sys, Rt;
18+import Sys, Rt, Check;
1919
2020 const
2121 Failed*: ERROR = "can't start a thread";
@@ -232,6 +232,7 @@
232232 return v;
233233 end;
234234
235+(* The module's test: starts threads in waves, and counts that each ran. *)
235236 proc main*;
236237 var n, alive, started, k, i: INT; t: ^Thread; err: ERROR; fail: BOOL;
237238 begin
@@ -258,8 +259,8 @@
258259 for i := 0 to k - 1 do join(wave[i]); end;
259260 started := started + k;
260261 end;
261- if testCount # started then Rt.stop("Thread: a thread didn't run"); end;
262- if fail and (started = LEN(wave)) then Rt.stop("Thread: spawn never failed"); end;
262+ Check.int("Thread: every thread ran", testCount, started);
263+ Check.ok(not (fail and (started = LEN(wave))), "Thread: spawn never failed");
263264 end;
264265
265266 end Thread.

lib/Width.raz (history) modified +21 -14

@@ -19,9 +19,10 @@
1919 zero width joiner, or a flag, two regional indicators, counts as its
2020 parts. The tables below are made from Unicode's data by
2121 tools/Widths.raz, which says how; they take about 4 KB, which a
22- program that doesn't call width or columns doesn't carry. *)
22+ program that doesn't call width or columns doesn't carry. Its test
23+ uses Check. *)
2324 module Width;
24-import Rt, Str;
25+import Str, Check;
2526
2627 (* The tables: each a sorted list of ranges, first and last. Made by
2728 tools/Widths.raz: don't edit them, make them again. *)
@@ -198,21 +199,27 @@
198199 return w;
199200 end;
200201
202+(* Checks that every character in cs has the width w. *)
203+proc allWidth(what: []U8; cs: []I32; w: INT);
204+var i: INT;
205+begin
206+ for i := 0 to LEN(cs) - 1 do Check.int(what, width(cs[i]), w); end;
207+end;
208+
201209 (* The module's test. *)
202210 proc main*;
203211 begin
204- if (width('a') # 1) or (width(' ') # 1) or (width(0) # 0) or (width(9) # 0)
205- or (width(7FH) # 0) then Rt.stop("Width.width: ASCII"); end;
206- if (width(0301H) # 0) or (width(200BH) # 0) or (width(0ADH) # 1)
207- or (width(1160H) # 0) then Rt.stop("Width.width: none"); end;
208- if (width(4E2DH) # 2) or (width(3042H) # 2) or (width(0FF21H) # 2)
209- or (width(01F600H) # 2) or (width(3000H) # 2) then Rt.stop("Width.width: wide"); end;
210- if (width(0E9H) # 1) or (width(03B1H) # 1) or (width(0FFFDH) # 1)
211- or (width(10FFFFH) # 1) then Rt.stop("Width.width: one"); end;
212- if (columns("") # 0) or (columns("abc") # 3) or (columns("e\u{301}") # 1)
213- or (columns("\u{4E2D}\u{6587}") # 4) or (columns("a\u{1F600}b") # 4) then
214- Rt.stop("Width.columns");
215- end;
212+ allWidth("Width.width: ASCII 1", [I32('a'), I32(' ')], 1);
213+ allWidth("Width.width: ASCII 0", [I32(0), 9, 7FH], 0);
214+ allWidth("Width.width: none", [I32(0301H), 200BH, 1160H], 0);
215+ Check.int("Width.width: soft hyphen", width(0ADH), 1);
216+ allWidth("Width.width: wide", [I32(4E2DH), 3042H, 0FF21H, 01F600H, 3000H], 2);
217+ allWidth("Width.width: one", [I32(0E9H), 03B1H, 0FFFDH, 10FFFFH], 1);
218+ Check.int("Width.columns empty", columns(""), 0);
219+ Check.int("Width.columns abc", columns("abc"), 3);
220+ Check.int("Width.columns combining", columns("e\u{301}"), 1);
221+ Check.int("Width.columns wide", columns("\u{4E2D}\u{6587}"), 4);
222+ Check.int("Width.columns mixed", columns("a\u{1F600}b"), 4);
216223 end;
217224
218225 end Width.

test/doc/Opt.expect (history) modified +1 -1

@@ -6,7 +6,7 @@
66 from it, so they can't drift from it.
77
88 Depends on Files for the words, and writes the help and the
9- messages itself, then ends the program.
9+ messages itself, then ends the program. Its test uses Check.
1010 Leaves out: options after operands (GNU's reordering), and
1111 completion for concha.
1212

test/doc/Thread.expect (history) modified +2 -1

@@ -14,7 +14,7 @@
1414 stores C's exit in Rt.cExit; so this module uses no C itself, and
1515 serves both kinds of program. C's thread has a stack and a guard
1616 page of C's; the mapping here holds only the handlers' stack and
17- the Thread.
17+ the Thread. Its test uses Check.
1818
1919 const Failed*: ERROR = "can't start a thread";
2020
@@ -54,3 +54,4 @@
5454 for it.
5555
5656 proc main*;
57+ The module's test: starts threads in waves, and counts that each ran.

test/run/CheckFail.expect (history) added +1 -0

@@ -0,0 +1 @@
1+before the failures

test/run/CheckFail.raz (history) added +15 -0

@@ -0,0 +1,15 @@
1+(* Four checks, two of them failing: the exit procedure, not main,
2+ must say so on standard error and set status 1. *)
3+module CheckFail;
4+import Check, Out;
5+
6+proc main*;
7+begin
8+ Out.str("before the failures\n");
9+ Check.ok(true, "passes");
10+ Check.ok(false, "ok fails");
11+ Check.str("str fails", "a\nb", "a\tb");
12+ Check.int("int fails", -9223372036854775807 - 1, 0);
13+end;
14+
15+end CheckFail.

test/run/CheckFail.status (history) added +1 -0

@@ -0,0 +1 @@
1+1

test/run/CheckFail.stderr (history) added +9 -0

@@ -0,0 +1,9 @@
1+FAIL ok fails
2+FAIL str fails
3+ got: "a\nb"
4+ want: "a\tb"
5+ at byte 1
6+FAIL int fails
7+ got: -9223372036854775808
8+ want: 0
9+Check: 3 of 4 failed