dumbstreaming / test_it.ml

module I = Iteratees.Make(It_Lwt_IO.It_Lwt_IO);

module D = Dumbstreaming_it.Make(I);

value ( >>% ) = I.It_IO.bind_rev;
value ( % ) f g = fun x -> f (g x);
module IO = I.It_IO;

value enum1 s i =
  match i with
  [ I.IE_cont None k -> k s >>% IO.return % fst
  | I.IE_cont (Some _) _ | I.IE_done _ -> IO.return i
  ]
;

value rec printexc e =
  match e with
  [ I.Iteratees_err_msg e -> printexc e
  | e -> Printexc.to_string e
  ]
;

value test_uint reader string =
  let stream = I.Chunk (I.Subarray.of_string string) in
  let () = Printf.printf "%S -> %!" string in
  match IO.runIO ((enum1 stream reader) >>% I.run) with
  [ `Ok r -> Printf.printf "ok %i\n%!" r
  | `Error e -> Printf.printf "error \"%s\"\n%!"
      (printexc e)
  ]
;


open Printf
;

value () =
  let maxint = Int64.of_int max_int in
  let pr = Printf.sprintf "%Li" in
  let samples =
    [ "0"
    ; "00"
    ; "123"
    ; "0123"
    ; "-123"
    ; pr maxint
    ; "000000" ^ pr maxint
    ; pr (Int64.add 1L maxint)
    ; "19223372036854775806"
    ]
  in
    ( printf "reading unsigned ints with leading zeroes allowed:\n"
    ; List.iter (test_uint D.read_uint) samples
    ; print_newline ()
    ; printf "reading unsigned ints with leading zeroes forbidden:\n"
    ; List.iter (test_uint D.read_uint_nz) samples
    ; print_newline ()
    )
;




value gather_to_string = I.break_chars (fun _ -> False)
;


value test_read_lengths count string =
  let stream = I.Chunk (I.Subarray.of_string string) in
  let () = Printf.printf "%S -> %!" string in
  let reader = D.read_lengths count in
  let ( >>= ) = I.( >>= ) in
  match IO.runIO
    ( (enum1 stream
        (reader >>= fun r ->
         gather_to_string >>= fun rest ->
         I.return (r, rest)
        )
      )
      >>% I.run
    )
  with
  [ `Ok (r, rest) -> Printf.printf "ok [%s], rest = %S\n%!"
       (String.concat "; " (List.map string_of_int r))
       rest
  | `Error e -> Printf.printf "error \"%s\"\n%!"
      (printexc e)
  ]
;

value () =
( print_newline ()
; test_read_lengths 3 " 12 34 56 78 90"
)
;


value test_read_string = "\
12 3 2 4 6\n\
ab\n\
cdef\n\
ghijkl\n\
\n\
the-rest"
;


value test_read () =
  let stream = I.Chunk (I.Subarray.of_string test_read_string) in
  let ( >>= ) = I.( >>= ) in
  let it_res : I.iteratee char (option (list string)) =
    D.read
      ~get_piece_it :
        (fun ~totalsize ~totalcount ~piecesize ~piecenumber ->
           gather_to_string >>= fun str ->
           let msg = Printf.sprintf "[%i*%Li,#%i*%i]<%s>"
             totalcount totalsize piecenumber piecesize str in
           (* let () = Printf.eprintf "get_piece_it: %s\n%!" msg in *)
           I.return msg
        )
      ~combine_it : I.stream2list
  in
  match IO.runIO
    ( (enum1 stream
        (it_res >>= fun r ->
         gather_to_string >>= fun rest ->
         I.return (r, rest)
        )
      )
      >>% I.run
    )
  with
  [ `Ok (r, rest) -> Printf.printf "ok [%s], rest = %S\n%!"
      (match r with
       [ None -> "None"
       | Some r ->
           (String.concat " + " r)
       ])
       rest
  | `Error e -> Printf.printf "error \"%s\"\n%!"
      (printexc e)
  ]
;


value () = test_read ()
;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.