listtest.cd 1.22 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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 = [
  ("Test stdlib.length.1 failed", length [2 5 4], 3)
  ("Test stdlib.length.2 failed", length [], 0)
] in

let hd_tests = [
  ("Test stdlib.hd.1 failed", hd [1], 1)
  ("Test stdlib.hd.2 failed", hd [2 5 4], 2)
] in

let tl_tests = [
  ("Test stdlib.tl.1 failed", tl [1], [1])
  ("Test stdlib.tl.2 failed", tl [2 5 4], [5 4])
] in

(* Run tests *)

run_test_suite length_tests;
run_test_suite hd_tests;
run_test_suite tl_tests;
print (string_of !nb_success); print " / "; print (string_of !nb_tests)