Skip to content

Commit

Permalink
Gen: enforce consistency in suffixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vch9 committed Jan 17, 2022
1 parent 28c2d3d commit 98c4704
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/core/QCheck2.ml
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,13 @@ module Gen = struct
let oneof (l : 'a t list) : 'a t =
int_range 0 (List.length l - 1) >>= List.nth l

let oneofl (l : 'a list) : 'a t =
let oneof_l (l : 'a list) : 'a t =
int_range 0 (List.length l - 1) >|= List.nth l
let oneofl = oneof_l

let oneofa (a : 'a array) : 'a t =
let oneof_a (a : 'a array) : 'a t =
int_range 0 (Array.length a - 1) >|= Array.get a
let oneofa = oneof_a

let int_small_signed : int t = fun st ->
if RS.bool st
Expand All @@ -503,11 +505,13 @@ module Gen = struct
in
aux 0 l

let frequencyl (l : (int * 'a) list) : 'a t =
let frequency_l (l : (int * 'a) list) : 'a t =
List.map (fun (weight, value) -> (weight, pure value)) l
|> frequency
let frequencyl = frequency_l

let frequencya a = frequencyl (Array.to_list a)
let frequency_a a = frequencyl (Array.to_list a)
let frequencya = frequency_a

let char_range ?(origin : char option) (a : char) (b : char) : char t =
(int_range ~origin:(Char.code (Option.value ~default:a origin)) (Char.code a) (Char.code b)) >|= Char.chr
Expand Down
46 changes: 45 additions & 1 deletion src/core/QCheck2.mli
Original file line number Diff line number Diff line change
Expand Up @@ -551,18 +551,40 @@ module Gen : sig
@raise Invalid_argument or Failure if [l] is empty
*)

val oneofl : 'a list -> 'a t
val oneof_l : 'a list -> 'a t
(** [oneof_l l] constructs a generator that selects among the given list of values [l].
Shrinks towards the first element of the list.
@raise Invalid_argument or Failure if [l] is empty
@since NEXT_RELEASE
*)

val oneofl : 'a list -> 'a t
(** [oneofl l] constructs a generator that selects among the given list of values [l].
Shrinks towards the first element of the list.
@raise Invalid_argument or Failure if [l] is empty
@deprecated use {!oneof_l}
*)

val oneof_a : 'a array -> 'a t
(** [oneof_a a] constructs a generator that selects among the given array of values [a].
Shrinks towards the first element of the array.
@raise Invalid_argument or Failure if [l] is empty
@since NEXT_RELEASE
*)

val oneofa : 'a array -> 'a t
(** [oneofa a] constructs a generator that selects among the given array of values [a].
Shrinks towards the first element of the array.
@raise Invalid_argument or Failure if [l] is empty
@deprecated use {!oneof_a}
*)

val frequency : (int * 'a t) list -> 'a t
Expand All @@ -572,18 +594,40 @@ module Gen : sig
Shrinks towards the first element of the list.
*)

val frequency_l : (int * 'a) list -> 'a t
(** Constructs a generator that selects among a given list of values.
Each of the given values are chosen based on a positive integer weight.
Shrinks towards the first element of the list.
@since NEXT_RELEASE
*)

val frequencyl : (int * 'a) list -> 'a t
(** Constructs a generator that selects among a given list of values.
Each of the given values are chosen based on a positive integer weight.
Shrinks towards the first element of the list.
@deprecated use {!frequency_l}
*)

val frequency_a : (int * 'a) array -> 'a t
(** Constructs a generator that selects among a given array of values.
Each of the array entries are chosen based on a positive integer weight.
Shrinks towards the first element of the array.
@since NEXT_RELEASE
*)

val frequencya : (int * 'a) array -> 'a t
(** Constructs a generator that selects among a given array of values.
Each of the array entries are chosen based on a positive integer weight.
Shrinks towards the first element of the array.
@deprecated use {!frequency_a}
*)

(** {3 Shuffling elements} *)
Expand Down

0 comments on commit 98c4704

Please sign in to comment.