text stringlengths 0 601k |
|---|
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 ) ; ( " blocks in chain " , test_mem ) ; ( " new blocks " , test_new_bl... |
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 ) :: acc ) ( i + 1 ) | ` Not_found -> loop ( ( - ... |
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 " , res ) |
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 ) -> Str . bounded_split 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 = ( fun re ( r , s ) -> Str . global_replace re r s ) } |
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 . regexp re ) args ) |
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 " ( fun ( ) -> eq_match " ab " " ab " ; eq_match " ab " " ac " ; ) ; expect_pass " epsi... |
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 ( ( String . make ( Stream . key_size - 1 ) 1 ' A ' ) ' A ' ^ "... |
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 ( ( String . make ( Stream . nonce_size - 1 ) 1 ' \ x00 ' ) x00... |
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 = Buffer . create 4 in let s = buffer b in Buffer . add_string b " f... |
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 rec build_string f n accu = if n <= 0 then String . concat " " accu else build_string f ( n - 1 ) ( f ( n - 1 ) :: accu ) ; ; |
let char n = String . make 1 ( Char . chr n ) ; ; |
let reference n = if n = 8 then " \\ b " else if n = 9 then " \\ t " else if n = 10 then " \\ n " else if n = 13 then " \\ r " else if n = Char . code ' " ' \ then " " " \\\ else if n = Char . code ' ' \\ then " " \\\\ else if n < 32 || n > 126 then Printf . sprintf " \\% 03d " n else char n ; ; |
let raw_string = build_string char 256 [ ] ; ; |
let ref_string = build_string reference 256 [ ] ; ; |
let check_split sep s = let l = String . split_on_char sep s in assert ( List . length l > 0 ) ; assert ( String . concat ( String . make 1 sep ) l = s ) ; List . iter ( String . iter ( fun c -> assert ( c <> sep ) ) ) l ; ; |
let ( ) = let s = " abc def " in for i = 0 to String . length s do check_split ' ' ( String . sub s 0 i ) done ; ; |
let ( ) = if Sys . word_size = 32 then begin let big = String . make Sys . max_string_length ' x ' in let push x l = l := x :: ! l in let ( ) += a b = a := ! a + b in let sz , l = ref 0 , ref [ ] in while ! sz >= 0 do push big l ; sz += Sys . max_string_length done ; while ! sz <= 0 do push big l ; sz += Sys . max_stri... |
let ( ) |> x f = f x |
let printer elems = " [ " ^ ( elems |> List . map ( fun x -> " " " \ ^ x ^ " " " ) \ |> String . concat " ; " ) ^ " ] " |
let test_split_1 _ = let strings = Stringext . split " test : one : two " ~ on ' ' :: in assert_equal ~ printer [ " test " ; " one " ; " two " ] strings |
let test_split_bounded_1 _ = let strings = Stringext . split " testing : foo : bar " ~ on ' ' :: ~ max : 2 in assert_equal ~ printer [ " testing " ; " foo : bar " ] strings |
let test_split_none _ = let s = " foo : bar " in assert_equal ~ printer [ s ] ( Stringext . split s ~ on ' ' ) := |
let split_trim_left1 _ = let strings = Stringext . split_trim_left " testing , stuff ; \ t again " ~ on " , ; " : ~ trim " : \ t " in assert_equal ~ printer [ " testing " ; " stuff " ; " again " ] strings |
let split_trim_left2 _ = let strings = Stringext . split_trim_left " testing , stuff ; \ t again " ~ on " , ; " : ~ trim " : \ t " in assert_equal ~ printer [ " testing " ; " stuff " ; " again " ] strings |
let split_trim_left3 _ = assert_equal ~ printer [ " a " ; " b " ; " c " ] ( Stringext . split_trim_left ~ on " , " : ~ trim " : " " a , b , c " ) |
let split_trim_left4 _ = assert_equal ~ printer [ " vpof " ; " hbjeu " ; " " ; " c " ] ( Stringext . split_trim_left ~ on " , " : ~ trim " : " " vpof , hbjeu , , c " ) |
let printer s = " ' " ^ ( String . concat " ; " s ) ^ " ' " |
let full_split1 _ = let strings = Stringext . full_split " // var / test // ocaml " / ~ on ' ' :/ in assert_equal ~ printer [ " " ; " " ; " // var " ; " " ; " / test " ; " " ; " " ; " // ocaml " ; " " ] / strings |
let full_split2 _ = let strings = Stringext . full_split " // foobar . com / quux " ~ on ' ' :/ in assert_equal ~ printer [ " " ; " " ; " // foobar . com " ; " " ; " / quux " ] strings |
let full_split3 _ = let strings = Stringext . full_split " foobar . com / quux " ~ on ' ' :/ in assert_equal ~ printer [ " foobar . com " ; " " ; " / quux " ] strings |
let full_split4 _ = let strings = Stringext . full_split " a / path / fragment " ~ on ' ' :/ in assert_equal ~ printer [ " a " ; " " ; " / path " ; " " ; " / fragment " ] strings |
let ( to_list , of_list ) = Stringext . ( to_list , of_list ) |
let to_list1 _ = assert_equal [ ' o ' ; ' c ' ; ' a ' ; ' m ' ; ' l ' ] ( to_list " ocaml " ) |
let to_list2 _ = assert_equal [ ] ( to_list " " ) |
let of_list1 _ = assert_equal " " ( of_list [ ] ) |
let of_list2 _ = assert_equal " ocaml " ( of_list [ ' o ' ; ' c ' ; ' a ' ; ' m ' ; ' l ' ] ) |
let s = " testing one two three " |
let opt_int = function | None -> " none " | Some x -> string_of_int x |
let find_from1 _ = let r = Stringext . find_from s ~ pattern " : ocaml " in assert_equal None r |
let find_from2 _ = let r = Stringext . find_from s ~ pattern " : testing " in assert_equal ( Some 0 ) r |
let find_from3 _ = let r = Stringext . find_from s ~ pattern " : one " in assert_equal ( Some 8 ) r |
let find_from4 _ = let r = Stringext . find_from s ~ pattern " : threee " in assert_equal None r |
let find_from5 _ = let r = Stringext . find_from s ~ pattern " : " in assert_equal ( Some 7 ) r |
let find_from6 _ = let pattern = " three " in let r = Stringext . find_from s ~ pattern in assert_equal ~ printer : opt_int ( Some ( String . length s - String . length pattern ) ) r |
let replace_all1 _ = let s = " the quick brown fox brown . " in let s ' = Stringext . replace_all s ~ pattern " : brown " ~ with_ " : blue " in assert_equal ~ printer ( : fun x -> x ) " the quick blue fox blue . " s ' |
let replace_all2 _ = let s = " one two three " in let s ' = Stringext . replace_all s ~ pattern " : " ~ with_ " : _ " in assert_equal ~ printer ( : fun x -> x ) " one_two_three " s ' |
let replace_all_assoc1 _ = let s = " hello from ocaml " in let tbl = [ ( " hello " , " goodbye " ) ; ( " ocaml " , " haskell " ) ] in let s ' = Stringext . replace_all_assoc s tbl in assert_equal ~ printer ( : fun x -> x ) " goodbye from haskell " s ' |
let replace_all_assoc2 _ = let s = " one two three " in let t = [ ( " one " , " four " ) ; ( " two " , " five " ) ; ( " three " , " six " ) ; ( " " , " _ " ) ] in let s ' = Stringext . replace_all_assoc s t in assert_equal ~ printer ( : fun x -> x ) " four_five_six " s ' |
let replace_all_assoc3 _ = let s = " one two three " in let t = [ ( " " , " _ " ) ] in let s ' = Stringext . replace_all_assoc s t in assert_equal ~ printer ( : fun x -> x ) " one_two_three " s ' |
let replace_all_assoc4 _ = let s = " onetwo " in let t = [ ( " one " , " xxxx " ) ; ( " two " , " yyy " ) ] in let s ' = Stringext . replace_all_assoc s t in assert_equal ~ printer ( : fun x -> x ) " xxxxyyy " s ' |
let of_array1 _ = let s = [ | ' a ' ; ' b ' ; ' c ' ] | in assert_equal " abc " ( Stringext . of_array s ) |
let trim_left_sub1 _ = let s = " testing " in assert_equal ~ printer ( : fun x -> x ) s ( Stringext . trim_left_sub s ~ pos : 0 ~ len ( : String . length s ) ~ chars " : " ) |
let trim_left_sub2 _ = let s = " , testing " in assert_equal ~ printer ( : fun x -> x ) " testing " ( Stringext . trim_left_sub s ~ pos : 0 ~ len ( : String . length s ) ~ chars " : , " ) |
let trim_left_sub3 _ = let s = " , testing " in assert_equal ~ printer ( : fun x -> x ) " test " ( Stringext . trim_left_sub s ~ pos : 0 ~ len ( : 7 ) ~ chars " : , " ) |
let trim_left_sub4 _ = let s = " a a " in assert_equal ~ printer ( : fun x -> x ) s ( Stringext . trim_left_sub s ~ pos : 0 ~ len : 3 ~ chars " : " ) |
let trim_left_sub5 _ = assert_equal ~ printer ( : fun x -> x ) " a " ( Stringext . trim_left_sub " a " ~ pos : 0 ~ len : 1 ~ chars " : " ) |
let trim_left1 _ = assert_equal ~ printer ( : fun x -> x ) " " ( Stringext . trim_left " " ) |
let trim_left2 _ = assert_equal ~ printer ( : fun x -> x ) " " ( Stringext . trim_left " " ) |
let test_fixtures = " test various string functions " >::: [ " test split char 1 " >:: test_split_1 ; " test split bounded 1 " >:: test_split_bounded_1 ; " test split none " >:: test_split_none ; " split trim left1 " >:: split_trim_left1 ; " split trim left2 " >:: split_trim_left2 ; " split trim left3 " >:: split_trim_... |
let _ = run_test_tt_main test_fixtures |
let char_of_string s = UChar . chr_of_uint ( int_of_string ( " 0x " ^ s ) ) |
let type_pat = Str . regexp [ " \\ t ] t ( [ *\\ EDBP ] EDBP ) ; " \\ |
let profile_pat = Str . regexp [ " \\ t ] t ( *\\ node \\| name \\| res \\| sasl \\| trace \\| iscsi \\| mib ) ; " \\ |
let val_pat = Str . regexp [ " \\ t ] t ( [ *\\ 0 - 9a - fA - F ] F ) " +\\ |
let sep_pat = Str . regexp [ " \\ t ] t ; " * |
type test = | E of ( UTF8 . t * UTF8 . t ) | D of ( UTF8 . t * UTF8 . t ) | B of UTF8 . t | P of UTF8 . t |
let text_of_uchars l = let b = UTF8 . Buf . create 0 in List . iter ( fun x -> UTF8 . Buf . add_char b x ) x l ; UTF8 . Buf . contents b |
let parse_single s p = let rec f p = if Str . string_match sep_pat s p then [ ] else if Str . string_match val_pat s p then let c = char_of_string ( Str . matched_group 1 s ) s in c ( :: f ( Str . match_end ( ) ) ) else failwith " parse error " in text_of_uchars ( f p ) p |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.