text
stringlengths 0
601k
|
---|
let find t c = let rec aux = function | T . Leaf -> raise Not_found | T . Node ( _ , l , cell , r , _ ) -> let o = compare c cell . cursor in if o = 0 then cell . value else if o < 0 then aux l else aux r in aux t . tree |
let position t c0 = let rec traverse n = function | T . Leaf -> raise Not_found | T . Node ( _ , l , cell , r , _ ) -> let o = compare c0 cell . cursor in if o < 0 then traverse n l else let n = n + T . measure l + cell . offset in if o > 0 then traverse n r else n in traverse 0 t . tree |
let rec shift_tree n = function | T . Leaf -> T . leaf | T . Node ( _ , T . Leaf , cell , r , _ ) -> T . node T . leaf ( shift_cell n cell ) r | T . Node ( _ , l , cell , r , _ ) -> T . node ( shift_tree n l ) cell r |
let shift_tree n tree = if n = 0 then tree else shift_tree n tree |
let rem_cursor t c0 = let rec traverse = function | T . Leaf -> raise Not_found | T . Node ( _ , l , cell , r , _ ) -> let o = compare c0 cell . cursor in if o < 0 then let l , shift = traverse l in T . node l ( shift_cell shift cell ) r , 0 else if o > 0 then let r , shift = traverse r in T . node l cell r , shift else if is_leaf r then l , cell . offset else T . join l ( shift_tree cell . offset r ) , 0 in fst ( update t traverse ) |
let put_cursor t ~ at value = if at < 0 then invalid_arg " Trope . put_cursor : [ at ] must be >= 0 " ; let rec traverse before at = function | T . Leaf -> let cursor = O . after before in let cell = make_cell at cursor value in T . node T . leaf cell T . leaf , cursor | T . Node ( _ , l , cell , r , _ ) -> let pos = T . measure l + cell . offset in if at < pos then let l , cursor = traverse before at l in T . node l ( make_cell ( pos - T . measure l ) cell . cursor cell . value ) r , cursor else let r , cursor = traverse cell . cursor ( at - pos ) r in T . node l cell r , cursor in update t ( traverse t . root at ) |
let insert ? left_of t ~ at ~ len = if at < 0 then invalid_arg " Trope . insert : [ at ] must be >= 0 " ; if len < 0 then invalid_arg " Trope . insert : [ len ] must be >= 0 " ; let right = ( left_of : unit option ) = None in let rec aux n = function | T . Leaf -> T . leaf , len | T . Node ( _ , l , cell , r , _ ) -> let n0 = T . measure l + cell . offset in if ( if right then n < n0 else n <= n0 ) then let l , shift = aux n l in T . node l ( shift_cell shift cell ) r , 0 else let r , shift = aux ( n - n0 ) r in T . node l cell r , shift in fst ( update t ( aux at ) ) |
let remove ? left_of t ~ at ~ len = if at < 0 then invalid_arg " Trope . remove : [ at ] must be >= 0 " ; if len < 0 then invalid_arg " Trope . remove : [ len ] must be >= 0 " ; if len = 0 then t else let rec rem len = function | T . Leaf -> T . leaf | T . Node ( _ , l , cell , r , _ ) -> let n0 = T . measure l + cell . offset in if len <= n0 then let l = rem len l in let cell = make_cell ( n0 - len - T . measure l ) cell . cursor cell . value in T . node l cell r else let len = len - n0 in let r = rem len r in r in let right = ( left_of : unit option ) = None in let rec aux n len = function | T . Leaf -> T . leaf | T . Node ( _ , l , cell , r , _ ) -> let n0 = T . measure l + cell . offset in if n + len <= n0 then let l = aux n len l in let cell = make_cell ( n0 - len - T . measure l ) cell . cursor cell . value in T . node l cell r else if ( if right then n >= n0 else n > n0 ) then T . node l cell ( aux ( n - n0 ) len r ) else let l = aux n len l in let len = len - ( n0 - n ) in let r = shift_tree ( n - T . measure l ) ( rem len r ) in T . join l r in update ' t ( aux at len ) |
let cursor_at_origin t = O . after t . root |
let remove_between t c1 c2 = validate t c1 " Trope . remove_between : cursor not in buffer " ; validate t c2 " Trope . remove_between : cursor not in buffer " ; if c1 == c2 then t else if compare c1 c2 > 0 then invalid_arg " Trope . remove_between : cursors must be in increaing order " else begin let rec cut_left = function | T . Leaf -> invalid_arg " Trope . remove_between : cursor not in buffer " | T . Node ( _ , l , cell , r , _ ) -> let c1 = compare c1 cell . cursor in if c1 < 0 then cut_left l else if c1 > 0 then T . node l cell ( cut_left r ) else T . node l cell T . leaf in let rec cut_right = function | T . Leaf -> invalid_arg " Trope . remove_between : cursor not in buffer " | T . Node ( _ , l , cell , r , _ ) -> let c2 = compare c2 cell . cursor in if c2 > 0 then cut_right r else if c2 < 0 then T . node ( cut_right l ) cell r else T . node T . leaf ( make_cell 0 cell . cursor cell . value ) r in let rec aux = function | T . Leaf -> invalid_arg " Trope . remove_between : cursor not in buffer " | T . Node ( _ , l , cell , r , _ ) -> let c1 = compare c1 cell . cursor and c2 = compare c2 cell . cursor in if c2 < 0 then T . node ( aux l ) cell r else if c1 > 0 then T . node l cell ( aux r ) else if c1 = 0 then T . node l cell ( cut_right r ) else if c2 = 0 then T . node ( cut_left l ) ( make_cell 0 cell . cursor cell . value ) r else T . join ( cut_left l ) ( cut_right r ) in update ' t aux end |
let remove_after t c len = validate t c " Trope . remove_after : cursor not in buffer " ; if len < 0 then invalid_arg " Trope . remove_after : len must be >= 0 " else if len = 0 then t else let rec rem n = function | T . Leaf -> T . leaf , n | T . Node ( _ , l , cell , r , _ ) -> let n0 = T . measure l in let n1 = n0 + cell . offset in if n <= n0 then let l , shift = rem n l in T . node l ( shift_cell ( - shift ) cell ) r , 0 else if n <= n1 then T . node T . leaf ( make_cell ( n1 - n ) cell . cursor cell . value ) r , 0 else rem ( n - n1 ) r in let rec seek = function | T . Leaf -> invalid_arg " Trope . remove_after : cursor not in buffer " | T . Node ( _ , l , cell , r , _ ) -> let c = compare c cell . cursor in if c < 0 then let l , shift = seek l in if shift > cell . offset then let r , shift = rem ( shift - cell . offset ) r in T . join l r , shift else T . node l ( shift_cell ( - shift ) cell ) r , 0 else let r , shift = if c = 0 then rem len r else seek r in T . node l cell r , shift in fst ( update t seek ) |
let remove_before t c len = validate t c " Trope . remove_before : cursor not in buffer " ; if len < 0 then invalid_arg " Trope . remove_before : len must be >= 0 " else if len = 0 then t else let rec rem n = function | T . Leaf -> T . leaf | T . Node ( _ , l , cell , r , _ ) -> let n0 = T . measure r in if n <= n0 then let r = rem n r in T . node l cell r else let n = n - n0 - cell . offset in if n > 0 then rem n l else l in let rec seek = function | T . Leaf -> invalid_arg " Trope . remove_before : cursor not in buffer " | T . Node ( _ , l , cell , r , _ ) -> let c = compare c cell . cursor in if c < 0 then T . node ( seek l ) cell r else if c = 0 then if len <= cell . offset then let cell = shift_cell ( - len ) cell in T . node l cell r else let len = len - cell . offset in let n0 = T . measure l in if len <= n0 then let l = rem len l in T . node l ( make_cell ( n0 - T . measure l - len ) cell . cursor cell . value ) r else T . node T . leaf ( make_cell 0 cell . cursor cell . value ) r else let n0 = T . measure r in let r = seek r in let len = len - ( n0 - T . measure r ) in assert ( len >= 0 ) ; if len = 0 then T . node l cell r else if len <= cell . offset then T . join l ( shift_tree ( cell . offset - len ) r ) else let len = len - cell . offset in let n0 = T . measure l in if len <= n0 then let l = rem len l in T . join l ( shift_tree ( n0 - T . measure l - len ) r ) else r in update ' t seek |
let insert_before t c len = validate t c " Trope . insert_before : cursor not in buffer " ; if len < 0 then invalid_arg " Trope . insert_before : len must be >= 0 " else if len = 0 then t else let rec aux = function | T . Leaf -> invalid_arg " Trope . insert_before : cursor not in buffer " | T . Node ( _ , l , cell , r , _ ) -> let c = compare c cell . cursor in if c < 0 then T . node ( aux l ) cell r else if c > 0 then T . node l cell ( aux r ) else T . node l ( shift_cell len cell ) r in update ' t aux |
let insert_after t c len = validate t c " Trope . insert_after : cursor not in buffer " ; if len < 0 then invalid_arg " Trope . insert_after : len must be >= 0 " else if len = 0 then t else let rec aux = function | T . Leaf -> invalid_arg " Trope . insert_after : cursor not in buffer " | T . Node ( _ , l , cell , r , _ ) as tree -> let c = compare c cell . cursor in if c < 0 then let l , shift = aux l in T . node l ( shift_cell shift cell ) r , 0 else if c > 0 then let r , shift = aux r in T . node l cell r , shift else if is_leaf r then tree , len else T . node l cell ( shift_tree len r ) , 0 in fst ( update t aux ) |
let _put_before t c0 value = validate t c0 " Trope . put_before : cursor not in buffer " ; let aux t = let c = O . before c0 in let rec aux = function | T . Leaf -> invalid_arg " Trope . put_before : cursor not in buffer " | T . Node ( _ , l , cell , r , _ ) -> let i = compare c0 cell . cursor in if i = 0 then T . node ( T . node l ( make_cell cell . offset c value ) T . leaf ) ( make_cell 0 cell . cursor cell . value ) r else if i < 0 then T . node ( aux l ) cell r else T . node l cell ( aux r ) in aux t , c in update t aux |
let _put_after t c0 value = validate t c0 " Trope . put_after : cursor not in buffer " ; let aux t = let c = O . after c0 in let rec aux = function | T . Leaf -> invalid_arg " Trope . put_after : cursor not in buffer " | T . Node ( _ , l , cell , r , _ ) -> let i = compare c0 cell . cursor in if i = 0 then T . node ( T . node l cell T . leaf ) ( make_cell 0 c value ) r else if i < 0 then T . node ( aux l ) cell r else T . node l cell ( aux r ) in aux t , c in update t aux |
let put_left t c v = validate t c " Trope . put_left : cursor from different buffer " ; let rec aux = function | T . Leaf -> T . node T . leaf ( make_cell 0 c v ) T . leaf | T . Node ( _ , l , cell , r , _ ) -> let i = compare c cell . cursor in if i = 0 then T . node l ( make_cell cell . offset c v ) r else if i < 0 then T . node ( aux l ) cell r else T . node l cell ( aux r ) in try update ' t aux with Exit -> t |
let put_right t c v = validate t c " Trope . put_right : cursor from different buffer " ; let rec aux pad = function | T . Leaf -> T . node T . leaf ( make_cell pad c v ) T . leaf , 0 | T . Node ( _ , l , cell , r , _ ) -> let i = compare c cell . cursor in if i = 0 then T . node l ( make_cell cell . offset c v ) r , pad else if i < 0 then ( let l ' , pad ' = aux cell . offset l in T . node l ' ( make_cell pad ' cell . cursor cell . value ) r , pad ) else let r ' , pad ' = aux pad r in T . node l cell r ' , pad ' in try fst ( update t ( aux 0 ) ) with Exit -> t |
let find_before t n = let rec aux n = function | T . Leaf -> None | T . Node ( _ , l , cell , r , _ ) -> let n0 = T . measure l + cell . offset in if n < n0 then aux n l else match aux ( n - n0 ) r with | Some _ as result -> result | None -> Some ( cell . cursor , cell . value ) in aux n t . tree |
let find_after t n = let rec aux n = function | T . Leaf -> None | T . Node ( _ , l , cell , r , _ ) -> let n0 = T . measure l in if n <= n0 && not ( is_leaf l ) then aux n l else let n1 = n0 + cell . offset in if n <= n1 then Some ( cell . cursor , cell . value ) else aux ( n - n1 ) r in aux n t . tree |
let seek_before t c = validate t c " Trope . seek_before : cursor not in buffer " ; let rec aux = function | T . Leaf -> None | T . Node ( _ , l , cell , r , _ ) -> let c = compare c cell . cursor in if c <= 0 then aux l else match aux r with | Some _ as result -> result | None -> Some ( cell . cursor , cell . value ) in aux t . tree |
let seek_after t c = validate t c " Trope . seek_after : cursor not in buffer " ; let rec aux = function | T . Leaf -> None | T . Node ( _ , l , cell , r , _ ) -> let c = compare c cell . cursor in if c >= 0 then aux r else match aux l with | Some _ as result -> result | None -> Some ( cell . cursor , cell . value ) in aux t . tree |
let to_list t = let rec aux acc n = function | T . Leaf -> acc | T . Node ( _ , l , cell , r , _ ) -> let n ' = n + T . measure l + cell . offset in let acc = aux acc n ' r in aux ( ( n ' , cell . cursor , cell . value ) :: acc ) n l in aux [ ] 0 t . tree |
module Actions = struct type action = | Incoming_connection_error | Outgoing_connection_error | Gossiped_old_transition of int64 * int | Gossiped_future_transition | Gossiped_invalid_transition | Disconnected_chain | Sent_bad_hash | Sent_invalid_signature | Sent_invalid_transaction | Sent_invalid_proof | Sent_invalid_signature_or_proof | Sent_invalid_protocol_version | Sent_mismatched_protocol_version | Has_invalid_genesis_protocol_state | Sent_invalid_transition_chain_merkle_proof | Violated_protocol | Made_request | Connected | Requested_unknown_item | Fulfilled_request | Epoch_ledger_provided | Sent_useful_gossip | Sent_useless_gossip | Sent_old_gossip | No_reply_from_preferred_peer | Unknown_rpc | Decoding_failed [ @@ deriving show ] show type t = action * ( string * ( string , Yojson . Safe . t ) t List . Assoc . t ) t option let to_trust_response ( action , _ ) _ = let open Peer_trust . Trust_response in let fulfilled_increment = Peer_trust . max_rate 10 . in let request_increment = 0 . 90 . * fulfilled_increment in let connected_increment = 0 . 10 . * fulfilled_increment in let epoch_ledger_provided_increment = 10 . . * fulfilled_increment in let old_gossip_increment = Peer_trust . max_rate 20 . in match action with | Gossiped_old_transition ( slot_diff , delta ) delta -> let c = 0 . 1 in let y = ( Float . of_int delta ** 2 . 0 ) 0 . / 2 . 0 in let f x = ( 1 . 0 . / y . * ( x ** 2 . 0 ) 0 ) 0 . + c in Trust_decrease ( f ( Int64 . to_float slot_diff ) slot_diff ) slot_diff | Gossiped_future_transition -> Insta_ban | Gossiped_invalid_transition -> Insta_ban | Disconnected_chain -> Insta_ban | Sent_bad_hash -> Insta_ban | Sent_invalid_signature -> Insta_ban | Sent_invalid_transaction -> Insta_ban | Sent_invalid_proof -> Insta_ban | Sent_invalid_signature_or_proof -> Insta_ban | Sent_invalid_protocol_version -> Insta_ban | Sent_mismatched_protocol_version -> Trust_decrease 0 . 25 | Has_invalid_genesis_protocol_state -> Insta_ban | Sent_invalid_transition_chain_merkle_proof -> Insta_ban | Incoming_connection_error -> Trust_decrease 0 . 05 | Outgoing_connection_error -> Trust_decrease 0 . 05 | No_reply_from_preferred_peer -> Trust_decrease 0 . 05 | Violated_protocol -> Insta_ban | Made_request -> Trust_decrease request_increment | Connected -> Trust_decrease connected_increment | Requested_unknown_item -> Trust_decrease ( Peer_trust . max_rate 1 ) . | Fulfilled_request -> Trust_increase fulfilled_increment | Epoch_ledger_provided -> Trust_increase epoch_ledger_provided_increment | Sent_useful_gossip -> Trust_increase ( old_gossip_increment . * 10 ) . | Sent_useless_gossip -> Trust_decrease ( old_gossip_increment . * 3 ) . | Sent_old_gossip -> Trust_decrease old_gossip_increment | Unknown_rpc -> Trust_decrease 0 . 05 | Decoding_failed -> Insta_ban let to_trust_response t = match to_trust_response t with | Insta_ban -> Peer_trust . Trust_response . Insta_ban | _ -> Trust_decrease 0 . let to_log : t -> string * ( string , Yojson . Safe . t ) t List . Assoc . t = fun ( action , extra_opt ) extra_opt -> match extra_opt with | None -> ( show_action action , [ ] ) | Some ( fmt , metadata ) metadata -> ( sprintf " !% s ( % s ) s " ( show_action action ) action fmt , metadata ) metadata end |
module Peer_trust = Peer_trust . Make ( Actions ) Actions |
let record_envelope_sender : t -> Logger . t -> Network_peer . Envelope . Sender . t -> Actions . t -> unit Deferred . t = fun t logger sender action -> match sender with | Local -> let action_fmt , action_metadata = Actions . to_log action in [ % log debug ] debug ~ metadata ( ( " : action " , ` String action_fmt ) action_fmt :: action_metadata ) action_metadata " Attempted to record trust action of ourselves : $ action " ; Deferred . unit | Remote peer -> record t logger peer action |
let subject a = Alcotest . testable ( pp ( Alcotest . pp a ) ) ( equal ( Alcotest . equal a ) ) ; ; |
let validate_name ( name , age ) = if String . length name > 1 then Ok ( name , age ) else Error ( Invalid_name name ) ; ; |
let validate_age ( name , age ) = if age >= 18 then Ok ( name , age ) else Error ( Invalid_age age ) ; ; |
let simple_validation_success ( ) = let open Monad . Infix in let computed = Monad . return ( " Xavier " , 30 ) >>= validate_name >>= validate_age and expected = Ok ( " Xavier " , 30 ) in Alcotest . ( check ( subject ( pair string int ) ) ) " should be valid " expected computed ; ; |
let simple_validation_failure_1 ( ) = let open Monad . Infix in let computed = Monad . return ( " X " , 30 ) >>= validate_name >>= validate_age and expected = Error ( Invalid_name " X " ) in Alcotest . ( check ( subject ( pair string int ) ) ) " should be invalid " expected computed ; ; |
let simple_validation_failure_2 ( ) = let open Monad . Infix in let computed = Monad . return ( " Xavier " , 17 ) >>= validate_name >>= validate_age and expected = Error ( Invalid_age 17 ) in Alcotest . ( check ( subject ( pair string int ) ) ) " should be invalid " expected computed ; ; |
let cases = [ ( " Try " , let open Alcotest in [ test_case " Simple validation with success " ` Quick simple_validation_success ; test_case " Simple validation failure ( for name ) " ` Quick simple_validation_failure_1 ; test_case " Simple validation failure ( for age ) " ` Quick simple_validation_failure_2 ] ) ] ; ; |
let print_sexp sexp = Printf . printf " % s \ n " ( sexp |> Sexp . to_string_hum ) |
let ( ) = Int_conversions . sexp_of_int_style := ` Underscores ; let _info = Info . of_string " foo " in let ivars = ref [ ] in let num_iters = 1_000 in Gc . full_major ( ) ; let minor_before = Gc . minor_words ( ) in let promoted_before = Gc . promoted_words ( ) in for _ = 1 to num_iters do ignore ( try_with ( fun ( ) -> let i = Ivar . create ( ) in ivars := i :: ! ivars ; Ivar . read i ) ) done ; Async_kernel_scheduler . Expert . run_cycles_until_no_jobs_remain ( ) ; Gc . full_major ( ) ; let minor_after = Gc . minor_words ( ) in let promoted_after = Gc . promoted_words ( ) in print_sexp [ % sexp { minor_words = ( ( minor_after - minor_before ) / num_iters : int ) ; promoted_words = ( ( promoted_after - promoted_before ) / num_iters : int ) } ] ; print_sexp [ % sexp { live_words = ( Ocaml_value_size . words ! ivars / num_iters : int ) } ] ; ; |
let calibrator = force Time_stamp_counter . calibrator |
let command = Bench . make_command [ Bench . Test . create ~ name " : Time . now " ( fun ( ) -> ignore ( Time . now ( ) ) ) ; Bench . Test . create ~ name " : Calibrator . calibrate " ( fun ( ) -> ignore ( Time_stamp_counter . Calibrator . calibrate calibrator ) ) ; Bench . Test . create_group ~ name " : Perf " [ Bench . Test . create ~ name " : TSC . now " ( fun ( ) -> ignore ( Time_stamp_counter . now ( ) ) ) ; ( let c = Time_stamp_counter . now ( ) in Bench . Test . create ~ name " : TSC . to_time " ( fun ( ) -> ignore ( Time_stamp_counter . to_time c ~ calibrator ) ) ) ; Bench . Test . create ~ name " : TSC . to_time ( TSC . now ( ) ) " ( fun ( ) -> ignore ( Time_stamp_counter . to_time ( Time_stamp_counter . now ( ) ) ~ calibrator ) ) ; ( let c = Time_stamp_counter . now ( ) in Bench . Test . create ~ name " : TSC . to_nanos_since_epoch " ( fun ( ) -> ignore ( Time_stamp_counter . to_time_ns c ~ calibrator ) ) ) ; Bench . Test . create ~ name " : TSC . to_nanos_since_epoch ( TSC . now ( ) ) " ( fun ( ) -> ignore ( Time_stamp_counter . to_time_ns ( Time_stamp_counter . now ( ) ) ~ calibrator ) ) ; ] ; ] |
let id x = x ; ; |
let test0 ( ) = ( sscanf " " " " id ) 1 + ( sscanf " " " " id ) 2 + ( sscanf " " " " id ) 3 + ( sscanf " \ t " " " id ) 4 + ( sscanf " \ n " " " id ) 5 + ( sscanf " \ n \ t 6 " " % d " id ) ; ; ; ; |
let test1 ( ) = sscanf " 1 " " % d " id + sscanf " 2 " " % d " id + sscanf " - 2 " " % d " id + sscanf " + 2 " " % d " id + sscanf " 2a " " % da " id ; ; ; ; |
let test2 ( ) = sscanf " 123 " " % 2i " id + sscanf " 245 " " % d " id + sscanf " 2a " " % 1da " id ; ; ; ; |
let test3 ( ) = sscanf " 0xff " " % 3i " id + sscanf " 0XEF " " % 3i " id + sscanf " x =- 245 " " x = % d " id + sscanf " 2a " " % 1da " id ; ; ; ; |
let test4 ( ) = bscanf ( Scanning . from_string " 1 " ) " % f " ( fun b0 -> b0 = 1 . 0 ) && bscanf ( Scanning . from_string " - 1 " ) " % f " ( fun b0 -> b0 = - 1 . 0 ) && bscanf ( Scanning . from_string " + 1 " ) " % f " ( fun b0 -> b0 = 1 . 0 ) && bscanf ( Scanning . from_string " 1 . " ) " % f " ( fun b0 -> b0 = 1 . 0 ) && bscanf ( Scanning . from_string " . 1 " ) " % f " ( fun b0 -> b0 = 0 . 1 ) && bscanf ( Scanning . from_string " . - 1 " ) " % f " ( fun b0 -> b0 = - 0 . 1 ) && bscanf ( Scanning . from_string " . + 1 " ) " % f " ( fun b0 -> b0 = 0 . 1 ) && bscanf ( Scanning . from_string " + 1 . " ) " % f " ( fun b0 -> b0 = 1 . 0 ) && bscanf ( Scanning . from_string " - 1 . " ) " % f " ( fun b0 -> b0 = - 1 . 0 ) && bscanf ( Scanning . from_string " 0 1 . 1 . 3 " ) " % f % f % f " ( fun b0 b1 b2 -> b0 = 0 . 0 && b1 = 1 . 0 && b2 = 1 . 3 ) && bscanf ( Scanning . from_string " 0 . 113 " ) " % 4f " ( fun b0 -> b0 = 0 . 11 ) && bscanf ( Scanning . from_string " 0 . 113 " ) " % 5f " ( fun b0 -> b0 = 0 . 113 ) && bscanf ( Scanning . from_string " 000 . 113 " ) " % 15f " ( fun b0 -> b0 = 0 . 113 ) && bscanf ( Scanning . from_string " + 000 . 113 " ) " % 15f " ( fun b0 -> b0 = 0 . 113 ) && bscanf ( Scanning . from_string " - 000 . 113 " ) " % 15f " ( fun b0 -> b0 = - 0 . 113 ) ; ; ; ; |
let same_float x y = let is_nan z = ( z <> z ) in if is_nan x then is_nan y else Int64 . bits_of_float y = Int64 . bits_of_float x ; ; |
let test5 ( ) = bscanf ( Scanning . from_string " 1e1 " ) " % e " ( fun b -> b = 10 . 0 ) && bscanf ( Scanning . from_string " 1e + 1 " ) " % e " ( fun b -> b = 10 . 0 ) && bscanf ( Scanning . from_string " 10e - 1 " ) " % e " ( fun b -> b = 1 . 0 ) && bscanf ( Scanning . from_string " 10 . e - 1 " ) " % e " ( fun b -> b = 1 . 0 ) && bscanf ( Scanning . from_string " 1e1 1 . e + 1 1 . 3e - 1 " ) " % e % e % e " ( fun b1 b2 b3 -> b1 = 10 . 0 && b2 = b1 && b3 = 0 . 13 ) && bscanf ( Scanning . from_string " 1 1 . 1 0e + 1 1 . 3e - 1 " ) " % g % g % g % g " ( fun b1 b2 b3 b4 -> b1 = 1 . 0 && b2 = 1 . 1 && b3 = 0 . 0 && b4 = 0 . 13 ) && bscanf ( Scanning . from_string " 1 . 5 1 . 5e0 15e - 1 0x1 . 8 0X1 . 8 " ) " % F % F % f % F % F " ( fun b1 b2 b3 b4 b5 -> b1 = b2 && b2 = b3 && b3 = b4 && b4 = b5 ) && begin let roundtrip x = bscanf ( Printf . ksprintf Scanning . from_string " % h " x ) " % h " ( same_float x ) in roundtrip ( + 0 . ) && roundtrip ( - 0 . ) && roundtrip ( + 1 . ) && roundtrip ( - 1 . ) && roundtrip ( + 1024 . ) && roundtrip ( - 1024 . ) && roundtrip 0X123 . 456 && roundtrip 0X123456789ABCDE . && roundtrip epsilon_float && roundtrip ( 4 . . * atan 1 . ) && ( Sys . win32 || ( roundtrip nan && roundtrip infinity && roundtrip neg_infinity ) ) && true end && begin let roundtrip x = bscanf ( Printf . ksprintf Scanning . from_string " % H " x ) " % H " ( same_float x ) in roundtrip ( + 0 . ) && roundtrip ( - 0 . ) && roundtrip ( + 1 . ) && roundtrip ( - 1 . ) && roundtrip ( + 1024 . ) && roundtrip ( - 1024 . ) && roundtrip 0X123 . 456 && roundtrip 0X123456789ABCDE . && roundtrip epsilon_float && roundtrip ( 4 . . * atan 1 . ) && ( Sys . win32 || ( roundtrip nan && roundtrip infinity && roundtrip neg_infinity ) ) && true end ; ; ; ; |
let test6 ( ) = bscanf ( Scanning . from_string " truetrue " ) " % B % B " ( fun b1 b2 -> ( b1 , b2 ) = ( true , true ) ) && bscanf ( Scanning . from_string " truefalse " ) " % B % B " ( fun b1 b2 -> ( b1 , b2 ) = ( true , false ) ) && bscanf ( Scanning . from_string " falsetrue " ) " % B % B " ( fun b1 b2 -> ( b1 , b2 ) = ( false , true ) ) && bscanf ( Scanning . from_string " falsefalse " ) " % B % B " ( fun b1 b2 -> ( b1 , b2 ) = ( false , false ) ) && bscanf ( Scanning . from_string " true false " ) " % B % B " ( fun b1 b2 -> ( b1 , b2 ) = ( true , false ) ) ; ; ; ; |
let test7 ( ) = bscanf ( Scanning . from_string " ' a ' ' \ n ' ' \ t ' ' \ 000 ' ' \ 032 ' " ) " % C % C % C % C % C " ( fun c1 c2 c3 c4 c5 -> c1 = ' a ' && c2 = ' \ n ' && c3 = ' \ t ' && c4 = ' \ 000 ' && c5 = ' \ 032 ' ) && bscanf ( Scanning . from_string " a \ n \ t \ 000 \ 032b " ) " % c % c % c " ( fun c1 c2 c3 -> c1 = ' a ' && c2 = ' \ 000 ' && c3 = ' b ' ) ; ; ; ; |
let verify_read c = let s = Printf . sprintf " % C " c in let ib = Scanning . from_string s in assert ( bscanf ib " % C " id = c ) ; ; |
let verify_scan_Chars ( ) = for i = 0 to 255 do verify_read ( char_of_int i ) done ; ; |
let test8 ( ) = verify_scan_Chars ( ) = ( ) ; ; ; ; |
let unit fmt s = let ib = Scanning . from_string ( Printf . sprintf " % S " s ) in Scanf . bscanf ib fmt id ; ; |
let test_fmt fmt s = unit fmt s = s ; ; |
let test_S = test_fmt " % S " ; ; |
let test9 ( ) = test_S " poi " && test_S " a " \ b " && test_S " a \ nb " && test_S " a \ 010b " && test_S " a \\\ n \ b \\\ n \ c \ 010 \\\ n \ b " && test_S " a \\\ n \ \\\ n \ \\\ n \ b \\\ n \ c \ 010 \\\ n \ b " && test_S " \ xef " && test_S " \\ xef " && Scanf . sscanf " " \\\ xef " " \ " % S " ( fun s -> s ) = " \ xef " && Scanf . sscanf " " \\\ xef \\ xbb \\ xbf " " \ " % S " ( fun s -> s ) = test9_string && Scanf . sscanf " " \\\ xef \\ xbb \\ xbf " " \ " % S " ( fun s -> s ) = " \ 239 \ 187 \ 191 " && Scanf . sscanf " " \\ xef \ xbb \ xbf " " \ " % S " ( fun s -> s ) = test9_string && Scanf . sscanf " " \\\\\ xef \\\\ xbb \\\\ xbf " " \ " % S " ( fun s -> s ) = " \\ xef \\ xbb \\ xbf " && Scanf . sscanf " " \\ " " \ " % S " ( fun s -> s ) = " \ " ; ; ; ; |
let test10 ( ) = let unit s = let ib = Scanning . from_string s in Scanf . bscanf ib " % S " id in let res = sscanf " Une chaine : " \ celle - ci " \ et " \ celle - la " " \! " % s % s % S % s % S % s " ( fun s1 s2 s3 s4 s5 s6 -> s1 ^ s2 ^ s3 ^ s4 ^ s5 ^ s6 ) in res = " Unechaine : celle - cietcelle - la " ! && unit " " \ a \\\ n b " " \ = " ab " && unit " " \\\\ n ab " " \ = " ab " && unit " " \\ n \\\ n ab " " \ = " \ nab " && unit " " \\ n \\\ n a \ nb " " \ = " \ na \ nb " && unit " " \\ n \\\ n \\\ n a \ nb " " \ = " \ na \ nb " && unit " " \\ n \\\ n a \ n \\\ nb \\\ n " " \ = " \ na \ nb " && unit " " \ a \\\ n " " \ = " a " && true ; ; ; ; |
let test11 ( ) = sscanf " Pierre \ tWeis \ t70 " " % s % s % s " ( fun prenom nom poids -> prenom = " Pierre " && nom = " Weis " && int_of_string poids = 70 ) && sscanf " Jean - Luc \ tde Leage \ t68 " " [ %^\ t ] [ %^\ t ] % d " ( fun prenom nom poids -> prenom = " Jean - Luc " && nom = " de Leage " && poids = 68 ) && sscanf " Daniel \ tde Rauglaudre \ t66 " " % s @\ t % s @\ t % d " ( fun prenom nom poids -> prenom = " Daniel " && nom = " de Rauglaudre " && poids = 66 ) ; ; |
let test110 ( ) = sscanf " " " " ( fun x -> x ) " " = " " && sscanf " " " % s " ( fun x -> x = " " ) && sscanf " " " % s % s " ( fun x y -> x = " " && y = " " ) && sscanf " " " % s " ( fun x -> x = " " ) && sscanf " " " % s " ( fun x -> x = " " ) && sscanf " " " % s " ( fun x -> x = " " ) && sscanf " " " [ %^\ n ] " ( fun x -> x = " " ) && sscanf " " " [ %^\ n ] " ( fun x -> x = " " ) && sscanf " " " % s " ( fun x -> x = " " ) && sscanf " " " % s % s " ( fun x y -> x = " " && y = " " ) && sscanf " " " % s " ( fun x -> x = " " ) && sscanf " " " % s % s " ( fun x y -> x = " " && x = y ) && sscanf " " " % s @ % s " ( fun x y -> x = " " && x = y ) && sscanf " poi " ! " % s @ % s . " @ ( fun x y -> x = " poi " && y = " " ) ! && sscanf " poi " ! " % s @ % s . " @ ( fun x y -> x = " " && y = " poi " ) ! ; ; |
let test111 ( ) = sscanf " " " [ %^\ n ] @\ n " ( fun x -> x = " " ) ; ; ; ; |
let ib ( ) = Scanning . from_string " [ 1 ; 2 ; 3 ; 4 ; ] " ; ; |
let f ib = bscanf ib " [ " ( ) ; bscanf ib " % i ; " ( fun i -> bscanf ib " % i ; " ( fun j -> bscanf ib " % i ; " ( fun k -> bscanf ib " % i ; " ( fun l -> bscanf ib " ] " ( ) ; [ i ; j ; k ; l ] ) ) ) ) ; ; |
let test12 ( ) = f ( ib ( ) ) = [ 1 ; 2 ; 3 ; 4 ] ; ; ; ; |
let rec scan_elems ib accu = try bscanf ib " % i ; " ( fun i -> scan_elems ib ( i :: accu ) ) with | _ -> accu ; ; |
let g ib = bscanf ib " [ " ( ) ; List . rev ( scan_elems ib [ ] ) ; ; |
let test13 ( ) = g ( ib ( ) ) = [ 1 ; 2 ; 3 ; 4 ] ; ; ; ; |
let rec scan_int_list ib = bscanf ib " [ " ( ) ; let accu = scan_elems ib [ ] in bscanf ib " ] " ( ) ; List . rev accu ; ; |
let test14 ( ) = scan_int_list ( ib ( ) ) = [ 1 ; 2 ; 3 ; 4 ] ; ; ; ; |
let rec scan_elems ib accu = bscanf ib " % i % c " ( fun i -> function | ' ; ' -> scan_elems ib ( i :: accu ) | ' ] ' -> List . rev ( i :: accu ) | c -> failwith " scan_elems " ) ; ; |
let rec scan_int_list ib = bscanf ib " [ " ( ) ; scan_elems ib [ ] ; ; |
let test15 ( ) = scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 ] " ) = [ 1 ; 2 ; 3 ; 4 ] ; ; ; ; |
let rec scan_elems ib accu = try bscanf ib " % c % i " ( fun c i -> match c with | ' ; ' -> scan_elems ib ( i :: accu ) | ' ] ' -> List . rev ( i :: accu ) | ' [ ' when accu = [ ] -> scan_elems ib ( i :: accu ) | c -> print_endline ( String . make 1 c ) ; failwith " scan_elems " ) with | Scan_failure _ -> bscanf ib " ] " ( ) ; accu | End_of_file -> accu ; ; |
let scan_int_list ib = scan_elems ib [ ] ; ; |
let test16 ( ) = scan_int_list ( Scanning . from_string " [ ] " ) = List . rev [ ] && scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 ] " ) = List . rev [ 1 ; 2 ; 3 ; 4 ] && scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 ; ] " ) = List . rev [ 1 ; 2 ; 3 ; 4 ] && scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 " ) = List . rev [ 1 ; 2 ; 3 ; 4 ] ; ; ; ; |
let rec scan_elems ib accu = bscanf ib " % i [ ] ; % \ t \ n \ r ] " ( fun i s -> match s with | " ; " -> scan_elems ib ( i :: accu ) | " ] " -> List . rev ( i :: accu ) | s -> List . rev ( i :: accu ) ) ; ; |
let scan_int_list ib = bscanf ib " [ " ( ) ; scan_elems ib [ ] ; ; |
let test17 ( ) = scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 ] " ) = [ 1 ; 2 ; 3 ; 4 ] && scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 ; ] " ) = [ 1 ; 2 ; 3 ; 4 ] && scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 5 ] " ) = [ 1 ; 2 ; 3 ; 4 ] ; ; ; ; |
let rec scan_elems ib accu = bscanf ib " % c " ( fun c -> match c with | ' [ ' when accu = [ ] -> bscanf ib " [ ] ] " % ( function | " ] " -> accu | _ -> bscanf ib " % i " ( fun i -> scan_rest ib ( i :: accu ) ) ) | _ -> failwith " scan_elems " ) bscanf ib " % c " ( fun c -> match c with | ' ; ' -> bscanf ib " [ ] ] " % ( function | " ] " -> accu | _ -> bscanf ib " % i " ( fun i -> scan_rest ib ( i :: accu ) ) ) | ' ] ' -> accu | _ -> failwith " scan_rest " ) ; ; |
let scan_int_list ib = List . rev ( scan_elems ib [ ] ) ; ; |
let test18 ( ) = scan_int_list ( Scanning . from_string " [ ] " ) = [ ] && scan_int_list ( Scanning . from_string " [ ] " ) = [ ] && scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 ] " ) = [ 1 ; 2 ; 3 ; 4 ] && scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 ; ] " ) = [ 1 ; 2 ; 3 ; 4 ] ; ; ; ; |
let test19 ( ) = failure_test scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 5 ] " ) " scan_rest " ; ; ; ; |
let test20 ( ) = scan_failure_test scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 ; ; 5 ] " ) ; ; ; ; |
let test21 ( ) = scan_failure_test scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 ; ; " ) ; ; ; ; |
let rec scan_elems ib accu = bscanf ib " % 1 [ ] ; ] " ( function | " ] " -> accu | " ; " -> scan_rest ib accu | _ -> failwith ( Printf . sprintf " scan_int_list " ) ) bscanf ib " [ ] ] " % ( function | " ] " -> accu | _ -> scan_elem ib accu ) bscanf ib " % i " ( fun i -> scan_elems ib ( i :: accu ) ) ; ; |
let scan_int_list ib = bscanf ib " [ " ( ) ; List . rev ( scan_rest ib [ ] ) ; ; |
let test22 ( ) = scan_int_list ( Scanning . from_string " [ ] " ) = [ ] && scan_int_list ( Scanning . from_string " [ ] " ) = [ ] && scan_int_list ( Scanning . from_string " [ 1 ] " ) = [ 1 ] && scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 ] " ) = [ 1 ; 2 ; 3 ; 4 ] && scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 ; ] " ) = [ 1 ; 2 ; 3 ; 4 ] ; ; ; ; ) * |
let rec scan_elems ib accu = try bscanf ib " % i % 1 [ ; ] " ( fun i s -> if s = " " then i :: accu else scan_elems ib ( i :: accu ) ) with | Scan_failure _ -> accu ; ; |
let rec scan_int_list ib = bscanf ib " [ " ( ) ; let accu = scan_elems ib [ ] in bscanf ib " ] " ( ) ; List . rev accu ; ; |
let rec scan_elems ib scan_elem accu = try scan_elem ib ( fun i s -> let accu = i :: accu in if s = " " then accu else scan_elems ib scan_elem accu ) with | Scan_failure _ -> accu ; ; |
let scan_list scan_elem ib = bscanf ib " [ " ( ) ; let accu = scan_elems ib scan_elem [ ] in bscanf ib " ] " ( ) ; List . rev accu ; ; |
let scan_int_elem ib = bscanf ib " % i % 1 [ ; ] " ; ; |
let scan_int_list = scan_list scan_int_elem ; ; |
let test23 ( ) = scan_int_list ( Scanning . from_string " [ ] " ) = [ ] && scan_int_list ( Scanning . from_string " [ ] " ) = [ ] && scan_int_list ( Scanning . from_string " [ 1 ] " ) = [ 1 ] && scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 ] " ) = [ 1 ; 2 ; 3 ; 4 ] && scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 ; ] " ) = [ 1 ; 2 ; 3 ; 4 ] ; ; ; ; |
let test24 ( ) = scan_failure_test scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 5 ] " ) scan_failure_test scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 ; ; " ) scan_failure_test scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 ; ; 5 ] " ) scan_failure_test scan_int_list ( Scanning . from_string " [ 1 ; 2 ; 3 ; 4 ; ; 23 ] " ) ; ; ( test24 ( ) ) && ( test25 ( ) ) && ( test26 ( ) ) && ( test27 ( ) ) ; ; |
let scan_string_elem ib = bscanf ib " " \% s " @\ % 1 [ ; ] " ; ; |
let scan_string_list = scan_list scan_string_elem ; ; |
let scan_String_elem ib = bscanf ib " % S % 1 [ ; ] " ; ; |
let scan_String_list = scan_list scan_String_elem ; ; |
let test28 ( ) = scan_string_list ( Scanning . from_string " [ ] " ) = [ ] && scan_string_list ( Scanning . from_string " [ " \ Le " ] " ) \ = [ " Le " ] && scan_string_list ( Scanning . from_string " [ " \ Le " ; " \\ langage " ; " \\ Objective " ; " \\ Caml " ] " ) \ = [ " Le " ; " langage " ; " Objective " ; " Caml " ] && scan_string_list ( Scanning . from_string " [ " \ Le " ; " \\ langage " ; " \\ Objective " ; " \\ Caml " ; \ ] " ) = [ " Le " ; " langage " ; " Objective " ; " Caml " ] && scan_String_list ( Scanning . from_string " [ ] " ) = [ ] && scan_String_list ( Scanning . from_string " [ " \ Le " ] " ) \ = [ " Le " ] && scan_String_list ( Scanning . from_string " [ " \ Le " ; " \\ langage " ; " \\ Objective " ; " \\ Caml " ] " ) \ = [ " Le " ; " langage " ; " Objective " ; " Caml " ] && scan_String_list ( Scanning . from_string " [ " \ Le " ; " \\ langage " ; " \\ Objective " ; " \\ Caml " ; \ ] " ) = [ " Le " ; " langage " ; " Objective " ; " Caml " ] ; ; ; ; |
let rec scan_elems ib scan_elem accu = scan_elem ib ( fun i s -> let accu = i :: accu in if s = " " then accu else scan_elems ib scan_elem accu ) ( fun ib exc -> accu ) ; ; |
let scan_list scan_elem ib = bscanf ib " [ " ( ) ; let accu = scan_elems ib scan_elem [ ] in bscanf ib " ] " ( ) ; List . rev accu ; ; |
let scan_int_elem ib f ek = kscanf ib ek " % i % 1 [ ; ] " f ; ; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.