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.
| @@ -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. |
| @@ -8,7 +8,7 @@ | ||
| 8 | 8 | Left out: the hyperbolic functions, fmod and hypot, until a program |
| 9 | 9 | needs them. *) |
| 10 | 10 | module Math; |
| 11 | -import Rt; | |
| 11 | +import Check; | |
| 12 | 12 | |
| 13 | 13 | const |
| 14 | 14 | pi* = 3.141592653589793; (* the F64 nearest to pi *) |
| @@ -925,25 +925,22 @@ | ||
| 925 | 925 | proc testToReal; |
| 926 | 926 | var b: [900]U8; i: INT; |
| 927 | 927 | 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"); | |
| 939 | 937 | (* 2^53 + 1, then 801 zeros and a 1: just above the half between two |
| 940 | 938 | F64s, though the 1 is past the digits kept, so it rounds up *) |
| 941 | 939 | COPY(b[0..16], "9007199254740993"); |
| 942 | 940 | for i := 16 to 816 do b[i] := '0'; end; |
| 943 | 941 | b[817] := '1'; |
| 944 | 942 | 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"); | |
| 947 | 944 | end; |
| 948 | 945 | |
| 949 | 946 | proc near(got, want: F64): BOOL; |
| @@ -962,47 +959,46 @@ | ||
| 962 | 959 | inf := 1.0 / 0.0; |
| 963 | 960 | nan := inf - inf; |
| 964 | 961 | 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) | |
| 969 | 966 | 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) | |
| 972 | 969 | 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) | |
| 975 | 972 | or not near(sin(pi), 1.2246467991473532E-16) or not near(sin(1.0E6), -0.34999350217129294) |
| 976 | 973 | 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) | |
| 979 | 976 | or not near(cos(pi / 2.0), 6.123233995736766E-17) or not near(cos(1.0E6), 0.9367521275331447) |
| 980 | 977 | 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) | |
| 983 | 980 | or not near(atan2(5.0, 0.1), 1.550798992821746) or not near(atan2(1.0, -1.0E-20), 1.5707963267948966) |
| 984 | 981 | 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) | |
| 987 | 984 | 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) | |
| 990 | 987 | or not near(pow(1.0000001, 1.0E9), 2.6881038582144647E43) or (pow(-2.0, -3.0) # -0.125) |
| 991 | 988 | 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) |
| 992 | 989 | 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) | |
| 995 | 992 | or not near(log2(3.0E-310), -1028.212746914361) or (log10(1000.0) # 3.0) or (log10(1.0E22) # 22.0) |
| 996 | 993 | 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) | |
| 1002 | 998 | or (asin(1.0) # pi / 2.0) or (asin(1.5) = asin(1.5)) or not near(acos(0.7), 0.7953988301841436) |
| 1003 | 999 | or not near(acos(-0.2), 1.7721542475852274) or (acos(1.0) # 0.0) or (acos(-1.0) # pi) |
| 1004 | 1000 | 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"); | |
| 1006 | 1002 | testToReal; |
| 1007 | 1003 | end; |
| 1008 | 1004 |
| @@ -5,11 +5,11 @@ | ||
| 5 | 5 | from it, so they can't drift from it. |
| 6 | 6 | |
| 7 | 7 | 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. | |
| 9 | 9 | Leaves out: options after operands (GNU's reordering), and |
| 10 | 10 | completion for concha. *) |
| 11 | 11 | module Opt; |
| 12 | -import Files, Str, Buf, Fmt, Heap, Out, Sys, Rt; | |
| 12 | +import Files, Str, Buf, Fmt, Heap, Sys, Rt, Check; | |
| 13 | 13 | |
| 14 | 14 | type |
| 15 | 15 | Row* = variant |
| @@ -534,8 +534,9 @@ | ||
| 534 | 534 | return n; |
| 535 | 535 | end; |
| 536 | 536 | |
| 537 | +(* Runs one case in a child and checks how it ended and what it wrote. *) | |
| 537 | 538 | 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; | |
| 539 | 540 | begin |
| 540 | 541 | outR, outW := Sys.pipe(); |
| 541 | 542 | errR, errW := Sys.pipe(); |
| @@ -546,11 +547,13 @@ | ||
| 546 | 547 | pid, st, status := Sys.wait(pid, true); |
| 547 | 548 | no := readAll(outR, out); |
| 548 | 549 | 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); | |
| 554 | 557 | end; |
| 555 | 558 | |
| 556 | 559 | proc main*; |
| @@ -2,10 +2,10 @@ | ||
| 2 | 2 | few steps to a number. The same seed gives the same numbers, on |
| 3 | 3 | every machine, so a failing run can be replayed. It is fast and |
| 4 | 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. *) | |
| 5 | + Needs Sys (for a fresh seed) and Rt; its test uses Check. | |
| 6 | + Leaves out reals, shuffles and other distributions. *) | |
| 7 | 7 | module Rand; |
| 8 | -import Sys, Rt; | |
| 8 | +import Sys, Rt, Check; | |
| 9 | 9 | |
| 10 | 10 | type |
| 11 | 11 | Rand* = record |
| @@ -69,29 +69,28 @@ | ||
| 69 | 69 | var r, q: Rand; i: INT; x: U64; v: I64; lo, hi: BOOL; |
| 70 | 70 | begin |
| 71 | 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; | |
| 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"); | |
| 76 | 75 | init(r, 42); init(q, 42); |
| 77 | 76 | 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"); | |
| 79 | 78 | end; |
| 80 | 79 | 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"); | |
| 83 | 82 | 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"); | |
| 85 | 84 | end; |
| 86 | 85 | lo := false; hi := false; |
| 87 | 86 | for i := 1 to 1000 do |
| 88 | 87 | 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"); | |
| 90 | 89 | lo := lo or (v = -2); hi := hi or (v = 2); |
| 91 | 90 | 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"); | |
| 93 | 92 | 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); | |
| 95 | 94 | x := seed(); |
| 96 | 95 | end; |
| 97 | 96 |
| @@ -13,9 +13,9 @@ | ||
| 13 | 13 | stores C's exit in Rt.cExit; so this module uses no C itself, and |
| 14 | 14 | serves both kinds of program. C's thread has a stack and a guard |
| 15 | 15 | page of C's; the mapping here holds only the handlers' stack and |
| 16 | - the Thread. *) | |
| 16 | + the Thread. Its test uses Check. *) | |
| 17 | 17 | module Thread; |
| 18 | -import Sys, Rt; | |
| 18 | +import Sys, Rt, Check; | |
| 19 | 19 | |
| 20 | 20 | const |
| 21 | 21 | Failed*: ERROR = "can't start a thread"; |
| @@ -232,6 +232,7 @@ | ||
| 232 | 232 | return v; |
| 233 | 233 | end; |
| 234 | 234 | |
| 235 | +(* The module's test: starts threads in waves, and counts that each ran. *) | |
| 235 | 236 | proc main*; |
| 236 | 237 | var n, alive, started, k, i: INT; t: ^Thread; err: ERROR; fail: BOOL; |
| 237 | 238 | begin |
| @@ -258,8 +259,8 @@ | ||
| 258 | 259 | for i := 0 to k - 1 do join(wave[i]); end; |
| 259 | 260 | started := started + k; |
| 260 | 261 | 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"); | |
| 263 | 264 | end; |
| 264 | 265 | |
| 265 | 266 | end Thread. |
| @@ -19,9 +19,10 @@ | ||
| 19 | 19 | zero width joiner, or a flag, two regional indicators, counts as its |
| 20 | 20 | parts. The tables below are made from Unicode's data by |
| 21 | 21 | 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. *) | |
| 23 | 24 | module Width; |
| 24 | -import Rt, Str; | |
| 25 | +import Str, Check; | |
| 25 | 26 | |
| 26 | 27 | (* The tables: each a sorted list of ranges, first and last. Made by |
| 27 | 28 | tools/Widths.raz: don't edit them, make them again. *) |
| @@ -198,21 +199,27 @@ | ||
| 198 | 199 | return w; |
| 199 | 200 | end; |
| 200 | 201 | |
| 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 | + | |
| 201 | 209 | (* The module's test. *) |
| 202 | 210 | proc main*; |
| 203 | 211 | 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); | |
| 216 | 223 | end; |
| 217 | 224 | |
| 218 | 225 | end Width. |
| @@ -6,7 +6,7 @@ | ||
| 6 | 6 | from it, so they can't drift from it. |
| 7 | 7 | |
| 8 | 8 | 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. | |
| 10 | 10 | Leaves out: options after operands (GNU's reordering), and |
| 11 | 11 | completion for concha. |
| 12 | 12 |
| @@ -14,7 +14,7 @@ | ||
| 14 | 14 | stores C's exit in Rt.cExit; so this module uses no C itself, and |
| 15 | 15 | serves both kinds of program. C's thread has a stack and a guard |
| 16 | 16 | page of C's; the mapping here holds only the handlers' stack and |
| 17 | - the Thread. | |
| 17 | + the Thread. Its test uses Check. | |
| 18 | 18 | |
| 19 | 19 | const Failed*: ERROR = "can't start a thread"; |
| 20 | 20 | |
| @@ -54,3 +54,4 @@ | ||
| 54 | 54 | for it. |
| 55 | 55 | |
| 56 | 56 | proc main*; |
| 57 | + The module's test: starts threads in waves, and counts that each ran. |
| @@ -0,0 +1 @@ | ||
| 1 | +before the failures |
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +1 |
| @@ -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 |