| 1 |
MODULE testreadint ; |
| 2 |
|
| 3 |
FROM ChanConsts IMPORT OpenResults, old, read, write ; |
| 4 |
FROM IOChan IMPORT ChanId ; |
| 5 |
FROM StdChans IMPORT StdOutChan ; |
| 6 |
IMPORT StreamFile ; |
| 7 |
FROM TextIO IMPORT SkipLine, WriteLn, WriteString ; |
| 8 |
FROM WholeIO IMPORT ReadCard, ReadInt, WriteCard, WriteInt ; |
| 9 |
FROM libc IMPORT printf, exit ; |
| 10 |
|
| 11 |
|
| 12 |
CONST |
| 13 |
TestFileName = "testdata" ; |
| 14 |
|
| 15 |
|
| 16 |
PROCEDURE Assert (condition: BOOLEAN; name, result: ARRAY OF CHAR) ; |
| 17 |
BEGIN |
| 18 |
IF NOT condition |
| 19 |
THEN |
| 20 |
code := 1 ; |
| 21 |
printf ("assert failed, procedure: %s failed to read number: %s\n", name, result) |
| 22 |
END |
| 23 |
END Assert ; |
| 24 |
|
| 25 |
|
| 26 |
PROCEDURE StressReadInt ; |
| 27 |
VAR |
| 28 |
in, |
| 29 |
out : ChanId ; |
| 30 |
result: OpenResults ; |
| 31 |
int : INTEGER ; |
| 32 |
card : CARDINAL ; |
| 33 |
BEGIN |
| 34 |
(* Create a new file and use WriteCard to populate the file. *) |
| 35 |
printf ("creating test file: %s\n", TestFileName) ; |
| 36 |
|
| 37 |
StreamFile.Open (out, TestFileName, write+old, result); |
| 38 |
IF result = opened |
| 39 |
THEN |
| 40 |
WriteString (out, ' ') ; |
| 41 |
WriteCard (out, 123, 3) ; |
| 42 |
WriteLn (out) ; |
| 43 |
WriteCard (out, 456, 3) ; |
| 44 |
WriteLn (out) ; |
| 45 |
StreamFile.Close (out) |
| 46 |
ELSE |
| 47 |
printf ("unable to create: %s\n", TestFileName) ; |
| 48 |
exit (1) |
| 49 |
END ; |
| 50 |
|
| 51 |
(* Now attempt to read the data using ReadCard. *) |
| 52 |
printf ("reading test file using ReadCard: %s\n", TestFileName) ; |
| 53 |
StreamFile.Open (in, TestFileName, read, result) ; |
| 54 |
IF result = opened |
| 55 |
THEN |
| 56 |
ReadCard (in, card) ; |
| 57 |
printf ("first cardinal: %d\n", card) ; |
| 58 |
Assert (card = 123, "ReadCard", "123") ; |
| 59 |
SkipLine (in) ; |
| 60 |
ReadCard (in, card) ; |
| 61 |
printf ("second cardinal: %d\n", card) ; |
| 62 |
Assert (card = 456, "ReadCard", "456") ; |
| 63 |
StreamFile.Close (in) |
| 64 |
END ; |
| 65 |
|
| 66 |
(* Now attempt to read the data using ReadInt. *) |
| 67 |
printf ("reading test file using ReadInt: %s\n", TestFileName) ; |
| 68 |
StreamFile.Open (in, TestFileName, read, result) ; |
| 69 |
IF result = opened |
| 70 |
THEN |
| 71 |
ReadInt (in, int) ; |
| 72 |
printf ("first integer: %d\n", int) ; |
| 73 |
Assert (int = 123, "ReadInt", "123") ; |
| 74 |
SkipLine (in) ; |
| 75 |
ReadInt (in, int) ; |
| 76 |
printf ("second integer: %d\n", int) ; |
| 77 |
Assert (int = 456, "ReadInt", "456") ; |
| 78 |
StreamFile.Close (in) |
| 79 |
END |
| 80 |
END StressReadInt ; |
| 81 |
|
| 82 |
|
| 83 |
VAR |
| 84 |
code: INTEGER ; |
| 85 |
BEGIN |
| 86 |
code := 0 ; |
| 87 |
StressReadInt ; |
| 88 |
exit (code) |
| 89 |
END testreadint. |