Editing and cleaning of Makefiles of all levels.
[Faustine.git] / interpretor / lib / src / libsndfile-ocaml / test_sndfile.ml
1 (* Rudimentary testing of the Ocaml libsndfile wrapper. *)
2
3 let write_test filename =
4 let fmt = Sndfile.format Sndfile.MAJOR_WAV Sndfile.MINOR_PCM_16 in
5 let file = Sndfile.openfile ~info:(Sndfile.WRITE, fmt, 2, 44100) filename in
6 let writecount = Sndfile.write file [| 0.0 ; 0.0 ; 0.0 ; 0.0 ; 0.0 ; 0.0 ; 0.0 ; 0.5 |] in
7 Printf.printf "Wrote %d items.\n" writecount ;
8 Sndfile.close file
9
10 let read_test filename =
11 let file = Sndfile.openfile filename in
12 Printf.printf "File contains %Ld frames.\n" (Sndfile.frames file) ;
13 let data = Array.create 100 0.0 in
14 let readcount = Sndfile.read file data in
15 Printf.printf "Read %d items.\n" readcount ;
16 Sndfile.close file
17
18 let finalize_test filename =
19 let sub_open_file =
20 let file = Sndfile.openfile filename in
21 ignore file
22 in
23 (* Compact the heap. *)
24 Gc.compact () ;
25 let pre_stat = Gc.stat () in
26 sub_open_file ;
27 (* Compact the heap again. *)
28 Gc.compact () ;
29 (* Compare before and after. *)
30 let post_stat = Gc.stat () in
31 if pre_stat.Gc.heap_words != post_stat.Gc.heap_words then
32 ( Printf.printf "\nFinalize not working : before %d -> after %d\n\n" pre_stat.Gc.heap_words post_stat.Gc.heap_words ;
33 exit 1
34 )
35 else ()
36
37 let bad_read_test filename =
38 try
39 let file = Sndfile.openfile filename in
40 ignore file ;
41 print_endline "Ooops, this should have failed." ;
42 exit 1
43 with
44 Sndfile.Error (e, s) ->
45 if s = "System error." then () else
46 ( Printf.printf "Bad error '%s'\n" s ;
47 exit 1
48 )
49
50
51 let _ =
52 print_endline "------------------------" ;
53 let filename = "a.wav" in
54 write_test filename ;
55 read_test filename ;
56 finalize_test filename ;
57 bad_read_test "this_file_does_not_exist.wav" ;
58 print_endline "Done : All passed."
59