text
stringlengths
0
601k
let test_array_finaliser _ = let module Array = CArray in let finaliser_completed = ref false in let finalise a = begin assert_equal 10 ( Array . length a ) ; assert_equal [ 1 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7 ; 8 ; 9 ; 10 ] ( Array . to_list a ) ; finaliser_completed := true ; end in let ( ) = let p = let a = Array . make ~ fin...
let test_struct_finaliser _ = let module M = struct type s let s : s structure typ = structure " s " let i = field s " i " int32_t let c = field s " c " char let ( ) = seal s let finaliser_completed = ref false let finalise s = begin assert_equal 10l ( getf s i ) ; assert_equal ' e ' ( getf s c ) ; finaliser_completed ...
let suite = " Finaliser tests " >::: [ " array finalisation " >:: test_array_finaliser ; " struct finalisation " >:: test_struct_finaliser ; ]
let _ = run_test_tt_main suite
let callback_from_emacs_to_ocaml = Function . to_value ( lambda_nullary_nil [ % here ] ( fun ( ) -> ( ) ) ) ; ;
let make_ocaml_garbage_not_keep_emacs_values_alive ( ) = Gc . compact ( ) ; Gc . compact ( ) ; Value . funcall0_i callback_from_emacs_to_ocaml ; ;
let emacs_garbage_collect = let garbage_collect = Funcall . Wrap . ( " garbage - collect " <: nullary @-> return nil ) in fun ( ) -> garbage_collect ( ) ; Value . funcall0_i callback_from_emacs_to_ocaml ; ; let dump_embedded_values ( ) = Ecaml . debug_embedded_caml_values ( ) |> [ % of_sexp : ( int * Sexp . t ) list ] ...
module Variable_never_used = struct type t = { foo : ' a . int } [ @@ deriving_inline sexp_grammar ] let _ = fun ( _ : t ) -> ( ) let ( t_sexp_grammar : Ppx_sexp_conv_lib . Sexp . Private . Raw_grammar . t ) = let ( _the_generic_group : Ppx_sexp_conv_lib . Sexp . Private . Raw_grammar . generic_group ) = { implicit_var...
module Variable_used = struct type t = { foo : ' a . ' a option } [ @@ deriving_inline sexp_grammar ] let _ = fun ( _ : t ) -> ( ) let ( t_sexp_grammar : Ppx_sexp_conv_lib . Sexp . Private . Raw_grammar . t ) = let ( _the_generic_group : Ppx_sexp_conv_lib . Sexp . Private . Raw_grammar . generic_group ) = { implicit_va...
let level_zero = Raw_level_repr . of_int32_exn 0l
let round_of_int32_exn i = match Round_repr . of_int32 i with | Ok i -> i | Error _ -> Stdlib . failwith " Invalid round representation "
let make_tuple ( level , r_opt , r0 , r1 ) = let r_opt = Option . map round_of_int32_exn r_opt in let r0 = round_of_int32_exn r0 in let r1 = round_of_int32_exn r1 in ( level , r_opt , r0 , r1 )
let test_cases = List . map make_tuple [ ( 3l , Some 1l , 1l , 12l ) ; ( 10l , Some 1l , 1l , 12l ) ; ( 10l , Some 4l , 2l , 6l ) ; ( 10l , Some 4l , 1l , 12l ) ; ( 9l , Some 2l , 0l , 3l ) ; ( 7l , None , 0l , 3l ) ; ( 7l , None , 1l , 3l ) ; ( 0l , None , 0l , 0l ) ; ( 12l , Some 2l , 8l , 7l ) ; ( 10l , Some 0l , 1l...
let rec product l1 l2 = match l1 with | [ ] -> [ ] | h :: tl -> List . map ( fun x -> ( h , x ) ) l2 @ product tl l2
let test_product_cases = product test_cases test_cases
let tuple_to_fitness ( level , locked_round , predecessor_round , round ) = Fitness_repr . create ~ level ( : Raw_level_repr . of_int32_exn level ) ~ locked_round ~ predecessor_round ~ round
let tuple_to_fitness_exn tuple = tuple_to_fitness tuple |> function | Ok f -> f | Error err -> Format . kasprintf Stdlib . failwith " cannot create fitness from tuple : % a " pp_print_trace ( Environment . wrap_tztrace err )
let test_from_to_raw_fitness tuple = let fitness = tuple_to_fitness_exn tuple in Fitness_repr . from_raw ( Fitness_repr . to_raw fitness ) |> function | Ok new_fitness -> assert ( fitness = new_fitness ) | Error _x -> assert false
let test_from_to_raw_fitness_all ( ) = List . iter test_from_to_raw_fitness test_cases ; return_unit
let test_locked_round ( ) = let test_bad_cases = List . map make_tuple [ ( 8l , Some 7l , 1l , 1l ) ; ( 9l , Some 8l , 0l , 3l ) ; ( 10l , Some 7l , 2l , 6l ) ; ( 11l , Some 5l , 5l , 1l ) ; ( 8l , Some 2l , 1l , 1l ) ; ( 9l , Some 3l , 0l , 3l ) ; ( 11l , Some 5l , 5l , 1l ) ; ( 13l , Some 2l , 1l , 1l ) ; ( 10l , Som...
let test_compare ( tuple1 , tuple2 ) = let fitness1 = tuple_to_fitness_exn tuple1 in let fitness2 = tuple_to_fitness_exn tuple2 in let raw_fitness1 = Fitness_repr . to_raw fitness1 in let raw_fitness2 = Fitness_repr . to_raw fitness2 in let cmp_fitness = Fitness_repr . Internal_for_tests . compare fitness1 fitness2 in ...
let test_compare_all ( ) = List . iter_es test_compare test_product_cases
let tests = [ Tztest . tztest " from / to raw fitness is identity " ` Quick test_from_to_raw_fitness_all ; Tztest . tztest " locked round is smaller than round " ` Quick test_locked_round ; Tztest . tztest " compare fitness = compare raw_fitness " ` Quick test_compare_all ; ]
let err x = Exn ( Fixed_point_test_error x )
module type Arith = sig type t val zero : t val equal : t -> t -> bool val random : unit -> t val add : t -> t -> t val sub : t -> t -> t end
let n = Z . of_int 42
let n ' = Z . of_int 43
let basic_arith name ( module A : Arith ) = let err msg = err ( Format . asprintf " % s test : % s " name msg ) in let x = A . random ( ) in fail_unless A . ( add zero x = x ) ( err " zero is neutral for " ) + >>=? fun ( ) -> let x = A . random ( ) in let y = A . random ( ) in fail_unless A . ( add x y = add y x ) ( er...
let arith_from_integral : ( module Fixed_point_repr . Full ) -> ( module Arith ) = fun ( module FP ) -> let module Arith = struct type t = FP . integral let zero = FP . zero let equal = FP . equal let random ( ) = FP . integral_of_int_exn ( Random . int 898987 ) let add = FP . add let sub = FP . sub end in ( module Ari...
let arith_from_fp : ( module Fixed_point_repr . Full ) -> ( module Arith ) = fun ( module FP ) -> let module Arith = struct type t = FP . fp let zero = FP . zero let equal = FP . equal let random ( ) = FP . unsafe_fp ( Z . of_int ( Random . int 898987 ) ) let add = FP . add let sub = FP . sub end in ( module Arith )
let integral_tests ( ) = let module FP = Gas_limit_repr . Arith in fail_unless ( FP . ( integral_to_z ( integral_exn n ) ) = n ) ( err " roundtrip > 0 " ) >>=? fun ( ) -> fail_unless ( FP . ( integral_to_z ( integral_exn Z . zero ) ) = Z . zero ) ( err " roundtrip = 0 " ) >>=? fun ( ) -> fail_unless FP . ( ceil ( fp ( ...
let fp_nonzero ( ) = let decimals = 3 in let module FP = Gas_limit_repr . Arith in let prefix msg = Format . asprintf " ( % d decimals ) % s " decimals msg in let err msg = err ( prefix msg ) in basic_arith ( prefix " integral arith " ) ( arith_from_integral ( module FP ) ) >>=? fun ( ) -> basic_arith ( prefix " fp ari...
let fp_pp ( ) = let module FP = Gas_limit_repr . Arith in let prefix msg = Format . asprintf " ( % d decimals ) % s " 3 msg in let err msg = err ( prefix msg ) in let epsilon = FP . unsafe_fp Z . one in let ( =:= ) x expected = Format . asprintf " % a " FP . pp x = expected in fail_unless ( epsilon =:= " 0 . 001 " ) ( ...
let tests = [ Tztest . tztest " Integral tests ( 3 decimals ) " ` Quick integral_tests ; Tztest . tztest " FP tests ( 3 decimals ) " ` Quick fp_nonzero ; Tztest . tztest " FP pp tests ( 3 decimals ) " ` Quick fp_pp ; ]
let a = Int63 . of_int 0x1
let b = Int63 . of_int 0x2
let c = Int63 . of_int 0xC
let d = create ~ bit : 62 List . iter [ - 1 ; 63 ] ~ f ( : fun bit -> show_raise ~ hide_positions : true ( fun ( ) -> ignore ( create ~ bit : Int63 . t ) ) ) ; [ % expect { | ( raised ( " Flags . create got invalid ~ bit ( must be between 0 and 62 ) " - 1 lib / flags / src / flags . ml : LINE : COL ) ) ( raised ( " Fla...
module M = Make ( struct let allow_intersecting = false let should_print_error = true let known = [ a , " a " ; b , " b " ; c , " c " ] let remove_zero_flags = false end ) print_and_check_comparable_sexps [ % here ] ( module M ) [ a ; b ; c ] ; [ % expect { | ( Set ( ( a ) ( b ) ( c ) ) ) ( Map ( ( ( a ) 0 ) ( ( b ) 1 ...
let print_sexp_of t = print_s [ % sexp ( t : t ) ] print_sexp_of empty ; [ % expect { | ( ) } ] | ; ; print_sexp_of a ; [ % expect { | ( a ) } ] | ; ; print_sexp_of c ; [ % expect { | ( c ) } ] | ; ; print_sexp_of ( a + b ) ; [ % expect { | ( a b ) } ] | ; ; print_sexp_of ( Int63 . of_int 0x10 ) ; [ % expect { | ( ( ) ...
let check_t_of_sexp here expect string = require here ( equal expect ( t_of_sexp ( Sexp . of_string string ) ) ) ; ; List . iter [ " a " ; " ( ( ) ) " ; " ( a ( ) ) " ; " ( d ) " ] ~ f ( : fun string -> show_raise ( fun ( ) -> print_s [ % message " " ~ input : string ] ; ignore ( t_of_sexp ( Sexp . of_string string ) :...
let print_compare t1 t2 = print_s [ % sexp ( compare t1 t2 : int ) ] print_compare a empty ; [ % expect { | 1 } ] ; | print_compare c empty ; [ % expect { | 1 } ] ; | print_compare a a ; [ % expect { | 0 } ] ; | print_compare c c ; [ % expect { | 0 } ] ; | print_compare empty empty ; [ % expect { | 0 } ] ; | print_comp...
type t16 = A : _ abs -> t16 [ @@ ocaml . unboxed ] ; ; [ %% expect { |
type ' a abs ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it might contain both float and non - float values . You should annotate it with [ @@ ocaml . boxed ] . } ] ; ; |
type t18 = A : _ list abs -> t18 [ @@ ocaml . unboxed ] ; ; [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it might contain both float and non - float values . You should annotate it with [ @@ ocaml . boxed ] . } ] ; ; |
type ' a s = S : ' a -> ' a s [ @@ unboxed ] ; ;
type t = T : _ s -> t [ @@ unboxed ] ; ; [ %% expect { |
type ' a s = S : ' a -> ' a s [ @@ unboxed ] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it might contain both float and non - float values . You should annotate it with [ @@ ocaml . boxed ] . } ] ; ; |
type ' a s = S : ' a -> ' a option s [ @@ unboxed ] ; ;
type t = T : _ s -> t [ @@ unboxed ] ; ; [ %% expect { |
type ' a s = S : ' a -> ' a option s [ @@ unboxed ] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it might contain both float and non - float values . You should annotate it with [ @@ ocaml . boxed ] . } ] ; ; |
module M : sig type ' a r constraint ' a = unit -> ' b val inj : ' b -> ( unit -> ' b ) r type ' a r = ' b constraint ' a = unit -> ' b let inj x = x end ; ; [ %% expect { |
module M : sig type ' a r constraint ' a = unit -> ' b val inj : ' b -> ( unit -> ' b ) r end } ] ; ; |
type t = T : ( unit -> _ ) M . r -> t [ @@ unboxed ] ; ; [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it might contain both float and non - float values . You should annotate it with [ @@ ocaml . boxed ] . } ] ; ; |
type ' a s = S : ( unit -> ' a ) M . r -> ' a option s [ @@ unboxed ] ; ; [ %% expect { |
type ' a s = S : ( unit -> ' a ) M . r -> ' a option s [ @@ unboxed ] } ] ; ; |
type t = T : _ s -> t [ @@ unboxed ] ; ; [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it might contain both float and non - float values . You should annotate it with [ @@ ocaml . boxed ] . } ] ; ; |
type ' a t = T : ' a s -> ' a t [ @@ unboxed ] ; ; [ %% expect { |
type ' a t = T : ' a s -> ' a t [ @@ unboxed ] } ] ; ; |
type _ s = S : ' a t -> _ s [ @@ unboxed ] and _ t = T : ' a -> ' a s t ; ; [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it might contain both float and non - float values . You should annotate it with [ @@ ocaml . boxed ] . } ] ; ; |
type ' a s = S : ' a -> ' a s [ @@ unboxed ] ; ;
type t = T : _ s -> t [ @@ unboxed ] ; ; [ %% expect { |
type ' a s = S : ' a -> ' a s [ @@ unboxed ] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it might contain both float and non - float values . You should annotate it with [ @@ ocaml . boxed ] . } ] ; ; |
type ' a s = S : ' a -> ' a option s [ @@ unboxed ] ; ;
type t = T : _ s -> t [ @@ unboxed ] ; ; [ %% expect { |
type ' a s = S : ' a -> ' a option s [ @@ unboxed ] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it might contain both float and non - float values . You should annotate it with [ @@ ocaml . boxed ] . } ] ; ; |
module M : sig type ' a r constraint ' a = unit -> ' b val inj : ' b -> ( unit -> ' b ) r type ' a r = ' b constraint ' a = unit -> ' b let inj x = x end ; ; [ %% expect { |
module M : sig type ' a r constraint ' a = unit -> ' b val inj : ' b -> ( unit -> ' b ) r end } ] ; ; |
type t = T : ( unit -> _ ) M . r -> t [ @@ unboxed ] ; ; [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it might contain both float and non - float values . You should annotate it with [ @@ ocaml . boxed ] . } ] ; ; |
type ' a s = S : ( unit -> ' a ) M . r -> ' a option s [ @@ unboxed ] ; ; [ %% expect { |
type ' a s = S : ( unit -> ' a ) M . r -> ' a option s [ @@ unboxed ] } ] ; ; |
type t = T : _ s -> t [ @@ unboxed ] ; ; [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it might contain both float and non - float values . You should annotate it with [ @@ ocaml . boxed ] . } ] ; ; |
type ' a t = T : ' a s -> ' a t [ @@ unboxed ] ; ; [ %% expect { |
type ' a t = T : ' a s -> ' a t [ @@ unboxed ] } ] ; ; |
type _ s = S : ' a t -> _ s [ @@ unboxed ] and _ t = T : ' a -> ' a s t ; ; [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it might contain both float and non - float values . You should annotate it with [ @@ ocaml . boxed ] . } ] ; ; |
let same_bits x y = let open Int64 in bits_of_float x = bits_of_float y ; ;
let args = [ Float . infinity ; - 0x1p + 63 ; - 0x1p + 62 ; - 3 . 50 ; - 3 . 25 ; - 3 . 00 ; - 2 . 75 ; - 2 . 50 ; - 2 . 25 ; - 2 . 00 ; - 1 . 75 ; - 1 . 50 ; - 1 . 25 ; - 1 . 00 ; - 0 . 75 ; - 0 . 50 ; - 0 . 25 ; - 0 . 00 ; 0 . 00 ; 0 . 25 ; 0 . 50 ; 0 . 75 ; 1 . 00 ; 1 . 25 ; 1 . 50 ; 1 . 75 ; 2 . 00 ; 2 . 25 ; 2 . 5...
let z = let x = - 0 . and y = 0 . in if mod_float x 1 . >= 0 . then x else if false then x else y in Printf . printf " % g \ n " ( 1 . . / z ) ; [ % expect { |- inf } ] |
module Float = struct include Float external acosh : float -> float = " caml_acosh_float " external asinh : float -> float = " caml_asinh_float " external atanh : float -> float = " caml_atanh_float " external erf : float -> float = " caml_erf_float " external erfc : float -> float = " caml_erfc_float " external cbrt :...
let print f = match Float . classify_float f with | FP_nan -> print_endline " nan " | _ -> Printf . printf " % f \ n " f let p x = print ( Float . acosh x ) in p ( - 1 . 0 ) ; [ % expect { | nan } ] ; | p 0 . 0 ; [ % expect { | nan } ] ; | p 0 . 5 ; [ % expect { | nan } ] ; | p 1 . 0 ; [ % expect { | 0 . 000000 } ] ; |...
let diff prev next tolerance message = printf " !====== { % Sexp } ======\ n " message ; patdiff ~ extra_flags [ : " - float - tol " ; Percent . to_string tolerance ; " - ascii " ] ~ prev ~ next ; ;
let test prev next = let % bind ( ) = diff prev next ( Percent . of_mult 0 . ) [ % message " strict " ] in let % bind ( ) = diff prev next ( Percent . of_mult 0 . 1 ) [ % message " 10 " ] % in Deferred . unit ; ; let % bind ( ) = test { | foo bar bax baz foo bar baz [ % expect { | ====== strict ====== ------ prev / fil...
let prev = { | ( bar 0 . 5 ) ) % } |
let next = { | ( ) } | let % bind ( ) = patdiff ~ prev ~ next ~ extra_flags [ ] : in [ % expect { | ( fg : red ) ------ ( + bold ) prev / file ( fg : green ) ++++++ ( + bold ) next / file ( fg : black ) ( @|+ bold ) - 1 , 3 + 1 , 2 ( off ) ============================================================ ( fg : black ) | ( ...
let finally f g = let res = try f ( ) with e -> g ( ) ; raise e in g ( ) ; res
let with_open_file path f = let fd = Unix . ( openfile path [ O_CREAT ; O_TRUNC ; O_RDWR ] 0o600 ) in finally ( fun ( ) -> f fd ) ( fun ( ) -> Unix . close fd )
let with_temp_file f = let path = Filename . temp_file " test_flock " " . lock " in finally ( fun ( ) -> with_open_file path ( f path ) ) ( fun ( ) -> Unix . unlink path )
let string_of_operation = function | LOCK_EX -> " exclusive " | LOCK_SH -> " shared " | LOCK_UN -> " unlock "
let sleep s = Unix . select [ ] [ ] [ ] s |> ignore
let smoke_test = " Smoke test that we can call the function at all " >:: fun ( ) -> with_temp_file ( fun path fd -> flock fd LOCK_EX )
let exclusion_tests = let try_lock with_mode try_mode expectation = with_temp_file ( fun path fd -> flock fd with_mode ; with_open_file path ( fun fd ' -> let lock_f ( ) = flock ~ nonblocking : true fd ' try_mode in if expectation then lock_f ( ) else assert_raises Unix . ( Unix_error ( EAGAIN , " flock " , " " ) ) loc...
let test_unlock = " Test that unlocking allows re - acquisition of the lock " >:: fun ( ) -> with_temp_file ( fun path fd -> flock fd LOCK_EX ; with_open_file path ( fun fd ' -> let lock_f ( ) = flock ~ nonblocking : true fd ' LOCK_EX in assert_raises Unix . ( Unix_error ( EAGAIN , " flock " , " " ) ) lock_f ; flock fd...
let test_unlock_blocking = " Test unlocking unblocks a blocked attempt to acquire the lock " >:: fun ( ) -> with_temp_file ( fun path fd -> flock fd LOCK_EX ; let _ = Unix . open_process ( " flock - x " ^ path ^ " - c true " ) in assert_bool " flock ( 1 ) did not block waiting for lock " Unix . ( waitpid [ WNOHANG ] 0 ...
let test_operation_idemptotence = " Check all operations are idempotent on a given file desciptor " >:: fun ( ) -> List . iter ( fun op -> let f fd = flock ~ nonblocking : true fd op in with_temp_file ( fun _ fd -> f fd ; f fd ) ) [ LOCK_EX ; LOCK_SH ; LOCK_UN ]
let test_bad_fd = " Test expected exception for locking a bad file descriptor " >:: fun ( ) -> let closed_fd = with_temp_file ( fun _ fd -> fd ) in assert_raises Unix . ( Unix_error ( EBADF , " flock " , " " ) ) ( fun ( ) -> flock closed_fd LOCK_EX )
let _ = let suite = " flock " >::: [ smoke_test ; test_unlock ; test_unlock_blocking ; test_operation_idemptotence ; test_bad_fd ; ] @ exclusion_tests in OUnit2 . run_test_tt_main @@ ounit2_of_ounit1 suite
let string_of_fpclass = function | Float . FP_normal -> " normal " | FP_subnormal -> " subnormal " | FP_zero -> " zero " | FP_infinite -> " infinite " | FP_nan -> " nan "
let error x y z r c = Printf . fprintf stdout " FAIL !\ tfma ( % h , % h , % h ) returned % h ( % s ) instead of % h . \ n " x y z c ( string_of_fpclass ( Float . classify_float c ) ) ( List . hd r )
let success ( ) = print_endline " OK " !
let fma_test x y z r = let c = Float . fma x y z in if List . exists ( fun i -> i = c ) r then success ( ) else error x y z r c fma_test 0x1p + 0 0x2p + 0 0x3p + 0 [ 0x5p + 0 ] ; [ % expect { | OK ! } ] ; | fma_test 0x1 . 4p + 0 0xcp - 4 0x1p - 4 [ 0x1p + 0 ] ; [ % expect { | OK ! } ] ; | fma_test 0x0p + 0 0x0p + 0 0x0...