text
stringlengths
0
601k
module Garbled_data = struct let encoding = let open Data_encoding in dynamic_size @@ option @@ string let garbled_msg = let buf = Bytes . create ( 1 lsl 4 ) in TzEndian . set_int32 buf 0 ( Int32 . of_int 4 ) ; TzEndian . set_int32 buf 4 ( Int32 . of_int ( - 1 ) ) ; TzEndian . set_int32 buf 8 ( Int32 . of_int ( - 1 ) )...
let log_config = ref None
let spec = Arg . [ ( " -- addr " , String ( fun p -> addr := Ipaddr . V6 . of_string_exn p ) , " Listening addr " ) ; ( " - v " , Unit ( fun ( ) -> log_config := Some ( Lwt_log_sink_unix . create_cfg ~ rules " : test . p2p . connection -> info ; p2p . connection -> info " ( ) ) ) , " Log up to info msgs " ) ; ( " - vv ...
let init_logs = lazy ( Tezos_base_unix . Internal_event_unix . init ? lwt_log_sink :! log_config ( ) )
let wrap n f = Alcotest_lwt . test_case n ` Quick ( fun _ ( ) -> let open Lwt_syntax in let * ( ) = Lazy . force init_logs in let * r = f ( ) in match r with | Ok ( ) -> Lwt . return_unit | Error error -> Format . kasprintf Stdlib . failwith " % a " pp_print_trace error )
let main ( ) = let anon_fun _num_peers = raise ( Arg . Bad " No anonymous argument . " ) in let usage_msg = " Usage : % s . \ nArguments are " : in Arg . parse spec anon_fun usage_msg ; Lwt_main . run @@ Alcotest_lwt . run ~ argv [ " " ] :|| " tezos - p2p " [ ( " p2p - connection . " , [ wrap " low - level " Low_level ...
let ( ) = Sys . catch_break true ; try main ( ) with _ -> ( )
module Heap = Pairing_heap let test list = let heap = Heap . of_list list ~ cmp : Int . compare in print_s [ % sexp ( heap : int Heap . t ) ] ; while not ( Heap . is_empty heap ) do ignore ( Heap . pop_exn heap : int ) ; print_s [ % sexp ( heap : int Heap . t ) ] done in test [ ] ; [ % expect { | ( ) } ] ; | test [ 3 ]...
let test_copy ~ add_removable ~ remove = let sum t = fold t ~ init : 0 ~ f ( : fun acc i -> acc + i ) in let t = create ~ cmp : Int . compare ( ) in for i = 1 to 99 do add t i ; if i % 10 = 0 then ( ignore ( pop t ) ; add t i ) done ; let token = add_removable t 100 in invariant Fn . ignore t ; let t ' = copy t in inva...
let test_removal ~ add_removable ~ remove ~ elt_value_exn = let t = create ~ cmp : Int . compare ( ) in let tokens = ref [ ] in for i = 1 to 10_000 do tokens := add_removable t i :: ! tokens done ; invariant Fn . ignore t ; List . iter ! tokens ~ f ( : fun token -> if elt_value_exn token t % 2 <> 0 then remove t token ...
let test_ordering ( ) = let t = create ~ cmp : Int . compare ( ) in for _ = 1 to 10_000 do add t ( Random . int 100_000 ) done ; let rec loop last count = if count % 1_000 = 0 then invariant Fn . ignore t ; match pop t with | None -> ( ) | Some v -> assert ( v >= last ) ; loop v ( count + 1 ) in loop ( - 1 ) 0 ; ; let ...
module Q = struct open Caqti_request . Infix open Caqti_type . Std let create = unit . --> unit @:- " CREATE TABLE test_parallel ( x int NOT NULL , y int NOT NULL ) " let drop = unit . --> unit @:- " DROP TABLE IF EXISTS test_parallel " let insert = tup2 int int . --> unit @:- " INSERT INTO test_parallel VALUES ( , ? )...
let random_int ( ) = Random . int ( 1 + Random . int 16 )
module Make ( Ground : Testlib . Sig . Ground ) = struct open Ground let do_query pool = pool |> Caqti_sys . Pool . use @@ fun ( module C : Caqti_sys . CONNECTION ) -> ( match Random . int 4 with | 0 -> C . exec Q . insert ( random_int ( ) , random_int ( ) ) >>=? fun ( ) -> return ( Ok 0 ) | 1 -> C . exec Q . delete ( ...
module Q = struct open Caqti_request . Infix open Caqti_type . Std let nonlin1 = tup3 int int int -->! int @@ fun _ -> S [ L " SELECT 2 * " ; P 2 ; L " + " ; P 2 ; L " - 3 * " ; P 0 ; L " + 5 * " ; P 1 ] let nonlin2 = tup3 int int int -->! int @:- " SELECT 2 * $ 3 + $ 3 - 3 * $ 1 + 5 * $ 2 " let env1 = let env = functi...
module Make ( Ground : Testlib . Sig . Ground ) = struct open Ground let nonlin ( p0 , p1 , p2 ) = 2 * p2 + p2 - 3 * p0 + 5 * p1 let test_nonlin ( module Db : Caqti_sys . CONNECTION ) = let rec loop n = if n = 0 then return ( ) else let p = ( Random . int 1000 , Random . int 1000 , Random . int 1000 ) in ( Db . find Q ...
let compare_ids ( ID expected_id ) ( ID actual_id ) = String . equal expected_id actual_id
let compare_params ( Param ( t1 , id1 ) ) ( Param ( t2 , id2 ) ) = ( t1 == t2 ) && ( compare_ids id1 id2 )
let compare_consts expected actual = match expected , actual with | Int i1 , Int i2 -> i1 == i2 | Char c1 , Char c2 -> c1 == c2 | _ -> false
let rec compare_exps expected actual = match expected , actual with | Const expected_const , Const actual_const -> compare_consts expected_const actual_const | Var id1 , Var id2 -> compare_ids id1 id2 | BinOp ( expected_op , expected_e1 , expected_e2 ) , BinOp ( actual_op , actual_e1 , actual_e2 ) -> ( expected_op == a...
let compare_optional_exps expected actual = match expected , actual with | None , None -> true | Some e1 , Some e2 -> compare_exps e1 e2 | _ -> false
let compare_declarations { var_type = t1 ; var_name = id1 ; init = init1 ; } { var_type = t2 ; var_name = id2 ; init = init2 ; } = t1 == t2 && compare_ids id1 id2 && match init1 , init2 with | None , None -> true | Some e1 , Some e2 -> compare_exps e1 e2 | _ -> false
let rec compare_statements expected actual = match expected , actual with | ReturnVal v1 , ReturnVal v2 -> compare_exps v1 v2 | Block stmts1 , Block stmts2 -> List . for_all2 compare_block_items stmts1 stmts2 | If { cond = cond1 ; if_body = then1 ; else_body = else1 } , If { cond = cond2 ; if_body = then2 ; else_body =...
let compare_funs expected actual = match expected , actual with | FunDecl { fun_type = t1 ; name = name1 ; params = params1 ; body = Some body1 } , FunDecl { fun_type = t2 ; name = name2 ; params = params2 ; body = Some body2 } -> t1 == t2 && compare_ids name1 name2 && List . for_all2 compare_params params1 params2 && ...
let compare_asts ( Prog fun_list1 ) ( Prog fun_list2 ) = List . for_all2 compare_funs fun_list1 fun_list2
let make_int i = Const ( Int i )
let make_exp_statement e = Statement ( Exp ( Some e ) )
let print_test_failure expected actual = let _ = print_string " Expected AST :\ n " in let _ = Pprint . pprint expected in let _ = print_string " Actual AST :\ n " in let _ = Pprint . pprint actual in ( )
let test_expect_failure input fail_msg test_ctxt = let f = fun ( ) -> Parse . parse input in assert_raises ( Failure fail_msg ) f
let test_compare_asts tokens expected_ast test_ctxt = let actual_ast = Parse . parse tokens in let same = compare_asts expected_ast actual_ast in let _ = if ( not same ) then ( print_test_failure expected_ast actual_ast ) else ( ) in assert_bool " Mismatched ASTS " same
let make_ast params block_items = let body = block_items in let name = ID " main " in let f = FunDecl { fun_type = IntType ; name ; params ; body = Some body } in Prog ( [ f ] )
let make_simple_ast params exp = let ret = ReturnVal exp in make_ast params [ Statement ret ]
let simple_token_list = Lex . lex " int main ( ) { return 2 ; } "
let simple_ast = make_simple_ast [ ] ( make_int 2 )
let fun_arg_token_list = Lex . lex " int main ( int argc ) { return 2 ; } "
let fun_arg_ast = let params = [ Param ( IntType , ID " argc " ) ] in let return_exp = make_int 2 in make_simple_ast params return_exp
let return_char_tokens = Lex . lex " int main ( int argc ) { return ' a ' ; } "
let return_char_ast = let params = [ Param ( IntType , ID " argc " ) ] in let return_exp = Const ( Char ' a ' ) in make_simple_ast params return_exp
let basic_parse_tests = [ " test_simple_parse " >:: test_compare_asts simple_token_list simple_ast ; " test_fun_args " >:: test_compare_asts fun_arg_token_list fun_arg_ast ; " test_return_char " >:: test_compare_asts return_char_tokens return_char_ast ; ]
let negation_tokens = Lex . lex " int main ( ) { return - 3 ; } "
let negation_ast = let unop = UnOp ( Negate , make_int 3 ) in make_simple_ast [ ] unop
let pos_tokens = Lex . lex " int main ( ) { return + 3 ; } "
let pos_ast = let unop = UnOp ( Pos , make_int 3 ) in make_simple_ast [ ] unop
let complement_tokens = Lex . lex " int main ( ) { return ~ 3 ; } "
let complement_ast = let unop = UnOp ( Complement , make_int 3 ) in make_simple_ast [ ] unop
let not_tokens = Lex . lex " int main ( ) { return ! 4 ; } "
let not_ast = let unop = UnOp ( Not , make_int 4 ) in make_simple_ast [ ] unop
let unop_parse_tests = [ " test_negation " >:: test_compare_asts negation_tokens negation_ast ; " test_pos " >:: test_compare_asts pos_tokens pos_ast ; " test_complement " >:: test_compare_asts complement_tokens complement_ast ; " test_not " >:: test_compare_asts not_tokens not_ast ; ]
let addition_tokens = Lex . lex " int main ( ) { return 1 + 2 ; } "
let addition_ast = let binop = BinOp ( Add , make_int 1 , make_int 2 ) in let params = [ ] in make_simple_ast params binop
let subtraction_tokens = Lex . lex " int main ( ) { return 4 - 3 ; } "
let subtraction_ast = let binop = BinOp ( Sub , make_int 4 , make_int 3 ) in make_simple_ast [ ] binop
let subtract_negative_tokens = Lex . lex " int main ( ) { return 4 - - 3 ; } "
let subtract_negative_ast = let unop = UnOp ( Negate , make_int 3 ) in let binop = BinOp ( Sub , make_int 4 , unop ) in make_simple_ast [ ] binop
let multi_addition_tokens = Lex . lex " int main ( ) { return 1 + 2 + 3 ; } "
let multi_addition_ast = let inner_binop = BinOp ( Add , make_int 1 , make_int 2 ) in let outer_binop = BinOp ( Add , inner_binop , make_int 3 ) in let params = [ ] in make_simple_ast params outer_binop
let division_tokens = Lex . lex " int main ( ) { return 4 / 5 ; } "
let division_ast = let binop = BinOp ( Div , make_int 4 , make_int 5 ) in make_simple_ast [ ] binop
let nested_addition_tokens = Lex . lex " int main ( ) { return 1 ( + 2 + 3 ) ; } "
let nested_addition_ast = let inner_binop = BinOp ( Add , make_int 2 , make_int 3 ) in let outer_binop = BinOp ( Add , make_int 1 , inner_binop ) in make_simple_ast [ ] outer_binop
let lots_of_parens_tokens = Lex . lex " int main ( ) { return ( ( 3 ) ) ; } "
let lots_of_parens_ast = make_simple_ast [ ] ( make_int 3 )
let left_nested_addition_tokens = Lex . lex " int main ( ) { return ( 1 + 2 ) + 3 ; } "
let left_nested_addition_ast = let inner_binop = BinOp ( Add , make_int 1 , make_int 2 ) in let outer_binop = BinOp ( Add , inner_binop , make_int 3 ) in let params = [ ] in make_simple_ast params outer_binop
let mult_tokens = Lex . lex " int main ( ) { return 4 * 5 ; } "
let mult_ast = let binop = BinOp ( Mult , make_int 4 , make_int 5 ) in make_simple_ast [ ] binop
let lots_of_parens_add_tokens = Lex . lex " int main ( ) { return ( ( 1 + 2 ) ) ; } "
let lots_of_parens_add_ast = let e1 = make_int 1 in let e2 = make_int 2 in let ret_exp = BinOp ( Add , e1 , e2 ) in make_simple_ast [ ] ret_exp
let mod_toks = Lex . lex " int main ( ) { return 3 % 2 ; } "
let mod_ast = let e1 = make_int 3 in let e2 = make_int 2 in let ret_exp = BinOp ( Mod , e1 , e2 ) in make_simple_ast [ ] ret_exp
let precedence_tokens = Lex . lex " int main ( ) { return 1 * 2 + 3 % 2 ; } "
let precedence_ast = let e1 = BinOp ( Mult , make_int 1 , make_int 2 ) in let e2 = BinOp ( Mod , make_int 3 , make_int 2 ) in let outer_binop = BinOp ( Add , e1 , e2 ) in make_simple_ast [ ] outer_binop
let associativity_tokens = Lex . lex " int main ( ) { return 1 / 2 * 3 ; } "
let associativity_ast = let inner_binop = BinOp ( Div , make_int 1 , make_int 2 ) in let outer_binop = BinOp ( Mult , inner_binop , make_int 3 ) in make_simple_ast [ ] outer_binop
let binop_parse_tests = [ " test_addition " >:: test_compare_asts addition_tokens addition_ast ; " test_subtraction " >:: test_compare_asts subtraction_tokens subtraction_ast ; " test_subtract_negative " >:: test_compare_asts subtract_negative_tokens subtract_negative_ast ; " test_multiplication " >:: test_compare_asts...
let bitwise_and_tokens = Lex . lex " int main ( ) { return 1 & 2 ; } "
let bitwise_and_ast = let binop = BinOp ( BitAnd , make_int 1 , make_int 2 ) in make_simple_ast [ ] binop
let bitwise_or_tokens = Lex . lex " int main ( ) { return 1 | 2 ; } "
let bitwise_or_ast = let binop = BinOp ( BitOr , make_int 1 , make_int 2 ) in make_simple_ast [ ] binop
let xor_tokens = Lex . lex " int main ( ) { return 1 ^ 2 ; } "
let xor_ast = let binop = BinOp ( Xor , make_int 1 , make_int 2 ) in make_simple_ast [ ] binop
let shiftl_tokens = Lex . lex " int main ( ) { return 1 << 2 ; } "
let shiftl_ast = let binop = BinOp ( ShiftL , make_int 1 , make_int 2 ) in make_simple_ast [ ] binop
let shiftr_tokens = Lex . lex " int main ( ) { return 1 >> 2 ; } "
let shiftr_ast = let binop = BinOp ( ShiftR , make_int 1 , make_int 2 ) in make_simple_ast [ ] binop
let bitwise_binops_tests = [ " test_bitwise_and " >:: test_compare_asts bitwise_and_tokens bitwise_and_ast ; " test_bitwise_or " >:: test_compare_asts bitwise_or_tokens bitwise_or_ast ; " test_xor " >:: test_compare_asts xor_tokens xor_ast ; " test_shiftl " >:: test_compare_asts shiftl_tokens shiftl_ast ; " test_shiftr...
let and_tokens = Lex . lex " int main ( ) { return 1 && 2 ; } "
let and_ast = let binop = BinOp ( And , make_int 1 , make_int 2 ) in make_simple_ast [ ] binop
let or_tokens = Lex . lex " int main ( ) { return 1 || 2 ; } "
let or_ast = let binop = BinOp ( Or , make_int 1 , make_int 2 ) in make_simple_ast [ ] binop
let bool_precedence_tokens = Lex . lex " int main ( ) { return 1 || 2 && 3 + 4 ; } "
let bool_precedence_ast = let add_binop = BinOp ( Add , make_int 3 , make_int 4 ) in let and_binop = BinOp ( And , make_int 2 , add_binop ) in let or_binop = BinOp ( Or , make_int 1 , and_binop ) in make_simple_ast [ ] or_binop
let boolean_binop_tests = [ " test_and " >:: test_compare_asts and_tokens and_ast ; " test_or " >:: test_compare_asts or_tokens or_ast ; " test_bool_precedence " >:: test_compare_asts bool_precedence_tokens bool_precedence_ast ; ]
let compare_eq_tokens = Lex . lex " int main ( ) { return 1 == 2 ; } "
let compare_eq_ast = let binop = BinOp ( Eq , make_int 1 , make_int 2 ) in make_simple_ast [ ] binop
let compare_neq_tokens = Lex . lex " int main ( ) { return 1 != 2 ; } "
let compare_neq_ast = let binop = BinOp ( Neq , make_int 1 , make_int 2 ) in make_simple_ast [ ] binop
let compare_gt_tokens = Lex . lex " int main ( ) { return 1 > 2 ; } "
let compare_gt_ast = let binop = BinOp ( Gt , make_int 1 , make_int 2 ) in make_simple_ast [ ] binop
let compare_ge_tokens = Lex . lex " int main ( ) { return 1 >= 2 ; } "