text
stringlengths
12
786k
let now = Time . System . to_protocol @@ Systime_os . now ( )
let forge_timestamp ~ delay = Time . Protocol . add now ( Int64 . of_int delay )
let timestamp_pp n = let delay = Time . Protocol . diff n now in Format . asprintf " delay : % Ld " delay
let timestamp = let open QCheck2 in Gen . map ( fun pre_delay -> let delay = ( pre_delay * 20 ) - 300 in forge_timestamp ~ delay ) Gen . ( oneof [ pure 5 ; 0 -- 20 ] )
let value = let open QCheck2 . Gen in pair timestamp peer_id
let print_value ( time_stamp , ( _ , peer_id_str ) ) = Printf . sprintf " ( % s , % s ) " ( timestamp_pp time_stamp ) peer_id_str
let values = let open QCheck2 . Gen in list value
let print_values = QCheck2 . Print . list print_value
let pp fmt = let open Reference in function | Synchronised { is_chain_stuck = true } -> Format . fprintf fmt " Synchronised ( stuck ) " | Not_synchronised -> Format . fprintf fmt " Not synchronised " | Synchronised { is_chain_stuck = false } -> Format . fprintf fmt " Synchronised ( ...
let make_tests check_update lcreate rcreate threshold latency = let open QCheck2 in let threshold_1 = Test . make ~ name : ( Format . asprintf " Shell . synchronisation_heuristic . equivalence - with - reference - implementation \ ( threshold % d ) ( latency % d ) " 1 latency ) ~ pri...
let tests = let module L = Synchronisation_heuristic . Core in let module R = Reference in let check_update state_left state_right ( time_stamp , ( peer_id , _ ) ) = let value = ( time_stamp , peer_id ) in L . update state_left value ; R . update state_right value ; qcheck_eq ' ~ pp...
let ( ) = Alcotest . run " synchronisation heuristic fuzzy " [ ( " synchronisation heuristic fuzzy " , qcheck_wrap tests ) ]
let assert_roundtrip printer encoder decoder str value = let e = Protobuf . Encoder . create ( ) in encoder value e ; assert_equal ~ printer ( : Printf . sprintf " % S " ) str ( Protobuf . Encoder . to_string e ) ; let d = Protobuf . Decoder . of_string str in assert_equal ~ pri...
type b = bool [ @@ deriving protobuf ]
let test_bool ctxt = assert_roundtrip string_of_bool b_to_protobuf b_from_protobuf " \ x08 \ x01 " true
type i1 = int [ @@ deriving protobuf ]
type i2 = int [ @ encoding ` zigzag ] [ @@ deriving protobuf ]
type i3 = int [ @ encoding ` bits32 ] [ @@ deriving protobuf ]
type i4 = int [ @ encoding ` bits64 ] [ @@ deriving protobuf ]
type il1 = int32 [ @ encoding ` varint ] [ @@ deriving protobuf ]
type il2 = int32 [ @ encoding ` zigzag ] [ @@ deriving protobuf ]
type il3 = Int32 . t [ @@ deriving protobuf ]
type il4 = int32 [ @ encoding ` bits64 ] [ @@ deriving protobuf ]
type iL1 = int64 [ @ encoding ` varint ] [ @@ deriving protobuf ]
type iL2 = int64 [ @ encoding ` zigzag ] [ @@ deriving protobuf ]
type iL3 = int64 [ @ encoding ` bits32 ] [ @@ deriving protobuf ]
type iL4 = Int64 . t [ @@ deriving protobuf ]
let test_ints ctxt = assert_roundtrip string_of_int i1_to_protobuf i1_from_protobuf " \ x08 \ xac \ x02 " 300 ; assert_roundtrip string_of_int i2_to_protobuf i2_from_protobuf " \ x08 \ xac \ x02 " 150 ; assert_roundtrip string_of_int i3_to_protobuf i3_from_protobuf " \ x0d \ x2c \ x01 \ x00 ...
type ul1 = uint32 [ @ encoding ` varint ] [ @@ deriving protobuf ]
type ul2 = uint32 [ @ encoding ` zigzag ] [ @@ deriving protobuf ]
type ul3 = Uint32 . t [ @@ deriving protobuf ]
type ul4 = uint32 [ @ encoding ` bits64 ] [ @@ deriving protobuf ]
type uL1 = uint64 [ @ encoding ` varint ] [ @@ deriving protobuf ]
type uL2 = uint64 [ @ encoding ` zigzag ] [ @@ deriving protobuf ]
type uL3 = uint64 [ @ encoding ` bits32 ] [ @@ deriving protobuf ]
type uL4 = Uint64 . t [ @@ deriving protobuf ]
let test_uints ctxt = assert_roundtrip Uint32 . to_string ul1_to_protobuf ul1_from_protobuf " \ x08 \ xac \ x02 " ( Uint32 . of_int 300 ) ; assert_roundtrip Uint32 . to_string ul2_to_protobuf ul2_from_protobuf " \ x08 \ xac \ x02 " ( Uint32 . of_int 150 ) ; assert_roundtrip Uint32 ....
type f1 = float [ @ encoding ` bits32 ] [ @@ deriving protobuf ]
type f2 = float [ @@ deriving protobuf ]
let test_floats ctxt = assert_roundtrip string_of_float f1_to_protobuf f1_from_protobuf " \ x0d \ x00 \ x00 \ xC0 \ x3f " 1 . 5 ; assert_roundtrip string_of_float f2_to_protobuf f2_from_protobuf " \ x09 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ xF8 \ x3f " 1 . 5
type s = string [ @@ deriving protobuf ]
let test_string ctxt = assert_roundtrip ( fun x -> x ) s_to_protobuf s_from_protobuf " \ x0a \ x03abc " " abc "
type by = bytes [ @@ deriving protobuf ]
let test_string ctxt = assert_roundtrip ( fun x -> Bytes . to_string x ) by_to_protobuf by_from_protobuf " \ x0a \ x03abc " ( Bytes . of_string " abc " )
type o = int option [ @@ deriving protobuf ]
let test_option ctxt = let printer x = match x with None -> " None " | Some v -> " Some " ^ ( string_of_int v ) in assert_roundtrip printer o_to_protobuf o_from_protobuf " " None ; assert_roundtrip printer o_to_protobuf o_from_protobuf " \ x08 \ xac \ x02 " ( Some 300 )
type l = int list [ @@ deriving protobuf ]
let test_list ctxt = let printer x = x |> List . map string_of_int |> String . concat " , " in assert_roundtrip printer l_to_protobuf l_from_protobuf " " [ ] ; assert_roundtrip printer l_to_protobuf l_from_protobuf " \ x08 \ xac \ x02 \ x08 \ x2a " [ 300 ; 42 ]
type a = int array [ @@ deriving protobuf ]
let test_array ctxt = let printer x = Array . to_list x |> List . map string_of_int |> String . concat " , " in assert_roundtrip printer a_to_protobuf a_from_protobuf " " [ ] ; || assert_roundtrip printer a_to_protobuf a_from_protobuf " \ x08 \ xac \ x02 \ x08 \ x2a " [ | 300 ; ...
type ts = int * string [ @@ deriving protobuf ]
let test_tuple ctxt = let printer ( x , y ) = Printf . sprintf " % d , % s " x y in assert_roundtrip printer ts_to_protobuf ts_from_protobuf " \ x08 \ xac \ x02 \ x12 \ x08spartans " ( 300 , " spartans " )
type r1 = { r1a : int [ @ key 1 ] ; r1b : string [ @ key 2 ] ;
let test_record ctxt = let printer r = Printf . sprintf " { r1a = % d , r1b = % s } " r . r1a r . r1b in assert_roundtrip printer r1_to_protobuf r1_from_protobuf " \ x08 \ xac \ x02 \ x12 \ x08spartans " { r1a = 300 ; r1b = " spartans " }
type r2 = { r2a : r1 [ @ key 1 ] ;
let test_nested ctxt = let printer r = Printf . sprintf " { r2a = { r1a = % d , r1b = % s } } " r . r2a . r1a r . r2a . r1b in assert_roundtrip printer r2_to_protobuf r2_from_protobuf " \ x0a \ x0d \ x08 \ xac \ x02 \ x12 \ x08spartans " { r2a = { r1a = 300 ; r1b = " ...
type r3 = { r3a : ( int [ @ encoding ` bits32 ] ) * string [ @ key 1 ] ;
let test_imm_tuple ctxt = let printer { r3a = a , b } = Printf . sprintf " { r3a = % d , % s } } " a b in assert_roundtrip printer r3_to_protobuf r3_from_protobuf " \ x0a \ x0f \ x0d \ x2c \ x01 \ x00 \ x00 \ x12 \ x08spartans " { r3a = 300 , " spartans " }
let test_variant ctxt = let printer v = match v with | V1A -> " V1A " | V1B -> " V1B " | V1C i -> Printf . sprintf " V1C ( % d ) " i | V1D ( s1 , s2 ) -> Printf . sprintf " V1D ( % S , % S ) " s1 s2 in assert_roundtrip printer v1_to_protobuf v1_from_protobuf " \ x08 ...
type v2 = r4a : v2 [ @ key 1 ] [ @ bare ]
let test_variant_bare ctxt = let printer { r4a } = match r4a with V2A -> " { r4a = V2A } " | V2B -> " { r4a = V2B } " in assert_roundtrip printer r4_to_protobuf r4_from_protobuf " \ x08 \ x02 " { r4a = V2B }
type ' a r5 = { r5a : ' a [ @ key 1 ]
let test_tvar ctxt = let printer f { r5a } = Printf . sprintf " { r5a = % s } " ( f r5a ) in assert_roundtrip ( printer string_of_int ) ( r5_to_protobuf i1_to_protobuf ) ( r5_from_protobuf i1_from_protobuf ) " \ x0a \ x02 \ x08 \ x01 " { r5a = 1 }
let test_mylist ctxt = let rec printer f v = match v with | Nil -> " Nil " | Cons ( a , r ) -> Printf . sprintf " Cons ( % s , % s ) " ( f a ) ( printer f r ) in assert_roundtrip ( printer string_of_int ) ( mylist_to_protobuf i1_to_protobuf ) ( mylist_from_protobuf i1_fro...
type v3 = [ ` V3A [ @ key 1 ] ]
let test_poly_variant ctxt = let printer v = match v with | ` V3A -> " ` V3A " | ` V3B i -> Printf . sprintf " ` V3B ( % d ) " i | ` V3C ( s1 , s2 ) -> Printf . sprintf " ` V3C ( % S , % S ) " s1 s2 in assert_roundtrip printer v3_to_protobuf v3_from_protobuf " \...
type r6 = { r6a : [ ` R6A [ @ key 1 ] | ` R6B [ @ key 2 ] ] [ @ key 1 ] ;
let test_imm_pvariant ctxt = let printer { r6a } = match r6a with ` R6A -> " { r6a = ` R6A } " | ` R6B -> " { r6a = ` R6B } " in assert_roundtrip printer r6_to_protobuf r6_from_protobuf " \ x0a \ x02 \ x08 \ x02 " { r6a = ` R6B }
type v4 = [ ` V4A [ @ key 1 ] | ` V4B [ @ key 2 ] ] r7a : v4 [ @ key 1 ] [ @ bare ]
let test_pvariant_bare ctxt = let printer { r7a } = match r7a with ` V4A -> " { r7a = ` V4A } " | ` V4B -> " { r7a = ` V4B } " in assert_roundtrip printer r7_to_protobuf r7_from_protobuf " \ x08 \ x01 " { r7a = ` V4A }
type r8 = { r8a : [ ` Request [ @ key 1 ] | ` Reply [ @ key 2 ] ] [ @ key 1 ] [ @ bare ] ; r8b : int [ @ key 2 ] ;
let test_imm_pv_bare ctxt = let printer { r8a ; r8b } = match r8a with | ` Request -> Printf . sprintf " { r8a = ` Request ; r8b = % d } " r8b | ` Reply -> Printf . sprintf " { r8a = ` Reply ; r8b = % d } " r8b in assert_roundtrip printer r8_to_protobuf r8_from_protobuf...
let test_variant_optrep ctxt = let printer v5 = match v5 with | V5A io -> ( match io with Some i -> Printf . sprintf " V5A % d " i | None -> " V5A None " ) | V5B sl -> Printf . sprintf " V5B [ % s ] " ( String . concat " ; " sl ) | V5C ia -> Printf . sprintf " V5C [ ...
type r9 = i1 r5 [ @@ deriving protobuf ]
let test_nonpoly ctxt = let printer { r5a } = Printf . sprintf " { r5a = % d } " r5a in assert_roundtrip printer r9_to_protobuf r9_from_protobuf " \ x0a \ x04 \ x0a \ x02 \ x08 \ x01 " { r5a = 1 }
type d = int [ @ default 42 ] [ @@ deriving protobuf ]
let test_default ctxt = assert_roundtrip string_of_int d_to_protobuf d_from_protobuf " " 42 ; assert_roundtrip string_of_int d_to_protobuf d_from_protobuf " \ x08 \ x01 " 1
type p = int list [ @ packed ] [ @@ deriving protobuf ]
let test_packed ctxt = let printer xs = Printf . sprintf " [ % s ] " ( String . concat " ; " ( List . map string_of_int xs ) ) in assert_roundtrip printer p_to_protobuf p_from_protobuf " " [ ] ; assert_roundtrip printer p_to_protobuf p_from_protobuf " \ x0a \ x01 \ x01 " ...
let test_errors ctxt = let d = Protobuf . Decoder . of_string " " in assert_raises Protobuf . Decoder . ( Failure ( Missing_field " Test_syntax . s " ) ) ( fun ( ) -> s_from_protobuf d ) ; let d = Protobuf . Decoder . of_string " \ x0d \ x00 \ x00 \ xC0 \ x3f " in as...
let test_skip ctxt = let d = Protobuf . Decoder . of_string " \ x15 \ x00 \ x00 \ xC0 \ x3f " in assert_raises Protobuf . Decoder . ( Failure ( Missing_field " Test_syntax . s " ) ) ( fun ( ) -> s_from_protobuf d )
module type Elem = sig type t [ @@ deriving protobuf ] end
module Collection ( Elem : Elem ) = struct type t = Elem . t list [ @@ deriving protobuf ] end
let suite = " Test syntax " >::: [ " test_bool " >:: test_bool ; " test_ints " >:: test_ints ; " test_uints " >:: test_uints ; " test_floats " >:: test_floats ; " test_string " >:: test_string ; " test_option " >:: test_option ; " test_list " >:: test_list ; " ...
let show t = Current_buffer . set_temporarily_to_temp_buffer Sync ( fun ( ) -> Current_buffer . set_syntax_table t ; let by_class = Class . Table . create ( ) in for i = 0 to 127 do let char = i |> Char . of_int_exn in if Char . is_print char then Hashtbl . add_multi by_class ~ key ( ...
[ % test_eq : string ] ( unix_quote " " ) { ' ' } ; || [ % test_eq : string ] ( unix_quote " a " ) { ' | a ' } ; | [ % test_eq : string ] ( unix_quote " a ' " ) { ' | a ' ' ' ' } ; \| [ % test_eq : string ] ( unix_quote " a ...
for _ = 1 to 1000 do for int = ( - Int . Private . length_preallocated_errnos - 1 ) to Int . Private . length_preallocated_ms - 1 do let t = Int . Private . of_int int in let before_minor = Gc . minor_words ( ) in let before_major = Gc . major_words ( ) in let result = Int . ...
module QCheck_legacy = struct let alpha st = Char . chr ( Char . code ' a ' + Random . State . int st ( Char . code ' z ' - Char . code ' a ' ) ) let string_len len st = let n = len st in assert ( n >= 0 ) ; let b = Buffer . create n in for _i = 0 to n - 1 do Buffer...
let priority = QCheck . int_bound 184
let priority_g = QCheck . Gen . int_bound 184
let ptime_g = let open QCheck . Gen in int_bound ( int_of_float @@ 2 . ** 29 . ) >|= fun n -> n |> Ptime . Span . of_int_s |> Ptime . of_span
let pp_ptime = function | None -> " < None " > | Some pt -> Rfc3164_Timestamp . encode pt
let ptime = QCheck . make ~ print : pp_ptime ptime_g
let valid_data_succeeds = let open QCheck in Test . make ~ count : 100 ~ name " : generating valid data gets a reasonable result " ( triple priority ptime QCheck_legacy . string ) @@ fun ( pri , pt , host ) -> assume ( pt <> None && String . length host > 1 ) ; match pt with | N...
let invalid_timestamp = let open QCheck in let pp = Print . ( quad int pp_ptime string string ) in Test . make ~ count : 100 ~ name " : parser substitutes the timestamp when it can ' t be parsed " ( make ~ print : pp Gen . ( quad priority_g ptime_g QCheck_legacy . string_g QCheck_legacy ....
let invalid_data_fails = QCheck . Test . make ~ count : 100 ~ name " : putting in invalid data always fails " QCheck . string @@ fun msg -> let ctx = { timestamp = Ptime . epoch ; hostname = " " ; set_hostname = false } in match decode ~ ctx msg with | Error _ -> true | Ok _ -...
let ( ) = let suite = [ invalid_data_fails ; valid_data_succeeds ; invalid_timestamp ] in QCheck_runner . run_tests_main suite
let test_mode = " test - tabulated - list - mode " |> Symbol . intern
type entry = { id : string ; s1 : string ; i2 : int ; s3 : string }
module M = ( val define_derived_mode test_mode [ % here ] ~ docstring " : for testing " ~ mode_line " : Test - mode " ~ parent : Tabulated_list_mode . major_mode ( ) )