text
stringlengths
0
601k
let tag ( ? self_closing = false ) name attributes = { Token_tag . name ; attributes ; self_closing }
let expect ? state ( ? foreign = false ) text signals = let report , iterate , ended = expect_signals token_to_string text signals in let stream , set_state , set_foreign = text |> Markup__Stream_io . string |> Markup__Encoding . utf_8 |> Markup__Input . preprocess is_valid_html_char Error . ignore_errors |> Markup__Ht...
let char_sequence ( ? start = 1 ) ( ? no_eof = false ) s = let rec assemble acc index = if index >= String . length s then let acc = if no_eof then acc else ( 1 , index + start , S ` EOF ) :: acc in List . rev acc else assemble ( ( 1 , index + start , S ( ` Char ( Char . code s . [ index ] ) ) ) :: acc ) ( index + 1 ) ...
let tests = [ ( " html . tokenizer . empty " >:: fun _ -> expect " " [ 1 , 1 , S ` EOF ] ) ; ( " html . tokenizer . text " >:: fun _ -> expect " foo " ( char_sequence " foo " ) ; expect " f \ x00oo " ( [ 1 , 1 , S ( ` Char 0x66 ) ; 1 , 2 , E ( ` Bad_token ( " U + 0000 " , " content " , " null " ) ) ; 1 , 2 , S ( ` Char...
let expect id signals strings = let _ , iterate , ended = expect_strings id strings in signals |> Markup__Kstream . of_list |> Markup__Html_writer . write |> iter iterate ; ended ( )
let tests = [ ( " html . writer . empty " >:: fun _ -> expect " empty " [ ] [ ] ) ; ( " html . writer . text " >:: fun _ -> expect " text " [ ` Text [ " foo " ] ] [ S " foo " ] ; expect " adjacent text " [ ` Text [ " foo " ] ; ` Text [ " bar " ] ] [ S " foo " ; S " bar " ] ; expect " empty text " [ ` Text [ " " ] ] [ ]...
( module struct module Counter = struct type t = | Compare | Hash | Of_string | Sexp_of_t | T_of_sexp | To_string [ @@ deriving compare , hash , sexp ] end open Counter module Counts = struct module Map = Map . Make ( Counter ) type t = int Map . t ref [ @@ deriving sexp_of ] let actual = ref Map . empty let expected =...
let read_file f = Stdio . In_channel . with_file f ~ f : Stdio . In_channel . input_all
let partial_let = read_file " . . / passing / tests / partial . ml "
module Partial_ast = struct let tests_indent_range = let test name ~ input : source ~ range ~ expected = let test_name = " Partial_ast . indent_range : " ^ name in ( test_name , ` Quick , fun ( ) -> let range = Range . make ~ range source in let indent = Indent . Partial_ast . indent_range ~ source ~ range in let outpu...
let x = y in 2 } | ~ expected { :| let f = let x = y in 2 } | ; test " after in " ~ range ( : 1 , 12 ) ~ input : { | let f =
let x = let y = foooooooooooooooooooo in let x = fooooooooooooo in let k = fooooooo in foooooooooooooooooooooooooo foooooooooooooooooooo foooooooooooooooo fooooooooo in } | ~ expected : { | let f = let x = let y = foooooooooooooooooooo in let x = fooooooooooooo in let k = fooooooo in foooooooooooooooooooooooooo foooooo...
module Yaml = struct type t = | Scalar of string | List of t list | Record of ( string * t ) list let scalar str = Scalar str let list lst = List lst let record lst = Record lst module Pretty = Fmlib_pretty . Print let rec to_doc : t -> Pretty . doc = function | Scalar str -> Pretty . text str | List lst -> let open Pr...
module CP = Character . Make ( Unit ) ( Yaml ) ( Unit )
let comment : char t = let * _ = char ' ' # in let * _ = ( charp ( fun c -> c <> ' \ n ' ) " comment character " ) |> skip_zero_or_more in return ' ' #
let whitespace : int t = char ' ' </> char ' \ n ' </> comment |> skip_zero_or_more |> no_expectations |> detach
let lexeme ( p : ' a t ) : ' a t = let * a = p in let * _ = whitespace in return a
let raw_string : string t = let expect = " chars not containing colon and newline " in let inner c = c <> ' \ n ' && c <> ' ' : && c <> ' ' # in let first c = c <> ' " ' && inner c in ( word first inner expect ) |> map String . trim |> lexeme
let quoted_string : string t = let expect = " chars except newline and dquote " in let ok c = c <> ' \ n ' && c <> ' " ' in let * _ = char ' " ' in let * str = ( word ok ok expect ) </> return " " in let * _ = char ' " ' |> lexeme in return str
let scalar : Yaml . t t = quoted_string </> raw_string |> map Yaml . scalar <?> " scalar "
let dash : char t = char ' ' - |> lexeme
let key : string t = backtrack ( let * str = raw_string </> quoted_string in let * _ = char ' ' : |> lexeme in return str ) " < key " >:
let rec yaml ( ) : Yaml . t t = sequence_block ( ) </> record_block ( ) </> scalar one_or_more ( sequence_element ( ) <?> " sequence element : " \- < yaml value " " >\ |> align ) |> map ( fun ( a , lst ) -> Yaml . list ( a :: lst ) ) <?> " sequence of aligned " \- < yaml value " " >\ one_or_more ( record_element ( ) <?...
let parse : Parser . t = make ( ) ( let * _ = whitespace in yaml ( ) )
let write_errors ( source : string ) ( p : Parser . t ) : unit = let open Parser in let open Error_reporter in assert ( has_result p ) ; assert ( not ( has_succeeded p ) ) ; make_syntax p |> run_on_string source |> Pretty . layout 50 |> Pretty . write_to_channel stdout
let write_result ( source : string ) ( p : Parser . t ) : unit = let open Parser in if has_succeeded p then Yaml . ( to_json ( final p ) ) |> print_endline else if has_failed_syntax p then write_errors source p else assert false
let print_expectations ( p : Parser . t ) : unit = let open Printf in let open Parser in assert ( has_failed_syntax p ) ; Stdlib . List . iter ( fun ( str , ind ) -> match ind with | None -> printf " - % s \ n " str | Some ind -> match ind with | Indent . Indent i -> printf " - Indent % d : % s \ n " i str | Indent . A...
module type S = Indexed_container . S1 with type ' a t = ' a list
module This_list : S = struct include List include Indexed_container . Make ( struct type ' a t = ' a list let fold = List . fold let iter = ` Custom List . iter let length = ` Custom List . length let foldi = ` Define_using_fold let iteri = ` Define_using_fold end ) end
module That_list : S = List
let examples = [ [ ] ; [ 1 ] ; [ 2 ; 3 ] ; [ 4 ; 5 ; 1 ] ; List . init 8 ~ f ( : fun i -> i * i ) ]
module type Output = sig type t [ @@ deriving compare , sexp_of ] end
module Int_list = struct type t = int list [ @@ deriving compare , sexp_of ] end
module Int_pair_option = struct type t = ( int * int ) option [ @@ deriving compare , sexp_of ] end
module Int_option = struct type t = int option [ @@ deriving compare , sexp_of ] end
let check ( type a ) here examples ~ actual ~ expect ( module Output : Output with type t = a ) = List . iter examples ~ f ( : fun example -> let actual = actual example in let expect = expect example in require here ( Output . compare actual expect = 0 ) ~ if_false_then_print_s ( : lazy [ % message ( expect : Output ....
let bool_examples = [ [ ] ; [ true ] ; [ false ] ; [ false ; false ] ; [ true ; false ] ; [ false ; true ] ; [ true ; true ] ] ; ; let f _i elt = elt in check [ % here ] bool_examples ( module Bool ) ~ actual ( : fun list -> This_list . for_alli list ~ f ) ~ expect ( : fun list -> That_list . for_alli list ~ f ) ; [ % ...
let fail = Fail None
let error msg = Fail ( Some msg )
let test_infer = [ ]
let test_infer_and_syntax = [ ]
let normalize ty_str = string_of_s_ty ( Parser . ty_forall_eof Lexer . token ( Lexing . from_string ty_str ) )
let cmp_result result1 result2 = match ( result1 , result2 ) with
let make_single_test_case check_typed_syntax ( code , expected_result ) = try in try end
let extract_exact model = match exact_inference model with | Primitive xs -> let supp = match Primitive . support xs with | DiscreteFinite xs -> xs | _ -> raise Undefined in List . map supp ~ f ( : fun x -> ( x , ( Primitive . pdf xs ) x ) ) | _ -> [ ]
let test_exact_inference_grass ( ) = let grass_model ' = let * cloudy = bernoulli 0 . 5 in let * rain = bernoulli ( if cloudy then 0 . 8 else 0 . 2 ) in let * sprinkler = bernoulli ( if cloudy then 0 . 1 else 0 . 5 ) in let wet_grass = rain || sprinkler in condition wet_grass ( return rain ) in let p_rain_given_wet_gra...
let test_exact_inference_adding ( ) = let model = let * x = discrete_uniform [ 0 ; 1 ] in let * y = discrete_uniform [ 0 ; 1 ] in condition ( x = 1 ) ( return ( x + y ) ) in let x = extract_exact model in let x = List . sort x ~ compare ( : fun ( a , _ ) ( b , _ ) -> Int . compare a b ) in Alcotest . ( check ( list ( p...
let test_exact_inference_no_condition ( ) = let dist = bernoulli 0 . 5 in let model = let * x = dist in return x in let x = extract_exact model in let x = List . sort x ~ compare ( : fun ( a , _ ) ( b , _ ) -> Bool . compare a b ) in Alcotest . ( check ( list ( pair bool ( float 0 . 0000001 ) ) ) ) " check same dist no...
let infer_strats = [ MH 100 ; PC 100 ; Prior ; SMC 100 ; PIMH 100 ; Importance 100 ; Rejection ( 100 , Hard ) ; Rejection ( 100 , Soft ) ; Prior ; Enum ; Forward ; ]
let inf_with_desc = List . map infer_strats ~ f ( : fun x -> ( x , show_infer_strat x ) )
let inferred = sample @@ mh_sampler 100 Unit_test_models . single_coin
let inferred ' = sample @@ mh ' ' 100 Unit_test_models . single_coin
type model = F of float dist | B of bool dist
let models = Unit_test_models . [ ( B grass_model , " sprinkler " ) ; ( F single_coin , " coin " ) ]
let isf = function F _ -> true | _ -> false
let isb = function B _ -> true | _ -> false
let inference_tests = List . map models ~ f ( : fun ( model , model_desc ) -> List . map inf_with_desc ~ f ( : fun ( inf , desc ) -> ( desc ^ " , model " : ^ model_desc , ` Quick , fun ( ) -> if Poly . ( ( isf model && inf = Enum ) || ( isb model && inf = PC 100 ) ) then printf " " --- else match model with | B m -> Al...
let test_print ( ) = Alcotest . ( check pass ) " printing " ( List . map infer_strats ~ f ( : fun i -> ( print_infer_strat i , print_infer_strat_short i ) ) ) [ ]
let tests : unit Alcotest . test list = [ ( " exact inference " , [ ( " adding model " , ` Quick , test_exact_inference_adding ) ; ( " grass model " , ` Quick , test_exact_inference_grass ) ; ( " no conditioning " , ` Quick , test_exact_inference_no_condition ) ; ] ) ; ( " inference is sampleable " , [ ] @ inference_te...
print_endline ( to_string_hum ( of_exn ( Failure " foo " ) ) ) ; [ % expect { | ( Failure foo ) } ] | ; ; print_endline ( to_string_hum ( tag ( of_string " b " ) ~ tag " : a " ) ) ; [ % expect { | ( a b ) } ] | ; ; print_endline ( to_string_hum ( of_list ( List . map ~ f : of_string [ " a " ; " b " ; " c " ] ) ) ) ; [ ...
let of_strings strings = of_list ( List . map ~ f : of_string strings )
let nested = of_list ( List . map ~ f : of_strings [ [ " a " ; " b " ; " c " ] ; [ " d " ; " e " ; " f " ] ; [ " g " ; " h " ; " i " ] ] ) ; ; print_endline ( to_string_hum nested ) ; [ % expect { | ( a b c d e f g h i ) } ] | ; ; require_equal [ % here ] ( module Sexp ) ( sexp_of_t nested ) ( sexp_of_t ( of_strings [ ...
let round t = let sexp = sexp_of_t t in require [ % here ] ( Sexp . ( = ) sexp ( sexp_of_t ( t_of_sexp sexp ) ) ) ; ; tag_arg ( of_string " hello " ) " tag " 13 [ % sexp_of : int ] |> sexp_of_t |> print_s ; [ % expect { | ( tag 13 hello ) } ] | ; ; tag_arg ( of_string " hello " ) " " 13 [ % sexp_of : int ] |> sexp_of_t...
let test_s_create ctxt = let tmpdir = bracket_tmpdir ctxt in let inotify = Inotify . create ( ) in Unix . set_nonblock inotify ; let watch = Inotify . add_watch inotify tmpdir [ Inotify . S_Create ] in FileUtil . touch ( Printf . sprintf " % s / test " tmpdir ) ; assert_equal [ watch , [ Inotify . Create ] , 0l , Some ...
let test_s_create_blocking ctxt = let tmpdir = bracket_tmpdir ctxt in let inotify = Inotify . create ( ) in let watch = Inotify . add_watch inotify tmpdir [ Inotify . S_Create ] in ( ) |> Thread . create ( fun ( ) -> Unix . sleep 1 ; FileUtil . touch ( Printf . sprintf " % s / test " tmpdir ) ) |> ignore ; assert_equal...
let test_s_move ctxt = let tmpdir = bracket_tmpdir ctxt in let inotify = Inotify . create ( ) in Unix . set_nonblock inotify ; let watch = Inotify . add_watch inotify tmpdir [ Inotify . S_Move ] in FileUtil . touch ( Printf . sprintf " % s / foo " tmpdir ) ; FileUtil . mv ( Printf . sprintf " % s / foo " tmpdir ) ( Pri...
let test_error ctxt = let tmpdir = bracket_tmpdir ctxt in let inotify = Inotify . create ( ) in let tmpfile = Printf . sprintf " % s / nonexistent " tmpdir in assert_raises ( Unix . Unix_error ( Unix . ENOENT , " inotify_add_watch " , tmpfile ) ) ( fun ( ) -> Inotify . add_watch inotify tmpfile [ Inotify . S_Modify ] )
let test_nonblock ctxt = let tmpdir = bracket_tmpdir ctxt in let inotify = Inotify . create ( ) in Unix . set_nonblock inotify ; let _ = Inotify . add_watch inotify tmpdir [ Inotify . S_Create ] in assert_raises ( Unix . Unix_error ( Unix . EAGAIN , " read " , " " ) ) ( fun ( ) -> Inotify . read inotify )
let suite = " Test Inotify " >::: [ " Test S_Create watch " >:: test_s_create ; " Test blocking watch " >:: test_s_create_blocking ; " Test S_Move watch " >:: test_s_move ; " Test error handling " >:: test_error ; " Test nonblocking mode " >:: test_nonblock ; ]
let _ = run_test_tt_main suite
let ok = wrong_k " failed "
let tests = [ ( " input . xml " >:: fun _ -> expect_error ( 4 , 2 ) ( ` Bad_token ( " U + 0000 " , " input " , " out of range " ) ) begin fun report -> let s , get_location = string " fo \ no \ xc2 \ xa0ba \ rr \ xa0ba \ r \ nz \ x00quux " |> utf_8 |> preprocess is_valid_xml_char report in to_list s ok ( assert_equal [...
let examples = [ " C - c " ; " C - M - q " ; " < f5 " ; > " C -< f5 " ; > " C -< right " ; > " < mouse - 2 " ; > " C -< down - mouse - 3 " > ] ; ; List . iter examples ~ f ( : fun example -> print_s [ % message " " ~ _ : example ~ _ ( : create_exn example : t ) ] ) ; [ % expect { | ( C - c C - c ) ( C - M - q C - M - q...
let start_timer label = print_endline [ % string " start { -% label } " ]
let stop_timer label = print_endline [ % string " stop { -% label } " ]
let test_location_reference_point = ref [ % here ]
let test_start here = test_location_reference_point := here
let add_graph_info_nodes buffer graph_info = Map . iteri graph_info . Graph_info . info ~ f ( : fun ~ key ~ data -> let node_type = data . Graph_info . Node_info . node_type in let node_id = [ % string " " { \% Node_path . to_string key } " " ] \ in Buffer . add_string buffer [ % string " { % node_id } [ style = filled...
let graph_info_to_dot filename graph_info = let buffer = Buffer . create 1024 in Buffer . add_string buffer " digraph { \ n " ; add_graph_info_nodes buffer graph_info ; Map . iteri graph_info . Graph_info . dag ~ f ( : fun ~ key : from ~ data : to_ -> List . iter to_ ~ f ( : fun to_ -> let from = [ % string " " { \% No...
let print_graph_info ( graph_info : Graph_info . t ) = let with_line_and_column_string id = let id_string = Node_path . to_string id in match Map . find graph_info . info id with | Some { node_type ; here } -> ( match here with | Some { Source_code_position . pos_lnum ; pos_cnum ; pos_bol ; pos_fname } -> let pos_lnum ...
let write_computation_to_dot filename component = let graph_info = ref Graph_info . empty in let ( _ : _ Private . Computation . packed ) = Graph_info . iter_graph_updates_packed ( Bonsai . Private . reveal_computation component ) ~ on_update ( : fun gm -> graph_info := gm ) in graph_info_to_dot filename ! graph_info ;...
let instrument_computation component = let graph_info = ref Graph_info . empty in let print_graph_info_on_update = ref false in let c = Graph_info . iter_graph_updates_packed ( Bonsai . Private . reveal_computation component ) ~ on_update ( : fun gm -> graph_info := gm ; if ! print_graph_info_on_update then print_graph...
let many_aliases ( ) = test_start [ % here ] ; let % sub a = Bonsai . const 1 in let % sub a = return a in let % sub a = return a in let % sub _ = Bonsai . const 1 in let % sub a = return a in let % sub a = return a in let % sub a = return a in return a ; ; let ( _ : int Computation . t ) = instrument_computation ( man...
let subst_tree ( ) = test_start [ % here ] ; let % sub a = Bonsai . const 1 in let % sub b = Bonsai . const 2 in let % sub c = Bonsai . const 3 in let % sub d = Bonsai . const 4 in let % sub e = Bonsai . const 5 in let % arr a = a and b = b and c = c and d = d and e = e in a + b + c + d + e ; ; let c = instrument_compu...
let diamond ( ) = test_start [ % here ] ; let % sub a = Bonsai . const 0 in let % sub b = Bonsai . pure Fn . id a in let % sub c = Bonsai . pure Fn . id a in let % arr b = b and c = c in b + c ; ; let ( _ : int Computation . t ) = instrument_computation ( diamond ( ) ) in [ % expect { | tree : 1_1 return -> _1 subst_st...
let state ( ) = test_start [ % here ] ; let % sub state = Bonsai . state [ % here ] ( module Int ) ~ default_model : 0 in return state ; ; let c = instrument_computation ( state ( ) ) in [ % expect { | tree : 1_1 leaf0 -> _1 subst_stateless_into @ 1 : 2 2_1 return -> _1 subst_stateless_into @ 1 : 2 2_2 named -> 2_1 ret...
let dynamic_state ( ) = test_start [ % here ] ; let % sub state = Bonsai . state_machine1 [ % here ] ( module Int ) ( module Unit ) ~ default_model : 0 ~ apply_action ( : fun ~ inject : _ ~ schedule_event : _ ( ) model ( ) -> model + 1 ) ( Value . return ( ) ) in return state ; ; let c = instrument_computation ( dynami...
let dynamic_scope ( ) = test_start [ % here ] ; let dynamic_var = Bonsai . Dynamic_scope . create ~ name " : a " ~ fallback : 0 ( ) in Bonsai . Dynamic_scope . set dynamic_var ( Bonsai . Value . return 10 ) ~ inside : ( let % sub a = Bonsai . Dynamic_scope . lookup dynamic_var in let % arr a = a in a ) ; ; let ( _ : in...
let cutoff ( ? var = Bonsai . Var . create 0 ) ( ) = test_start [ % here ] ; let value = Bonsai . Var . value var in return ( Value . cutoff value ~ equal ( : fun _ _ -> false ) ) ; ; let var = Bonsai . Var . create 0 in let c = instrument_computation ( cutoff ~ var ( ) ) in [ % expect { | tree : 1_1 incr -> _2 cutoff ...
let assoc_simpl ( ) = test_start [ % here ] ; Bonsai . assoc ( module Int ) ( Value . return ( Int . Map . of_alist_exn [ - 1 , ( ) ; 1 , ( ) ] ) ) ~ f ( : fun i _ -> return i ) ; ; let c = instrument_computation ( assoc_simpl ( ) ) in [ % expect { | tree : _1 assoc_simpl -> _0 _2 constant -> _1 assoc_simpl dag : _1 as...
let assoc ( ) = test_start [ % here ] ; let % sub a = Bonsai . const 0 in Bonsai . assoc ( module Int ) ( Value . return ( Int . Map . of_alist_exn [ - 1 , ( ) ; 1 , ( ) ] ) ) ~ f ( : fun _ _ -> let % arr a = a in a ) ; ; let c = instrument_computation ( assoc ( ) ) in [ % expect { | tree : 1_1 return -> _1 subst_state...
let nested_values ( ? a = Value . return 0 ) ( ) = test_start [ % here ] ; return ( let % map a = let % map a = let % map a = a in a in a in a ) ; ; let a_var = Bonsai . Var . create 0 in let a = Bonsai . Var . value a_var in let c = instrument_computation ( nested_values ~ a ( ) ) in [ % expect { | tree : 1 - 1 - 1_1 ...
let enum ( ? a = Value . return 0 ) ( ? match_ = Value . return true ) ( ) = test_start [ % here ] ; Bonsai . enum ( module Bool ) ~ match_ ~ with_ ( : function | true -> let % arr a = a in a > 0 | false -> return match_ ) ; ; let match_var = Bonsai . Var . create true in let match_ = Bonsai . Var . value match_var in ...
let lazy_computation ( ? match_ = Value . return true ) ( ) = test_start [ % here ] ; let a = Bonsai . Value . return 0 in Bonsai . enum ( module Bool ) ~ match_ ~ with_ ( : function | true -> return a | false -> Bonsai . lazy_ ( lazy ( return a ) ) ) ; ; let match_var = Bonsai . Var . create true in let match_ = Bonsa...
let shared = let % sub ( ) = Bonsai . const ( ) in Bonsai . const ( ) ; ;
let name_used_twice ( ) = test_start [ % here ] ; let % sub ( ) = shared in let % sub ( ) = shared in Bonsai . const ( ) ; ; let ( _ : unit Computation . t ) = instrument_computation ( name_used_twice ( ) ) in [ % expect { | tree : 1 - 1_1 return -> 1_1 subst_stateless_from @ - 5 : 2 1 - 1_2 constant -> 1 - 1_1 return ...
type packed = T : ( unit -> ' a Computation . t ) -> packed
let command = Command . async ~ summary " : Run tests for bonsai instrumentation " ( Command . Param . return ( fun ( ) -> Writer . with_file " tests . md " ~ f ( : fun writer -> Writer . write_line writer " " ; --- Writer . write_line writer " title : Bonsai instrumentation tests DOT output " ; Writer . write_line wri...
let eval_int_var string = Form . Blocking . eval ( string |> Symbol . intern |> Form . symbol )
let most_negative_fixnum_value = eval_int_var " most - negative - fixnum "