aboutsummaryrefslogtreecommitdiffstats
path: root/bindings/python/llvm/tests
diff options
context:
space:
mode:
Diffstat (limited to 'bindings/python/llvm/tests')
-rw-r--r--bindings/python/llvm/tests/base.py6
-rw-r--r--bindings/python/llvm/tests/test.bcbin0 -> 1344 bytes
-rw-r--r--bindings/python/llvm/tests/test_bitreader.py15
-rw-r--r--bindings/python/llvm/tests/test_core.py109
-rw-r--r--bindings/python/llvm/tests/test_disassembler.py4
-rw-r--r--bindings/python/llvm/tests/test_file1
6 files changed, 134 insertions, 1 deletions
diff --git a/bindings/python/llvm/tests/base.py b/bindings/python/llvm/tests/base.py
index ff9eb2f..194f1a4 100644
--- a/bindings/python/llvm/tests/base.py
+++ b/bindings/python/llvm/tests/base.py
@@ -30,3 +30,9 @@ class TestBase(unittest.TestCase):
raise Exception('No suitable test binaries available!')
get_test_binary.__test__ = False
+
+ def get_test_file(self):
+ return os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_file")
+
+ def get_test_bc(self):
+ return os.path.join(os.path.dirname(os.path.abspath(__file__)), "test.bc")
diff --git a/bindings/python/llvm/tests/test.bc b/bindings/python/llvm/tests/test.bc
new file mode 100644
index 0000000..8d3d28f
--- /dev/null
+++ b/bindings/python/llvm/tests/test.bc
Binary files differ
diff --git a/bindings/python/llvm/tests/test_bitreader.py b/bindings/python/llvm/tests/test_bitreader.py
new file mode 100644
index 0000000..d585009
--- /dev/null
+++ b/bindings/python/llvm/tests/test_bitreader.py
@@ -0,0 +1,15 @@
+from .base import TestBase
+from ..core import OpCode
+from ..core import MemoryBuffer
+from ..core import PassRegistry
+from ..core import Context
+from ..core import Module
+from ..bit_reader import parse_bitcode
+
+class TestBitReader(TestBase):
+
+ def test_parse_bitcode(self):
+ source = self.get_test_bc()
+ m = parse_bitcode(MemoryBuffer(filename=source))
+ print m.target
+ print m.datalayout
diff --git a/bindings/python/llvm/tests/test_core.py b/bindings/python/llvm/tests/test_core.py
index 545abc8..63f84c8 100644
--- a/bindings/python/llvm/tests/test_core.py
+++ b/bindings/python/llvm/tests/test_core.py
@@ -1,6 +1,10 @@
from .base import TestBase
from ..core import OpCode
from ..core import MemoryBuffer
+from ..core import PassRegistry
+from ..core import Context
+from ..core import Module
+from ..bit_reader import parse_bitcode
class TestCore(TestBase):
def test_opcode(self):
@@ -13,7 +17,7 @@ class TestCore(TestBase):
self.assertEqual(op, OpCode.Ret)
def test_memory_buffer_create_from_file(self):
- source = self.get_test_binary()
+ source = self.get_test_file()
MemoryBuffer(filename=source)
@@ -21,3 +25,106 @@ class TestCore(TestBase):
with self.assertRaises(Exception):
MemoryBuffer(filename="/hopefully/this/path/doesnt/exist")
+ def test_memory_buffer_len(self):
+ source = self.get_test_file()
+ m = MemoryBuffer(filename=source)
+ self.assertEqual(len(m), 50)
+
+ def test_create_passregistry(self):
+ PassRegistry()
+
+ def test_create_context(self):
+ Context.GetGlobalContext()
+
+ def test_create_module_with_name(self):
+ # Make sure we can not create a module without a LLVMModuleRef.
+ with self.assertRaises(TypeError):
+ m = Module()
+ m = Module.CreateWithName("test-module")
+
+ def test_module_getset_datalayout(self):
+ m = Module.CreateWithName("test-module")
+ 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"
+ m.datalayout = dl
+ self.assertEqual(m.datalayout, dl)
+
+ def test_module_getset_target(self):
+ m = Module.CreateWithName("test-module")
+ target = "thumbv7-apple-ios5.0.0"
+ m.target = target
+ self.assertEqual(m.target, target)
+
+ def test_module_print_module_to_file(self):
+ m = Module.CreateWithName("test")
+ 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"
+ m.datalayout = dl
+ target = "thumbv7-apple-ios5.0.0"
+ m.target = target
+ m.print_module_to_file("test2.ll")
+
+ def test_module_function_iteration(self):
+ m = parse_bitcode(MemoryBuffer(filename=self.get_test_bc()))
+ i = 0
+ functions = ["f", "f2", "f3", "f4", "f5", "f6", "g1", "g2", "h1", "h2",
+ "h3"]
+ # Forward
+ for f in m:
+ self.assertEqual(f.name, functions[i])
+ f.dump()
+ i += 1
+ # Backwards
+ for f in reversed(m):
+ i -= 1
+ self.assertEqual(f.name, functions[i])
+ f.dump()
+
+ def test_function_basicblock_iteration(self):
+ m = parse_bitcode(MemoryBuffer(filename=self.get_test_bc()))
+ i = 0
+
+ bb_list = ['b1', 'b2', 'end']
+
+ f = m.first
+ while f.name != "f6":
+ f = f.next
+
+ # Forward
+ for bb in f:
+ self.assertEqual(bb.name, bb_list[i])
+ bb.dump()
+ i += 1
+
+ # Backwards
+ for bb in reversed(f):
+ i -= 1
+ self.assertEqual(bb.name, bb_list[i])
+ bb.dump()
+
+ def test_basicblock_instruction_iteration(self):
+ m = parse_bitcode(MemoryBuffer(filename=self.get_test_bc()))
+ i = 0
+
+ inst_list = [('arg1', OpCode.ExtractValue),
+ ('arg2', OpCode.ExtractValue),
+ ('', OpCode.Call),
+ ('', OpCode.Ret)]
+
+ bb = m.first.first
+
+ # Forward
+ for inst in bb:
+ self.assertEqual(inst.name, inst_list[i][0])
+ self.assertEqual(inst.opcode, inst_list[i][1])
+ for op in range(len(inst)):
+ o = inst.get_operand(op)
+ print o.name
+ o.dump()
+ inst.dump()
+ i += 1
+
+ # Backwards
+ for inst in reversed(bb):
+ i -= 1
+ self.assertEqual(inst.name, inst_list[i][0])
+ self.assertEqual(inst.opcode, inst_list[i][1])
+ inst.dump()
diff --git a/bindings/python/llvm/tests/test_disassembler.py b/bindings/python/llvm/tests/test_disassembler.py
index 46d12f7..e960dc0 100644
--- a/bindings/python/llvm/tests/test_disassembler.py
+++ b/bindings/python/llvm/tests/test_disassembler.py
@@ -16,6 +16,10 @@ class TestDisassembler(TestBase):
self.assertEqual(count, 3)
self.assertEqual(s, '\tjcxz\t-127')
+ def test_nonexistant_triple(self):
+ with self.assertRaisesRegexp(Exception, "Could not obtain disassembler for triple"):
+ Disassembler("nonexistant-triple-raises")
+
def test_get_instructions(self):
sequence = '\x67\xe3\x81\x01\xc7' # jcxz -127; addl %eax, %edi
diff --git a/bindings/python/llvm/tests/test_file b/bindings/python/llvm/tests/test_file
new file mode 100644
index 0000000..6c9b038
--- /dev/null
+++ b/bindings/python/llvm/tests/test_file
@@ -0,0 +1 @@
+I,"cAGxqԐdvl\L>g>``wɩ \ No newline at end of file