<commit_before>(*
QCheck: Random testing for OCaml
copyright (c) 2013-2017, <NAME>, <NAME>, <NAME>, <NAME>
all rights reserved.
*)

module Color = struct
  let fpf = Printf.fprintf
  type color =
    [ `Red
    | `Yellow
    | `Green
    | `Blue
    | `Normal
    | `Cyan
    ]

  let int_of_color_ : color -> int = function
    | `Normal -> 0
    | `Red -> 1
    | `Green -> 2
    | `Yellow -> 3
    | `Blue -> 4
    | `Cyan -> 6

  (* same as [pp], but in color [c] *)
  let in_color c pp out x =
    let n = int_of_color_ c in
    fpf out "\x1b[3%dm" n;
    pp out x;
    fpf out "\x1b[0m"

  (* same as [pp], but in bold color [c] *)
  let in_bold_color c pp out x =
    let n = int_of_color_ c in
    fpf out "\x1b[3%d;1m" n;
    pp out x;
    fpf out "\x1b[0m"

  let reset_line = "\x1b[2K\r"

  let pp_str_c ?(bold=true) ~colors c out s =
    if colors then
      if bold then in_bold_color c output_string out s
      else in_color c output_string out s
    else output_string out s
end

let seed = ref ~-1
let st = ref None

let set_seed_ ~colors s =
  seed := s;
  if colors then Printf.printf "%srandom seed: %d\n%!" Color.reset_line s
  else Printf.printf "random seed: %d\n%!" s;
  let state = Random.State.make [| s |] in
  st := Some state;
  state

(* time of last printed message. Useful for rate limiting in verbose mode *)
let last_msg = ref 0.

let time_between_msg = ref 0.1

let get_time_between_msg () = !time_between_msg

let set_time_between_msg f = time_between_msg := f

let set_seed s = ignore (set_seed_ ~colors:false s)

let setup_random_state_ ~colors () =
  let s = if !seed = ~-1 then (
      Random.self_init ();  (* make new, truly random seed *)
      Random.int (1 lsl 29);
    ) else !seed in
  set_seed_ ~colors s

(* initialize random generator from seed (if any) *)
let random_state_ ~colors () = match !st with
  | Some st -> st
  | None -> setup_random_state_ ~colors ()

let random_state() = random_state_ ~colors:false ()

let verbose, set_verbose =
  let r = ref false in
  (fun () -> !r), (fun b -> r := b)

let long_tests, set_long_tests =
  let r = ref false in
  (fun () -> !r), (fun b -> r := b)<commit_msg>add QCHECK_MSG_INTERVAL environment variable<commit_after>(*
QCheck: Random testing for OCaml
copyright (c) 2013-2017, <NAME>, <NAME>, <NAME>, <NAME>
all rights reserved.
*)

module Color = struct
  let fpf = Printf.fprintf
  type color =
    [ `Red
    | `Yellow
    | `Green
    | `Blue
    | `Normal
    | `Cyan
    ]

  let int_of_color_ : color -> int = function
    | `Normal -> 0
    | `Red -> 1
    | `Green -> 2
    | `Yellow -> 3
    | `Blue -> 4
    | `Cyan -> 6

  (* same as [pp], but in color [c] *)
  let in_color c pp out x =
    let n = int_of_color_ c in
    fpf out "\x1b[3%dm" n;
    pp out x;
    fpf out "\x1b[0m"

  (* same as [pp], but in bold color [c] *)
  let in_bold_color c pp out x =
    let n = int_of_color_ c in
    fpf out "\x1b[3%d;1m" n;
    pp out x;
    fpf out "\x1b[0m"

  let reset_line = "\x1b[2K\r"

  let pp_str_c ?(bold=true) ~colors c out s =
    if colors then
      if bold then in_bold_color c output_string out s
      else in_color c output_string out s
    else output_string out s
end

let seed = ref ~-1
let st = ref None

let set_seed_ ~colors s =
  seed := s;
  if colors then Printf.printf "%srandom seed: %d\n%!" Color.reset_line s
  else Printf.printf "random seed: %d\n%!" s;
  let state = Random.State.make [| s |] in
  st := Some state;
  state

(* time of last printed message. Useful for rate limiting in verbose mode *)
let last_msg = ref 0.

let time_between_msg =
  let env_var = "QCHECK_MSG_INTERVAL" in
  let default_interval = 0.1 in
  let interval = match Sys.getenv_opt env_var with
    | None -> default_interval
    | Some f -> float_of_string f in
  if interval < 0. then invalid_arg (env_var ^ " must be >= 0 but value is " ^ string_of_float interval);
  ref interval

let get_time_between_msg () = !time_between_msg

let set_time_between_msg f = time_between_msg := f

let set_seed s = ignore (set_seed_ ~colors:false s)

let setup_random_state_ ~colors () =
  let s = if !seed = ~-1 then (
      Random.self_init ();  (* make new, truly random seed *)
      Random.int (1 lsl 29);
    ) else !seed in
  set_seed_ ~colors s

(* initialize random generator from seed (if any) *)
let random_state_ ~colors () = match !st with
  | Some st -> st
  | None -> setup_random_state_ ~colors ()

let random_state() = random_state_ ~colors:false ()

let verbose, set_verbose =
  let r = ref false in
  (fun () -> !r), (fun b -> r := b)

let long_tests, set_long_tests =
  let r = ref false in
  (fun () -> !r), (fun b -> r := b)
{'fraction_non_alphanumeric': 0.12188306104901118, 'fraction_numerical': 0.014617368873602751, 'mean_word_length': 2.710526315789474, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1763390', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1716, 'n_words': 607}