Skip to content

Commit 2048a31

Browse files
authored
Merge pull request #307 from jmid/missing-int-32-64
Add missing int32 and int64 combinators
2 parents b5eb452 + 903bc9e commit 2048a31

File tree

5 files changed

+68
-9
lines changed

5 files changed

+68
-9
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## NEXT RELEASE
44

5+
- Add missing combinators `QCheck{,2}.Print.int{32,64}`, `QCheck.Gen.int{32,64}`,
6+
`QCheck{,2}.Observable.int{32,64}`, and deprecate `QCheck.Gen.{ui32,ui64}`
57
- Document `dune` usage in README
68

79
## 0.23

src/core/QCheck.ml

+15-6
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,11 @@ module Gen = struct
256256
done;
257257
Bytes.unsafe_to_string s
258258

259-
let ui32 st = Int32.of_string (random_binary_string st 32)
260-
let ui64 st = Int64.of_string (random_binary_string st 64)
259+
let int32 st = Int32.of_string (random_binary_string st 32)
260+
let int64 st = Int64.of_string (random_binary_string st 64)
261+
262+
let ui32 = int32
263+
let ui64 = int64
261264

262265
let list_size size gen st =
263266
foldn ~f:(fun acc _ -> (gen st)::acc) ~init:[] (size st)
@@ -477,6 +480,8 @@ module Print = struct
477480

478481
let unit _ = "()"
479482
let int = string_of_int
483+
let int32 i = Int32.to_string i ^ "l"
484+
let int64 i = Int64.to_string i ^ "L"
480485
let bool = string_of_bool
481486
let float = string_of_float
482487
let string s = Printf.sprintf "%S" s
@@ -961,6 +966,8 @@ module Observable = struct
961966
let combine a b = Hashtbl.seeded_hash a b
962967
let combine_f f s x = Hashtbl.seeded_hash s (f x)
963968
let int i = i land max_int
969+
let int32 (i:int32) = Hashtbl.hash i
970+
let int64 (i:int64) = Hashtbl.hash i
964971
let bool b = if b then 1 else 2
965972
let char x = Char.code x
966973
let bytes (x:bytes) = Hashtbl.hash x
@@ -977,6 +984,8 @@ module Observable = struct
977984
type 'a t = 'a -> 'a -> bool
978985

979986
let int : int t = (=)
987+
let int32 : int32 t = (=)
988+
let int64 : int64 t = (=)
980989
let bytes : bytes t = (=)
981990
let string : string t = (=)
982991
let bool : bool t = (=)
@@ -1010,6 +1019,8 @@ module Observable = struct
10101019
let unit : unit t = make ~hash:(fun _ -> 1) ~eq:Eq.unit Print.unit
10111020
let bool : bool t = make ~hash:H.bool ~eq:Eq.bool Print.bool
10121021
let int : int t = make ~hash:H.int ~eq:Eq.int Print.int
1022+
let int32 : int32 t = make ~hash:H.int32 ~eq:Eq.int32 Print.int32
1023+
let int64 : int64 t = make ~hash:H.int64 ~eq:Eq.int64 Print.int64
10131024
let float : float t = make ~eq:Eq.float Print.float
10141025
let bytes = make ~hash:H.bytes ~eq:Eq.bytes Print.bytes
10151026
let string = make ~hash:H.string ~eq:Eq.string Print.string
@@ -1122,11 +1133,9 @@ let small_int_corners () = make_int (Gen.nng_corners ())
11221133
let neg_int = make_int Gen.neg_int
11231134

11241135
let int32 =
1125-
make ~print:(fun i -> Int32.to_string i ^ "l") ~small:small1
1126-
~shrink:Shrink.int32 Gen.ui32
1136+
make ~print:Print.int32 ~small:small1 ~shrink:Shrink.int32 Gen.int32
11271137
let int64 =
1128-
make ~print:(fun i -> Int64.to_string i ^ "L") ~small:small1
1129-
~shrink:Shrink.int64 Gen.ui64
1138+
make ~print:Print.int64 ~small:small1 ~shrink:Shrink.int64 Gen.int64
11301139

11311140
let small_char target c = abs ((Char.code c) - (Char.code target))
11321141

src/core/QCheck.mli

+26-2
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,23 @@ module Gen : sig
337337

338338
val (--) : int -> int -> int t (** Synonym for {!int_range}. *)
339339

340-
val ui32 : int32 t (** Generates (unsigned) [int32] values. *)
340+
val int32 : int32 t
341+
(** Generates [int32] values uniformly.
342+
@since NEXT_RELEASE *)
343+
344+
val int64 : int64 t
345+
(** Generates [int64] values uniformly.
346+
@since NEXT_RELEASE *)
347+
348+
val ui32 : int32 t
349+
(** Generates [int32] values.
350+
@deprecated use {!val:int32} instead, the name is wrong, values {i are} signed.
351+
*)
341352

342-
val ui64 : int64 t (** Generates (unsigned) [int64] values. *)
353+
val ui64 : int64 t
354+
(** Generates [int64] values.
355+
@deprecated use {!val:int64} instead, the name is wrong, values {i are} signed.
356+
*)
343357

344358
val list : 'a t -> 'a list t
345359
(** Builds a list generator from an element generator. List size is generated by {!nat}. *)
@@ -639,6 +653,14 @@ module Print : sig
639653

640654
val int : int t (** Integer printer. *)
641655

656+
val int32 : int32 t
657+
(** 32-bit integer printer.
658+
@since NEXT_RELEASE *)
659+
660+
val int64 : int64 t
661+
(** 64-bit integer printer.
662+
@since NEXT_RELEASE *)
663+
642664
val bool : bool t (** Boolean printer. *)
643665

644666
val float : float t (** Floating point number printer. *)
@@ -1625,6 +1647,8 @@ module Observable : sig
16251647
val unit : unit t
16261648
val bool : bool t
16271649
val int : int t
1650+
val int32 : int32 t (** @since NEXT_RELEASE *)
1651+
val int64 : int64 t (** @since NEXT_RELEASE *)
16281652
val float : float t
16291653
val string : string t
16301654
val bytes : bytes t (** @since 0.20 *)

src/core/QCheck2.ml

+8
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,8 @@ module Print = struct
792792
let unit _ = "()"
793793

794794
let int = string_of_int
795+
let int32 i = Int32.to_string i ^ "l"
796+
let int64 i = Int64.to_string i ^ "L"
795797

796798
let bool = string_of_bool
797799

@@ -979,6 +981,8 @@ module Observable = struct
979981
let combine_f f s x = Hashtbl.seeded_hash s (f x)
980982

981983
let int i = i land max_int
984+
let int32 (i:int32) = Hashtbl.hash i
985+
let int64 (i:int64) = Hashtbl.hash i
982986

983987
let bool b = if b then 1 else 2
984988

@@ -1002,6 +1006,8 @@ module Observable = struct
10021006
type 'a t = 'a -> 'a -> bool
10031007

10041008
let int : int t = (=)
1009+
let int32 : int32 t = (=)
1010+
let int64 : int64 t = (=)
10051011

10061012
let bytes : bytes t = (=)
10071013

@@ -1043,6 +1049,8 @@ module Observable = struct
10431049
let bool : bool t = make ~hash:H.bool ~eq:Eq.bool Print.bool
10441050

10451051
let int : int t = make ~hash:H.int ~eq:Eq.int Print.int
1052+
let int32 : int32 t = make ~hash:H.int32 ~eq:Eq.int32 Print.int32
1053+
let int64 : int64 t = make ~hash:H.int64 ~eq:Eq.int64 Print.int64
10461054

10471055
let float : float t = make ~eq:Eq.float Print.float
10481056

src/core/QCheck2.mli

+17-1
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,15 @@ module Print : sig
11111111
*)
11121112

11131113
val int : int t
1114-
(** [int] is a printer of integer. *)
1114+
(** [int] is a printer of integers. *)
1115+
1116+
val int32 : int32 t
1117+
(** [int32] is a printer of 32-bit integers.
1118+
@since NEXT_RELEASE *)
1119+
1120+
val int64 : int64 t
1121+
(** [int64] is a printer of 64-bit integers.
1122+
@since NEXT_RELEASE *)
11151123

11161124
val bool : bool t
11171125
(** [bool] is a printer of boolean. *)
@@ -1334,6 +1342,14 @@ module Observable : sig
13341342
val int : int t
13351343
(** [int] is an observable of [int]. *)
13361344

1345+
val int32 : int32 t
1346+
(** [int32] is an observable of [int32].
1347+
@since NEXT_RELEASE *)
1348+
1349+
val int64 : int64 t
1350+
(** [int64] is an observable of [int64].
1351+
@since NEXT_RELEASE *)
1352+
13371353
val float : float t
13381354
(** [float] is an observable of [float]. *)
13391355

0 commit comments

Comments
 (0)