text
stringlengths
12
786k
let stepbystep_wrong s = while step s = Rc . ROW do for i = 0 to data_count s do printf " % s column [ column % d ] d % s = % s \ n " %! ( column_decltype s i ) i i ( column_name s i ) i ( Data . to_string_coerce ( column s i ) i ) i done done
let mk_tbl db id = let sql = sprintf " CREATE TABLE tbl % d ( a varchar ( varchar1 ) varchar1 , b INTEGER , c FLOAT ) FLOAT " id in Rc . check ( exec db sql ) sql ; let sql = sprintf " INSERT INTO tbl % d VALUES ( ' a ' , 3 , 3 . 14 ) 14 " id in Rc . check ( exec db sq...
module Int32 = struct type t = int32 let encoding = Data_encoding . int32 module Index = struct type t = int let path_length = 1 let to_path c l = string_of_int c :: l let of_path = function | [ ] | _ :: _ :: _ -> None | [ c ] -> int_of_string_opt c type ' a ipath = ' a * t let args ...
module Int64 = struct type t = int64 let encoding = Data_encoding . int64 module Index = struct type t = int64 let path_length = 1 let to_path c l = Int64 . to_string c :: l let of_path = function | [ ] | _ :: _ :: _ -> None | [ c ] -> Int64 . of_string_opt c type ' a ipath = ' a ...
let create_context name : ( module Raw_context . T with type t = Raw_context . t ) = ( module Make_subcontext ( Registered ) ( Raw_context ) ( struct let name = [ name ] end ) )
let create_subcontext name ( module Context : Raw_context . T with type t = Raw_context . t ) : ( module Raw_context . T with type t = Raw_context . t ) = ( module Make_subcontext ( Registered ) ( Context ) ( struct let name = [ name ] end ) )
let create_single_data_storage name ( module Context : Raw_context . T with type t = Raw_context . t ) : ( module Single_data_storage with type t = Context . t and type value = Int32 . t ) = ( module Make_single_data_storage ( Registered ) ( Context ) ( struct let name = [ name ] ...
let create_indexed_subcontext_int32 ( module Context : Raw_context . T with type t = Raw_context . t ) : ( module Data_set_storage with type t = Raw_context . t ) = ( module Make_data_set_storage ( Context ) ( Int32 . Index ) )
let create_indexed_subcontext_int64 ( module Context : Raw_context . T with type t = Raw_context . t ) : ( module Data_set_storage with type t = Raw_context . t ) = ( module Make_data_set_storage ( Context ) ( Int64 . Index ) )
let must_failwith f_prog error = try let _ = f_prog ( ) in Alcotest . fail " Unexpected successful result " with exc -> if exc = error then Lwt . return_unit else Alcotest . fail " Unexpected error result "
let test_register_single_data ( ) = let f_prog ( ) = let context = create_context " context1 " in let _single_data = create_single_data_storage " single_data " context in create_single_data_storage " single_data " context in let error = Invalid_argument " Could not register a value at [ co...
let test_register_named_subcontext ( ) = let f_prog ( ) = let context = create_context " context2 " in let subcontext = create_subcontext " sub_context " context in let _single_data = create_single_data_storage " error_register " subcontext in let subcontext = create_subcontext " error_regi...
let test_register_indexed_subcontext ( ) = let f_prog ( ) = let context = create_context " context3 " in let _ = create_single_data_storage " single_value " context in create_indexed_subcontext_int32 context in let error = Invalid_argument " Could not register an indexed subcontext at [ contex...
let test_register_indexed_subcontext_2 ( ) = let f_prog ( ) = let context = create_context " context4 " in let _ = create_indexed_subcontext_int32 context in create_indexed_subcontext_int64 context in let error = Invalid_argument " An indexed subcontext at [ context4 ] already exists but has a d...
let tests = [ Alcotest_lwt . test_case " register single data in existing path " ` Quick ( fun _ -> test_register_single_data ) ; Alcotest_lwt . test_case " register named subcontext in existing path " ` Quick ( fun _ -> test_register_named_subcontext ) ; Alcotest_lwt . test_case " ...
module Int32 = struct type t = int32 let encoding = Data_encoding . int32 module Index = struct type t = int let path_length = 1 let to_path c l = string_of_int c :: l let of_path = function | [ ] | _ :: _ :: _ -> None | [ c ] -> int_of_string_opt c type ' a ipath = ' a * t let args ...
module String = struct type t = string let encoding = Data_encoding . string end
module Root_raw_context = Make_subcontext ( Registered ) ( Raw_context ) ( struct let name = [ " test_storage_functors " ] end )
module Indexed_context = Make_indexed_subcontext ( Make_subcontext ( Registered ) ( Root_raw_context ) ( struct let name = [ " index " ] end ) ) ( Int32 . Index )
module Table = Make_carbonated_data_set_storage ( Make_subcontext ( Registered ) ( Raw_context ) ( struct let name = [ " table " ] end ) ) ( Int32 . Index )
let wrap m = m >|= Environment . wrap_tzresult
let test_fold_keys_unaccounted ( ) = let open Lwt_tzresult_syntax in let * ctxt = Context . default_raw_context ( ) in let * ( ctxt , _ ) = wrap ( Table . init ctxt 1 ) in let * ( ctxt , _ ) = wrap ( Table . init ctxt 2 ) in let *! items = Table . fold_keys_unaccounted c...
let tests = [ Tztest . tztest " fold_keys_unaccounted smoke test " ` Quick test_fold_keys_unaccounted ; ]
let test_init _ = return_unit
let test_cycles store = let open Lwt_result_syntax in let chain_store = Store . main_chain_store store in let * blocks = List . fold_left_es ( fun acc _ -> let * ( blocks , _head ) = append_cycle ~ should_set_head : true chain_store in return ( blocks @ acc ) ) [ ] ( 1 -- 10 ) i...
let test_cases = let wrap_test ( s , f ) = let f _ = f in wrap_test ( s , f ) in List . map wrap_test [ ( " initialisation " , test_init ) ; ( " store cycles " , test_cycles ) ]
let rec compare_path is_eq p1 p2 = match ( p1 , p2 ) with | ( [ ] , [ ] ) -> true | ( h1 :: p1 , h2 :: p2 ) -> is_eq h1 h2 && compare_path is_eq p1 p2 | _ -> false
let vblock tbl k = Nametbl . find tbl k |> WithExceptions . Option . to_exn ~ none : Not_found
let pp_print_list fmt l = Format . fprintf fmt " [ @< h >% a ] " @ ( Format . pp_print_list ~ pp_sep : Format . pp_print_space Block_hash . pp_short ) ( List . map Store . Block . hash l )
let test_path chain_store tbl = let open Lwt_syntax in let check_path h1 h2 p2 = let * o = Store . Chain_traversal . path chain_store ~ from_block ( : vblock tbl h1 ) ~ to_block ( : vblock tbl h2 ) in match o with | None -> Assert . fail_msg " cannot compute path % s -> % s " h1 h2 | Som...
let test_ancestor chain_store tbl = let open Lwt_syntax in let check_ancestor h1 h2 expected = let * o = Store . Chain_traversal . common_ancestor chain_store ( vblock tbl h1 ) ( vblock tbl h2 ) in match o with | None -> Assert . fail_msg " not ancestor found " | Some a -> if not ( Block_has...
let seed = let receiver_id = P2p_peer . Id . of_string_exn ( String . make P2p_peer . Id . size ' r ' ) in let sender_id = P2p_peer . Id . of_string_exn ( String . make P2p_peer . Id . size ' s ' ) in { Block_locator . receiver_id ; sender_id }
let iter2_exn f l1 l2 = List . iter2 ~ when_different_lengths ( : Failure " iter2_exn " ) f l1 l2 |> function | Ok ( ) -> ( ) | _ -> assert false
let test_locator chain_store tbl = let open Lwt_syntax in let check_locator length h1 expected = let * { Block_locator . history ; _ } = Store . Chain . compute_locator chain_store ~ max_size : length ( vblock tbl h1 ) seed in if Compare . List_lengths . ( history <> expected ) then Asse...
let compare s name heads l = List . iter ( fun b -> Format . printf " % s . " @ ( rev_lookup b s ) ) heads ; if Compare . List_lengths . ( heads <> l ) then Assert . fail_msg " unexpected known_heads size ( % s : % d % d ) " name ( List . length heads ) ( List . ...
let test_known_heads chain_store tbl = let open Lwt_result_syntax in let *! heads = Store . Chain . known_heads chain_store in let heads = List . map fst heads in compare tbl " initial " heads [ " Genesis " ] ; let * _ = Store . Chain . set_head chain_store ( vblock tbl " A8 " ) i...
let test_head chain_store tbl = let open Lwt_result_syntax in let *! head = Store . Chain . current_head chain_store in let *! genesis_block = Store . Chain . genesis_block chain_store in if not ( Store . Block . equal head genesis_block ) then Assert . fail_msg " unexpected head " ; let *...
let test_mem chain_store tbl = let open Lwt_result_syntax in let mem x = let b = vblock tbl x in let b_descr = Store . Block . ( hash b , level b ) in Store . Chain . is_in_chain chain_store b_descr in let test_mem x = let *! b = mem x in match b with true -> Lwt . return_unit | false -> Asser...
let test_new_blocks chain_store tbl = let open Lwt_syntax in let test head h expected_ancestor expected = let to_block = vblock tbl head and from_block = vblock tbl h in let * ( ancestor , blocks ) = Store . Chain_traversal . new_blocks chain_store ~ from_block ~ to_block in if not ( Block_hash . ...
let test_basic_checkpoint chain_store table = let open Lwt_result_syntax in let block = vblock table " A1 " in let * _prev_head = Store . Chain . set_head chain_store block in let * ( ) = Store . Chain . set_target chain_store ( Store . Block . hash block , Store . Block . level block...
let test_acceptable_block chain_store table = let open Lwt_result_syntax in let block = vblock table " A2 " in let block_hash = Store . Block . hash block in let level = Store . Block . level block in let * _prev_head = Store . Chain . set_head chain_store block in let * ( ) = Store . Cha...
let test_is_valid_target chain_store table = let open Lwt_result_syntax in let block = vblock table " A2 " in let block_hash = Store . Block . hash block in let level = Store . Block . level block in let * _prev_head = Store . Chain . set_head chain_store block in let * ( ) = Store . Chai...
let test_best_know_head_for_checkpoint chain_store table = let open Lwt_result_syntax in let block = vblock table " A2 " in let block_hash = Store . Block . hash block in let level = Store . Block . level block in let checkpoint = ( block_hash , level ) in let * _prev_head = Store . Chain . ...
let test_future_target chain_store _ = let open Lwt_result_syntax in let *! genesis_block = Store . Chain . genesis_block chain_store in let genesis_descr = Store . Block . descriptor genesis_block in let *! ( bad_chain , bad_head ) = make_raw_block_list genesis_descr 5 in let *! ( good_chain ,...
let test_reach_target chain_store table = let open Lwt_result_syntax in let mem x = let b = vblock table x in Store . Chain . is_in_chain chain_store Store . Block . ( hash b , level b ) in let test_mem x = let *! b = mem x in match b with true -> Lwt . return_unit | false -> Assert . fail_ms...
let test_not_may_update_target chain_store table = let open Lwt_result_syntax in let block_a2 = vblock table " A2 " in let target_hash = Store . Block . hash block_a2 in let target_level = Store . Block . level block_a2 in let target = ( target_hash , target_level ) in let * _pred_head = Store...
let testable_hash = Alcotest . testable ( fun fmt h -> Format . fprintf fmt " % s " ( Block_hash . to_b58check h ) ) Block_hash . equal
let init_block_of_identifier_test chain_store table = let open Lwt_result_syntax in let + _ = vblock table " A8 " |> Store . Chain . set_head chain_store in ( )
let vblock_hash table name = vblock table name |> Store . Block . hash
let assert_successful_block_of_identifier ( ? init = init_block_of_identifier_test ) ~ input ~ expected chain_store table = let open Lwt_result_syntax in let * _ = init chain_store table in let + found = Store . Chain . block_of_identifier chain_store input in Alcotest . check testable_hash " same...
let assert_failing_block_of_identifier ( ? init = init_block_of_identifier_test ) ~ input chain_store table = let open Lwt_result_syntax in let * _ = init chain_store table in let *! r = Store . Chain . block_of_identifier chain_store input in match r with | Ok b -> Assert . fail_msg ~ given ( ...
let test_block_of_identifier_success_block_from_level chain_store table = let a5 = vblock table " A5 " in assert_successful_block_of_identifier ~ input ( ` : Level ( Store . Block . level a5 ) ) ~ expected ( : Store . Block . hash a5 ) chain_store table
let test_block_of_identifier_success_block_from_hash chain_store table = let a5_hash = vblock_hash table " A5 " in assert_successful_block_of_identifier ~ input ( ` : Hash ( a5_hash , 0 ) ) ~ expected : a5_hash chain_store table
let test_block_of_identifier_success_block_from_hash_predecessor chain_store table = assert_successful_block_of_identifier ~ input ( ` : Hash ( vblock_hash table " A5 " , 2 ) ) ~ expected ( : vblock_hash table " A3 " ) chain_store table
let test_block_of_identifier_success_block_from_hash_successor chain_store table = assert_successful_block_of_identifier ~ input ( ` : Hash ( vblock_hash table " A5 " , - 2 ) ) ~ expected ( : vblock_hash table " A7 " ) chain_store table
let test_block_of_identifier_success_caboose chain_store table = assert_successful_block_of_identifier ~ input ( ` : Alias ( ` Caboose , 0 ) ) ~ expected ( : vblock_hash table " Genesis " ) chain_store table
let test_block_of_identifier_success_caboose_successor chain_store table = assert_successful_block_of_identifier ~ input ( ` : Alias ( ` Caboose , - 2 ) ) ~ expected ( : vblock_hash table " A2 " ) chain_store table
let test_block_of_identifier_failure_caboose_predecessor chain_store table = assert_failing_block_of_identifier ~ input ( ` : Alias ( ` Caboose , 2 ) ) chain_store table
let test_block_of_identifier_success_checkpoint chain_store table = let open Lwt_result_syntax in let a5 = vblock table " A5 " in let a5_hash = Store . Block . hash a5 in let a5_descriptor = ( a5_hash , Store . Block . level a5 ) in assert_successful_block_of_identifier ~ init ( : fun cs t ->...
let test_block_of_identifier_success_checkpoint_predecessor chain_store table = let open Lwt_result_syntax in let a5 = vblock table " A5 " in let a5_hash = Store . Block . hash a5 in let a5_descriptor = ( a5_hash , Store . Block . level a5 ) in assert_successful_block_of_identifier ~ init ( : ...
let test_block_of_identifier_success_checkpoint_successor chain_store table = let open Lwt_result_syntax in let a5 = vblock table " A5 " in let a5_hash = Store . Block . hash a5 in let a5_descriptor = ( a5_hash , Store . Block . level a5 ) in assert_successful_block_of_identifier ~ init ( : fu...
let test_block_of_identifier_failure_checkpoint_successor chain_store table = let open Lwt_result_syntax in let a5 = vblock table " A5 " in let a5_hash = Store . Block . hash a5 in let a5_descriptor = ( a5_hash , Store . Block . level a5 ) in assert_failing_block_of_identifier ~ init ( : fun c...
let test_block_of_identifier_success_savepoint chain_store table = assert_successful_block_of_identifier ~ input ( ` : Alias ( ` Savepoint , 0 ) ) ~ expected ( : vblock_hash table " Genesis " ) chain_store table
let tests = let test_tree_cases = List . map wrap_test [ ( " path between blocks " , test_path ) ; ( " common ancestor " , test_ancestor ) ; ( " block locators " , test_locator ) ; ( " known heads " , test_known_heads ) ; ( " set head " , test_head ) ; (...
module type Str_intf = module type of Str
module Test_matches ( R : Str_intf ) = struct let groups ( ) = let group i = try ` Found ( R . group_beginning i ) with | Not_found -> ` Not_found | Invalid_argument _ -> ` Not_exists in let rec loop acc i = match group i with | ` Found p -> loop ( ( p , R . group_end i ) ::...
let eq_match ? pos ? case r s = expect_equal_app ~ msg ( : str_printer s ) ~ printer ( : opt_printer ( list_printer ofs_printer ) ) ( fun ( ) -> T_str . eq_match ? pos ? case r s ) ( ) ( fun ( ) -> T_re . eq_match ? pos ? case r s ) ( ) ; ;
let split_result_conv = List . map ( function | Str . Delim x -> Re . Str . Delim x | Str . Text x -> Re . Str . Text x )
let pp_split_result_list = Fmt . pp_olist ( fun fmt x -> let ( tag , arg ) = match x with | Re . Str . Delim x -> ( " Delim " , x ) | Re . Str . Text x -> ( " Text " , x ) in Fmt . fprintf fmt " % s @ ( " \% s " ) " \ tag arg )
let pp_fs pp_args pp_out fmt ( name , re , args , ex , res ) = let f fmt ( mod_ , r ) = Fmt . fprintf fmt " % s . % s % a % a = % a " mod_ name Fmt . quote re pp_args args pp_out r in Fmt . fprintf fmt " . @% a . @% a " f ( " Str " , ex ) f ( " Re . Str " ...
type ( ' a , ' b ) test = { name : string ; pp_args : ' a Fmt . t ; pp_out : ' b Fmt . t ; re_str : Re . Str . regexp -> ' a -> ' b ; str : Str . regexp -> ' a -> ' b }
let bounded_split_t = { name = " bounded_split " ; pp_args = ( fun fmt ( s , n ) -> Fmt . fprintf fmt " % a % d " Fmt . quote s n ) ; pp_out = Fmt . pp_str_list ; re_str = ( fun re ( s , n ) -> Re . Str . bounded_split re s n ) ; str = ( fun re ( s , n ) ...
let bounded_full_split_t = { bounded_split_t with name = " bounded_full_split " ; pp_out = pp_split_result_list ; re_str = ( fun re ( s , n ) -> Re . Str . bounded_full_split re s n ) ; str = ( fun re ( s , n ) -> split_result_conv ( Str . bounded_full_split re s n ) ) }...
let full_split_t = { bounded_full_split_t with name = " full_split " ; pp_args = ( fun fmt s -> Fmt . fprintf fmt " % a " Fmt . quote s ) ; re_str = ( fun re s -> Re . Str . full_split re s ) ; str = ( fun re s -> split_result_conv ( Str . full_split re s ) ) }
let split_delim_t = { full_split_t with name = " split_delim " ; pp_out = Fmt . pp_str_list ; re_str = ( fun re s -> Re . Str . split_delim re s ) ; str = ( fun re s -> Str . split_delim re s ) }
let split_t = { name = " split " ; pp_out = Fmt . pp_str_list ; pp_args = full_split_t . pp_args ; re_str = ( fun re s -> Re . Str . split re s ) ; str = ( fun re s -> Str . split re s ) }
let global_replace_t = { name = " global_replace " ; pp_out = Fmt . pp_print_string ; pp_args = ( fun fmt ( r , s ) -> Fmt . fprintf fmt " % a % a " Fmt . quote r Fmt . quote s ) ; re_str = ( fun re ( r , s ) -> Re . Str . global_replace re r s ) ; str = ( f...
let test t re args = assert_equal ~ pp_diff ( : fun fmt ( ex , act ) -> pp_fs t . pp_args t . pp_out fmt ( t . name , re , args , ex , act ) ) ~ printer ( : Fmt . to_to_string t . pp_out ) ( t . re_str ( Re . Str . regexp re ) args ) ( t . str ( Str . rege...
let split_delim re s = test split_delim_t re s
let split re s = test split_t re s
let full_split re s = test full_split_t re s
let bounded_split re s n = test bounded_split_t re ( s , n )
let bounded_full_split re s n = test bounded_full_split_t re ( s , n )
let global_replace re r s = test global_replace_t re ( r , s )
let _ = expect_pass " str " ( fun ( ) -> eq_match " a " " a " ; eq_match " a " " b " ; ) ; expect_pass " alt " ( fun ( ) -> eq_match " a \\| b " " a " ; eq_match " a \\| b " " b " ; eq_match " a \\| b " " c " ; ) ; expect_pass " seq ...
let add_byte b = Bytes . concat ( Bytes . of_string ) " " [ b ; Bytes . of_string " \ x00 ] "
let test_equal_keys ctxt = let sk = Bytes . of_string ( String . make ( Stream . key_size ) key_size ' A ' ) ' A ' in let sk ' = Bytes . of_string ( " B " ^ ( String . make ( Stream . key_size - 1 ) 1 ' A ' ) ' A ' ) ' A ' in let sk ' ' = Bytes . of_string ( ( ...
let test_permute ctxt = let k = Stream . random_key ( ) in assert_raises ( Size_mismatch " Stream . to_key ) " ( fun ( ) -> ( Stream . Bytes . to_key ( add_byte ( Stream . Bytes . of_key k ) k ) k ) k ) k
let test_nonce_operations ctxt = let n = Bytes . of_string ( String . make Stream . nonce_size ' \ x00 ' ) x00 ' in let n ' = Bytes . of_string ( ( String . make ( Stream . nonce_size - 1 ) 1 ' \ x00 ' ) x00 ' ^ " \ x01 ) " in let n ' ' = Bytes . of_string ( ( ...
let setup ( ) = Stream . random_key ( ) , Bytes . of_string " wild wild fox " , Stream . random_nonce ( )
let test_stream ctxt = let k , msg , nonce = setup ( ) in let nonce ' = Stream . random_nonce ( ) in let cmsg = Stream . Bytes . stream k 10 nonce in let cmsg ' = Stream . Bytes . stream k 10 nonce ' in assert_bool " not equal " ( cmsg <> cmsg ' ) cmsg '
let test_stream_xor ctxt = let k , msg , nonce = setup ( ) in let cmsg = Stream . Bytes . stream_xor k msg nonce in let msg ' = Stream . Bytes . stream_xor k cmsg nonce in assert_equal msg msg '
let suite = " Stream " >::: [ " test_equal_keys " >:: test_equal_keys ; " test_nonce_operations " >:: test_nonce_operations ; " test_permute " >:: test_permute ; " test_stream " >:: test_stream ; " test_stream_xor " >:: test_stream_xor ; ]
let exn = Failure ( " failure " )
let fails = fun ( ) -> raise exn
let with_file_reading name f = let c = open_in name in try f c ; close_in_noerr c with exn -> close_in_noerr c ; raise exn
let with_file_writing name f = let c = open_out name in try f c ; close_out_noerr c with exn -> close_out_noerr c ; raise exn
let tests = [ ( " stream_io . sync . string " >:: fun _ -> let s = string " foo " in to_list s |> assert_equal [ ' f ' ; ' o ' ; ' o ' ] ; next s |> assert_equal None ; next s |> assert_equal None ) ; ( " stream_io . sync . buffer " >:: fun _ -> let b = ...
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 " ) -> ( )