Skip to content

Commit 5e65bfe

Browse files
committed
Add Windows workaround to avoid leading exponent 0 in counterexamples
1 parent 9ffc075 commit 5e65bfe

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/core/QCheck.ml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ let string_fold_right f s acc =
9898
else loop (i-1) (f s.[i] acc) in
9999
loop (len-1) acc
100100

101+
let cut_exp_zero s =
102+
match String.split_on_char 'e' s with
103+
| [signif;exponent] ->
104+
(match exponent.[0] with (* int_of_string removes a leading '0' *)
105+
| '+' -> Printf.sprintf "%se+%i" signif (int_of_string exponent) (* keep a leading '+' *)
106+
| _ -> Printf.sprintf "%se%i" signif (int_of_string exponent)) (* keep a leading '-' *)
107+
| _ -> s
108+
101109
exception No_example_found of string
102110
(* raised if an example failed to be found *)
103111

@@ -502,7 +510,10 @@ module Print = struct
502510
let int32 i = Int32.to_string i ^ "l"
503511
let int64 i = Int64.to_string i ^ "L"
504512
let bool = string_of_bool
505-
let float = string_of_float
513+
let float f = (* Windows workaround to avoid leading exponent zero such as "-1.00001604579e-010" *)
514+
if Sys.win32
515+
then string_of_float f |> cut_exp_zero
516+
else string_of_float f
506517
let string s = Printf.sprintf "%S" s
507518
let bytes b = string (Bytes.to_string b)
508519
let char c = Printf.sprintf "%C" c

src/core/QCheck2.ml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ let rec list_split l len acc = match len,l with
6060
| 0,_ -> List.rev acc, l
6161
| _,x::xs -> list_split xs (len-1) (x::acc)
6262

63+
let cut_exp_zero s =
64+
match String.split_on_char 'e' s with
65+
| [signif;exponent] ->
66+
(match exponent.[0] with (* int_of_string removes a leading '0' *)
67+
| '+' -> Printf.sprintf "%se+%i" signif (int_of_string exponent) (* keep a leading '+' *)
68+
| _ -> Printf.sprintf "%se%i" signif (int_of_string exponent)) (* keep a leading '-' *)
69+
| _ -> s
70+
6371
exception Failed_precondition
6472
(* raised if precondition is false *)
6573

@@ -924,7 +932,10 @@ module Print = struct
924932

925933
let bool = string_of_bool
926934

927-
let float = string_of_float
935+
let float f = (* Windows workaround to avoid leading exponent zero such as "-1.00001604579e-010" *)
936+
if Sys.win32
937+
then string_of_float f |> cut_exp_zero
938+
else string_of_float f
928939

929940
let string s = Printf.sprintf "%S" s
930941

@@ -2118,10 +2129,6 @@ module Test = struct
21182129
(* print entries of the table, sorted by increasing index *)
21192130
let out = Buffer.create 128 in
21202131
(* Windows workaround to avoid annoying exponent zero such as "1.859e+018" *)
2121-
let cut_exp_zero s =
2122-
match String.split_on_char '+' s with
2123-
| [signif;exponent] -> Printf.sprintf "%s+%i" signif (int_of_string exponent)
2124-
| _ -> failwith "cut_exp_zero failed to parse scientific notation " ^ s in
21252132
let fmt_float f =
21262133
if f > 1e7 || f < -1e7 then cut_exp_zero (Printf.sprintf "%.3e" f) else Printf.sprintf "%.2f" f in
21272134
Printf.bprintf out "stats %s:\n" name;

0 commit comments

Comments
 (0)