From 501c3d0f614a6252865028871a28bcfaf318c730 Mon Sep 17 00:00:00 2001 From: Ilan Schnell Date: Sun, 19 Aug 2012 17:08:34 -0500 Subject: [PATCH] move native tests --- llvm/test_llvmpy.py | 51 +++++++++++++++++++- test/native.py | 53 -------------------- test/objcache.py | 115 -------------------------------------------- 3 files changed, 50 insertions(+), 169 deletions(-) delete mode 100644 test/native.py delete mode 100644 test/objcache.py diff --git a/llvm/test_llvmpy.py b/llvm/test_llvmpy.py index f1f2a56..f0e5143 100644 --- a/llvm/test_llvmpy.py +++ b/llvm/test_llvmpy.py @@ -4,6 +4,7 @@ LLVM tests import os import sys import unittest +import subprocess is_py3k = bool(sys.version_info[0] == 3) @@ -14,7 +15,8 @@ else: from llvm import __version__ -from llvm.core import Module, Type, GlobalVariable, Function, Builder +from llvm.core import (Module, Type, GlobalVariable, Function, Builder, + Constant) import llvm.passes as lp import llvm.ee as le @@ -290,6 +292,53 @@ tests.append(TestObjCache) # --------------------------------------------------------------------------- +class TestNative(unittest.TestCase): + + def _make_module(self): + m = Module.new('module1') + m.add_global_variable(Type.int(), 'i') + + fty = Type.function(Type.int(), []) + f = m.add_function(fty, name='main') + + bldr = Builder.new(f.append_basic_block('entry')) + bldr.ret(Constant.int(Type.int(), 0xab)) + + return m + + def _compile(self, src): + dst = '/tmp/llvmobj.out' + s = subprocess.call(['cc', '-o', dst, src]) + if s != 0: + raise Exception("Cannot compile") + + s = subprocess.call([dst]) + self.assertEqual(s, 0xab) + + def test_assembly(self): + m = self._make_module() + output = m.to_native_assembly() + + src = '/tmp/llvmasm.s' + with open(src, 'wb') as fout: + fout.write(output) + + self._compile(src) + + def test_object(self): + m = self._make_module() + output = m.to_native_object() + + src = '/tmp/llvmobj.o' + with open(src, 'wb') as fout: + fout.write(output) + + self._compile(src) + +tests.append(TestNative) + +# --------------------------------------------------------------------------- + def run(verbosity=1): print('llvmpy is installed in: ' + os.path.dirname(__file__)) print('llvmpy version: ' + __version__) diff --git a/test/native.py b/test/native.py deleted file mode 100644 index f4287c9..0000000 --- a/test/native.py +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/env python - -from llvm import * -from llvm.core import * - -import unittest, subprocess - -class TestNative(unittest.TestCase): - - def _make_module(self): - m = Module.new('module1') - m.add_global_variable(Type.int(), 'i') - - fty = Type.function(Type.int(), []) - f = m.add_function(fty, name='main') - - bldr = Builder.new(f.append_basic_block('entry')) - bldr.ret(Constant.int(Type.int(), 0xab)) - - return m - - def _compile(self, src): - dst = '/tmp/llvmobj.out' - s = subprocess.call(['cc', '-o', dst, src]) - if s != 0: - raise Exception("Cannot compile") - - s = subprocess.call([dst]) - self.assertEqual(s, 0xab) - - - def test_assembly(self): - m = self._make_module() - output = m.to_native_assembly() - - src = '/tmp/llvmasm.s' - with open(src, 'wb') as fout: - fout.write(output) - - self._compile(src) - - def test_object(self): - m = self._make_module() - output = m.to_native_object() - - src = '/tmp/llvmobj.o' - with open(src, 'wb') as fout: - fout.write(output) - - self._compile(src) - -if __name__ == '__main__': - unittest.main() diff --git a/test/objcache.py b/test/objcache.py deleted file mode 100644 index c8bc7e4..0000000 --- a/test/objcache.py +++ /dev/null @@ -1,115 +0,0 @@ -#!/usr/bin/env python - -from llvm.core import * - -import logging, sys, unittest - -class TestObjCache(unittest.TestCase): - - if sys.version_info[:2] < (2, 7): - def assertIs(self, expr1, expr2, msg=None): - if expr1 is not expr2: - standardMsg = '%s is not %s' % (safe_repr(expr1), - safe_repr(expr2)) - self.fail(self._formatMessage(msg, standardMsg)) - - def test_objcache(self): - logging.debug("Testing module aliasing ..") - m1 = Module.new('a') - t = Type.int() - ft = Type.function(t, [t]) - f1 = m1.add_function(ft, "func") - m2 = f1.module - self.assertIs(m1, m2) - - logging.debug("Testing global vairable aliasing 1 .. ") - gv1 = GlobalVariable.new(m1, t, "gv") - gv2 = GlobalVariable.get(m1, "gv") - self.assertIs(gv1, gv2) - - logging.debug("Testing global vairable aliasing 2 .. ") - gv3 = m1.global_variables[0] - self.assertIs(gv1, gv3) - - logging.debug("Testing global vairable aliasing 3 .. ") - - gv2 = None - gv3 = None - - gv1.delete() - gv4 = GlobalVariable.new(m1, t, "gv") - - self.assert_(gv1 is not gv4) - - logging.debug("Testing function aliasing 1 ..") - b1 = f1.append_basic_block('entry') - f2 = b1.function - self.assertIs(f1, f2) - - logging.debug("Testing function aliasing 2 ..") - f3 = m1.get_function_named("func") - self.assertIs(f1, f3) - - logging.debug("Testing function aliasing 3 ..") - f4 = Function.get_or_insert(m1, ft, "func") - self.assertIs(f1, f4) - - logging.debug("Testing function aliasing 4 ..") - f5 = Function.get(m1, "func") - self.assertIs(f1, f5) - - logging.debug("Testing function aliasing 5 ..") - f6 = m1.get_or_insert_function(ft, "func") - self.assertIs(f1, f6) - - logging.debug("Testing function aliasing 6 ..") - f7 = m1.functions[0] - self.assertIs(f1, f7) - - logging.debug("Testing argument aliasing .. ") - a1 = f1.args[0] - a2 = f1.args[0] - self.assertIs(a1, a2) - - logging.debug("Testing basic block aliasing 1 .. ") - b2 = f1.basic_blocks[0] - self.assertIs(b1, b2) - - logging.debug("Testing basic block aliasing 2 .. ") - b3 = f1.get_entry_basic_block() - self.assertIs(b1, b3) - - logging.debug("Testing basic block aliasing 3 .. ") - b31 = f1.entry_basic_block - self.assertIs(b1, b31) - - logging.debug("Testing basic block aliasing 4 .. ") - bldr = Builder.new(b1) - b4 = bldr.basic_block - self.assertIs(b1, b4) - - logging.debug("Testing basic block aliasing 5 .. ") - i1 = bldr.ret_void() - b5 = i1.basic_block - self.assertIs(b1, b5) - - logging.debug("Testing instruction aliasing 1 .. ") - i2 = b5.instructions[0] - self.assertIs(i1, i2) - - # phi node - phi = bldr.phi(t) - phi.add_incoming(f1.args[0], b1) - v2 = phi.get_incoming_value(0) - b6 = phi.get_incoming_block(0) - - logging.debug("Testing PHI / basic block aliasing 5 .. ") - self.assertIs(b1, b6) - - logging.debug("Testing PHI / value aliasing .. ") - self.assertIs(f1.args[0], v2) - - - -if __name__ == '__main__': - unittest.main()