text
stringlengths
0
601k
module Small = Make_test_bounded_int32 ( struct let min_int = 1l let max_int = 3l end )
module Small_with_neg = Make_test_bounded_int32 ( struct let min_int = - 10l let max_int = 10l end )
module Full = Make_test_bounded_int32 ( Int32 )
let int32_checks = let open Alcotest in [ test_case " 0 not in empty " ` Quick ( fun ( ) -> assert ( Empty . of_int32 0l = None ) ) ; test_case " 123 not in empty " ` Quick ( fun ( ) -> assert ( Empty . of_int32 123l = None ) ) ; test_case " Int32 . min_int not in empty " ` Quick ( fun ( ) -> assert ( Empty . of_int32 ...
let ( ) = Alcotest . run " Bounded " [ ( " Int32 " , int32_checks ) ; ( " Int32 Small " , Lib_test . Qcheck2_helpers . qcheck_wrap Small . tests ) ; ( " Int32 Small_with_neg " , Lib_test . Qcheck2_helpers . qcheck_wrap Small_with_neg . tests ) ; ( " Int32 Full " , Lib_test . Qcheck2_helpers . qcheck_wrap Full . tests )...
module B = Bounded_heap . Make ( Int )
let take_nth_biggest n l = let rec take_n acc i l = if i = n then acc else match l with [ ] -> acc | x :: xs -> take_n ( x :: acc ) ( i + 1 ) xs in take_n [ ] 0 ( List . sort ( fun x y -> - Compare . Int . compare x y ) l )
let list_size = QCheck . Gen . int_range 2 1000
let test_bounded_heap = QCheck . Test . make ~ name " : bounded_heap ( qcheck ) " ~ count : 1000 QCheck . ( list_of_size list_size int ) ( fun l -> let sz = List . length l / 2 in QCheck . assume ( sz > 0 ) ; let t = B . create sz in List . iter ( fun x -> B . insert x t ) l ; let contents = B . get t in let biggest = ...
let test_empty_works ( ) = let b = B . create 0 in Assert . equal ~ msg : __LOC__ ( B . get b ) [ ] ; Assert . equal ~ msg : __LOC__ ( B . peek b ) None ; B . insert 1 b ; Assert . equal ~ msg : __LOC__ ( B . get b ) [ ]
let ( ) = Alcotest . run " stdlib " [ ( " Bounded_heap " , [ ( " create 0 works " , ` Quick , test_empty_works ) ; QCheck_alcotest . to_alcotest test_bounded_heap ; ] ) ; ]
( module Stable_unit_test . Make ( struct module M = Make ( struct let label = " index " let module_name = " Test_bounded_index " end ) type t = M . Stable . V1 . t [ @@ deriving sexp , bin_io , compare ] let equal x y = compare x y = 0 let make index min max = M . create index ~ min ~ max let tests = [ make 0 0 0 , " ...
( module struct let of_list keys = let t = create ~ num_keys : 10 ~ key_to_int : Fn . id ~ sexp_of_key : Int . sexp_of_t ( ) in List . iter keys ~ f ( : fun key -> add_exn t ~ key ~ data : key ) ; t ; ; let test_exists_like_function exists = exists ( of_list [ ] ) ~ f ( : fun _ -> assert false ) = false && exists ( of_...
module type IO = sig include Sodium . Box . S val ts : string -> storage val st : storage -> string end
module Test ( TestIn : IO ) IO ( IOOut : IO ) IO = struct module Box = Sodium . Box let oi x = Out . ts ( In . st x ) x let setup ( ) = ( Box . random_keypair ( ) , Box . random_keypair ( ) , " The rooster crows at midnight . " , Box . random_nonce ( ) ) let drop_byte s = String . sub s 0 ( ( String . length s ) s - 1 ...
module IO_bigbytes = struct include Sodium . Box . Bigbytes let ts s = let len = String . length s in let t = Bigarray ( . Array1 . create char c_layout len ) len in for i = 0 to len - 1 do t { . i } i <- s [ . i ] i done ; t let st t = let len = Bigarray . Array1 . dim t in let b = Bytes . create len in for i = 0 to l...
module IO_bytes = struct include Sodium . Box . Bytes let ts s = Bytes . of_string s let st t = Bytes . to_string t end
let suite = " Box " >::: [ " Bytes -> Bytes " >::: ( let module M = Test ( TestIO_bytes ) TestIO_bytes ( TestIO_bytesIO_bytes ) TestIO_bytesIO_bytes in M . suite ) suite ; " Bigbytes -> Bytes " >::: ( let module M = Test ( TestIO_bigbytes ) TestIO_bigbytes ( TestIO_bigbytesIO_bytes ) TestIO_bigbytesIO_bytes in M . suit...
let operation = ref 0
let logical_shift_right a b = incr operation ; a lsr b
let logical_or a b = incr operation ; a lor b
let shift_right a b = incr operation ; a asr b
let logical_and a b = incr operation ; a land b
let logical_not a = incr operation ; lnot a
let minus a = incr operation ; ( - a )
let sub a b = incr operation ; a - b logical_shift_right n ( sub Sys . int_size 1 ) let mask = shift_right ( logical_or ( minus choose_b ) choose_b ) Sys . int_size in logical_or ( logical_and a ( logical_not mask ) ) ( logical_and b mask )
let one_if_not_zero_ops = let _ = one_if_not_zero 0x7eadbeef in Format . printf " [ one_if_not_zero ] : % d operation ( s ) . \ n " %! ! operation ; ! operation
let ( ) = operation := 0
let zero_if_not_zero_ops = let _ = zero_if_not_zero 0x7eadbeef in Format . printf " [ zero_if_not_zero ] : % d operation ( s ) . \ n " %! ! operation ; ! operation
let ( ) = operation := 0
let select_int_ops = let _ = select_int 0 1 2 in Format . printf " [ select_int ] : % d operation ( s ) . \ n " %! ! operation ; ! operation
let eqaf_sleep ( ) = Unix . sleep 1
let logical_shift_right a b = eqaf_sleep ( ) ; a lsr b
let logical_or a b = eqaf_sleep ( ) ; a lor b
let shift_right a b = eqaf_sleep ( ) ; a asr b
let logical_and a b = eqaf_sleep ( ) ; a land b
let logical_not a = eqaf_sleep ( ) ; lnot a
let minus a = eqaf_sleep ( ) ; ( - a )
let sub a b = eqaf_sleep ( ) ; a - b logical_shift_right n ( sub Sys . int_size 1 ) let mask = shift_right ( logical_or ( minus choose_b ) choose_b ) Sys . int_size in logical_or ( logical_and a ( logical_not mask ) ) ( logical_and b mask )
let time ( ) = Clock . now ( )
let fdiv a b = a . / b
let ( ) = let t0 = time ( ) in let _ = one_if_not_zero 0x7eadbeef in let t1 = time ( ) in let v0 = Int64 . ( floor ( fdiv ( to_float ( sub t1 t0 ) ) 1000000000 . ) ) in Format . printf " [ one_if_not_zero 0x7eadbeef ] : % fs . \ n " %! v0 ; let t0 = time ( ) in let _ = one_if_not_zero 0x0 in let t1 = time ( ) in let v1...
let ( ) = let t0 = time ( ) in let _ = zero_if_not_zero 0x7eadbeef in let t1 = time ( ) in let v0 = Int64 . ( floor ( fdiv ( to_float ( sub t1 t0 ) ) 1000000000 . ) ) in Format . printf " [ zero_if_not_zero 0x7eadbeef ] : % fs . \ n " %! v0 ; let t0 = time ( ) in let _ = zero_if_not_zero 0x0 in let t1 = time ( ) in let...
let ( ) = let t0 = time ( ) in let _ = select_int 0 1 2 in let t1 = time ( ) in let v0 = Int64 . ( floor ( fdiv ( to_float ( sub t1 t0 ) ) 1000000000 . ) ) in Format . printf " [ select_int 0 1 2 ] : % fs . \ n " %! v0 ; let t0 = time ( ) in let _ = select_int 2 1 0 in let t1 = time ( ) in let v1 = Int64 . ( floor ( fd...
let ctx = Secp256k1 . Context . create ( )
let prevTx = Transaction . of_hex ( ` Hex " 0100000001c3798bf6520ac4e95e24c587b6ee25a1c492f33ddd35615f90f38d89e8e2b47c010000006b483045022100dc48cef9d3e1eb71e84bcf51ceaf7f938328573e482bf8af951e9b53e87a74c802206280177d6ac07455d9984a8dd62f8d9f87c91884820c9fa587c8ada46750a44d4121032af552f85308e3c68c9751c415a5efe01fc165a955...
let nextTx2 = Transaction . of_hex ( ` Hex " 01000000018f514dcffb6242ff6c2b01aa888700dce343f57e265e72c9fb46607065ffad48000000001976a914b2483f8a51043dca4144748f91e45e83e473730d88acffffffff01e0567b4d000000005d14cf244fd81c2d7cee628190f75b48ca621cbf3fcc75522102e8f164c9c12b039d3e522aa291e3544ddc99240a878080669aee1c359f66d25...
let path = Bitcoin . Wallet . KeyPath . [ to_hardened 44l ; to_hardened 1l ; to_hardened 0l ; 0l ; 0l ]
let nextTx = let open Bitcoin . Protocol in let prev_out_hash = Transaction . hash256 prevTx in let my_out = prevTx . outputs . ( 0 ) in let input = TxIn . create ' ~ prev_out_hash ~ prev_out_i : 0 ~ script : my_out . script ( ) in let value = Int64 . sub my_out . value 100000L in let output = TxOut . create ~ value ~ ...
let with_connection f = let h = Hidapi . open_id_exn ~ vendor_id : 0x2C97 ~ product_id : 0x0001 in try match f h with | Result . Ok ( ) -> Hidapi . close h | Result . Error e -> failwith ( Format . asprintf " Ledger error : % a " Ledgerwallet . Transport . pp_error e ) with exn -> Hidapi . close h ; raise exn
let test_open_close ( ) = with_connection ( fun _ -> R . ok ( ) )
let test_ping ( ) = with_connection Ledgerwallet . Transport . ping
let test_get_info ( ) = with_connection begin fun h -> get_firmware_version h >>= fun firmware_version -> Printf . printf " Firmware : % s \ n " ( Sexplib . Sexp . to_string_hum ( Firmware_version . sexp_of_t firmware_version ) ) ; get_operation_mode h >>| fun op_mode -> Printf . printf " Operation mode : % s \ n " ( S...
let test_get_random ( ) = with_connection ( fun h -> get_random h 200 >>| ignore )
let test_get_wallet_pk ( ) = with_connection begin fun h -> get_wallet_public_key h path >>| fun pk -> let pk_computed = Secp256k1 . Key . read_pk_exn ctx pk . uncompressed . buffer in let addr_received = Base58 . Bitcoin . of_string_exn c pk . b58addr in let addr_computed = Bitcoin . Wallet . Address . of_pubkey ctx p...
let test_get_trusted_input ( ) = with_connection begin fun h -> get_trusted_input h prevTx 0 >>| fun out -> Format . printf " Trusted input % a . " @ Hex . pp ( Hex . of_cstruct out ) end
let test_sign_segwit ( ) = with_connection begin fun h -> get_wallet_public_key h path >>= fun pk -> let pk_computed = Secp256k1 . Key . read_pk_exn ctx pk . uncompressed . buffer in let pk_compressed = Secp256k1 . Key . to_bytes ctx pk_computed |> Cstruct . of_bigarray in sign_segwit ~ bch : true ~ path ~ prev_amounts...
let basic = [ " open_close " , ` Quick , test_open_close ; " ping " , ` Quick , test_ping ; " get_info " , ` Quick , test_get_info ; " get_random " , ` Quick , test_get_random ; " get_wallet_pk " , ` Quick , test_get_wallet_pk ; " get_trusted_input " , ` Quick , test_get_trusted_input ; " sign_segwit " , ` Quick , test...
let ( ) = Alcotest . run " ledgerwallet . btc " [ " basic " , basic ; ]
let show t = print_s [ % sexp ( t : t ) ]
let num_live ( ) = List . length ( all_live ( ) )
let num_live_at_start = num_live ( ) print_s [ % sexp ( all_live ( ) : t list ) ] ; [ % expect { | ( " #< buffer * scratch " *> " #< buffer * Minibuf - 0 " *> " #< buffer * Messages " *> " #< buffer * code - conversion - work " ) *> } ] ; | return ( ) ; ;
let require_clean ( ) = require [ % here ] ( num_live ( ) = num_live_at_start ) let t1 = create ~ name " : foo " in show t1 ; [ % expect { | " #< buffer foo " > } ] ; | let t2 = create ~ name " : foo " in show t2 ; [ % expect { | " #< buffer foo < 2 " >> } ] ; | Blocking . kill t1 ; Blocking . kill t2 ; require_clean (...
let int = defvar ( " some - int " |> Symbol . intern ) [ % here ] ~ type_ : Value . Type . ( option int ) ~ default_value : None ( ) ; ; print_s [ % sexp ( symbol int : Symbol . t ) ] ; [ % expect { | some - int } ] ; | return ( ) ; ;
let show_in_current_buffer ( ) = print_s [ % sexp ( Current_buffer . get_buffer_local int : int option ) ] ; ; show_in_current_buffer ( ) ; [ % expect { | ( ) } ] ; | require_does_raise [ % here ] ( fun ( ) -> Current_buffer . get_buffer_local_exn int ) ; [ % expect { | ( " buffer has no value for variable " ( variable...
let does_raise = Exn . does_raise struct module Callback_arity = Callback_arity type nonrec ( ' a , ' b ) t = ( ' a , ' b ) t type ( ' a , ' b ) bus = ( ' a , ' b ) t module Read_only = struct type ' a t = ' a Read_only . t [ @@ deriving sexp_of ] let invariant = Read_only . invariant end module Read_write = struct typ...
let to_t x = x
let s = Bytes . make 10 ' \ x00 '
let assert_bound_check2 f v1 v2 = try ignore ( f v1 v2 ) ; assert false with | Invalid_argument ( " index out of bounds " ) -> ( )
let assert_bound_check3 f v1 v2 v3 = try ignore ( f v1 v2 v3 ) ; assert false with | Invalid_argument ( " index out of bounds " ) -> ( )
let test1 ( ) = assert_bound_check2 BE . get_int8 ( to_t s ) ( - 1 ) ; assert_bound_check2 BE . get_int8 ( to_t s ) 10 ; assert_bound_check2 BE . get_uint16 ( to_t s ) ( - 1 ) ; assert_bound_check2 BE . get_uint16 ( to_t s ) 9 ; assert_bound_check2 BE . get_int32 ( to_t s ) ( - 1 ) ; assert_bound_check2 BE . get_int32 ...
let test2 ( ) = BE . set_int8 s 0 63 ; assert ( BE . get_uint8 ( to_t s ) 0 = 63 ) ; assert ( BE . get_int8 ( to_t s ) 0 = 63 ) ; BE . set_int8 s 0 155 ; assert ( BE . get_uint8 ( to_t s ) 0 = 155 ) ; BE . set_int8 s 0 ( - 103 ) ; assert ( BE . get_int8 ( to_t s ) 0 = ( - 103 ) ) ; BE . set_int8 s 0 0x1234 ; assert ( B...
let test_64 ( ) = BE . set_int64 s 0 0x1234567890ABCDEFL ; assert ( BE . get_int64 ( to_t s ) 0 = 0x1234567890ABCDEFL ) ; assert ( LE . get_int64 ( to_t s ) 0 = 0xEFCDAB9078563412L ) ; if big_endian then assert ( BE . get_int64 ( to_t s ) 0 = NE . get_int64 ( to_t s ) 0 ) else assert ( LE . get_int64 ( to_t s ) 0 = NE ...
let test_raises_invalid_argument f x = ignore ( Testing . test_raises_exc_p ( function Invalid_argument _ -> true | _ -> false ) f x )
let check b offset s = let rec loop i = i = String . length s || Bytes . get b ( i + offset ) = String . get s i && loop ( i + 1 ) in loop 0
let ( ) = let abcde = Bytes . of_string " abcde " in let open Bytes in begin Testing . test ( length ( extend abcde 7 ( - 7 ) ) = 5 ) ; Testing . test ( length ( extend abcde ( - 7 ) 7 ) = 5 ) ; Testing . test ( let r = extend abcde 0 0 in length r = 5 && check r 0 " abcde " && r != abcde ) ; Testing . test ( let r = e...
let bytes_per_word = Float . of_int ( Int . ( / ) ( Word_size . num_bits Word_size . word_size ) 8 )
let kbyte = 1024 . print_string ( Byte_units . to_string_hum ( Byte_units . of_bytes_int 1000 ) ) ; [ % expect { | 1000B } ] ; | print_string ( Byte_units . to_string_hum ( Byte_units . of_bytes_int 1500 ) ) ; [ % expect { | 1 . 46484K } ] | ; ; ( module struct let f input expected_output = let observed_output = to_str...
let examples = [ Byte_units . zero ; Byte_units . of_bytes_int 1 ; Byte_units . of_bytes_int 10 ; Byte_units . of_bytes_int 100 ; Byte_units . of_bytes_int 1000 ; Byte_units . of_bytes_int 10000 ; Byte_units . of_bytes_int 100000 ; Byte_units . of_bytes_int 1000000 ; Byte_units . of_kilobytes 0 . 1 ; Byte_units . of_ki...
let print_all_byte_units fmt = List . iter examples ~ f ( : fun n -> printf " { !% sexp : Byte_units . Stable . V2 . t } -> % s \ n " n ( sprintf fmt n ) ) ; ; print_all_byte_units " { !% Byte_units } " ; [ % expect { | ( Bytes 0 ) -> 0B ( Bytes 1 ) -> 1B ( Bytes 10 ) -> 10B ( Bytes 100 ) -> 100B ( Bytes 1_000 ) -> 100...
let print_all_of_string of_string = List . iter ~ f ( : fun str -> match of_string str with | t -> printf " !% s -> { % sexp : Byte_units . Stable . V2 . t } \ n " str t | exception exn -> printf " !% s -> ERROR { % Exn } \ n " str exn ) ; ;
let print_all_of_sexp of_sexp = print_all_of_string ( fun str -> Sexp . of_string_conv_exn str of_sexp ) ; ; print_all_of_string Byte_units . of_string [ " 1b " ; " 100000b " ; " 0 . 1k " ; " 1k " ; " 1 . 1k " ; " 1m " ; " 10g " ; " 100t " ; " 10p " ; " 1e " ] ; [ % expect { | 1b -> ( Bytes 1 ) 100000b -> ( Bytes 100_0...
let ensure_round_trippable ( type via ) ( to_ : Byte_units . t -> via ) ( from : via -> Byte_units . t ) ~ tolerance = let equal a b = let open Float . Robustly_comparable in match tolerance with | ` Zero -> [ % compare . equal : Byte_units . t ] a b | ` Epsilon -> let a = Byte_units . bytes_float a in let b = Byte_uni...
let stripe_cnv_rect c ~ x ~ y ~ w ~ h = let x = truncate x and y = truncate y in let w = truncate w and h = truncate h in let idata = C2d . get_image_data c ~ x ~ y ~ w ~ h in let d = C2d . Image_data . data idata in for y = 0 to h - 1 do for x = 0 to w - 1 do if x mod 4 <> 0 then ( ) else let off = 4 * ( y * w + x ) i...
let draw_brr c ~ x ~ y = let size = truncate ( 96 . . * Window . device_pixel_ratio G . window ) in C2d . set_font c Jstr . ( v " bold " + of_int size + v " px SourceSansPro " ) ; C2d . fill_text c ( Jstr . v " Brr " ) ! ~ x ~ y
let draw_rect c ~ x ~ y ~ w ~ h = C2d . set_fill_style c ( C2d . color ( Jstr . v " # 000 " ) ) ; C2d . fill_rect c ~ x ~ y ~ w ~ h
let draw cnv = let c = C2d . create cnv in let w = float @@ Canvas . w cnv in let h = float @@ Canvas . h cnv in C2d . stroke_rect c ~ x : 0 . ~ y : 0 . ~ w ~ h ; let w = 0 . 5 . * w and h = 0 . 5 . * h in let x = w and y = h in draw_rect c ~ x ~ y ~ w ~ h ; stripe_cnv_rect c ~ x ~ y ~ w ~ h ; draw_brr c ~ x : 10 . ~ y...
let set_size cnv = let el = Canvas . to_el cnv in let w = El . inner_w el in let h = Jstr . ( of_int ( truncate ( ( w . * 3 . ) . / 4 . ) ) + v " px " ) in El . set_inline_style El . Style . height h el ; Canvas . set_size_to_layout_size cnv
let main ( ) = let h1 = El . h1 [ El . txt ' " 2D canvas " ] in let info = let brr = El . strong [ El . txt ' " Brr " ] ! in [ El . txt ' " Draws " ; brr ; El . txt ' " and a black and red striped corner . " ] in let cnv = Canvas . create [ ] in let children = [ h1 ; El . p info ; Canvas . to_el cnv ] in El . set_child...
let ( ) = ignore ( main ( ) )
let position_of_assoc ~ equal k ks = let rec aux i = function | [ ] -> None | ( k ' , _ ) :: ks -> if equal k k ' then Some i else aux ( i + 1 ) ks in aux 0 ks
let gen_layout = QCheck . ( make Gen . ( list small_int ) )
let almost_full_cache cache ~ cache_index = match ( cache_size cache ~ cache_index , cache_size_limit cache ~ cache_index ) with | ( Some size , Some limit ) -> size + entry_size >= limit | ( _ , _ ) -> assert false
let equal_identifiers k1 k2 = identifier_of_key k1 = identifier_of_key k2
let number_of_keys cache = let r = ref 0 in for cache_index = 0 to number_of_caches cache - 1 do match list_keys cache ~ cache_index with | Some l -> r := ! r + List . length l | None -> assert false done ; ! r
let gen_entries ( ? high_init_entries = high_init_entries ) ncaches = QCheck . Gen . ( list_size ( int_range low_init_entries high_init_entries ) ( pair ( int_bound ( ncaches - 1 ) ) ( pair string_printable small_int ) ) >>= fun entries -> List . sort_uniq ( fun ( n1 , ( k1 , _ ) ) ( n2 , ( k2 , _ ) ) -> let c = compar...
let insert_entries cache entries = List . fold_left ( fun cache ( _ , _ , k , v ) -> update cache k ( Some ( v , entry_size ) ) ) cache entries
let gen_cache ( ? high_init_entries = high_init_entries ) ( ) = QCheck . ( Gen . ( int_range 1 3 >>= fun ncaches -> let layout = generate ~ n : ncaches ( int_range low_size high_size ) in let cache = from_layout layout in int_range 0 100 >>= fun k -> if k = 0 then return ( layout , [ ] , cache ) else gen_entries ~ high...
let pp_option what fmt = function | None -> Format . fprintf fmt " None " | Some x -> Format . fprintf fmt " Some % a " what x
let pp_int fmt x = Format . fprintf fmt " % d " x