Merge branch 'master' into msp430
[llvm/msp430.git] / test / Bindings / Ocaml / bitreader.ml
blob776228fc164889834edad02fe253a46d88accce2
1 (* RUN: %ocamlc -warn-error A llvm.cma llvm_bitreader.cma llvm_bitwriter.cma %s -o %t 2> /dev/null
2 * RUN: ./%t %t.bc
3 * RUN: llvm-dis < %t.bc | grep caml_int_ty
4 *)
6 (* Note that this takes a moment to link, so it's best to keep the number of
7 individual tests low. *)
9 let test x = if not x then exit 1 else ()
11 let _ =
12 let fn = Sys.argv.(1) in
13 let m = Llvm.create_module "ocaml_test_module" in
15 ignore (Llvm.define_type_name "caml_int_ty" Llvm.i32_type m);
17 test (Llvm_bitwriter.write_bitcode_file m fn);
19 Llvm.dispose_module m;
21 (* parse_bitcode *)
22 begin
23 let mb = Llvm.MemoryBuffer.of_file fn in
24 begin try
25 let m = Llvm_bitreader.parse_bitcode mb in
26 Llvm.dispose_module m
27 with x ->
28 Llvm.MemoryBuffer.dispose mb;
29 raise x
30 end
31 end;
33 (* MemoryBuffer.of_file *)
34 test begin try
35 let mb = Llvm.MemoryBuffer.of_file (fn ^ ".bogus") in
36 Llvm.MemoryBuffer.dispose mb;
37 false
38 with Llvm.IoError _ ->
39 true
40 end;
42 (* get_module_provider *)
43 begin
44 let mb = Llvm.MemoryBuffer.of_file fn in
45 let mp = begin try
46 Llvm_bitreader.get_module_provider mb
47 with x ->
48 Llvm.MemoryBuffer.dispose mb;
49 raise x
50 end in
51 Llvm.ModuleProvider.dispose mp
52 end;
54 (* corrupt the bitcode *)
55 let fn = fn ^ ".txt" in
56 begin let oc = open_out fn in
57 output_string oc "not a bitcode file\n";
58 close_out oc
59 end;
61 (* test get_module_provider exceptions *)
62 test begin
63 try
64 let mb = Llvm.MemoryBuffer.of_file fn in
65 let mp = begin try
66 Llvm_bitreader.get_module_provider mb
67 with x ->
68 Llvm.MemoryBuffer.dispose mb;
69 raise x
70 end in
71 Llvm.ModuleProvider.dispose mp;
72 false
73 with Llvm_bitreader.Error _ ->
74 true
75 end