File: textio.sml

package info (click to toggle)
mlton 20130715-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 60,900 kB
  • ctags: 69,386
  • sloc: xml: 34,418; ansic: 17,399; lisp: 2,879; makefile: 1,605; sh: 1,254; pascal: 256; python: 143; asm: 97
file content (346 lines) | stat: -rw-r--r-- 9,937 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
(* Auxiliary functions for test cases *)

infix 1 seq
fun e1 seq e2 = e2;
fun check b = if b then "OK" else "WRONG";
fun check' f = (if f () then "OK" else "WRONG") handle _ => "EXN";

fun range (from, to) p = 
    let open Int 
    in
        (from > to) orelse (p from) andalso (range (from+1, to) p)
    end;

fun checkrange bounds = check o range bounds;

fun tst0 s s' = print (s ^ "    \t" ^ s' ^ "\n");
fun tst  s b = tst0 s (check  b);
fun tst' s f = tst0 s (check' f);

fun tstrange s bounds = (tst s) o range bounds  

(* test/textio.sml
   PS 1995-11-22, 1996-04-18
*)

(*KILL 05/11/1997 11:03. tho.:
use "auxil.sml";
*)

val _ = print "Testing TextIO...\n";

local 
    open TextIO

    fun fileSize s = 
        let val is = openIn s
        in size (inputAll is) before closeIn is end;

    fun dup 0 s = s
      | dup n s = dup (n-1) (s^s)

    val longstring = dup 5 "abcdefg"  (* was 16 but because `limit stack` = 8192 kbytes on frigg
                                       * I received a SIGSEGV for stack growth failure. *)
in

val empty  = openOut "empty.dat";
val small  = openOut "small1.dat";
val medium = openOut "medium.dat";
val text   = openOut "text.dat";

val test1 = 
    tst' "test1" (fn _ => 
           (closeOut empty; 
            fileSize "empty.dat" = 0 
            andalso fileSize "empty.dat" = 0));
    
val test2 = 
    tst' "test2" (fn _ => 
           (output1(small, #"+");
            closeOut small;
            fileSize "small1.dat" = 1
            andalso fileSize "small1.dat" = 1));

val test3 = 
    tst' "test3" (fn _ =>
           let val small = openOut "small2.dat"
           in 
               output(small, "*");
               closeOut small;
               fileSize "small2.dat" = 1 andalso fileSize "small2.dat" = 1
           end);

val test4 = 
    tst' "test4" (fn _ => 
           (output(medium, longstring);
            closeOut medium;
            fileSize "medium.dat" = size longstring
            andalso fileSize "medium.dat" = size longstring))

val test5 = 
    tst' "test5" (fn _ => 
           let val small = openAppend "small2.dat"
           in 
               output(small, "1");
               closeOut small;
               fileSize "small2.dat" = 2 andalso fileSize "small2.dat" = 2
           end);

val test6 = 
    tst' "test6" (fn _ => 
           (output(text, "Line 1\n");
            output(text, "Line 2\n");
            output(text, "Line 3");
            closeOut text;
            fileSize "text.dat" = 20 andalso fileSize "text.dat" = 20));
    
(* Test that stdErr is flushed immediately, that flushOut works, and
 * that print flushes stdOut.  Assumes that stdOut is *not* flushed
 * immediately: *)

val _ = 
    let fun stdo s = output(stdOut, s)
        fun stde s = output(stdErr, s)
    in
        print "Two lines of output follow:\n";
        stdo "3"; stde "1"; stdo "4"; stde "2";
        flushOut stdOut; 
        stde "  <--- this should read 1234\n";
        stdo "2"; stde "1"; print "3"; stde "4"; stdo "5"; 
        flushOut stdOut; 
        stde " <--- this should read 12345\n"
    end;

val test7a = 
    tst' "test7a" (fn _ => 
           let val is = openIn "empty.dat"
           in 
               (endOfStream is 
                andalso input1 is = NONE 
                andalso endOfStream is 
                andalso input1 is = NONE)
               before closeIn is 
           end);

val test7b = 
    tst' "test7b" (fn _ => 
           let val is = openIn "small1.dat"
           in 
               (not (endOfStream is)
                andalso input1 is = SOME #"+" 
                andalso endOfStream is
                andalso input1 is = NONE 
                andalso input1 is = NONE)
               before closeIn is 
           end);

val test7c = 
    tst' "test7c" (fn _ => 
           let val is = openIn "small2.dat"
           in 
               (not (endOfStream is)
                andalso input1 is = SOME #"*" 
                andalso not (endOfStream is)
                andalso input1 is = SOME #"1" 
                andalso endOfStream is
                andalso input1 is = NONE 
                andalso input1 is = NONE)
               before closeIn is 
           end);

val test8a =
    tst' "test8a" (fn _ =>
           let val is = openIn "empty.dat"
           in 
               (inputN(is, 0) = ""
                andalso inputN(is, 1) = ""
                andalso inputN(is, 100) = ""
                andalso endOfStream is)
               before closeIn is 
           end);

val test8b =
    tst' "test8b" (fn _ =>
           let val is = openIn "small1.dat"
           in 
               (inputN(is, 0) = ""
                andalso inputN(is, 1) = "+"
                andalso inputN(is, 100) = "")
               before closeIn is 
           end);

val test8c =
    tst' "test8c" (fn _ =>
           let val is = openIn "small1.dat"
           in 
               (inputN(is, 0) = ""
                andalso inputN(is, 100) = "+"
                andalso inputN(is, 100) = "")
               before closeIn is 
           end);

val test8d =
    tst' "test8d" (fn _ =>
           let val is = openIn "small2.dat"
           in 
               (inputN(is, 0) = ""
                andalso inputN(is, 1) = "*"
                andalso inputN(is, 100) = "1"
                andalso inputN(is, 100) = "")
               before closeIn is 
           end);

val test8e =
    tst' "test8e" (fn _ =>
           let val is = openIn "medium.dat"
           in 
               (inputN(is, 0) = ""
                andalso inputN(is, 15) = "abcdefgabcdefga"
                andalso inputN(is, 15) = "bcdefgabcdefgab"
                andalso inputN(is, 0) = ""
                andalso not (endOfStream is))
               before closeIn is 
           end);
   
val test8f =
    tst' "test8f" (fn _ =>
           let val is = openIn "medium.dat"
           in 
               (inputN(is, 500000) = longstring
                andalso inputN(is, 100) = ""
                andalso endOfStream is)
               before closeIn is 
           end);
   
val test9a =
    tst' "test9a" (fn _ =>
           let val is = openIn "empty.dat"
           in 
               (lookahead is = NONE 
                andalso input is = "" 
                andalso lookahead is = NONE
                andalso input is = "")
               before closeIn is 
           end);

val test9b =
    tst' "test9b" (fn _ =>
           let val is = openIn "small1.dat"
           in 
               (lookahead is = SOME #"+"
                andalso input is = "+" 
                andalso input is = ""
                andalso lookahead is = NONE)
               before closeIn is 
           end);

val test9c =
    tst' "test9c" (fn _ =>
           let val is = openIn "small2.dat"
           in 
               (lookahead is = SOME #"*"
                andalso input is = "*1" 
                andalso input is = ""
                andalso lookahead is = NONE)
               before closeIn is 
           end);

val test9d =
    tst' "test9d" (fn _ =>
           let val is = openIn "small2.dat"
           in 
               (input is = "*1" 
                andalso input is = "")
               before closeIn is 
           end);

val test9e =
    tst' "test9e" (fn _ =>
           let val is = openIn "medium.dat"
           in 
               lookahead is = SOME #"a"
               andalso String.substring(input is, 0, 15) = "abcdefgabcdefga"
               before closeIn is 
           end);
   
val test10 = 
    tst' "test10" (fn _ =>
           let val is = openIn "medium.dat"
           in 
               (lookahead is = SOME #"a"
                andalso input1 is = SOME #"a"
                andalso lookahead is = SOME #"b"
                andalso input1 is = SOME #"b"
                andalso lookahead is = SOME #"c")
               before closeIn is 
           end);

val test11 = 
    tst' "test11" (fn _ =>
           let val is = openIn "medium.dat"
           in 
               (lookahead is = SOME #"a"
                andalso inputN(is, 5) = "abcde"
                andalso lookahead is = SOME #"f"
                andalso inputN(is, 4) = "fgab"
                andalso lookahead is = SOME #"c")
               before closeIn is 
           end);

val test12a =
    tst' "test12a" (fn _ => 
           let val is = openIn "empty.dat"
           in 
               (inputLine is = NONE
                andalso inputLine is = NONE)
               before closeIn is
           end);

val test12b =
    tst' "test12b" (fn _ => 
           let val is = openIn "small1.dat"
           in 
               (inputLine is = SOME "+\n"
                andalso inputLine is = NONE)
               before closeIn is
           end);

val test12c =
    tst' "test12c" (fn _ => 
           let val is = openIn "text.dat"
           in 
               (inputLine is = SOME "Line 1\n"
               andalso inputLine is = SOME "Line 2\n"
               andalso inputLine is = SOME "Line 3\n"
               andalso inputLine is = NONE)
               before closeIn is
           end);

val test12d =
    tst' "test12d" (fn _ => 
           let val is = openIn "medium.dat"
           in 
               (inputLine is = SOME (longstring ^ "\n")
               andalso inputLine is = NONE)
               before closeIn is
           end);

(* Test that outputSubstr works *)

val _ = 
    let fun stdo s i n = outputSubstr(stdOut, Substring.substring(s, i, n))
        fun stde s = output(stdErr, s)
        val abcde = "abcde"
    in
        print "Two lines of output follow:\n";
        stdo abcde 0 1; stdo abcde 1 3; 
        stdo "" 0 0; stdo abcde 0 0; stdo abcde 5 0; stdo abcde 3 0; 
        stdo abcde 4 1;
        flushOut stdOut; 
        stde " <--- this should read abcde\n";
        stdo abcde 0 5;
        flushOut stdOut; 
        stde " <--- this should read abcde\n"
    end;

end