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