Skip to content

Instantly share code, notes, and snippets.

@Octachron
Created March 2, 2022 16:05
Show Gist options
  • Save Octachron/2a72556bba4ca1cdfdd04ea9133ddec3 to your computer and use it in GitHub Desktop.
Save Octachron/2a72556bba4ca1cdfdd04ea9133ddec3 to your computer and use it in GitHub Desktop.
type 'a printer = Format.formatter -> 'a -> unit
type 'a rand = unit -> 'a
let flushed_list pr ppf x =
List.iter (Format.fprintf ppf "%a@." pr) x
module type testable = sig
type t
val pp: t printer
val rand: t rand
end
(* the exponential distribution is the minimal-entropy distribution for a fixed mean *)
let randexp scale () =
int_of_float ( float_of_int scale *. -. (log (Random.float 1.)))
module String = struct
type t = string
let pp ppf s = Format.fprintf ppf "%s" s
let rand () = String.init (randexp 100 ()) (fun _ -> Char.chr (32 + Random.int 95))
end
module Flushed_list_f(X:testable) = struct
type t = X.t list
let pp ppf x = flushed_list X.pp ppf x
let rand () = List.init (randexp 100_000 ()) (fun _ -> X.rand ())
end
module T = Flushed_list_f(String)
let tests: (module testable) list = [
(module T);
]
let test (module X:testable) = Format.printf "@[%a@]@." X.pp (X.rand ())
(* Compatibility *)
module Domain = struct
let spawn f = ()
let join () = ()
end
open Stdlib
let () =
let spawn = ref false in
let seed = 10 in
let args =
[
"-spawn", Arg.Set spawn, "spawn a domain";
]
in
Arg.parse args ignore "test -spawn=<bool> -seed=<int>...";
let () = Random.init seed in
let maybe_join_later =
if !spawn then
let d = Domain.spawn ignore in
fun () -> Domain.join d
else ignore
in
Format.set_geometry ~margin:100 ~max_indent:99;
List.iter test tests;
maybe_join_later ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment