Columns NOT numbered from zero (unless -z given).
[ocaml-csv.git] / test.ml
1 (* $Id: test.ml,v 1.2 2006-02-23 15:24:25 rich Exp $ *)
2
3 open Printf
4 open Csv
5
6 let do_testcsv filename expected =
7   let csv = load filename in
8   if csv <> expected then (
9     printf "input file: %s\n" filename;
10     printf "Csv library produced:\n";
11     print csv;
12     printf "Expected:\n";
13     print expected;
14     failwith "failed"
15   )
16   else ()
17
18 let () =
19   do_testcsv
20     "testcsv1.csv"
21     [ [ "This is a test\nwith commas,,,,,\n\nand carriage returns." ] ]
22 let () =
23   do_testcsv
24     "testcsv2.csv"
25     [ [ "Normal field"; "Quoted field"; "Quoted field with \"\" quotes" ] ]
26 let () =
27   do_testcsv
28     "testcsv3.csv"
29     [ [ "" ];
30       [ ""; "" ];
31       [ ""; ""; "" ];
32       [ ""; ""; ""; "" ];
33       [ ""; ""; ""; ""; "" ] ]
34 let () =
35   do_testcsv
36     "testcsv4.csv"
37     []
38 let () =
39   do_testcsv
40     "testcsv5.csv"
41     [ [ "This is a test\nwith commas,,,,,\n\nand carriage returns.";
42         "a second field"; "a third field" ];
43       [ "a fourth field on a new line" ] ]
44 let () =
45   do_testcsv
46     "testcsv6.csv"
47     [ [ "This is a test\nwith commas,,,,,\n\nand carriage returns\nand \000";
48         "a second field"; "a third field" ];
49       [ "a fourth field on a new line" ] ]
50
51 let () =
52   let csv1 = [ [ "a"; "b"; "c"; ""; "" ];
53                [ "f"; "g"; "h"; "i"; "" ];
54                [ "" ];
55                [ ] ] in
56   let csv2 = trim ~top:false ~left:false ~right:true ~bottom:true csv1 in
57   assert (compare csv1 csv2 = 0)
58 let () =
59   let csv1 = [ [ "a"; "b"; "c"; ""; "" ];
60                [ "f"; "g"; "h"; "i"; "" ];
61                [ "" ];
62                [ ] ] in
63   let csv2 = [ [ "a"; "b"; "c"; "d"; "" ];
64                [ "f"; "g"; "h"; "i"; "" ];
65                [ "" ];
66                [ ] ] in
67   assert (compare csv1 csv2 < 0)
68
69
70 ;;
71
72 print_endline "All tests succeeded."