listtest.cd 1.77 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
include "list.cd"

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

(* Note: We are using List.iter here *)
let run_test_suite (l : [(Latin1, ('a), ('a)) *]) : [] = match l with
  | [] -> []
  | [(_, x, y) rest::(Latin1, ('a), ('a))*] -> nb_tests := !nb_tests + 1;
    (if x = y then nb_success := !nb_success + 1 else []);
    run_test_suite rest

let run_test_suite_debug (l : [(Latin1, ('a), ('a)) *]) : [] = match l with
  | [] -> []
  | [(msg, x, y) rest::(Latin1, ('a), ('a))*] -> nb_tests := !nb_tests + 1;
    (if not (x = y) then print msg; print "\nExpected: "; print (string_of y);
       print "\nGot:"; print (string_of x); print "\n"
     else nb_success := !nb_success + 1); run_test_suite rest

(* Tests *)

let length_tests = [
23
24
  ("Test stdlib.list.length.1 failed", length [2 5 4], 3)
  ("Test stdlib.list.length.2 failed", length [], 0)
25
26
27
] in

let hd_tests = [
28
29
  ("Test stdlib.list.hd.1 failed", hd [1], 1)
  ("Test stdlib.list.hd.2 failed", hd [2 5 4], 2)
30
31
32
] in

let tl_tests = [
33
34
  ("Test stdlib.list.tl.1 failed", tl [1], [1])
  ("Test stdlib.list.tl.2 failed", tl [2 5 4], [5 4])
35
36
] in

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
*)

53
54
55
56
57
(* Run tests *)

run_test_suite length_tests;
run_test_suite hd_tests;
run_test_suite tl_tests;
58
59
run_test_suite nth_tests;
(* run_test_suite rev_tests; *)
60
print (string_of !nb_success); print " / "; print (string_of !nb_tests)