Skip to content

Commit df4a6db

Browse files
authored
Merge pull request #298 from adhameer/test_lib_additions
Some convenience functions for graders
2 parents 145fabc + d32d612 commit df4a6db

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

src/grader/learnocaml_report.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,21 @@ let result items =
109109
let (n, b) = do_report items in
110110
(max n 0, b)
111111

112+
let rec scale ?(penalties = true) factor items =
113+
List.map (scale_item penalties factor) items
114+
and scale_item penalties factor = function
115+
| Section (text, report) ->
116+
Section (text, scale ~penalties factor report)
117+
| SectionMin (text, report, min) ->
118+
SectionMin (text,
119+
scale ~penalties factor report,
120+
if penalties then factor * min else min)
121+
| Message (text, Success n) ->
122+
Message (text, Success (factor * n))
123+
| Message (text, Penalty n) when penalties ->
124+
Message (text, Penalty (factor * n))
125+
| item -> item
126+
112127
let enc =
113128
let open Json_encoding in
114129
let text_enc =

src/grader/learnocaml_report.mli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ and inline =
3434
(** Gets the total successes of a report, and tells if a failure happened *)
3535
val result : t -> int * bool
3636

37+
(** Scales all of the point values of the items in a report by an integer
38+
factor. Useful for weighting different components of an exercise.
39+
If [penalties] ([true] by default), scales the values of [Penalty]
40+
items and the minimum values for [SectionMin], otherwise leaves them
41+
untouched. *)
42+
val scale : ?penalties: bool -> int -> t -> t
43+
3744
(** Gets a report as HTML in a string
3845
(if [not bare] add a container div and inline style) *)
3946
val to_html : ?bare: bool -> t -> string

src/grader/test_lib.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ module type S = sig
119119
val sample_bool : bool sampler
120120
val sample_list : ?min_size: int -> ?max_size: int -> ?dups: bool -> ?sorted: bool -> 'a sampler -> 'a list sampler
121121
val sample_array : ?min_size: int -> ?max_size: int -> ?dups: bool -> ?sorted: bool -> 'a sampler -> 'a array sampler
122+
val sample_pair : 'a sampler -> 'b sampler -> ('a * 'b) sampler
122123
val sample_alternatively : 'a sampler list -> 'a sampler
123124
val sample_cases : 'a list -> 'a sampler
124125
val sample_option : 'a sampler -> 'a option sampler
@@ -461,6 +462,8 @@ module type S = sig
461462
end
462463

463464
val (@@@) : ('a -> Learnocaml_report.t) -> ('a -> Learnocaml_report.t) -> ('a -> Learnocaml_report.t)
465+
val (@@>) : Learnocaml_report.t -> (unit -> Learnocaml_report.t) -> Learnocaml_report.t
466+
val (@@=) : Learnocaml_report.t -> (unit -> Learnocaml_report.t) -> Learnocaml_report.t
464467

465468
(**/**)
466469
include (module type of Ast_checker)
@@ -1860,6 +1863,9 @@ module Make
18601863
let sample_list ?min_size ?max_size ?dups ?sorted sample () =
18611864
Array.to_list (sample_array ?min_size ?max_size ?dups ?sorted sample ())
18621865
1866+
let sample_pair sample1 sample2 () =
1867+
(sample1 (), sample2 ())
1868+
18631869
let printable_funs = ref []
18641870
18651871
let fun_printer ppf f =
@@ -1883,6 +1889,9 @@ module Make
18831889
end
18841890
18851891
let (@@@) f g = fun x -> f x @ g x
1892+
let (@@>) r1 f = if snd (Learnocaml_report.result r1) then r1 else f ()
1893+
let (@@=) r1 f = if snd (Learnocaml_report.result r1) then r1 else r1 @ f ()
1894+
18861895
(**/**)
18871896
include Ast_checker
18881897
include Tester

src/grader/test_lib.mli

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,11 @@ module type S = sig
472472
-> 'a sampler
473473
-> 'a array sampler
474474

475+
(** [sample_pair s1 s2] returns a sampler that generates a value
476+
of type ['a * 'b] using [s1] and [s2] to generate values of
477+
type ['a] and ['b], respectively, on each call. *)
478+
val sample_pair : 'a sampler -> 'b sampler -> ('a * 'b) sampler
479+
475480
(** [sample_alternatively s] returns a sampler that mimics
476481
randomly the behavior of one of [s] sampler and change at each
477482
call. *)
@@ -1243,6 +1248,22 @@ module type S = sig
12431248
-> ('a -> Learnocaml_report.t)
12441249
-> ('a -> Learnocaml_report.t)
12451250

1251+
(** [r1 @@> f] returns [r1] if [r1] is a failure report,
1252+
otherwise returns the result of report generator
1253+
[f ()]. *)
1254+
val (@@>) :
1255+
Learnocaml_report.t
1256+
-> (unit -> Learnocaml_report.t)
1257+
-> Learnocaml_report.t
1258+
1259+
(** [r1 @@= f] returns [r1] if [r1] is a failure report,
1260+
otherwise concatenates [r1] to the result of report
1261+
generator [f ()]. *)
1262+
val (@@=) :
1263+
Learnocaml_report.t
1264+
-> (unit -> Learnocaml_report.t)
1265+
-> Learnocaml_report.t
1266+
12461267
(**/**)
12471268
include (module type of Ast_checker)
12481269
include (module type of Tester)

0 commit comments

Comments
 (0)