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

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

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

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

let tl_tests = [
38
39
  ("Test stdlib.list.tl.1 failed", tl [1], [1])
  ("Test stdlib.list.tl.2 failed", tl [2 5 4], [5 4])
40
41
] in

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

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
75

76
77
78
79
80
(* Run tests *)

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