text
stringlengths
0
601k
module type Value = sig type t [ @@ deriving compare , quickcheck , sexp_of ] val zero : t val one : t val num_bits : int val ( + ) : t -> t -> t val ( lsr ) : t -> int -> t val ( land ) : t -> t -> t val to_int_exn : t -> int end
module type Test = sig type t val count_set_bits : t -> int val count_leading_zeros : t -> int val count_trailing_zeros : t -> int val count_leading_zeros_nonzero_arg : t -> int val count_trailing_zeros_nonzero_arg : t -> int end
module Make ( V : Value ) ( T : Test with type t = V . t ) = struct let count_set_bits_naive ( v : V . t ) : int = let open V in let rec loop n count = if V . compare n zero <> 0 then loop ( n lsr 1 ) ( count + ( n land one ) ) else count in loop v zero |> to_int_exn ; ; let % test_unit " count_set_bits " = Base_quickc...
module BI = struct include Base . Int type t = int [ @@ deriving quickcheck ] end
module I = struct include Ocaml_intrinsics . Int let count_trailing_zeros_nonzero_arg = count_trailing_zeros let count_leading_zeros_nonzero_arg = count_leading_zeros type t = int end include Make ( BI ) ( struct include I let count_leading_zeros = Ocaml_intrinsics . Int . count_leading_zeros2 let count_set_bits = Ocam...
module RS = struct let split rs = let bits = Random . State . bits rs in let rs ' = Random . State . make [ | bits ] | in rs ' include Random . State end
let rec foldn ~ f ~ init : acc i = if i = 0 then acc else foldn ~ f ~ init ( : f acc i ) i ( i - 1 ) 1
let _opt_map_2 ~ f a b = match a , b with | Some x , Some y -> Some ( f x y ) y | _ -> None
let _opt_map_3 ~ f a b c = match a , b , c with | Some x , Some y , Some z -> Some ( f x y z ) z | _ -> None
let _opt_map_4 ~ f a b c d = match a , b , c , d with | Some x , Some y , Some z , Some w -> Some ( f x y z w ) w | _ -> None
let _opt_sum a b = match a , b with | Some _ , _ -> a | None , _ -> b
let sum_int = List . fold_left ( ) + 0
let assume b = if not b then raise Failed_precondition
let assume_fail ( ) = raise Failed_precondition
let ( ) ==> b1 b2 = if b1 then b2 else raise Failed_precondition
module Seq = struct include Seq let rec unfold f u ( ) = match f u with | None -> Nil | Some ( x , u ' ) u ' -> Cons ( x , unfold f u ' ) u ' let rec append seq1 seq2 ( ) = match seq1 ( seq1 ) seq1 with | Nil -> seq2 ( seq2 ) seq2 | Cons ( x , next ) next -> Cons ( x , append next seq2 ) seq2 let cons x next ( ) = Cons...
module Shrink = struct module type Number = sig type t val equal : t -> t -> bool val div : t -> t -> t val add : t -> t -> t val sub : t -> t -> t val of_int : int -> t end let number_towards ( type a ) a ( module Number : Number with type t = a ) a ( ~ destination : a ) a ( x : a ) a : a Seq . t = fun ( ) -> Seq . un...
module Tree = struct type ' a t = Tree of ' a * ( ' a t ) t Seq . t let root ( Tree ( root , _ ) _ : ' a t ) t : ' a = root let children ( Tree ( _ , children ) children : ' a t ) t : ( ' a t ) t Seq . t = children let rec pp ( ? depth : int option ) option ( inner_pp : Format . formatter -> ' a -> unit ) unit ( ppf : ...
module Gen = struct type ' a t = RS . t -> ' a Tree . t type ' a sized = int -> RS . t -> ' a Tree . t let map f x = fun st -> Tree . map f ( x st ) st let ( ) >|= x f = map f x let ( ) <$> = map let pure ( a : ' a ) ' a : ' a t = fun _ -> Tree . pure a let ap ( f : ( ' a -> ' b ) ' b t ) t ( x : ' a t ) t : ' b t = fu...
module Print = struct type ' a t = ' a -> string let unit _ = ( ) " " let int = string_of_int let bool = string_of_bool let float = string_of_float let string s = Printf . sprintf " % S " s let char c = Printf . sprintf " % C " c let option f = function | None -> " None " | Some x -> " Some ( " ^ f x ^ ) " " let pair a...
module Observable = struct type - ' a t = { print : ' a Print . t ; eq : ( ' a -> ' a -> bool ) bool ; hash : ( ' a -> int ) int ; } let hash o x = o . hash x let equal o x y = o . eq x y let print o x = o . print x let make ( ? eq ( ) ) == ( ? hash = Hashtbl . hash ) hash print = { print ; eq ; hash ; } module H = str...
type ' a stat = string * ( ' a -> int ) int
module Poly_tbl : sig type ( ' key , ' value ) ' value t val create : ' key Observable . t -> ? v_print : ' value Print . t -> ' value Gen . t -> int -> ( ' key , ' value ) ' value t Gen . t val get : ( ' key , ' value ) ' value t -> ' key -> ' value option val size : ( ' value -> int ) int -> ( ' key , ' value ) ' val...
type ( ' a , ' b ) ' b fun_repr_tbl = { fun_tbl : ( ' a , ' b ) ' b Poly_tbl . t ; fun_gen : ' b Gen . t ; fun_print : ' b Print . t option ; fun_default : ' b ; }
type ' f fun_repr = | Fun_tbl : ( ' a , ' ret ) ' ret fun_repr_tbl -> ( ' a -> ' ret ) ' ret fun_repr | Fun_map : ( ' f1 -> ' f2 ) ' f2 * ' f1 fun_repr -> ' f2 fun_repr
type ' f fun_ = Fun of ' f fun_repr * ' f
module Fn = struct let apply ( Fun ( _repr , real_function ) real_function ) real_function = real_function let rec function_of_repr : type f . f fun_repr -> f = function | Fun_tbl { fun_tbl ; fun_default ; _ } _ -> ( fun x -> match Poly_tbl . get fun_tbl x with | None -> fun_default | Some y -> y ) y | Fun_map ( g , su...
let fun1 obs ? print gen = Fn . gen obs ? print gen
module Tuple = struct type ' a t = | Nil : unit t | Cons : ' a * ' b t -> ( ' a * ' b ) ' b t let nil = Nil let cons x tail = Cons ( x , tail ) tail type ' a obs = | O_nil : unit obs | O_cons : ' a Observable . t * ' b obs -> ( ' a * ' b ) ' b obs let o_nil = O_nil let o_cons x tail = O_cons ( x , tail ) tail let rec h...
let fun_nary ( o : _ Tuple . obs ) obs ? print ret : _ Gen . t = Tuple . gen o ? print ret
let fun2 o1 o2 ? print ret = Gen . map ( Fn . map_fun ( fun g x y -> g Tuple ( . x @:: y @:: nil ) nil ) nil ) nil ( fun_nary Tuple ( . o1 @-> o2 @-> o_nil ) o_nil ? print ret ) ret
let fun3 o1 o2 o3 ? print ret = Gen . map ( Fn . map_fun ( fun g x y z -> g Tuple ( . x @:: y @:: z @:: nil ) nil ) nil ) nil ( fun_nary Tuple ( . o1 @-> o2 @-> o3 @-> o_nil ) o_nil ? print ret ) ret
let fun4 o1 o2 o3 o4 ? print ret = Gen . map ( Fn . map_fun ( fun g x y z w -> g Tuple ( . x @:: y @:: z @:: w @:: nil ) nil ) nil ) nil ( fun_nary Tuple ( . o1 @-> o2 @-> o3 @-> o4 @-> o_nil ) o_nil ? print ret ) ret
module TestResult = struct type ' a counter_ex = { instance : ' a ; shrink_steps : int ; msg_l : string list ; } type ' a state = | Success | Failed of { instances : ' a counter_ex list ; } | Failed_other of { msg : string } string | Error of { instance : ' a counter_ex ; exn : exn ; backtrace : string ; } type ' a t =...
module Test_exceptions = struct exception Test_fail of string * string list exception Test_error of string * string * exn * string end
module Test = struct type ' a cell = { count : int ; long_factor : int ; max_gen : int ; max_fail : int ; retries : int ; law : ' a -> bool ; gen : ' a Gen . t ; print : ' a Print . t option ; collect : ( ' a -> string ) string option ; stats : ' a stat list ; qcheck1_shrink : ( ' a -> ( ' a -> unit ) unit -> unit ) un...
let find_example ( ? name : string = " < example ) " > ( ? count : int option ) option ( ~ f : ' a -> bool ) bool ( gen : ' a Gen . t ) t : ' a Gen . t = let gen st = let cell = Test . make_cell ~ max_fail : 1 ? count gen ( fun x -> not ( f x ) x ) x in let res = Test . check_cell ~ rand : st cell in begin match res . ...
let find_example_gen ( ? rand : RS . t option ) option ( ? name : string option ) option ( ? count : int option ) option ( ~ f : ' a -> bool ) bool ( gen : ' a Gen . t ) t : ' a = let g = find_example ? name ? count ~ f gen in Gen . generate1 ? rand g
let qcheck_wrap ? verbose ? long ? rand = List . map ( QCheck_alcotest . to_alcotest ? verbose ? long ? rand )
let qcheck_eq ? pp ? cmp ? eq expected actual = let pass = match ( eq , cmp ) with | ( Some eq , _ ) -> eq expected actual | ( None , Some cmp ) -> cmp expected actual = 0 | ( None , None ) -> Stdlib . compare expected actual = 0 in if pass then true else match pp with | None -> QCheck2 . Test . fail_reportf " [ @< h 0...
let qcheck_eq_tests ~ eq ~ gen ~ eq_name = let reflexivity_test = QCheck2 . Test . make ~ name ( : Printf . sprintf " % s is reflexive : forall t , % s t t " eq_name eq_name ) gen ( fun t -> if eq t t then true else QCheck2 . Test . fail_reportf " [ @< v 2 [ >% s t t ] should hold , but it doesn ' t , !@\ [ t ] is prin...
let qcheck_eq ' ? pp ? cmp ? eq ~ expected ~ actual ( ) = qcheck_eq ? pp ? cmp ? eq expected actual
let qcheck_cond ? pp ~ cond e ( ) = if cond e then true else match pp with | None -> QCheck . Test . fail_reportf " [ @< h 0 > The condition check failed , but no pretty printer was \ provided . ] " @ | Some pp -> QCheck . Test . fail_reportf " [ @< v 2 > The condition check failed , !@% a ] " @ pp e
let int64_range_gen a b = let gen a b st = let range = Int64 . sub b a in let raw_val = Random . State . int64 st range in let res = Int64 . add a raw_val in assert ( a <= res && res <= b ) ; res in let shrink b ( ) = QCheck2 . Shrink . int64_towards a b ( ) in QCheck2 . Gen . make_primitive ~ gen ( : gen a b ) ~ shrin...
let int64_strictly_positive_gen = int64_range_gen 1L
let int_strictly_positive_gen = QCheck2 . Gen . int_range 1
let uint16 = QCheck2 . Gen . ( 0 -- 65535 )
let int16 = QCheck2 . Gen . ( - 32768 -- 32767 )
let uint8 = QCheck2 . Gen . ( 0 -- 255 )
let int8 = QCheck2 . Gen . ( - 128 -- 127 )
let string_fixed n = QCheck2 . Gen . ( string_size ( pure n ) )
let bytes_gen = QCheck2 . Gen . ( map Bytes . of_string string )
let sublist : ' a list -> ' a list QCheck2 . Gen . t = let rec take_n n = function | x :: xs when n > 0 -> x :: take_n ( n - 1 ) xs | _ -> [ ] in fun elems -> let open QCheck2 . Gen in match elems with | [ ] -> return [ ] | _ -> let * res_len = 0 -- List . length elems in let + shuffle = shuffle_l elems in take_n res_l...
let holey ( l : ' a list ) : ' a list QCheck2 . Gen . t = let open QCheck2 . Gen in let + bools = list_size ( return ( List . length l ) ) bool in let rev_result = List . fold_left ( fun acc ( elem , pick ) -> if pick then elem :: acc else acc ) [ ] ( List . combine l bools ) in List . rev rev_result
let endpoint_gen = let open QCheck2 in let open Gen in let protocol_gen = oneofl [ " http " ; " https " ] in let path_gen = let + path_chunks = list_size ( 1 -- 8 ) ( string_size ~ gen ( : char_range ' a ' ' z ' ) ( 1 -- 8 ) ) in String . concat " . " path_chunks in let port_gen = let + port = 1 -- 32768 in " " : ^ Int...
module MakeMapGen ( Map : Stdlib . Map . S ) = struct open QCheck2 let gen_of_size ( size_gen : int Gen . t ) ( key_gen : Map . key Gen . t ) ( val_gen : ' v Gen . t ) : ' v Map . t Gen . t = let open Gen in map ( fun entries -> List . to_seq entries |> Map . of_seq ) ( list_size size_gen @@ pair key_gen val_gen ) let ...
module IntTree = struct type tree = Leaf of int | Node of tree * tree let leaf x = Leaf x let node x y = Node ( x , y ) y let rec depth = function | Leaf _ -> 1 | Node ( x , y ) y -> 1 + max ( depth x ) x ( depth y ) y let rec print_tree = function | Leaf x -> Printf . sprintf " Leaf % d " x | Node ( x , y ) y -> Print...
module Overall = struct open QCheck2 let passing = Test . make ~ name " : list_rev_is_involutive " ~ count : 100 ~ long_factor : 100 ~ print : Print ( . list int ) int Gen ( . list small_int ) small_int ( fun l -> List . rev ( List . rev l ) l = l ) l let failing = Test . make ~ name " : should_fail_sort_id " ~ count :...
module Generator = struct open QCheck2 let char_dist_issue_23 = Test . make ~ name " : char never produces ' \\ 255 ' " ~ count : 1_000_000 ~ print : Print . char Gen . char ( fun c -> c <> ' \ 255 ' ) 255 ' let char_test = Test . make ~ name " : char has right range ' " ~ count : 1000 ~ print : Print . char Gen . char...
module Shrink = struct open QCheck2 let rec fac n = match n with | 0 -> 1 | n -> n * fac ( n - 1 ) 1 let test_fac_issue59 = Test . make ~ name " : test fac issue59 " ( Gen . make_primitive ~ gen ( : fun st -> Gen . generate1 ~ rand : st ( Gen . small_int_corners ( ) ) ) ~ shrink ( : fun _ -> Seq . empty ) empty ) empty...
module Function = struct open QCheck2 let fail_pred_map_commute = Test . make ~ name " : fail_pred_map_commute " ~ count : 100 ~ long_factor : 100 ~ print : Print ( . triple ( list int ) int Fn . print Fn . print ) print Gen ( . triple ( small_list small_int ) small_int ( fun1 ~ print : Print . int Observable . int int...
module FindExample = struct open QCheck2 let find_ex = Test . make ~ name " : find_example " ~ print : Print . int Gen ( . 2 -- 50 ) 50 ( fun n -> let st = Random . State . make [ | 0 ] | in let f m = n < m && m < 2 * n in try let m = find_example_gen ~ rand : st ~ count : 100_000 ~ f Gen ( . 0 -- 1000 ) 1000 in f m wi...
module Stats = struct open QCheck2 let bool_dist = Test . make ~ name " : bool dist " ~ count : 500_000 ~ collect : Bool . to_string Gen . bool ( fun _ -> true ) true let char_dist = Test . make ~ name " : char code dist " ~ count : 500_000 ~ stats [ ( " : char code " , Char . code ) code ] code Gen . char ( fun _ -> t...
module Shrink = struct let test_int_towards ( ) = Alcotest ( . check ' ( list int ) int ) int ~ msg " : int_towards 0 100 " ~ actual ( : Shrink . int_towards 0 100 |> List . of_seq ) of_seq ~ expected [ : 0 ; 50 ; 75 ; 88 ; 94 ; 97 ; 99 ] 99 ; Alcotest ( . check ' ( list int ) int ) int ~ msg " : int_towards 500 1000 "...
module Gen = struct let test_gen_option ~ ratio = let opt_int = Gen . option ? ratio Gen . int in let nb = ref 0 in for _i = 0 to 1000 do Gen . generate1 opt_int |> function None -> ( ) | Some _ -> nb := ! nb + 1 done ; ! nb let test_gen_option_default ( ) = let nb = test_gen_option ~ ratio : None in let b = nb > 800 &...
module TestCount = struct let test_count_n ? count expected = let t = QCheck2 ( . Test . make ? count Gen . int ( fun _ -> true ) true ) true in let msg = Printf . sprintf " QCheck2 . Test . make ~ count :% s |> get_count = % d " ( Option . fold ~ none " : None " ~ some : string_of_int count ) count expected in Alcotes...
module TestLongFactor = struct let test_long_factor_n ? long_factor expected = let t = QCheck2 ( . Test . make ? long_factor Gen . int ( fun _ -> true ) true ) true in let msg = Printf . sprintf " QCheck2 . Test . make ~ long_factor :% s |> long_factor = % d " ( Option . fold ~ none " : None " ~ some : string_of_int lo...
module String = struct let test_string_shrinking ( ) = let shrink_result = QCheck2 ( . find_example_gen ~ f ( : fun s -> s <> s ^ s ) s Gen . string ) string in Alcotest ( . check string ) string " Shrinking a non - empty string shrinks to " \ a " " \ " a " shrink_result let tests = ( " String " , Alcotest [ . test_cas...
module Check_exn = struct let check_exn = Test . check_exn let test_pass_trivial ( ) = let run_test ( ) = check_exn QCheck2 ( . Test . make Gen . int ( fun _ -> true ) true ) true in Alcotest ( . check unit ) unit " Success - trivial " ( ) @@ run_test ( ) let test_pass_random ( ) = let run_test ( ) = check_exn QCheck2 ...
let ( ) = Alcotest . run " QCheck2 " [ Shrink . tests ; Gen . tests ; TestCount . tests ; TestLongFactor . tests ; String . tests ; Check_exn . tests ; ]
let seed_ = lazy ( let s = try int_of_string @@ Sys . getenv " QCHECK_SEED " with _ -> Random . self_init ( self_init ) self_init ; Random . int 1_000_000_000 in Printf . printf " qcheck random seed : % d \ n " %! s ; s )
let default_rand ( ) = Random . State . make [ | Lazy . force seed_ ] |
let verbose_ = lazy ( match Sys . getenv " QCHECK_VERBOSE " with | " 1 " | " true " -> true | _ -> false | exception Not_found -> false )
let long_ = lazy ( match Sys . getenv " QCHECK_LONG " with | " 1 " | " true " -> true | _ -> false | exception Not_found -> false )
let to_alcotest ( ? colors = false ) false ( ? verbose = Lazy . force verbose_ ) verbose_ ( ? long = Lazy . force long_ ) long_ ( ? debug_shrink = None ) None ? debug_shrink_list ( ? rand = default_rand ( default_rand ) default_rand ) default_rand ( t : T . t ) t = let T . Test cell = t in let handler name cell r = mat...
let passing = QCheck . Test . make ~ count : 1000 ~ name " : list_rev_is_involutive " QCheck ( . list small_int ) small_int ( fun l -> List . rev ( List . rev l ) l = l ) l ; ;
let failing = QCheck . Test . make ~ count : 10 ~ name " : fail_sort_id " QCheck ( . list small_int ) small_int ( fun l -> l = List . sort compare l ) l ; ;
let error = QCheck . Test . make ~ count : 10 ~ name " : error_raise_exn " QCheck . int ( fun _ -> raise Error ) Error
let simple_qcheck = QCheck . Test . make ~ name " : fail_check_err_message " ~ count : 100 QCheck . small_int ( fun _ -> QCheck . Test . fail_reportf [ " @< v > this @ will @ always @ fail ] ) " @
type tree = Leaf of int | Node of tree * tree
let leaf x = Leaf x
let node x y = Node ( x , y ) y
let gen_tree = QCheck . Gen ( . sized @@ fix ( fun self n -> match n with | 0 -> map leaf nat | n -> frequency [ 1 , map leaf nat ; 2 , map2 node ( self ( n / 2 ) 2 ) 2 ( self ( n / 2 ) 2 ) 2 ] 2 ) )
let rec rev_tree = function | Node ( x , y ) y -> Node ( rev_tree y , rev_tree x ) x | Leaf x -> Leaf x
let passing_tree_rev = QCheck . Test . make ~ count : 1000 ~ name " : tree_rev_is_involutive " QCheck ( . make gen_tree ) gen_tree ( fun tree -> rev_tree ( rev_tree tree ) tree = tree ) tree
let debug_shrink = QCheck . Test . make ~ count : 10 ~ name " : debug_shrink " QCheck ( . pair ( 1 -- 3 ) 3 ( 1 -- 3 ) 3 ) 3 ( fun ( a , b ) b -> a = b ) b ; ;
let ( ) = Printexc . record_backtrace true ; let module A = Alcotest in let suite = List . map QCheck_alcotest . to_alcotest [ passing ; failing ; error ; simple_qcheck ; passing_tree_rev ] in A . run ~ show_errors : true " my test " [ " suite " , suite ; " shrinking " , [ QCheck_alcotest . to_alcotest ~ verbose : true...
module Non_empty = struct type ' a t = { head : ' a ; tail : ' a list } exception Empty_list let of_pair ( x , y ) = { head = x ; tail = [ y ] } let of_list_exn x = match x with [ ] -> raise Empty_list | x :: xs -> { head = x ; tail = xs } let length : type a . a t -> int = fun { tail ; _ } -> List . length tail + 1 le...
module Functor = struct module type S = sig type ' a t val map : ( ' a -> ' b ) -> ' a t -> ' b t end end
module Applicative = struct module type S = sig include Functor . S val return : ' a -> ' a t val map2 : ( ' a -> ' b -> ' c ) -> ' a t -> ' b t -> ' c t val product : ' a t -> ' b t -> ( ' a * ' b ) t end end
module Monad = struct module type S = sig include Applicative . S val join : ' a t t -> ' a t val bind : ' a t -> ( ' a -> ' b t ) -> ' b t end module Syntax ( M : S ) = struct let ( let * ) = M . bind let ( let + ) x f = M . map f x let ( and + ) = M . product let ( and * ) = M . product end end
module Identity = struct type ' a t = ' a let return x = x let map f x = f x let map2 f x y = f x y let join x = x let bind x f = f x let product x y = ( x , y ) let run x = x end
module Stateful_gen = struct module type S = sig type ' a m include Monad . S val bool : bool t val nat_less_than : int -> int t val small_int : int t val replicate : int -> ' a t -> ' a list t val replicate_for_each : ' a list -> ' b t -> ( ' a * ' b ) list t val traverse : ( ' a -> ' b t ) -> ' a list -> ' b list t v...
let ty = Ldot ( Ldot ( Lident " QCheck " , " Gen ) " , " t ) "
let unit loc = [ % expr QCheck . Gen . unit ] unit
let int loc = [ % expr QCheck . Gen . int ] int
let string loc = [ % expr QCheck . Gen . string ] string
let char loc = [ % expr QCheck . Gen . char ] char
let bool loc = [ % expr QCheck . Gen . bool ] bool
let float loc = [ % expr QCheck . Gen . float ] float