1 from __future__
import print_function
3 from .base
import TestBase
4 from ..core
import MemoryBuffer
5 from ..core
import PassRegistry
6 from ..core
import Context
7 from ..core
import Module
8 from ..core
import Enums
9 from ..core
import OpCode
10 from ..bit_reader
import parse_bitcode
12 class TestCore(TestBase
):
13 def test_enumerations(self
):
14 for enum_cls
, enum_spec
in Enums
:
15 for enum_name
, enum_value
in enum_spec
:
16 # First make sure that enum_cls has the name of the enum as an
17 # attribute. People will access these values as
19 self
.assertTrue(hasattr(enum_cls
, enum_name
))
20 v_attr
= getattr(enum_cls
, enum_name
)
21 self
.assertTrue(isinstance(v_attr
, enum_cls
))
23 # Then make sure that the value returned for this attribute is
24 # correct in both ways.
25 self
.assertEqual(v_attr
.value
, enum_value
)
27 e
= enum_cls
.from_value(enum_value
)
28 self
.assertTrue(isinstance(e
, enum_cls
))
29 self
.assertEqual(e
, v_attr
)
31 def test_memory_buffer_create_from_file(self
):
32 source
= self
.get_test_file()
34 MemoryBuffer(filename
=source
)
36 def test_memory_buffer_failing(self
):
37 with self
.assertRaises(Exception):
38 MemoryBuffer(filename
="/hopefully/this/path/doesnt/exist")
40 def test_memory_buffer_len(self
):
41 source
= self
.get_test_file()
42 m
= MemoryBuffer(filename
=source
)
43 self
.assertEqual(len(m
), 50)
45 def test_create_passregistry(self
):
48 def test_create_context(self
):
49 Context
.GetGlobalContext()
51 def test_create_module_with_name(self
):
52 # Make sure we can not create a module without a LLVMModuleRef.
53 with self
.assertRaises(TypeError):
55 m
= Module
.CreateWithName("test-module")
57 def test_module_getset_datalayout(self
):
58 m
= Module
.CreateWithName("test-module")
59 dl
= "e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:32:64-v128:32:128-a0:0:32-n32-S32"
61 self
.assertEqual(m
.datalayout
, dl
)
63 def test_module_getset_target(self
):
64 m
= Module
.CreateWithName("test-module")
65 target
= "thumbv7-apple-ios5.0.0"
67 self
.assertEqual(m
.target
, target
)
69 def test_module_print_module_to_file(self
):
70 m
= Module
.CreateWithName("test")
71 dl
= "e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:32:64-v128:32:128-a0:0:32-n32-S32"
73 target
= "thumbv7-apple-ios5.0.0"
75 m
.print_module_to_file("test2.ll")
77 def test_module_function_iteration(self
):
78 m
= parse_bitcode(MemoryBuffer(filename
=self
.get_test_bc()))
80 functions
= ["f", "f2", "f3", "f4", "f5", "f6", "g1", "g2", "h1", "h2",
84 self
.assertEqual(f
.name
, functions
[i
])
90 self
.assertEqual(f
.name
, functions
[i
])
93 def test_function_basicblock_iteration(self
):
94 m
= parse_bitcode(MemoryBuffer(filename
=self
.get_test_bc()))
97 bb_list
= ['b1', 'b2', 'end']
100 while f
.name
!= "f6":
105 self
.assertEqual(bb
.name
, bb_list
[i
])
110 for bb
in reversed(f
):
112 self
.assertEqual(bb
.name
, bb_list
[i
])
115 def test_basicblock_instruction_iteration(self
):
116 m
= parse_bitcode(MemoryBuffer(filename
=self
.get_test_bc()))
119 inst_list
= [('arg1', OpCode
.ExtractValue
),
120 ('arg2', OpCode
.ExtractValue
),
128 self
.assertEqual(inst
.name
, inst_list
[i
][0])
129 self
.assertEqual(inst
.opcode
, inst_list
[i
][1])
130 for op
in range(len(inst
)):
131 o
= inst
.get_operand(op
)
138 for inst
in reversed(bb
):
140 self
.assertEqual(inst
.name
, inst_list
[i
][0])
141 self
.assertEqual(inst
.opcode
, inst_list
[i
][1])