text
stringlengths
12
786k
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 ( ...
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 . ...
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 ...
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...
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 ; w...
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 ( :...
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_l...
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
let parse_couple s p = let s1 = parse_single s p in let p = Str . match_end ( ) in let s2 = parse_single s p in s1 , s2
let parse_type s p = if Str . string_match type_pat s p then ( match Str . matched_group 1 s with | " E " -> E ( parse_couple s ( Str . match_end ( ) ) ) | " D " -> D ( parse_couple s ( Str . match_end ( ) ) ) | " B " -> B ( parse_single s ( Str . match_end ( ...
let profile_of_string = function | " node " -> ` Nodeprep | " name " -> ` Nameprep | " res " -> ` Resourceprep | " sasl " -> ` Saslprep | " trace " -> ` Trace | " iscsi " -> ` Iscsi | " mib " -> ` Mib | _ -> failwith " profile parse error "
let parse_line s = if Str . string_match profile_pat s 0 then Some ( let profile = profile_of_string ( Str . matched_group 1 s ) s in let p = Str . match_end ( ) in let t = parse_type s p in profile , t ) else None
let check_line n s = match parse_line s with | None -> 0 | Some ( profile , t ) t -> ( match t with | E ( s1 , s2 ) s2 -> expect_equal ~ msg ( : lazy ( " line : " ^ ( string_of_int n ) n ) n ) n ( stringprep profile s1 ) s1 ( stringprep profile s2 ) s2 | D ( s1 , s...
let check_file f = let c = open_in_bin f in let rec check line n = try raise ( Ok ( input_line c ) c ) c with | End_of_file -> n | Ok s -> let n = n + ( check_line line s ) s in check ( line + 1 ) 1 n in let _ = check 1 0 in close_in c
let testfile = input_filename " data / stringprep "
let _ = test ~ desc " : stringprep " ~ body ( : fun ( ) -> expect_pass ~ body ( : fun ( ) -> check_file testfile ) )
let x = Sys . opaque_identity " one string " in let y = Sys . opaque_identity " another " in require_no_allocation [ % here ] ( fun ( ) -> ignore ( Sys . opaque_identity ( String . Caseless . equal x y ) : bool ) ) ; [ % expect { } ] || ; ; let string = String...
module Unit_tests_util = struct let to_bin_str t ~ bin_writer_t ~ bin_size_t = let write = bin_writer_t . Bin_prot . Type_class . write in let size = bin_size_t t in let buf = Bigstring . create size in let len = write buf ~ pos : 0 t in assert ( len = size ) ; Bigstring . to_string buf ; ...
module Even_int_id = Make_with_validate_without_pretty_printer ( struct let module_name = " Even_int_id " let validate s = if Int . of_string s % 2 <> 0 then Or_error . error_s [ % message " Not a valid Even_int_id " ~ _ ( : s : string ) ] else Ok ( ) ; ; end ) ( )
let even_int_id_to_bin_str t = let bin_writer_t = Even_int_id . bin_writer_t in let bin_size_t = Even_int_id . bin_size_t in Unit_tests_util . to_bin_str t ~ bin_writer_t ~ bin_size_t ; ;
let even_int_id_of_bin_str str = Even_int_id . bin_reader_t . Bin_prot . Type_class . read ( Bigstring . of_string str ) ~ pos_ref ( : ref 0 ) ; ; [ % test_result : string ] ( Even_int_id . of_string " 14 " |> Even_int_id . to_string ) ~ expect " : 14 " ; ; [ ...
let print_error or_error = match or_error with | Ok _ -> printf " < OK " > | Error error -> Error . to_string_hum error |> printf " % s " ; ; Or_error . try_with ( fun ( ) -> Even_int_id . of_string " 15 " ) |> print_error ; [ % expect { | ( Invalid_argument " ( ...
module M = Make_without_pretty_printer ( struct let module_name = " test " end ) ( ) [ % test_result : string ] ( M . of_string " FOOBAR " |> M . to_string ) ~ expect " : FOOBAR " ; ; [ % test_result : string ] ( M . t_of_sexp ( Sexp . of_string " FOOBAR " ) ...
let string_id_to_bin_str t = let bin_writer_t = M . bin_writer_t in let bin_size_t = M . bin_size_t in Unit_tests_util . to_bin_str t ~ bin_writer_t ~ bin_size_t ; ;
let string_id_of_bin_str str = M . bin_reader_t . Bin_prot . Type_class . read ( Bigstring . of_string str ) ~ pos_ref ( : ref 0 ) ; ; [ % test_result : string ] ( M . of_string " FOOBAR " |> string_id_to_bin_str |> string_id_of_bin_str |> M . to_string ) ~ expect " : ...
let print_error or_error = match or_error with | Ok _ -> printf " < OK " > | Error error -> Error . to_string_hum error |> printf " % s " ; ; Or_error . try_with ( fun ( ) -> M . of_string " " ) |> print_error ; [ % expect { | ( Invalid_argument " ' ' is not a v...
module Record = struct [ %% h5struct sf64 " sf64 " Float64 Seek ; si " si " Int Seek ; si64 " si64 " Int64 Seek ; ss " ss " ( String 14 ) Seek ; f64 " f64 " Float64 ; i " i " Int ; i64 " i64 " Int64 ; s " s " ( String 16 ) ] end
module Subset = struct [ %% h5struct f64 " f64 " Float64 ; ] end
module Simple = struct [ %% h5struct f " F " Float64 Seek ] end
let ( ) = let len = 1000 in let a = Record . Array . init len ( fun i e -> let si = i * 2 in Record . set e ~ sf64 ( : float_of_int si ) ~ si ~ si64 ( : Int64 . of_int si ) ~ ss ( : Printf . sprintf " % 14d " si ) ~ f64 ( : float_of_int i ) ~ i ~ i64 ( : Int64...
module Big = struct [ %% h5struct id " ID " Int ; big " Big " Bigstring ; f32 " F32 " Array_float32 ; f64 " F64 " Array_float64 ; si8 " SI8 " Array_sint8 ; ui8 " UI8 " Array_uint8 ; si16 " SI16 " Array_sint16 ; ui16 " UI16 " Array_uint16 ; i32 " I32 " Array_int32 ...
let create_array len = Big . Array . init len ( fun i b -> let f32 = Array1 . create float32 c_layout i in for j = 0 to i - 1 do f32 . { j } <- float j done ; let f64 = Array1 . create float64 c_layout i in for j = 0 to i - 1 do f64 . { j } <- float j done ; let si8 = Array1 . ...
let ( ) = let check a = Big . Array . iteri a ~ f ( : fun i a -> Array . iter ( fun ( Big . Accessors . T acc ) -> match acc . field . type_ with | Int -> assert ( acc . get a = i ) | Int64 -> assert false | Float64 -> assert false | String _ -> assert false | Bigstring ...
let stress_test_bigarray num_arrays num_elements = let create_array ( ) = Big . Array . get ( create_array ( 1 + Random . int num_arrays ) ) 0 in let a = Array . init num_arrays ( fun _ -> create_array ( ) ) in let create_element ( ) = let a = a . ( Random . int num_array...
let ( ) = stress_test_bigarray 128 128
let ( ) = let s = Array . init 128 ( fun i -> String . init i ( fun i -> Char . chr ( i + 1 ) ) ) in let b = Array . map Bigstring . of_string s in Gc . full_major ( ) ; Array . iteri ( fun i b -> assert ( Bigstring . to_string b = s . ( i ) ) ) b
module Bigchar = struct [ %% h5struct id " ID " Int ; s " S " ( String 1 ) ; a " A " Bigstring ; b " B " Array_char ; ] end
let ( ) = let len = 1024 in let aa = Array . init len ( fun i -> String . init i ( fun i -> let i = i land 0xff in Char . chr ( if i = 0 then 1 else i ) ) ) in let ab = Array . init len ( fun i -> String . init i ( fun i -> Char . chr ( i land 0xff ) ) ) in let v ...
module Foo = struct [ %% h5struct a " A " Int ( Default 0 ) ; b " B " Float64 ; ] end
module Bar = struct [ %% h5struct b " B " Float64 ; d " D " ( String 16 ) ( Default " NONE " ) ; a " A " Int ; c " C " Int ( Default 1 ) ; ] end
module Ext = struct type t = private int end
module Mem = struct module T = struct type t = { refcount : int ; data : Ext . t ; capacity : int ; mutable nmemb : int ; size : int ; nfields : int ; } end type t = { ops : Ext . t ; t : T . t ; } let data t : Hdf5_raw . H5tb . Data . t = Obj . magic t . t . data...
let ( ) = let len = 16 in let h5 = H5 . create_trunc " test . h5 " in let a = Foo . Array . init len ( fun i -> Foo . set ~ a : i ~ b ( : float i . * 0 . 25 ) ) in Foo . Array . make_table a h5 " a " ; H5 . close h5 ; let h5 = H5 . open_rdonly " test . h5 ...
module Short = struct [ %% h5struct a " A " ( String 16 ) ; ] end
module Shorter = struct [ %% h5struct a " A " ( String 8 ) ; ] end