| 1 |
(* Auxiliary functions for test cases *) |
| 2 |
|
| 3 |
infix 1 seq |
| 4 |
fun e1 seq e2 = e2; |
| 5 |
fun check b = if b then "OK" else "WRONG"; |
| 6 |
fun check' f = (if f () then "OK" else "WRONG") handle _ => "EXN"; |
| 7 |
|
| 8 |
fun range (from, to) p = |
| 9 |
let open Int |
| 10 |
in |
| 11 |
(from > to) orelse (p from) andalso (range (from+1, to) p) |
| 12 |
end; |
| 13 |
|
| 14 |
fun checkrange bounds = check o range bounds; |
| 15 |
|
| 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); |
| 19 |
|
| 20 |
fun tstrange s bounds = (tst s) o range bounds |
| 21 |
|
| 22 |
(* test/textio.sml |
| 23 |
PS 1995-11-22, 1996-04-18 |
| 24 |
*) |
| 25 |
|
| 26 |
(*KILL 05/11/1997 11:03. tho.: |
| 27 |
use "auxil.sml"; |
| 28 |
*) |
| 29 |
|
| 30 |
val _ = print "Testing TextIO...\n"; |
| 31 |
|
| 32 |
local |
| 33 |
open TextIO |
| 34 |
|
| 35 |
fun fileSize s = |
| 36 |
let val is = openIn s |
| 37 |
in size (inputAll is) before closeIn is end; |
| 38 |
|
| 39 |
fun dup 0 s = s |
| 40 |
| dup n s = dup (n-1) (s^s) |
| 41 |
|
| 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. *) |
| 44 |
in |
| 45 |
|
| 46 |
val empty = openOut "empty.dat"; |
| 47 |
val small = openOut "small1.dat"; |
| 48 |
val medium = openOut "medium.dat"; |
| 49 |
val text = openOut "text.dat"; |
| 50 |
|
| 51 |
val test1 = |
| 52 |
tst' "test1" (fn _ => |
| 53 |
(closeOut empty; |
| 54 |
fileSize "empty.dat" = 0 |
| 55 |
andalso fileSize "empty.dat" = 0)); |
| 56 |
|
| 57 |
val test2 = |
| 58 |
tst' "test2" (fn _ => |
| 59 |
(output1(small, #"+"); |
| 60 |
closeOut small; |
| 61 |
fileSize "small1.dat" = 1 |
| 62 |
andalso fileSize "small1.dat" = 1)); |
| 63 |
|
| 64 |
val test3 = |
| 65 |
tst' "test3" (fn _ => |
| 66 |
let val small = openOut "small2.dat" |
| 67 |
in |
| 68 |
output(small, "*"); |
| 69 |
closeOut small; |
| 70 |
fileSize "small2.dat" = 1 andalso fileSize "small2.dat" = 1 |
| 71 |
end); |
| 72 |
|
| 73 |
val test4 = |
| 74 |
tst' "test4" (fn _ => |
| 75 |
(output(medium, longstring); |
| 76 |
closeOut medium; |
| 77 |
fileSize "medium.dat" = size longstring |
| 78 |
andalso fileSize "medium.dat" = size longstring)) |
| 79 |
|
| 80 |
val test5 = |
| 81 |
tst' "test5" (fn _ => |
| 82 |
let val small = openAppend "small2.dat" |
| 83 |
in |
| 84 |
output(small, "1"); |
| 85 |
closeOut small; |
| 86 |
fileSize "small2.dat" = 2 andalso fileSize "small2.dat" = 2 |
| 87 |
end); |
| 88 |
|
| 89 |
val test6 = |
| 90 |
tst' "test6" (fn _ => |
| 91 |
(output(text, "Line 1\n"); |
| 92 |
output(text, "Line 2\n"); |
| 93 |
output(text, "Line 3"); |
| 94 |
closeOut text; |
| 95 |
fileSize "text.dat" = 20 andalso fileSize "text.dat" = 20)); |
| 96 |
|
| 97 |
(* Test that stdErr is flushed immediately, that flushOut works, and |
| 98 |
* that print flushes stdOut. Assumes that stdOut is *not* flushed |
| 99 |
* immediately: *) |
| 100 |
|
| 101 |
val _ = |
| 102 |
let fun stdo s = output(stdOut, s) |
| 103 |
fun stde s = output(stdErr, s) |
| 104 |
in |
| 105 |
print "Two lines of output follow:\n"; |
| 106 |
stdo "3"; stde "1"; stdo "4"; stde "2"; |
| 107 |
flushOut stdOut; |
| 108 |
stde " <--- this should read 1234\n"; |
| 109 |
stdo "2"; stde "1"; print "3"; stde "4"; stdo "5"; |
| 110 |
flushOut stdOut; |
| 111 |
stde " <--- this should read 12345\n" |
| 112 |
end; |
| 113 |
|
| 114 |
val test7a = |
| 115 |
tst' "test7a" (fn _ => |
| 116 |
let val is = openIn "empty.dat" |
| 117 |
in |
| 118 |
(endOfStream is |
| 119 |
andalso input1 is = NONE |
| 120 |
andalso endOfStream is |
| 121 |
andalso input1 is = NONE) |
| 122 |
before closeIn is |
| 123 |
end); |
| 124 |
|
| 125 |
val test7b = |
| 126 |
tst' "test7b" (fn _ => |
| 127 |
let val is = openIn "small1.dat" |
| 128 |
in |
| 129 |
(not (endOfStream is) |
| 130 |
andalso input1 is = SOME #"+" |
| 131 |
andalso endOfStream is |
| 132 |
andalso input1 is = NONE |
| 133 |
andalso input1 is = NONE) |
| 134 |
before closeIn is |
| 135 |
end); |
| 136 |
|
| 137 |
val test7c = |
| 138 |
tst' "test7c" (fn _ => |
| 139 |
let val is = openIn "small2.dat" |
| 140 |
in |
| 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) |
| 148 |
before closeIn is |
| 149 |
end); |
| 150 |
|
| 151 |
val test8a = |
| 152 |
tst' "test8a" (fn _ => |
| 153 |
let val is = openIn "empty.dat" |
| 154 |
in |
| 155 |
(inputN(is, 0) = "" |
| 156 |
andalso inputN(is, 1) = "" |
| 157 |
andalso inputN(is, 100) = "" |
| 158 |
andalso endOfStream is) |
| 159 |
before closeIn is |
| 160 |
end); |
| 161 |
|
| 162 |
val test8b = |
| 163 |
tst' "test8b" (fn _ => |
| 164 |
let val is = openIn "small1.dat" |
| 165 |
in |
| 166 |
(inputN(is, 0) = "" |
| 167 |
andalso inputN(is, 1) = "+" |
| 168 |
andalso inputN(is, 100) = "") |
| 169 |
before closeIn is |
| 170 |
end); |
| 171 |
|
| 172 |
val test8c = |
| 173 |
tst' "test8c" (fn _ => |
| 174 |
let val is = openIn "small1.dat" |
| 175 |
in |
| 176 |
(inputN(is, 0) = "" |
| 177 |
andalso inputN(is, 100) = "+" |
| 178 |
andalso inputN(is, 100) = "") |
| 179 |
before closeIn is |
| 180 |
end); |
| 181 |
|
| 182 |
val test8d = |
| 183 |
tst' "test8d" (fn _ => |
| 184 |
let val is = openIn "small2.dat" |
| 185 |
in |
| 186 |
(inputN(is, 0) = "" |
| 187 |
andalso inputN(is, 1) = "*" |
| 188 |
andalso inputN(is, 100) = "1" |
| 189 |
andalso inputN(is, 100) = "") |
| 190 |
before closeIn is |
| 191 |
end); |
| 192 |
|
| 193 |
val test8e = |
| 194 |
tst' "test8e" (fn _ => |
| 195 |
let val is = openIn "medium.dat" |
| 196 |
in |
| 197 |
(inputN(is, 0) = "" |
| 198 |
andalso inputN(is, 15) = "abcdefgabcdefga" |
| 199 |
andalso inputN(is, 15) = "bcdefgabcdefgab" |
| 200 |
andalso inputN(is, 0) = "" |
| 201 |
andalso not (endOfStream is)) |
| 202 |
before closeIn is |
| 203 |
end); |
| 204 |
|
| 205 |
val test8f = |
| 206 |
tst' "test8f" (fn _ => |
| 207 |
let val is = openIn "medium.dat" |
| 208 |
in |
| 209 |
(inputN(is, 500000) = longstring |
| 210 |
andalso inputN(is, 100) = "" |
| 211 |
andalso endOfStream is) |
| 212 |
before closeIn is |
| 213 |
end); |
| 214 |
|
| 215 |
val test9a = |
| 216 |
tst' "test9a" (fn _ => |
| 217 |
let val is = openIn "empty.dat" |
| 218 |
in |
| 219 |
(lookahead is = NONE |
| 220 |
andalso input is = "" |
| 221 |
andalso lookahead is = NONE |
| 222 |
andalso input is = "") |
| 223 |
before closeIn is |
| 224 |
end); |
| 225 |
|
| 226 |
val test9b = |
| 227 |
tst' "test9b" (fn _ => |
| 228 |
let val is = openIn "small1.dat" |
| 229 |
in |
| 230 |
(lookahead is = SOME #"+" |
| 231 |
andalso input is = "+" |
| 232 |
andalso input is = "" |
| 233 |
andalso lookahead is = NONE) |
| 234 |
before closeIn is |
| 235 |
end); |
| 236 |
|
| 237 |
val test9c = |
| 238 |
tst' "test9c" (fn _ => |
| 239 |
let val is = openIn "small2.dat" |
| 240 |
in |
| 241 |
(lookahead is = SOME #"*" |
| 242 |
andalso input is = "*1" |
| 243 |
andalso input is = "" |
| 244 |
andalso lookahead is = NONE) |
| 245 |
before closeIn is |
| 246 |
end); |
| 247 |
|
| 248 |
val test9d = |
| 249 |
tst' "test9d" (fn _ => |
| 250 |
let val is = openIn "small2.dat" |
| 251 |
in |
| 252 |
(input is = "*1" |
| 253 |
andalso input is = "") |
| 254 |
before closeIn is |
| 255 |
end); |
| 256 |
|
| 257 |
val test9e = |
| 258 |
tst' "test9e" (fn _ => |
| 259 |
let val is = openIn "medium.dat" |
| 260 |
in |
| 261 |
lookahead is = SOME #"a" |
| 262 |
andalso String.substring(input is, 0, 15) = "abcdefgabcdefga" |
| 263 |
before closeIn is |
| 264 |
end); |
| 265 |
|
| 266 |
val test10 = |
| 267 |
tst' "test10" (fn _ => |
| 268 |
let val is = openIn "medium.dat" |
| 269 |
in |
| 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") |
| 275 |
before closeIn is |
| 276 |
end); |
| 277 |
|
| 278 |
val test11 = |
| 279 |
tst' "test11" (fn _ => |
| 280 |
let val is = openIn "medium.dat" |
| 281 |
in |
| 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") |
| 287 |
before closeIn is |
| 288 |
end); |
| 289 |
|
| 290 |
val test12a = |
| 291 |
tst' "test12a" (fn _ => |
| 292 |
let val is = openIn "empty.dat" |
| 293 |
in |
| 294 |
(inputLine is = NONE |
| 295 |
andalso inputLine is = NONE) |
| 296 |
before closeIn is |
| 297 |
end); |
| 298 |
|
| 299 |
val test12b = |
| 300 |
tst' "test12b" (fn _ => |
| 301 |
let val is = openIn "small1.dat" |
| 302 |
in |
| 303 |
(inputLine is = SOME "+\n" |
| 304 |
andalso inputLine is = NONE) |
| 305 |
before closeIn is |
| 306 |
end); |
| 307 |
|
| 308 |
val test12c = |
| 309 |
tst' "test12c" (fn _ => |
| 310 |
let val is = openIn "text.dat" |
| 311 |
in |
| 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) |
| 316 |
before closeIn is |
| 317 |
end); |
| 318 |
|
| 319 |
val test12d = |
| 320 |
tst' "test12d" (fn _ => |
| 321 |
let val is = openIn "medium.dat" |
| 322 |
in |
| 323 |
(inputLine is = SOME (longstring ^ "\n") |
| 324 |
andalso inputLine is = NONE) |
| 325 |
before closeIn is |
| 326 |
end); |
| 327 |
|
| 328 |
(* Test that outputSubstr works *) |
| 329 |
|
| 330 |
val _ = |
| 331 |
let fun stdo s i n = outputSubstr(stdOut, Substring.substring(s, i, n)) |
| 332 |
fun stde s = output(stdErr, s) |
| 333 |
val abcde = "abcde" |
| 334 |
in |
| 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; |
| 338 |
stdo abcde 4 1; |
| 339 |
flushOut stdOut; |
| 340 |
stde " <--- this should read abcde\n"; |
| 341 |
stdo abcde 0 5; |
| 342 |
flushOut stdOut; |
| 343 |
stde " <--- this should read abcde\n" |
| 344 |
end; |
| 345 |
|
| 346 |
end |