Source

ocaml-llists / tests / test_common.ml

Full commit
(* Common functions for unit testing
 *
 * Author: Vadim Shender
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version,
 * with the special exception on linking described in file LICENSE.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)


let list_printer item_printer = function
    []     -> "[]"
  | h :: t ->
      let content =
        List.fold_left
          (fun res x -> res ^ "; " ^ item_printer x) (item_printer h) t
      in "[" ^ content ^ "]"

let array_printer item_printer a =
  if Array.length a = 0 then "[||]"
  else
    let content =
      Array.fold_left
        (fun res x -> res ^ "; " ^ item_printer x)
        (item_printer a.(0))
        (Array.sub a 1 (Array.length a - 1))
    in "[|" ^ content ^ "|]"

let pair_printer item_printer (a, b) =
  "(" ^ item_printer a ^ ", " ^ item_printer b ^ ")"

let pair_printer2 item_printer1 item_printer2 (a, b) =
  "(" ^ item_printer1 a ^ ", " ^ item_printer2 b ^ ")"

let triplet_printer item_printer (a, b, c) =
  "(" ^ item_printer a ^ ", " ^ item_printer b ^ ", " ^ item_printer c ^ ")"

let string_printer s =
  "\"" ^ s ^ "\""

let bool_printer = function
    true -> "true"
  | _    -> "false"


let test_laziness_aux ?limit () =
  let side_effect = ref [] in
  let f i = side_effect := i :: !side_effect ; i in
  Lazy_list.init ?limit f, side_effect