-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck.sml
81 lines (58 loc) · 2.33 KB
/
check.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
(*
ACKNOWLEDGEMENT: this file has been written by Professor Adam Shaw
from the University of Chicago and is taken from the course materials
for the Programming Languages Spring 2018 class.
http://people.cs.uchicago.edu/~adamshaw/cmsc22100-2018/index.html
*)
structure Check : sig
(* check if two items are equal by built-in polymorphic equality *)
val expect : ''a * ''a * string -> unit
(* check if given boolean is true *)
val assertT : bool * string -> unit
(* check if given boolean is false *)
val assertF : bool * string -> unit
(* check if two items are equal by equality function *)
val expectBy : ('a * 'a -> bool) * 'a * 'a * string -> unit
(* check if the first item is among the list by built-in polymorphic equality *)
val among : ''a * (''a list) * string -> unit
(* check if the first item is among the list by equality function *)
val amongBy : ('a * 'a -> bool) * 'a * ('a list) * string -> unit
(* check if two floating-point values are within epsilon of another *)
val within : real * real * real * string -> unit
(* check if given delayed computation raises an exception *)
val exn : ('a -> 'b) * 'a -> unit
end = struct
fun msg s m = "Check." ^ s ^ " failure: " ^ m ^ "\n"
fun assertT (b, m) =
if b then () else raise Fail (msg "assertT" m)
fun assertF (b, m) =
if not b then () else raise Fail (msg "assertF" m)
fun expect (x, y, m) =
if x=y then () else raise Fail (msg "expect" m)
fun expectBy (eq, x, y, m) =
if eq(x,y) then () else raise Fail (msg "expectBy" m)
fun among (x, ys, m) =
let
fun lp [] = raise Fail (msg "among" m)
| lp (y::ys) = if x=y then () else lp ys
in
lp ys
end
fun amongBy (eq, x, ys, m) =
let
fun lp [] = raise Fail (msg "amongBy" m)
| lp (y::ys) = if eq(x,y) then () else lp ys
in
lp ys
end
fun within (eps:real, x, y, m) =
if abs(x-y)<=eps then () else raise Fail (msg "within" m)
fun exn (compute, arg) =
let
val x = SOME (compute arg) handle _ => NONE
in
(case x
of NONE => ()
| SOME _ => raise Fail (""))
end
end