Skip to content

Commit

Permalink
expose eval_criterion
Browse files Browse the repository at this point in the history
  • Loading branch information
just-max committed Oct 13, 2023
1 parent 702eb72 commit ab97b45
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
22 changes: 11 additions & 11 deletions src/test-lib/grading.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ let[@warning "-32"] is_fixed_query query =

let parse_name = String.split_on_char ':'

let rec eval_criterion (tests : tests) = function
let rec evaluate_criterion (tests : tests) = function
| Passed query ->
let q = parse_name query in
let matched = List.filter (fun (test, _) -> matches_test ~query:q (parse_name test)) tests in
if matched = [] then raise Not_found; (* at least one test needs to match *)
List.for_all snd matched
| Failed query ->
eval_criterion (List.map (fun (test, ok) -> (test, not ok)) tests) (Passed query)
| Not crit -> eval_criterion tests crit |> not
| OneOf crits -> List.exists (eval_criterion tests) crits
| AllOf crits -> List.for_all (eval_criterion tests) crits
evaluate_criterion (List.map (fun (test, ok) -> (test, not ok)) tests) (Passed query)
| Not crit -> evaluate_criterion tests crit |> not
| OneOf crits -> List.exists (evaluate_criterion tests) crits
| AllOf crits -> List.for_all (evaluate_criterion tests) crits
| Constant const -> const

let mk_indent n = String.make n ' '
Expand All @@ -70,16 +70,16 @@ exception No_reason

let points ?(skip = Constant false) ?(reason = fun _ _ -> None) ?penalty title points test_case =
let reason t c =
if eval_criterion t skip then raise No_reason else
if evaluate_criterion t skip then raise No_reason else
match (penalty, points < 0) with
| Some false, _ | None, false -> (
match reason t c with
| Some s -> s
| None -> if eval_criterion t c then "PASS" else "FAIL")
| None -> if evaluate_criterion t c then "PASS" else "FAIL")
| Some true, _ | None, true -> (
match reason t c with
| Some s -> s
| None -> if eval_criterion t c then "PENALTY" else raise No_reason)
| None -> if evaluate_criterion t c then "PENALTY" else raise No_reason)
in
Points { title; points; test_case; reason }
Expand All @@ -90,7 +90,7 @@ let assertion ?(message = "ASSERTION FAILED") ?(title = "assertion") points test
points;
test_case = Not test_case; (* fail the assertion if the test case DOES NOT pass *)
reason = (fun t _ ->
if eval_criterion t test_case then "PASS"
if evaluate_criterion t test_case then "PASS"
else "\n\n" ^ message ^ ":\n" ^ string_of_grading_criterion test_case ^ "\n")
}
Expand Down Expand Up @@ -119,7 +119,7 @@ let evaluate_grading ?(points_step_count = 1) grading tests =
let rec collect (* tests *) = function
| Points { title; test_case; points; reason } -> (
let max_points = max points 0 in
match (eval_criterion tests test_case, reason tests test_case) with
match (evaluate_criterion tests test_case, reason tests test_case) with
| true, s -> { text = Printf.sprintf "%s: \t%(%f%)P \t%s\n" title pprec (pointf points) s; points; max_points }
| false, s ->
{ text = Printf.sprintf "%s: \t(%(%f%)P) \t%s\n" title pprec (pointf points) s; points = 0; max_points }
Expand All @@ -142,7 +142,7 @@ let evaluate_grading ?(points_step_count = 1) grading tests =
points;
max_points = List.fold_left (fun a b -> a + b.max_points) 0 results |> clamp_opt max_points;
}
| Conditional { condition; content; _ } when eval_criterion tests condition -> collect content
| Conditional { condition; content; _ } when evaluate_criterion tests condition -> collect content
| Conditional { content; message; _ } ->
let result = collect content in
let name = String.split_on_char '\n' result.text |> List.hd |> String.split_on_char '\t' |> List.hd in
Expand Down
6 changes: 4 additions & 2 deletions src/test-lib/grading.mli
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ type grading_criterion =
val implies : grading_criterion -> grading_criterion -> grading_criterion
(** Logical implication: [implies a c = OneOf [Not a; c]]. *)

(* val mk_indent : int -> string *)
val evaluate_criterion : tests -> grading_criterion -> bool
(** Evaluate the logical formula over the assignment
of test names to boolean pass/fail values. *)

val string_of_grading_criterion : ?indent:int -> grading_criterion -> string
(* val eval_criterion : tests -> grading_criterion -> bool *)

(** Grading scheme. Defines how to assign a numeric grade for a given set of test results. *)
type grading =
Expand Down

0 comments on commit ab97b45

Please sign in to comment.