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 (* test
/stringcvt
.sml
-- 1995-10-26, 1996-07-05 *)
25 (*KILL
05/11/1997 11:02. tho
.:
29 val _
= print
"\nFile stringcvt.sml: Testing structure StringCvt...\n";
33 (* Read all upper
case letters
, skip lowercase letters
, scan an
34 * integer
, and return the excess characters
: *)
38 val (str1
, src1
) = splitl
Char.isUpper getc src
39 val src2
= dropl
Char.isLower getc src1
40 in case Int.scan DEC getc src2
of
43 let val str2
= takel (fn _
=> true) getc src3
44 in SOME((str1
, i
, str2
), src3
) end
47 (* Testing
TextIO.scanStream
: *)
49 val tmpfile
= "textio.tmp";
50 fun putandscan scan s
=
52 val os
= openOut tmpfile
55 val is
= openIn tmpfile
62 fun testtrip (s
, res
) =
63 scanString triple s
= res
64 andalso putandscan triple s
= res
66 datatype result
= Bool of bool |
Int of int
68 fun backtrack getc src
=
69 case Bool.scan getc src
of
70 SOME(b
, rest
) => SOME (Bool b
, rest
)
72 case Int.scan
StringCvt.DEC getc src
of
73 SOME(i
, rest
) => SOME(Int i
, rest
)
75 case Int.scan
StringCvt.HEX getc src
of
76 SOME(i
, rest
) => SOME(Int i
, rest
)
79 fun testback (s
, res
) =
80 scanString backtrack s
= res
81 andalso putandscan backtrack s
= res
87 padLeft #
"#" 0 "abcdef" = "abcdef"
88 andalso padLeft #
"#" 6 "abcdef" = "abcdef"
89 andalso padLeft #
"#" 7 "abcdef" = "#abcdef"
90 andalso padLeft #
"#" 10 "abcdef" = "####abcdef"
91 andalso padLeft #
"#" ~
3 "abcdef" = "abcdef");
95 padRight #
"#" 0 "abcdef" = "abcdef"
96 andalso padRight #
"#" 6 "abcdef" = "abcdef"
97 andalso padRight #
"#" 7 "abcdef" = "abcdef#"
98 andalso padRight #
"#" 10 "abcdef" = "abcdef####"
99 andalso padRight #
"#" ~
3 "abcdef" = "abcdef");
102 tst
' "test3" (fn _
=>
104 andalso testtrip(" a1", NONE
)
105 andalso testtrip(" A1", NONE
)
106 andalso testtrip("ABC A1", NONE
)
107 andalso testtrip("ABC a1", NONE
)
108 andalso testtrip(" *1", NONE
)
109 andalso testtrip("ABC *1", NONE
));
112 tst
' "test4" (fn _
=>
113 testtrip ("1", SOME("", 1, ""))
114 andalso testtrip ("1", SOME("", 1, ""))
115 andalso testtrip (" 1", SOME("", 1, ""))
116 andalso testtrip (" 1 ", SOME("", 1, " ")));
119 tst
' "test5" (fn _
=>
120 testtrip ("1a123+ +&D", SOME("", 1, "a123+ +&D"))
121 andalso testtrip ("1a123+ +&D", SOME("", 1, "a123+ +&D"))
122 andalso testtrip ("a1a123+ +&D", SOME("", 1, "a123+ +&D"))
123 andalso testtrip ("a1a123+ +&D", SOME("", 1, "a123+ +&D"))
124 andalso testtrip ("azbc1a123+ +&D", SOME("", 1, "a123+ +&D"))
125 andalso testtrip ("azbc1a123+ +&D", SOME("", 1, "a123+ +&D"))
126 andalso testtrip ("azbc 1a123+ +&D", SOME("", 1, "a123+ +&D"))
127 andalso testtrip ("azbc 1a123+ +&D", SOME("", 1, "a123+ +&D")))
130 tst
' "test6" (fn _
=>
131 testtrip ("~1234a123+ +&D", SOME("", ~
1234, "a123+ +&D"))
132 andalso testtrip ("~1234a123+ +&D", SOME("", ~
1234, "a123+ +&D"))
133 andalso testtrip ("a~1234a123+ +&D", SOME("", ~
1234, "a123+ +&D"))
134 andalso testtrip ("a~1234a123+ +&D", SOME("", ~
1234, "a123+ +&D"))
135 andalso testtrip ("azbc~1234a123+ +&D", SOME("", ~
1234, "a123+ +&D"))
136 andalso testtrip ("azbc~1234a123+ +&D", SOME("", ~
1234, "a123+ +&D"))
137 andalso testtrip ("azbc ~1234a123+ +&D", SOME("", ~
1234, "a123+ +&D"))
138 andalso testtrip ("azbc ~1234a123+ +&D", SOME("", ~
1234, "a123+ +&D")))
141 tst
' "test7" (fn _
=>
142 testtrip ("A1a123+ +&D", SOME("A", 1, "a123+ +&D"))
143 andalso testtrip ("ABCDEFG1a123+ +&D", SOME("ABCDEFG", 1, "a123+ +&D"))
144 andalso testtrip ("Aa1a123+ +&D", SOME("A", 1, "a123+ +&D"))
145 andalso testtrip ("ABCDEFGa1a123+ +&D", SOME("ABCDEFG", 1, "a123+ +&D"))
146 andalso testtrip ("Aazbc1a123+ +&D", SOME("A", 1, "a123+ +&D"))
147 andalso testtrip ("ABCDEFGazbc1a123+ +&D", SOME("ABCDEFG", 1, "a123+ +&D"))
148 andalso testtrip ("Aazbc 1a123+ +&D", SOME("A", 1, "a123+ +&D"))
149 andalso testtrip ("ABCDEFGazbc 1a123+ +&D", SOME("ABCDEFG", 1, "a123+ +&D")))
152 tst
' "test8" (fn _
=>
153 testtrip ("A~1234a123+ +&D", SOME("A", ~
1234, "a123+ +&D"))
155 testtrip ("ABCDEFG~1234a123+ +&D", SOME("ABCDEFG", ~
1234, "a123+ +&D"))
156 andalso testtrip ("Aa~1234a123+ +&D", SOME("A", ~
1234, "a123+ +&D"))
158 testtrip ("ABCDEFGa~1234a123+ +&D", SOME("ABCDEFG", ~
1234, "a123+ +&D"))
159 andalso testtrip ("Aazbc~1234a123+ +&D", SOME("A", ~
1234, "a123+ +&D"))
161 testtrip ("ABCDEFGazbc~1234a123+ +&D", SOME("ABCDEFG", ~
1234, "a123+ +&D"))
162 andalso testtrip ("Aazbc ~1234a123+ +&D", SOME("A", ~
1234, "a123+ +&D"))
164 testtrip ("ABCDEFGazbc ~1234a123+ +&D", SOME("ABCDEFG", ~
1234, "a123+ +&D")))
167 tst
' "test9" (fn _
=>
168 let fun getstring b getc src
=
169 SOME(takel (fn _
=> b
) getc src
, src
)
171 | dup n s
= dup (n
-1) (s^s
);
172 val longstring
= dup
13 "abcDEFGHI"
174 scanString (getstring
true) longstring
= SOME longstring
175 andalso scanString (getstring
false) longstring
= SOME
""
176 andalso putandscan (getstring
true) longstring
= SOME longstring
180 tst
' "test10" (fn _
=>
182 [("false", SOME (Bool false)),
183 ("true", SOME (Bool true)),
185 ("fals e", SOME (Int 250)),
186 ("fa", SOME (Int 250)),
187 ("fa00", SOME (Int 64000)),
188 ("21a", SOME (Int 21)),
189 ("a21", SOME (Int 2593)),
194 (*val _
= FileSys
.remove tmpfile
*)