| 1 |
MODULE format2; |
| 2 |
|
| 3 |
FROM libc IMPORT exit, printf ; |
| 4 |
FROM Terminal IMPORT Write, WriteLn; |
| 5 |
FROM NumberIO IMPORT WriteCard; |
| 6 |
FROM DynamicStrings IMPORT String, Length, char, InitString; |
| 7 |
FROM FormatStrings IMPORT Sprintf1; |
| 8 |
|
| 9 |
PROCEDURE WriteString (s: String); |
| 10 |
VAR |
| 11 |
l, i: CARDINAL; |
| 12 |
BEGIN |
| 13 |
l := Length (s) ; |
| 14 |
i := 0 ; |
| 15 |
WHILE i < l DO |
| 16 |
Write (char (s, i)) ; |
| 17 |
INC (i) |
| 18 |
END |
| 19 |
END WriteString; |
| 20 |
|
| 21 |
|
| 22 |
(* |
| 23 |
assert - |
| 24 |
*) |
| 25 |
|
| 26 |
PROCEDURE assert (cond: BOOLEAN; line: CARDINAL; file: ARRAY OF CHAR) ; |
| 27 |
BEGIN |
| 28 |
IF NOT cond |
| 29 |
THEN |
| 30 |
printf ("%s:%d assertion failed\n", file, line); |
| 31 |
exit (1) |
| 32 |
END |
| 33 |
END assert ; |
| 34 |
|
| 35 |
|
| 36 |
VAR |
| 37 |
n: CARDINAL; |
| 38 |
r, s: String; |
| 39 |
BEGIN |
| 40 |
n := 2; |
| 41 |
r := InitString("%u pieces of cake") ; |
| 42 |
WriteString (r) ; WriteLn ; |
| 43 |
assert (Length (r) = 17, __LINE__, __FILE__) ; |
| 44 |
s := Sprintf1 (r, n) ; |
| 45 |
WriteCard (Length (s), 4) ; WriteLn ; |
| 46 |
assert (Length (s) = 16, __LINE__, __FILE__) ; |
| 47 |
|
| 48 |
r := InitString("%d pieces of cake") ; |
| 49 |
WriteString (r) ; WriteLn ; |
| 50 |
assert (Length (r) = 17, __LINE__, __FILE__) ; |
| 51 |
s := Sprintf1 (r, n) ; |
| 52 |
WriteCard (Length (s), 4) ; WriteLn ; |
| 53 |
assert (Length (s) = 16, __LINE__, __FILE__) ; |
| 54 |
|
| 55 |
r := InitString("%x pieces of cake") ; |
| 56 |
WriteString (r) ; WriteLn ; |
| 57 |
assert (Length (r) = 17, __LINE__, __FILE__) ; |
| 58 |
s := Sprintf1 (r, n) ; |
| 59 |
WriteCard (Length (s), 4) ; WriteLn ; |
| 60 |
assert (Length (s) = 16, __LINE__, __FILE__) ; |
| 61 |
|
| 62 |
WriteString (InitString ('all tests pass')) ; WriteLn ; |
| 63 |
END format2. |