listtest.cd 3 KB
Newer Older
1
2
exit 0;;

3
4
5
6
7
8
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
9
(* TODO: Bugfix: it works here if you replace 'a with 'b *)
10
11
12
13
 let run_test_suite (l : [(Latin1, 'a, 'a)*]) : [] =
   iter (fun ((Latin1, 'a, 'a) -> []) (_, x, y) -> nb_tests := !nb_tests + 1;
     if x = y then nb_success := !nb_success + 1 else []) l
 ;;
14

Julien Lopez's avatar
Julien Lopez committed
15
let run_test_suite (l : [(Latin1, 'a, 'a)*]) : [] = match l with
16
  | [] -> []
Julien Lopez's avatar
Julien Lopez committed
17
  | [(_, x, y) rest::(Latin1, 'a, 'a)*] -> nb_tests := !nb_tests + 1;
18
19
20
    (if x = y then nb_success := !nb_success + 1 else []);
    run_test_suite rest

Julien Lopez's avatar
Julien Lopez committed
21
let run_test_suite_debug (l : [(Latin1, 'a, 'a)*]) : [] = match l with
22
  | [] -> []
Julien Lopez's avatar
Julien Lopez committed
23
  | [(msg, x, y) rest::(Latin1, 'a, 'a)*] -> nb_tests := !nb_tests + 1;
24
25
26
27
28
29
30
    (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 = [
31
32
  ("Test stdlib.list.length.1 failed", length [2 5 4], 3)
  ("Test stdlib.list.length.2 failed", length [], 0)
33
34
35
] in

let hd_tests = [
36
37
  ("Test stdlib.list.hd.1 failed", hd [1], 1)
  ("Test stdlib.list.hd.2 failed", hd [2 5 4], 2)
38
39
40
] in

let tl_tests = [
41
42
  ("Test stdlib.list.tl.1 failed", tl [1], [1])
  ("Test stdlib.list.tl.2 failed", tl [2 5 4], [5 4])
43
44
] in

45
46
47
48
49
50
51
52
53
54
55
56
57
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

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
78

79
80
81
82
83
(* Run tests *)

run_test_suite length_tests;
run_test_suite hd_tests;
run_test_suite tl_tests;
84
run_test_suite nth_tests;
Julien Lopez's avatar
Julien Lopez committed
85
86
87
88
run_test_suite rev_tests;
run_test_suite append_tests;
run_test_suite rev_append_tests;
run_test_suite concat_tests;
89
print (string_of !nb_success); print " / "; print (string_of !nb_tests)
90
91

Tuareg finished at Fri Jul 11 14:24:59