Skip to content

Commit f5f284f

Browse files
authored
Merge pull request #486 from ocaml-multicore/util-pp-add-fun
Add Util.Pp.fun_ printer for generated QCheck.fun_ functions
2 parents 6f00a59 + a94a040 commit f5f284f

11 files changed

+40
-22
lines changed

CHANGES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Next release
44

5-
- ...
5+
- #486: Add `Util.Pp.pp_fun_` printer for generated `QCheck.fun_` functions
66

77
## 0.4
88

lib/util.ml

+2
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ module Pp = struct
297297
fprintf fmt "@[<2>{ ";
298298
pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt ";@ ") (fun fmt ppf -> ppf fmt) fmt fields;
299299
fprintf fmt "@ }@]"
300+
301+
let pp_fun_ par fmt f = fprintf fmt (if par then "(%s)" else "%s") (QCheck.Fn.print f)
300302
end
301303

302304
module Equal = struct

lib/util.mli

+3
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ module Pp : sig
226226
val pp_record : pp_field list t
227227
(** [pp_record flds] pretty-prints a record using the list of pretty-printers
228228
of its fields. *)
229+
230+
val pp_fun_ : _ QCheck.fun_ t
231+
(** Pretty-printer for QCheck's function type [fun_] *)
229232
end
230233

231234
module Equal : sig

src/array/stm_tests.ml

+6-11
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ open STM
55

66
module AConf =
77
struct
8-
type char_bool_fun = (char -> bool) fun_
9-
10-
let pp_char_bool_fun par fmt f =
11-
Format.fprintf fmt (if par then "(%s)" else "%s") (Fn.print f)
12-
138
type cmd =
149
| Length
1510
| Get of int
@@ -18,10 +13,10 @@ struct
1813
| Copy
1914
| Fill of int * int * char
2015
| To_list
21-
| For_all of char_bool_fun
22-
| Exists of char_bool_fun
16+
| For_all of (char -> bool) fun_
17+
| Exists of (char -> bool) fun_
2318
| Mem of char
24-
| Find_opt of char_bool_fun
19+
| Find_opt of (char -> bool) fun_
2520
(*| Find_index of char_bool_fun since 5.1*)
2621
| Sort
2722
| Stable_sort
@@ -38,10 +33,10 @@ struct
3833
| Copy -> cst0 "Copy" fmt
3934
| Fill (x, y, z) -> cst3 pp_int pp_int pp_char "Fill" par fmt x y z
4035
| To_list -> cst0 "To_list" fmt
41-
| For_all f -> cst1 pp_char_bool_fun "For_all" par fmt f
42-
| Exists f -> cst1 pp_char_bool_fun "Exists" par fmt f
36+
| For_all f -> cst1 pp_fun_ "For_all" par fmt f
37+
| Exists f -> cst1 pp_fun_ "Exists" par fmt f
4338
| Mem x -> cst1 pp_char "Mem" par fmt x
44-
| Find_opt f -> cst1 pp_char_bool_fun "Find_opt" par fmt f
39+
| Find_opt f -> cst1 pp_fun_ "Find_opt" par fmt f
4540
(*| Find_index f -> cst1 pp_char_bool_fun "Find_index" par fmt f*)
4641
| Sort -> cst0 "Sort" fmt
4742
| Stable_sort -> cst0 "Stable_sort" fmt

src/lazy/stm_tests.ml

+4-8
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,17 @@ struct
3737
| Force
3838
| Force_val
3939
| Is_val
40-
| Map of int_fun
41-
| Map_val of int_fun
42-
and int_fun = (int -> int) fun_
43-
44-
let pp_int_fun par fmt f =
45-
Format.fprintf fmt (if par then "(%s)" else "%s") (Fn.print f)
40+
| Map of (int -> int) fun_
41+
| Map_val of (int -> int) fun_
4642

4743
let pp_cmd par fmt x =
4844
let open Util.Pp in
4945
match x with
5046
| Force -> cst0 "Force" fmt
5147
| Force_val -> cst0 "Force_val" fmt
5248
| Is_val -> cst0 "Is_val" fmt
53-
| Map x -> cst1 pp_int_fun "Map" par fmt x
54-
| Map_val x -> cst1 pp_int_fun "Map_val" par fmt x
49+
| Map x -> cst1 pp_fun_ "Map" par fmt x
50+
| Map_val x -> cst1 pp_fun_ "Map_val" par fmt x
5551

5652
let show_cmd = Util.Pp.to_show pp_cmd
5753

test/dune

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
(name util_pp)
1111
(modules util_pp)
1212
(package qcheck-multicoretests-util)
13-
(libraries qcheck-multicoretests-util)
13+
(libraries qcheck-core qcheck-multicoretests-util)
1414
(action
1515
(setenv OCAMLRUNPARAM "%{env:OCAMLRUNPARAM=b},v=0"
1616
(setenv MCTUTILS_TRUNCATE ""

test/util_pp.expected

+3
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,6 @@ Test of pp_array pp_int (long):
7373
Test of pp_record:
7474
{ key = 123; value = "content" }
7575

76+
Test of pp_fun_:
77+
{(Some (-123456), a, xyz) -> true; (None, b, ) -> true; _ -> true}
78+

test/util_pp.ml

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ let seq_interval x y () =
1111
in
1212
aux x
1313

14+
let fun_val () =
15+
let open QCheck in
16+
let bool = set_gen (Gen.return true) bool in (* fix co-domain/range across RNGs *)
17+
let gen = fun3 Observable.(option int) Observable.char Observable.string bool in
18+
let fun_ = Gen.generate1 gen.gen in
19+
let _ = Fn.apply fun_ (Some (-123456)) 'a' "xyz" in
20+
let _ = Fn.apply fun_ None 'b' "" in
21+
fun_
22+
1423
let _ =
1524
pr "pp_bool" pp_bool true;
1625
pr "pp_int (positive)" pp_int 12345;
@@ -41,4 +50,5 @@ let _ =
4150
pr "pp_array pp_int" (pp_array pp_int) [| 1; 2; 3; -1; -2; -3 |];
4251
pr "pp_array pp_int (long)" (pp_array pp_int) (Array.make 100 0);
4352
pr "pp_record" pp_record
44-
[ pp_field "key" pp_int 123; pp_field "value" pp_string "content" ]
53+
[ pp_field "key" pp_int 123; pp_field "value" pp_string "content" ];
54+
pr "pp_fun_" pp_fun_ (fun_val ())

test/util_pp_trunc150.expected

+3
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,6 @@ Test of pp_array pp_int (long):
7373
Test of pp_record:
7474
{ key = 123; value = "content" }
7575

76+
Test of pp_fun_:
77+
{(Some (-123456), a, xyz) -> true; (None, b, ) -> true; _ -> true}
78+

test/util_pp_trunc5.expected

+3
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,6 @@ Test of pp_array pp_int (long):
7373
Test of pp_record:
7474
... (truncated)
7575

76+
Test of pp_fun_:
77+
... (truncated)
78+

test/util_pp_trunc79.expected

+3
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,6 @@ Test of pp_array pp_int (long):
7373
Test of pp_record:
7474
{ key = 123; value = "content" }
7575

76+
Test of pp_fun_:
77+
{(Some (-123456), a, xyz) -> true; (None, b, ) -> true; _ -> true}
78+

0 commit comments

Comments
 (0)