Import Debian changes 20180207-1
[hcoop/debian/mlton.git] / benchmark / tests / wc-scanStream.sml
1 (* Written by Stephen Weeks (sweeks@sweeks.com). *)
2
3 structure Main =
4 struct
5 fun doit n =
6 let
7 open TextIO
8 val f = OS.FileSys.tmpName ()
9 val out = openOut f
10 val _ =
11 output (out,
12 String.implode
13 (List.tabulate (1000000, fn i =>
14 if i mod 10 = 0 then #"\n" else #"a")))
15 val _ = closeOut out
16 fun wc f =
17 let
18 val ins = openIn f
19 in TextIO.scanStream
20 (fn reader => fn s =>
21 let
22 fun loop (s, ns) =
23 case reader s of
24 NONE => (closeIn ins
25 ; if ns <> 100000
26 then raise Fail "bug"
27 else ()
28 ; NONE)
29 | SOME (c, s') =>
30 loop (s', if c = #"\n" then ns + 1 else ns)
31 in loop (s, 0)
32 end)
33 ins
34 end
35 val rec loop =
36 fn 0 => ()
37 | n => (wc f; loop (n - 1))
38 val _ = loop n
39 val _ = OS.FileSys.remove f
40 in ()
41 end
42 end