Skip to content

Commit 6a3ce07

Browse files
committed
fix(grading): avoid failing on sampling arrays with unique elements
Closes ocaml-sf/learn-ocaml-corpus#39 The sampler was attempting to generate N distinct elements by restarting the provided sampler an arbitrary number of times (100). It would fail if the domain is to small: now it might still return duplicates in this case, which is better than failing for our use-cases.
1 parent cad060f commit 6a3ce07

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

src/grader/test_lib.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1762,7 +1762,7 @@ module Make
17621762
if dups then sample else
17631763
let prev = Hashtbl.create max_size in
17641764
let rec sample_new steps =
1765-
if steps = 0 then invalid_arg "sample_array" else
1765+
if steps = 0 then sample () else
17661766
let s = sample () in
17671767
try Hashtbl.find prev s ; sample_new (steps - 1)
17681768
with Not_found -> Hashtbl.add prev s () ; s in

src/grader/test_lib.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,9 @@ module type S = sig
466466
sorted.
467467
468468
If [~dups:false] ([true] by default), all elements of generated
469-
array are unique.*)
469+
arrays are unique, or at least try hard to be in a reasonable time:
470+
if the codomain of [sampler] is too small there might still be
471+
duplicates.*)
470472
val sample_array :
471473
?min_size: int -> ?max_size: int -> ?dups: bool -> ?sorted: bool
472474
-> 'a sampler

0 commit comments

Comments
 (0)