Fixed some bugs.
[llvm/zpu.git] / test / Bindings / Ocaml / bitreader.ml
bloba3bd91a0af2ace484baa0adfcba32ff302ea1cd3
1 (* RUN: %ocamlopt -warn-error A llvm.cmxa llvm_bitreader.cmxa llvm_bitwriter.cmxa %s -o %t
2 * RUN: %t %t.bc
3 * RUN: llvm-dis < %t.bc | grep caml_int_ty
4 * XFAIL: vg_leak
5 *)
7 (* Note that this takes a moment to link, so it's best to keep the number of
8 individual tests low. *)
10 let context = Llvm.global_context ()
12 let test x = if not x then exit 1 else ()
14 let _ =
15 let fn = Sys.argv.(1) in
16 let m = Llvm.create_module context "ocaml_test_module" in
18 ignore (Llvm.define_type_name "caml_int_ty" (Llvm.i32_type context) m);
20 test (Llvm_bitwriter.write_bitcode_file m fn);
22 Llvm.dispose_module m;
24 (* parse_bitcode *)
25 begin
26 let mb = Llvm.MemoryBuffer.of_file fn in
27 begin try
28 let m = Llvm_bitreader.parse_bitcode context mb in
29 Llvm.dispose_module m
30 with x ->
31 Llvm.MemoryBuffer.dispose mb;
32 raise x
33 end
34 end;
36 (* MemoryBuffer.of_file *)
37 test begin try
38 let mb = Llvm.MemoryBuffer.of_file (fn ^ ".bogus") in
39 Llvm.MemoryBuffer.dispose mb;
40 false
41 with Llvm.IoError _ ->
42 true
43 end;
45 (* get_module *)
46 begin
47 let mb = Llvm.MemoryBuffer.of_file fn in
48 let m = begin try
49 Llvm_bitreader.get_module context mb
50 with x ->
51 Llvm.MemoryBuffer.dispose mb;
52 raise x
53 end in
54 Llvm.dispose_module m
55 end;
57 (* corrupt the bitcode *)
58 let fn = fn ^ ".txt" in
59 begin let oc = open_out fn in
60 output_string oc "not a bitcode file\n";
61 close_out oc
62 end;
64 (* test get_module exceptions *)
65 test begin
66 try
67 let mb = Llvm.MemoryBuffer.of_file fn in
68 let m = begin try
69 Llvm_bitreader.get_module context mb
70 with x ->
71 Llvm.MemoryBuffer.dispose mb;
72 raise x
73 end in
74 Llvm.dispose_module m;
75 false
76 with Llvm_bitreader.Error _ ->
77 true
78 end