Blame test/test_ExtList.ml

rpm-build 0f2925
(*
rpm-build 0f2925
 * ExtLib Testing Suite
rpm-build 0f2925
 * Copyright (C) 2004 Janne Hellsten
rpm-build 0f2925
 * Copyright (C) 2008 Red Hat, Inc.
rpm-build 0f2925
 * Copyright (C) 2010 ygrek
rpm-build 0f2925
 *
rpm-build 0f2925
 * This library is free software; you can redistribute it and/or
rpm-build 0f2925
 * modify it under the terms of the GNU Lesser General Public
rpm-build 0f2925
 * License as published by the Free Software Foundation; either
rpm-build 0f2925
 * version 2.1 of the License, or (at your option) any later version,
rpm-build 0f2925
 * with the special exception on linking described in file LICENSE.
rpm-build 0f2925
 *
rpm-build 0f2925
 * This library is distributed in the hope that it will be useful,
rpm-build 0f2925
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 0f2925
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build 0f2925
 * Lesser General Public License for more details.
rpm-build 0f2925
 *
rpm-build 0f2925
 * You should have received a copy of the GNU Lesser General Public
rpm-build 0f2925
 * License along with this library; if not, write to the Free Software
rpm-build 0f2925
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
rpm-build 0f2925
 *)
rpm-build 0f2925
rpm-build 0f2925
(* Standard library list *)
rpm-build 0f2925
module StdList = List
rpm-build 0f2925
rpm-build 0f2925
open ExtList
rpm-build 0f2925
rpm-build 0f2925
exception Test_Exception
rpm-build 0f2925
rpm-build 0f2925
let check_empty_list_exn f = 
rpm-build 0f2925
  try f (); false with List.Empty_list -> true
rpm-build 0f2925
rpm-build 0f2925
(** Random length list with [0;1;2;..n] contents. *)
rpm-build 0f2925
let rnd_list () = 
rpm-build 0f2925
  let len = Random.int 3 in 
rpm-build 0f2925
  List.init len Std.identity
rpm-build 0f2925
rpm-build 0f2925
let test_iteri () = 
rpm-build 0f2925
  for i = 0 to 15 do
rpm-build 0f2925
    List.iteri (fun i e -> assert (i = e)) (rnd_list ());
rpm-build 0f2925
  done
rpm-build 0f2925
rpm-build 0f2925
let test_mapi () =
rpm-build 0f2925
  for i = 0 to 15 do
rpm-build 0f2925
    let rnd_list = rnd_list () in
rpm-build 0f2925
    let lst = List.mapi (fun n e -> (e,"foo")) rnd_list in
rpm-build 0f2925
    let lst' = 
rpm-build 0f2925
      List.mapi (fun n (e,s) -> assert (s = "foo"); assert (n = e); n) lst in
rpm-build 0f2925
    List.iteri (fun i e -> assert (i = e)) lst'
rpm-build 0f2925
  done
rpm-build 0f2925
rpm-build 0f2925
let test_exceptions () = 
rpm-build 0f2925
  assert (check_empty_list_exn (fun () -> List.hd []));
rpm-build 0f2925
  assert (check_empty_list_exn (fun () -> List.first []));
rpm-build 0f2925
  assert (check_empty_list_exn (fun () -> List.last []))
rpm-build 0f2925
rpm-build 0f2925
let test_find_exc () =
rpm-build 0f2925
  let check_exn f = try f (); false with Test_Exception -> true | _ -> false in
rpm-build 0f2925
  assert (check_exn (fun () -> (List.find_exc (fun _ -> true) Test_Exception [])));
rpm-build 0f2925
  try 
rpm-build 0f2925
    for i = 0 to 15 do
rpm-build 0f2925
      let rnd_lst = rnd_list () in
rpm-build 0f2925
      begin 
rpm-build 0f2925
        match rnd_lst with
rpm-build 0f2925
          [] -> ()
rpm-build 0f2925
        | lst -> 
rpm-build 0f2925
            let rnd_elem = Random.int (List.length lst) in
rpm-build 0f2925
            assert (check_exn 
rpm-build 0f2925
                      (fun () -> 
rpm-build 0f2925
                         List.find_exc (fun e -> e = List.length lst) Test_Exception lst));
rpm-build 0f2925
            assert (not (check_exn 
rpm-build 0f2925
                           (fun () -> 
rpm-build 0f2925
                              List.find_exc (fun e -> e = rnd_elem) Test_Exception lst)))
rpm-build 0f2925
      end
rpm-build 0f2925
    done
rpm-build 0f2925
  with _ -> assert false
rpm-build 0f2925
rpm-build 0f2925
let test_findi () =
rpm-build 0f2925
  let check_fn f = try (let e,i = f () in e<>i) with Not_found -> true in
rpm-build 0f2925
  try 
rpm-build 0f2925
    for i = 0 to 15 do
rpm-build 0f2925
      let rnd_lst = rnd_list () in
rpm-build 0f2925
      begin 
rpm-build 0f2925
        match rnd_lst with
rpm-build 0f2925
          [] -> ()
rpm-build 0f2925
        | lst -> 
rpm-build 0f2925
            let rnd_elem = Random.int (List.length lst) in
rpm-build 0f2925
            assert (check_fn 
rpm-build 0f2925
                      (fun () -> 
rpm-build 0f2925
                         List.findi (fun i e -> e = List.length lst) lst));
rpm-build 0f2925
            assert (not (check_fn 
rpm-build 0f2925
                           (fun () -> 
rpm-build 0f2925
                              List.findi (fun i e -> e = rnd_elem) lst)))
rpm-build 0f2925
      end
rpm-build 0f2925
    done
rpm-build 0f2925
  with _ -> assert false
rpm-build 0f2925
rpm-build 0f2925
let test_fold_right () = 
rpm-build 0f2925
  let maxlen = 2000 in
rpm-build 0f2925
  (* NOTE assuming we will not blow the stack with 2000 elements *)
rpm-build 0f2925
  let lst = List.init maxlen Std.identity in
rpm-build 0f2925
  let a = StdList.fold_right (fun e a -> e::a) lst [] in
rpm-build 0f2925
  let b = List.fold_right (fun e a -> e::a) lst [] in
rpm-build 0f2925
  assert (a = b)
rpm-build 0f2925
rpm-build 0f2925
let test_fold_right2 () = 
rpm-build 0f2925
  let len = 2000 in
rpm-build 0f2925
  let cnt = ref 0 in
rpm-build 0f2925
  let lst = List.init len Std.identity in
rpm-build 0f2925
  ignore (StdList.fold_right (fun e a -> incr cnt; e::a) lst []);
rpm-build 0f2925
  let cnt_std = !cnt in
rpm-build 0f2925
  cnt := 0;
rpm-build 0f2925
  ignore (List.fold_right (fun e a -> incr cnt; e::a) lst []);
rpm-build 0f2925
  assert (cnt_std = len);
rpm-build 0f2925
  assert (!cnt = cnt_std)
rpm-build 0f2925
rpm-build 0f2925
let test_map () = 
rpm-build 0f2925
  for i = 0 to 10 do
rpm-build 0f2925
    let f = ( * ) 2 in
rpm-build 0f2925
    let lst = rnd_list () in
rpm-build 0f2925
    let a = StdList.map f lst in
rpm-build 0f2925
    let b = List.map f lst in
rpm-build 0f2925
    assert (a = b)
rpm-build 0f2925
  done
rpm-build 0f2925
rpm-build 0f2925
let test_find_map () =
rpm-build 0f2925
  let f = function "this", v -> Some v | _ -> None in
rpm-build 0f2925
  (try
rpm-build 0f2925
     let r = List.find_map f [ "a", 1; "b", 2; "this", 3; "d", 4 ] in
rpm-build 0f2925
     assert (3 = r);
rpm-build 0f2925
     let r = List.find_map f [ "this", 1; "b", 2; "c", 3; "d", 4 ] in
rpm-build 0f2925
     assert (1 = r);
rpm-build 0f2925
     let r = List.find_map f [ "a", 1; "b", 2; "c", 3; "this", 4 ] in
rpm-build 0f2925
     assert (4 = r);
rpm-build 0f2925
     let r = List.find_map f [ "this", 1; "b", 2; "c", 3; "this", 4 ] in
rpm-build 0f2925
     assert (1 = r);
rpm-build 0f2925
     let r = List.find_map f [ "a", 1; "b", 2; "this", 3; "this", 4 ] in
rpm-build 0f2925
     assert (3 = r);
rpm-build 0f2925
     let r = List.find_map f [ "this", 5 ] in
rpm-build 0f2925
     assert (5 = r)
rpm-build 0f2925
   with
rpm-build 0f2925
     Not_found -> assert false
rpm-build 0f2925
  );
rpm-build 0f2925
  (try
rpm-build 0f2925
     ignore (List.find_map f []); assert false
rpm-build 0f2925
   with
rpm-build 0f2925
     Not_found -> ()
rpm-build 0f2925
  );
rpm-build 0f2925
  (try
rpm-build 0f2925
     ignore (List.find_map f [ "a", 1 ]); assert false
rpm-build 0f2925
   with
rpm-build 0f2925
     Not_found -> ()
rpm-build 0f2925
  );
rpm-build 0f2925
  (try
rpm-build 0f2925
     ignore (List.find_map f [ "a", 1; "b", 2 ]); assert false
rpm-build 0f2925
   with
rpm-build 0f2925
     Not_found -> ()
rpm-build 0f2925
  )
rpm-build 0f2925
rpm-build 0f2925
(* Issue 12: List.make not tail-recursive *)
rpm-build 0f2925
let test_make () =
rpm-build 0f2925
  let l = List.make 10_000_000 1 in
rpm-build 0f2925
  assert (List.length l = 10_000_000)
rpm-build 0f2925
rpm-build 0f2925
let () =
rpm-build 0f2925
  Util.register "ExtList" [
rpm-build 0f2925
    "iteri", test_iteri;
rpm-build 0f2925
    "mapi", test_mapi;
rpm-build 0f2925
    "exceptions", test_exceptions;
rpm-build 0f2925
    "find_exc", test_find_exc;
rpm-build 0f2925
    "findi", test_findi;
rpm-build 0f2925
    "fold_right", test_fold_right;
rpm-build 0f2925
    "fold_right2", test_fold_right2;
rpm-build 0f2925
    "map", test_map;
rpm-build 0f2925
    "find_map", test_find_map;
rpm-build 0f2925
    "make", test_make;
rpm-build 0f2925
  ]