Repository navigation
Expand file tree
/
Copy pathassert.ml
More file actions
125 lines (94 loc) · 3.86 KB
/
Copy pathassert.ml
File metadata and controls
125 lines (94 loc) · 3.86 KB
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
(* CIS341 Assertion Testing and Grading Infrastructure *)
(* Author: Steve Zdancewic *)
(* Do NOT modify this file -- we will overwrite it *)
(* with our own version when testing your code. *)
(* An assertion is just a unit->unit function that either *)
(* succeeds silently or throws an Failure exception. *)
type assertion = (unit -> unit)
type 'a test =
| GradedTest of string * int * (string * 'a) list
| Test of string * (string * 'a) list
type suite = (assertion test) list
(**************)
(* Assertions *)
let assert_eq v1 v2 : assertion =
fun () -> if v1 <> v2 then failwith "not equal" else ()
let assert_eqf f v2 : assertion =
fun () -> if (f ()) <> v2 then failwith "not equal" else ()
let assert_fail : assertion = fun () -> failwith "assert fail"
(***************************)
(* Generating Test Results *)
type result =
| Pass
| Fail of string
type outcome = (result test) list
let run_assertion (f:assertion) : result =
try
f ();
Pass
with
| Failure m -> Fail m
| e -> Fail ("test threw exception: " ^ (Printexc.to_string e))
let run_test (t:assertion test) : result test =
let run_case (cn, f) = (cn, run_assertion f) in
begin match t with
| GradedTest (n,s,cases) -> GradedTest(n,s,List.map run_case cases)
| Test (n, cases) -> Test(n, List.map run_case cases)
end
let run_suite (s:suite):outcome =
List.map run_test s
(***********************)
(* Reporting functions *)
let result_test_to_string (name_pts:string) (r:result test): string =
let string_of_case (name, res) =
" " ^ name ^ ": " ^ (
begin match res with
| Pass -> "passed"
| Fail msg -> "failed - " ^ msg
end
) in
begin match r with
| GradedTest (_, _, cases) ->
name_pts ^
(List.fold_left (fun rest -> fun case -> rest ^ "\n" ^ (string_of_case case)) "" cases)
| Test (_, cases) ->
name_pts ^ ":" ^
(List.fold_left (fun rest -> fun case -> rest ^ "\n" ^ (string_of_case case)) "" cases)
end
(* returns (name_pts, passed, failed, total, points_earned, max_given, max_hidden) *)
let get_results (t:result test) =
let num_passed cases =
List.fold_left (fun cnt (_,r) -> match r with Pass -> cnt + 1 | _ -> cnt) 0 cases in
let num_failed cases =
List.fold_left (fun cnt (_,r) -> match r with Fail _ -> cnt + 1 | _ -> cnt) 0 cases in
begin match t with
| GradedTest (name,pts,cases) ->
let passed = num_passed cases in
let failed = num_failed cases in
let total = List.length cases in
if total > 0 then
let points_earned = ((float_of_int passed) /. (float_of_int total)) *. (float_of_int pts) in
let name_pts = Printf.sprintf "%s (%1.f/%d points)" name points_earned pts in
(name_pts, passed, failed, total, points_earned, pts, 0)
else
let name_pts = Printf.sprintf "%s (??/%d points)" name pts in
(name_pts, passed, failed, total, 0.0, 0, pts)
| Test(name, cases) ->
let total = List.length cases in
let passed = num_passed cases in
let failed = num_failed cases in
(name, passed, failed, total, 0.0, 0, 0)
end
let outcome_to_string (o:outcome):string =
let sep = "\n---------------------------------------------------\n" in
let helper (passed, failed, total, pts, maxg, maxh, str) (t:result test) =
let (name_pts, p, f, tot, s, mg, mh) = get_results t in
(passed + p, failed + f, total + tot, s +. pts, maxg + mg, maxh + mh,
str ^ "\n" ^ (
if f > 0 then (result_test_to_string name_pts t) else
if tot > 0 then (name_pts ^ ":\n OK") else
(name_pts ^ ":\n Hidden")
)
) in
let (p,f,tot,pts,maxg, maxh,str) = List.fold_left helper (0,0,0,0.0,0,0,"") o in
str ^ sep ^ (Printf.sprintf "Passed: %d/%d\nFailed: %d/%d\nScore: %1.f/%d (given)\n ??/%d (hidden)" p tot f tot pts maxg maxh)