Source

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_int (reader : I.iteratee char int32) 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 %li\n%!" r
  | `Error e -> Printf.printf "error \"%s\"\n%!"
      (printexc e)
  ]
;


open Printf
;


value () =
  let max_int = Int64.of_int32 Int32.max_int in
  let pr = Printf.sprintf "%Li" in
  let samples_u =
    [ "0"
    ; "00"
    ; "123"
    ; "+123"
    ; "0123"
    ; "-123"
    ; pr max_int
    ; "000000" ^ pr max_int
    ; pr (Int64.add 1L max_int)
    ; pr (Int64.add 2L max_int)
    ; "19223372036854775806"
    ]
  in
  let samples = List.concat
    [ samples_u
    ; List.map (fun s -> "-" ^ s) samples_u
    ; List.map (fun s -> "+" ^ s) samples_u
    ]
  in
    ( printf "reading unsigned int32 with leading zeroes allowed:\n"
    ; List.iter (test_int D.read_uint32) samples
    ; print_newline ()
    ; printf "reading unsigned int32 with leading zeroes forbidden:\n"
    ; List.iter (test_int D.read_uint32_nz) samples
    ; print_newline ()

    ; printf "reading signed int32 with leading zeroes allowed:\n"
    ; List.iter (test_int D.read_int32) samples
    ; print_newline ()
    ; printf "reading signed int32 with leading zeroes forbidden:\n"
    ; List.iter (test_int D.read_int32_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 ()
;