listtest.cd 2.72 KB
Newer Older
1 2 3 4 5 6
include "list.cd"

let nb_success = ref Int (0)
let nb_tests = ref Int (0)

(* Note: We are using List.iter here *)
Julien Lopez's avatar
Julien Lopez committed
7
let run_test_suite (l : [(Latin1, 'a, 'a)*]) : [] =
8 9
   iter (fun ((Latin1, 'a, 'a) -> []) (_, x, y) -> nb_tests := !nb_tests + 1;
     if x = y then nb_success := !nb_success + 1 else []) l
10 11 12 13

(* Tests *)

let length_tests = [
14 15
  ("Test stdlib.list.length.1 failed", length [2 5 4], 3)
  ("Test stdlib.list.length.2 failed", length [], 0)
16 17 18
] in

let hd_tests = [
19 20
  ("Test stdlib.list.hd.1 failed", hd [1], 1)
  ("Test stdlib.list.hd.2 failed", hd [2 5 4], 2)
21 22 23
] in

let tl_tests = [
24 25
  ("Test stdlib.list.tl.1 failed", tl [1], [1])
  ("Test stdlib.list.tl.2 failed", tl [2 5 4], [5 4])
26 27
] in

28 29 30 31 32 33 34 35 36 37 38 39 40
let nth_tests = [
  ("Test stdlib.list.nth.1 failed", nth [1 2 3] 1, 2)
  ("Test stdlib.list.nth.2 failed", nth [4 5 6 7] 3, 7)
  ("Test stdlib.list.nth.3 failed", try nth [4 5 6 7] 4 with e -> e,
  "Failure \"List.nth\"")
  ("Test stdlib.list.nth.4 failed", try nth [4 5 6 7] -1 with e -> e,
  "Invalid_argument \"List.nth\"")
] in

let rev_tests = [
  ("Test stdlib.list.rev.1 failed", rev [], [])
  ("Test stdlib.list.rev.2 failed", rev [1 7 5 2], [2 5 7 1])
] in
Julien Lopez's avatar
Julien Lopez committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

let append_tests = [
  ("Test stdlib.list.append.1 failed", append [] [], [])
  ("Test stdlib.list.append.2 failed", append [1 7 5 2] [], [1 7 5 2])
  ("Test stdlib.list.append.3 failed", append [] [1 7 5 2], [1 7 5 2])
  ("Test stdlib.list.append.4 failed", append [1 7 5 2] [2 4 3], [1 7 5 2 2 4 3])
] in

let rev_append_tests = [
  ("Test stdlib.list.rev_append.1 failed", rev_append [] [], [])
  ("Test stdlib.list.rev_append.2 failed", rev_append [1 7 5 2] [], [2 5 7 1])
  ("Test stdlib.list.rev_append.3 failed", rev_append [] [1 7 5 2], [1 7 5 2])
  ("Test stdlib.list.rev_append.4 failed", rev_append [1 7 5 2] [2 4 3], [2 5 7 1 2 4 3])
] in

let concat_tests = [
  ("Test stdlib.list.concat.1 failed", concat [], [])
  ("Test stdlib.list.concat.2 failed", concat [[]], [])
  ("Test stdlib.list.concat.3 failed", flatten [[] [1 7 5 2] [] [2 4 7 4] [1]], [1 7 5 2 2 4 7 4 1])
] in
61

Julien Lopez's avatar
Julien Lopez committed
62 63 64 65 66 67 68 69 70 71 72 73
let even = fun (Int -> Bool; 'a\Int -> 'a\Int)
    | x & Int -> x mod 2 = 0
    | x -> x in

let mapf_tests = [
  ("Test stdlib.list.mapf.1 failed", mapf (fun ('a -> 'a) x -> x) [], [])
  ("Test stdlib.list.mapf.2 failed", mapf (fun ('a -> 'a) x -> x) [2 1 3], [2 1 3])
  ("Test stdlib.list.mapf.3 failed", mapf even [2 1 3], [`true `false `false])
] in

(* TODO: Tests on iterators *)

74 75 76 77 78
(* Run tests *)

run_test_suite length_tests;
run_test_suite hd_tests;
run_test_suite tl_tests;
79
run_test_suite nth_tests;
Julien Lopez's avatar
Julien Lopez committed
80 81 82 83
run_test_suite rev_tests;
run_test_suite append_tests;
run_test_suite rev_append_tests;
run_test_suite concat_tests;
Julien Lopez's avatar
Julien Lopez committed
84
run_test_suite mapf_tests;
85
print (string_of !nb_success); print " / "; print (string_of !nb_tests)