Commit | Line | Data |
---|---|---|
7f918cf1 CE |
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 | ||
23 | (* test/stringcvt.sml -- 1995-10-26, 1996-07-05 *) | |
24 | ||
25 | (*KILL 05/11/1997 11:02. tho.: | |
26 | use "auxil.sml"; | |
27 | *) | |
28 | ||
29 | val _ = print "\nFile stringcvt.sml: Testing structure StringCvt...\n"; | |
30 | ||
31 | local | |
32 | open StringCvt | |
33 | (* Read all upper case letters, skip lowercase letters, scan an | |
34 | * integer, and return the excess characters: *) | |
35 | ||
36 | fun triple getc src = | |
37 | let open StringCvt | |
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 | |
41 | NONE => NONE | |
42 | | SOME (i, src3) => | |
43 | let val str2 = takel (fn _ => true) getc src3 | |
44 | in SOME((str1, i, str2), src3) end | |
45 | end | |
46 | ||
47 | (* Testing TextIO.scanStream: *) | |
48 | ||
49 | val tmpfile = "textio.tmp"; | |
50 | fun putandscan scan s = | |
51 | let open TextIO | |
52 | val os = openOut tmpfile | |
53 | val _ = output(os, s) | |
54 | val _ = closeOut os | |
55 | val is = openIn tmpfile | |
56 | in | |
57 | scanStream scan is | |
58 | before | |
59 | closeIn is | |
60 | end; | |
61 | ||
62 | fun testtrip (s, res) = | |
63 | scanString triple s = res | |
64 | andalso putandscan triple s = res | |
65 | ||
66 | datatype result = Bool of bool | Int of int | |
67 | ||
68 | fun backtrack getc src = | |
69 | case Bool.scan getc src of | |
70 | SOME(b, rest) => SOME (Bool b, rest) | |
71 | | NONE => | |
72 | case Int.scan StringCvt.DEC getc src of | |
73 | SOME(i, rest) => SOME(Int i, rest) | |
74 | | NONE => | |
75 | case Int.scan StringCvt.HEX getc src of | |
76 | SOME(i, rest) => SOME(Int i, rest) | |
77 | | NONE => NONE | |
78 | ||
79 | fun testback (s, res) = | |
80 | scanString backtrack s = res | |
81 | andalso putandscan backtrack s = res | |
82 | ||
83 | in | |
84 | ||
85 | val test1 = | |
86 | tst' "test1" (fn _ => | |
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"); | |
92 | ||
93 | val test2 = | |
94 | tst' "test2" (fn _ => | |
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"); | |
100 | ||
101 | val test3 = | |
102 | tst' "test3" (fn _ => | |
103 | testtrip ("", NONE) | |
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)); | |
110 | ||
111 | val test4 = | |
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, " "))); | |
117 | ||
118 | val test5 = | |
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"))) | |
128 | ||
129 | val test6 = | |
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"))) | |
139 | ||
140 | val test7 = | |
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"))) | |
150 | ||
151 | val test8 = | |
152 | tst' "test8" (fn _ => | |
153 | testtrip ("A~1234a123+ +&D", SOME("A", ~1234, "a123+ +&D")) | |
154 | andalso | |
155 | testtrip ("ABCDEFG~1234a123+ +&D", SOME("ABCDEFG", ~1234, "a123+ +&D")) | |
156 | andalso testtrip ("Aa~1234a123+ +&D", SOME("A", ~1234, "a123+ +&D")) | |
157 | andalso | |
158 | testtrip ("ABCDEFGa~1234a123+ +&D", SOME("ABCDEFG", ~1234, "a123+ +&D")) | |
159 | andalso testtrip ("Aazbc~1234a123+ +&D", SOME("A", ~1234, "a123+ +&D")) | |
160 | andalso | |
161 | testtrip ("ABCDEFGazbc~1234a123+ +&D", SOME("ABCDEFG", ~1234, "a123+ +&D")) | |
162 | andalso testtrip ("Aazbc ~1234a123+ +&D", SOME("A", ~1234, "a123+ +&D")) | |
163 | andalso | |
164 | testtrip ("ABCDEFGazbc ~1234a123+ +&D", SOME("ABCDEFG", ~1234, "a123+ +&D"))) | |
165 | ||
166 | val test9 = | |
167 | tst' "test9" (fn _ => | |
168 | let fun getstring b getc src = | |
169 | SOME(takel (fn _ => b) getc src, src) | |
170 | fun dup 0 s = s | |
171 | | dup n s = dup (n-1) (s^s); | |
172 | val longstring = dup 13 "abcDEFGHI" | |
173 | in | |
174 | scanString (getstring true) longstring = SOME longstring | |
175 | andalso scanString (getstring false) longstring = SOME "" | |
176 | andalso putandscan (getstring true) longstring = SOME longstring | |
177 | end) | |
178 | ||
179 | val test10 = | |
180 | tst' "test10" (fn _ => | |
181 | List.all testback | |
182 | [("false", SOME (Bool false)), | |
183 | ("true", SOME (Bool true)), | |
184 | ("tru e", NONE), | |
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)), | |
190 | ("", NONE), | |
191 | ("gryf", NONE) | |
192 | ]); | |
193 | ||
194 | (*val _ = FileSys.remove tmpfile*) | |
195 | ||
196 | end |