forked from ibolit/coursera_ml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_test.sml
281 lines (212 loc) · 5.8 KB
/
3_test.sml
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
use "3.sml" ;
fun check [] = "ok"
| check (false::xs) = "<----------------------------FAIL---------<<<"
| check (true::xs) = "." ^ check xs
(* 1 *)
fun test_only_capitals() =
let
val test = [
only_capitals(["A", "b", "AB", "aB"]) = ["A", "AB"],
only_capitals(["Hello", "hello"]) = ["Hello"],
only_capitals([]) = []
]
in
check(test)
end
(* 2 *)
fun test_longest_string1() =
let
val test = [
longest_string1(["One", "two", "three", "four", "five", "six", "Seven"]) = "three",
longest_string1(["One", "two", "four", "five", "six", "Seven"]) = "Seven",
longest_string1([]) = ""
]
in
check(test)
end
(* 3 *)
fun test_longest_string2() =
let
val test = [
longest_string2(["One", "two", "three", "four", "five", "six", "Seven"]) = "Seven",
longest_string2(["One", "two", "four", "five", "six", "Seven"]) = "Seven",
longest_string2([]) = ""
]
in
check(test)
end
(* 4.2 *)
fun test_longest_string3() =
let
val test = [
longest_string3(["One", "two", "three", "four", "five", "six", "Seven"]) = "three",
longest_string3(["One", "two", "four", "five", "six", "Seven"]) = "Seven",
longest_string3([]) = ""
]
in
check(test)
end
(* 4.3 *)
fun test_longest_string4() =
let
val test = [
longest_string4(["One", "two", "three", "four", "five", "six", "Seven"]) = "Seven",
longest_string4(["One", "two", "four", "five", "six", "Seven"]) = "Seven",
longest_string4([]) = ""
]
in
check(test)
end
(* 5 *)
fun test_longest_capitalized() =
let
val test = [
longest_capitalized ["A", "vely rong stling", "Not so long"] = "Not so long",
longest_capitalized ["A", "vely rong stling", "not so long"] = "A",
longest_capitalized ["aA", "vely rong stling", "not so long"] = ""
]
in
check(test)
end
(* 6 *)
fun test_rev_string() =
let
val test = [
rev_string ("Hello, brother") = "rehtorb ,olleH"
]
in
check(test)
end
(* 8 *)
fun test_all_answers() =
let
fun test_function arg =
if ((arg mod 5) > 0) then
SOME[Int.toString(arg) ^ "--"]
else
NONE
val test = [
all_answers test_function [1,2,3] = SOME["1--","2--","3--"],
all_answers test_function [3,4,5] = NONE,
all_answers test_function [] = SOME[]
]
in
check(test)
end
(* 9 *)
fun test_count_whildcards() =
let
val wildcards = Wildcard
val wildcards_2 = TupleP ([Wildcard, TupleP([Wildcard, ConstP(10)])])
val test = [
count_wildcards wildcards = 1,
count_wildcards wildcards_2 = 2
]
in
check(test)
end
(* b *)
fun test_count_whild_and_variable_lengths() =
let
val wildcards = Wildcard
val wildcards_2 = TupleP ([Wildcard, TupleP([Wildcard, ConstP(10)])])
val wildcards_6 = TupleP ([Wildcard, Variable("Hello")])
val wildcards_5 = Variable("Hello")
val test = [
count_wild_and_variable_lengths wildcards = 1,
count_wild_and_variable_lengths wildcards_2 = 2,
count_wild_and_variable_lengths wildcards_6 = 6,
count_wild_and_variable_lengths wildcards_5 = 5
]
in
check(test)
end
(* c *)
fun test_count_some_var() =
let
val wildcards = Wildcard
val wildcards_2 = TupleP ([Wildcard, TupleP([Wildcard, ConstP(10)])])
val wildcards_6 = TupleP ([Wildcard, Variable("Hello"), Variable("a"), Variable("b")])
val wildcards_5 = Variable("Hello")
val wildcards_0 = TupleP([Variable("a"), Variable("b")])
val test = [
count_some_var ("Hello", wildcards) = 0,
count_some_var ("Hello", wildcards_2) = 0,
count_some_var ("Hello", wildcards_6) = 1,
count_some_var ("Hello", wildcards_5) = 1,
count_some_var ("Hello", wildcards_0) = 0
]
in
check(test)
end
(* 10 *)
fun test_strings_from_pat() =
let
val wildcards = Wildcard
val wildcards_2 = TupleP ([Wildcard, TupleP([Wildcard, ConstP(10)])])
val wildcards_6 = TupleP ([Wildcard, Variable("Hello"), Variable("a"), Variable("b")])
val wildcards_5 = TupleP ([Variable("Hello"), TupleP ([Wildcard, Variable("Hello"), Variable("a"), Variable("b")]), Variable("Bye")])
val test = [
strings_from_pat (wildcards) = [],
strings_from_pat (wildcards_2) = [],
strings_from_pat (wildcards_6) = ["Hello", "a", "b"],
strings_from_pat (wildcards_5) = ["Hello", "Hello", "a", "b", "Bye"]
]
in
check(test)
end
fun test_unique_in_list() =
let
val test = [
unique_in_list(["Hello", "Hello", "a", "b", "Bye"]) = false,
unique_in_list(["Hello", "a", "b", "Bye"]) = true
]
in
check(test)
end
fun test_check_pat() =
let
val wildcards = Wildcard
val wildcards_2 = TupleP ([Wildcard, TupleP([Wildcard, ConstP(10)])])
val wildcards_6 = TupleP ([Wildcard, Variable("Hello"), Variable("a"), Variable("b")])
val wildcards_5 = TupleP ([Variable("Hello"), TupleP ([Wildcard, Variable("Hello"), Variable("a"), Variable("b")]), Variable("Bye")])
val test = [
check_pat (wildcards) = true,
check_pat (wildcards_2) = true,
check_pat (wildcards_6) = true,
check_pat (wildcards_5) = false
]
in
check(test)
end
(*
fun test_() =
let
val test = [
true
]
in
check(test)
end
*)
(* 7 *)
(* 8 *)
;
print "\n\n\n";
val test_only_capitals_ = test_only_capitals();
val test_longest_string1_ = test_longest_string1();
val test_longest_string2_ = test_longest_string2();
val test_longest_string3_ = test_longest_string3();
val test_longest_string4_ = test_longest_string4();
val test_longest_capitalized_ = test_longest_capitalized();
val test_rev_string_ = test_rev_string();
val test_all_answers_ = test_all_answers();
val test_count_whildcards_ = test_count_whildcards();
val test_count_whild_and_variable_lengths_ = test_count_whild_and_variable_lengths();
val test_count_some_var_ = test_count_some_var();
val test_strings_from_pat_ = test_strings_from_pat();
val test_unique_in_list_ = test_unique_in_list();
val test_check_pat_ = test_check_pat();
(*
val test_ = ;
*)