Skip to content

Commit 08e5878

Browse files
authored
Merge pull request #391 from jmid/revisit-oneof-opt-args
The Great Renaming, Chapter 17: Revisiting `QCheck.oneof` optional arguments
2 parents 377b94e + 5bc4ad8 commit 08e5878

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@
102102
- Add `QCheck.Gen.oneof_array` and deprecate `QCheck.Gen.oneofa`
103103
- Add `QCheck.oneof_list` and deprecate `QCheck.oneofl`
104104
- Add `QCheck.oneof_array` and deprecate `QCheck.oneofa`
105-
- Un-deprecate `QCheck.oneof` with a better specification and deprecate `QCheck.choose` for consistency
105+
- Un-deprecate `QCheck.oneof` with a better specification and optional parameters
106+
like `QCheck.oneof_weighted` and deprecate `QCheck.choose` for consistency
106107
- Add `QCheck2.Gen.oneof_list` and deprecate `QCheck2.Gen.oneofl`
107108
- Add `QCheck2.Gen.oneof_array` and deprecate `QCheck2.Gen.oneofa`
108109
- Add missing `QCheck2.Gen.map_keep_input` for consistency

src/core/QCheck.ml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,14 +1276,15 @@ let make_int ?collect gen =
12761276
let adapt_ o gen =
12771277
make ?print:o.print ?small:o.small ?shrink:o.shrink ?collect:o.collect gen
12781278

1279-
let oneof l = match l with
1279+
let oneof ?print ?small ?shrink l = match l with
12801280
| [] -> raise (Invalid_argument "QCheck.oneof")
12811281
| l ->
1282-
let a = Array.of_list l in
1283-
adapt_ a.(0)
1284-
(fun st ->
1285-
let arb = a.(RS.int st (Array.length a)) in
1286-
arb.gen st)
1282+
let first = List.hd l in
1283+
let print = _opt_sum print first.print in
1284+
let small = _opt_sum small first.small in
1285+
let shrink = _opt_sum shrink first.shrink in
1286+
let gens = List.map (fun x -> x.gen) l in
1287+
make ?print ?small ?shrink (Gen.oneof gens)
12871288

12881289
let unit : unit arbitrary =
12891290
make ~small:small1 ~shrink:Shrink.nil ~print:Print.unit Gen.unit
@@ -1864,14 +1865,14 @@ let fun4 o1 o2 o3 o4 ret =
18641865
(* Generator combinators *)
18651866

18661867
(** given a list, returns generator that picks at random from list *)
1867-
let oneof_list ?print xs = make ?print (Gen.oneof_list xs)
1868+
let oneof_list ?print ?small xs = make ?print ?small (Gen.oneof_list xs)
18681869
let oneofl ?print ?collect xs = make ?print ?collect (Gen.oneof_list xs)
1869-
let oneof_array ?print xs = make ?print (Gen.oneof_array xs)
1870+
let oneof_array ?print ?small xs = make ?print ?small (Gen.oneof_array xs)
18701871
let oneofa ?print ?collect xs = make ?print ?collect (Gen.oneof_array xs)
18711872

18721873
(** Given a list of generators, returns generator that randomly uses one of the generators
18731874
from the list *)
1874-
let choose = oneof
1875+
let choose l = oneof l
18751876

18761877
(** Generator that always returns given value *)
18771878
let always ?print x =

src/core/QCheck.mli

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,7 +1769,7 @@ val choose : 'a arbitrary list -> 'a arbitrary
17691769
be empty; if it is [Invalid_argument] is raised.
17701770
@deprecated use {!oneof} instead. *)
17711771

1772-
val oneof_list : ?print:'a Print.t -> 'a list -> 'a arbitrary
1772+
val oneof_list : ?print:'a Print.t -> ?small:('a -> int) -> 'a list -> 'a arbitrary
17731773
(** Pick an element randomly in the list.
17741774
@since NEXT_RELEASE *)
17751775

@@ -1778,7 +1778,7 @@ val oneofl : ?print:'a Print.t -> ?collect:('a -> string) ->
17781778
(** Pick an element randomly in the list.
17791779
@deprecated use {!oneof_list} instead. *)
17801780

1781-
val oneof_array : ?print:'a Print.t -> 'a array -> 'a arbitrary
1781+
val oneof_array : ?print:'a Print.t -> ?small:('a -> int) -> 'a array -> 'a arbitrary
17821782
(** Pick an element randomly in the array.
17831783
@since NEXT_RELEASE *)
17841784

@@ -1787,10 +1787,12 @@ val oneofa : ?print:'a Print.t -> ?collect:('a -> string) ->
17871787
(** Pick an element randomly in the array.
17881788
@deprecated use {!oneof_array} instead. *)
17891789

1790-
val oneof : 'a arbitrary list -> 'a arbitrary
1790+
val oneof : ?print:'a Print.t -> ?small:('a -> int) ->
1791+
?shrink:'a Shrink.t -> 'a arbitrary list -> 'a arbitrary
17911792
(** Pick a generator among the list, randomly.
1792-
The resulting generator uses the printer, shrinker, etc.
1793-
from the first element in the argument list, if available.
1793+
The resulting generator uses any optional [print], [small], and [shrink]
1794+
parameter passed, and otherwise those from the first element in the argument
1795+
list, if available.
17941796
17951797
Consider using {!Gen.oneof} and then {!make} to build
17961798
an arbitrary instance with a different behaviour.

0 commit comments

Comments
 (0)