1 (* Auxiliary functions for test cases
*)
5 fun check b
= if b
then "OK" else "WRONG";
6 fun check
' f
= (if f () then "OK" else "WRONG") handle _
=> "EXN";
8 fun range (from
, to
) p
=
11 (from
> to
) orelse (p from
) andalso (range (from
+1, to
) p
)
14 fun checkrange bounds
= check
o range bounds
;
16 fun tst0 s s
' = print (s ^
" \t" ^ s
' ^
"\n");
17 fun tst s b
= tst0
s (check b
);
18 fun tst
' s f
= tst0
s (check
' f
);
20 fun tstrange s bounds
= (tst s
) o range bounds
23 PS
1995-11-22, 1996-04-18
26 (*KILL
05/11/1997 11:03. tho
.:
30 val _
= print
"Testing TextIO...\n";
37 in size (inputAll is
) before closeIn is
end;
40 | dup n s
= dup (n
-1) (s^s
)
42 val longstring
= dup
5 "abcdefg" (* was
16 but because `limit stack`
= 8192 kbytes on frigg
43 * I received a SIGSEGV for stack growth failure
. *)
46 val empty
= openOut
"empty.dat";
47 val small
= openOut
"small1.dat";
48 val medium
= openOut
"medium.dat";
49 val text
= openOut
"text.dat";
54 fileSize
"empty.dat" = 0
55 andalso fileSize
"empty.dat" = 0));
59 (output1(small
, #
"+");
61 fileSize
"small1.dat" = 1
62 andalso fileSize
"small1.dat" = 1));
66 let val small
= openOut
"small2.dat"
70 fileSize
"small2.dat" = 1 andalso fileSize
"small2.dat" = 1
75 (output(medium
, longstring
);
77 fileSize
"medium.dat" = size longstring
78 andalso fileSize
"medium.dat" = size longstring
))
82 let val small
= openAppend
"small2.dat"
86 fileSize
"small2.dat" = 2 andalso fileSize
"small2.dat" = 2
91 (output(text
, "Line 1\n");
92 output(text
, "Line 2\n");
93 output(text
, "Line 3");
95 fileSize
"text.dat" = 20 andalso fileSize
"text.dat" = 20));
97 (* Test that stdErr is flushed immediately
, that flushOut works
, and
98 * that print flushes stdOut
. Assumes that stdOut is
*not
* flushed
102 let fun stdo s
= output(stdOut
, s
)
103 fun stde s
= output(stdErr
, s
)
105 print
"Two lines of output follow:\n";
106 stdo
"3"; stde
"1"; stdo
"4"; stde
"2";
108 stde
" <--- this should read 1234\n";
109 stdo
"2"; stde
"1"; print
"3"; stde
"4"; stdo
"5";
111 stde
" <--- this should read 12345\n"
115 tst
' "test7a" (fn _
=>
116 let val is
= openIn
"empty.dat"
119 andalso input1 is
= NONE
120 andalso endOfStream is
121 andalso input1 is
= NONE
)
126 tst
' "test7b" (fn _
=>
127 let val is
= openIn
"small1.dat"
129 (not (endOfStream is
)
130 andalso input1 is
= SOME #
"+"
131 andalso endOfStream is
132 andalso input1 is
= NONE
133 andalso input1 is
= NONE
)
138 tst
' "test7c" (fn _
=>
139 let val is
= openIn
"small2.dat"
141 (not (endOfStream is
)
142 andalso input1 is
= SOME #
"*"
143 andalso not (endOfStream is
)
144 andalso input1 is
= SOME #
"1"
145 andalso endOfStream is
146 andalso input1 is
= NONE
147 andalso input1 is
= NONE
)
152 tst
' "test8a" (fn _
=>
153 let val is
= openIn
"empty.dat"
156 andalso inputN(is
, 1) = ""
157 andalso inputN(is
, 100) = ""
158 andalso endOfStream is
)
163 tst
' "test8b" (fn _
=>
164 let val is
= openIn
"small1.dat"
167 andalso inputN(is
, 1) = "+"
168 andalso inputN(is
, 100) = "")
173 tst
' "test8c" (fn _
=>
174 let val is
= openIn
"small1.dat"
177 andalso inputN(is
, 100) = "+"
178 andalso inputN(is
, 100) = "")
183 tst
' "test8d" (fn _
=>
184 let val is
= openIn
"small2.dat"
187 andalso inputN(is
, 1) = "*"
188 andalso inputN(is
, 100) = "1"
189 andalso inputN(is
, 100) = "")
194 tst
' "test8e" (fn _
=>
195 let val is
= openIn
"medium.dat"
198 andalso inputN(is
, 15) = "abcdefgabcdefga"
199 andalso inputN(is
, 15) = "bcdefgabcdefgab"
200 andalso inputN(is
, 0) = ""
201 andalso not (endOfStream is
))
206 tst
' "test8f" (fn _
=>
207 let val is
= openIn
"medium.dat"
209 (inputN(is
, 500000) = longstring
210 andalso inputN(is
, 100) = ""
211 andalso endOfStream is
)
216 tst
' "test9a" (fn _
=>
217 let val is
= openIn
"empty.dat"
220 andalso input is
= ""
221 andalso lookahead is
= NONE
222 andalso input is
= "")
227 tst
' "test9b" (fn _
=>
228 let val is
= openIn
"small1.dat"
230 (lookahead is
= SOME #
"+"
231 andalso input is
= "+"
232 andalso input is
= ""
233 andalso lookahead is
= NONE
)
238 tst
' "test9c" (fn _
=>
239 let val is
= openIn
"small2.dat"
241 (lookahead is
= SOME #
"*"
242 andalso input is
= "*1"
243 andalso input is
= ""
244 andalso lookahead is
= NONE
)
249 tst
' "test9d" (fn _
=>
250 let val is
= openIn
"small2.dat"
253 andalso input is
= "")
258 tst
' "test9e" (fn _
=>
259 let val is
= openIn
"medium.dat"
261 lookahead is
= SOME #
"a"
262 andalso String.substring(input is
, 0, 15) = "abcdefgabcdefga"
267 tst
' "test10" (fn _
=>
268 let val is
= openIn
"medium.dat"
270 (lookahead is
= SOME #
"a"
271 andalso input1 is
= SOME #
"a"
272 andalso lookahead is
= SOME #
"b"
273 andalso input1 is
= SOME #
"b"
274 andalso lookahead is
= SOME #
"c")
279 tst
' "test11" (fn _
=>
280 let val is
= openIn
"medium.dat"
282 (lookahead is
= SOME #
"a"
283 andalso inputN(is
, 5) = "abcde"
284 andalso lookahead is
= SOME #
"f"
285 andalso inputN(is
, 4) = "fgab"
286 andalso lookahead is
= SOME #
"c")
291 tst
' "test12a" (fn _
=>
292 let val is
= openIn
"empty.dat"
295 andalso inputLine is
= NONE
)
300 tst
' "test12b" (fn _
=>
301 let val is
= openIn
"small1.dat"
303 (inputLine is
= SOME
"+\n"
304 andalso inputLine is
= NONE
)
309 tst
' "test12c" (fn _
=>
310 let val is
= openIn
"text.dat"
312 (inputLine is
= SOME
"Line 1\n"
313 andalso inputLine is
= SOME
"Line 2\n"
314 andalso inputLine is
= SOME
"Line 3\n"
315 andalso inputLine is
= NONE
)
320 tst
' "test12d" (fn _
=>
321 let val is
= openIn
"medium.dat"
323 (inputLine is
= SOME (longstring ^
"\n")
324 andalso inputLine is
= NONE
)
328 (* Test that outputSubstr works
*)
331 let fun stdo s i n
= outputSubstr(stdOut
, Substring
.substring(s
, i
, n
))
332 fun stde s
= output(stdErr
, s
)
335 print
"Two lines of output follow:\n";
336 stdo abcde
0 1; stdo abcde
1 3;
337 stdo
"" 0 0; stdo abcde
0 0; stdo abcde
5 0; stdo abcde
3 0;
340 stde
" <--- this should read abcde\n";
343 stde
" <--- this should read abcde\n"