diff --git a/llvm/tests/__init__.py b/llvm/tests/__init__.py index 35670d1..0d95c7b 100644 --- a/llvm/tests/__init__.py +++ b/llvm/tests/__init__.py @@ -5,11 +5,11 @@ import unittest import subprocess import llvm -tests = [] +tests = [] # stores unittest.TestCase objects # Isolated tests # Tests that affect process-wide settings -isolated_tests = [] +isolated_tests = [] # stores modue name def run(verbosity=1): @@ -26,7 +26,8 @@ def run(verbosity=1): suite = unittest.TestSuite() for cls in tests: - suite.addTest(unittest.makeSuite(cls)) + if cls: + suite.addTest(unittest.makeSuite(cls)) # The default stream fails in IPython qtconsole on Windows, # so just using sys.stdout @@ -44,8 +45,10 @@ def run(verbosity=1): for test in isolated_tests: print(('testing %s' % test).center(80)) - subprocess.check_call([sys.executable, '-m', test]) - + term = subprocess.check_output([sys.executable, '-m', test], + stderr=subprocess.STDOUT) + print(term) + return testresult diff --git a/llvm/tests/support.py b/llvm/tests/support.py index a661a85..03c4c3c 100644 --- a/llvm/tests/support.py +++ b/llvm/tests/support.py @@ -1,14 +1,20 @@ +from __future__ import print_function, division import sys +import platform import unittest import contextlib +import types from llvm.tests import tests, isolated_tests # re-expose symbol IS_PY3K = sys.version_info[0] >= 3 BITS = tuple.__itemsize__ * 8 +OS = sys.platform +MACHINE = platform.machine() +INTEL_CPUS = 'i386', 'x86_64' if sys.version_info[:2] <= (2, 6): # create custom TestCase - class TestCase(unittest.TestCase): + class _TestCase(unittest.TestCase): def assertIn(self, item, container): self.assertTrue(item in container) @@ -31,4 +37,42 @@ if sys.version_info[:2] <= (2, 6): raise self.failureException("Did not raise %s" % exc) else: - TestCase = unittest.TestCase + _TestCase = unittest.TestCase + +class TestCase(_TestCase): + def assertClose(self, got, expect): + rel = abs(got - expect) / expect + self.assertTrue(rel < 1e-6, 'relative error = %f' % rel) + +#------------------------------------------------------------------------------- +# Tests decorators + +def _skipped(name, msg): + def _test(self): + if hasattr(unittest, 'SkipTest'): + raise unittest.SkipTest(msg) + else: + print('skipped %s' % name, msg) + return _test + +def skip_if(cond, msg=''): + def skipper(test): + if not isinstance(test, types.FunctionType): + repl = None + else: + repl = _skipped(test, msg) + return repl if cond else test + return skipper + +skip_if_not_64bits = skip_if(BITS != 64, msg='skipped not 64-bit') + +skip_if_not_32bits = skip_if(BITS != 32, msg='skipped not 32-bits') + +skip_if_win32 = skip_if(OS.startswith('win32'), msg='skipped win32') + +skip_if_not_win32 = skip_if(not OS.startswith('win32'), + msg='skipped not win32') +skip_if_not_intel_cpu = skip_if(MACHINE not in INTEL_CPUS, + msg='skipped not Intel CPU') + + diff --git a/llvm/tests/test_arith.py b/llvm/tests/test_arith.py index c6043ff..5c50aeb 100644 --- a/llvm/tests/test_arith.py +++ b/llvm/tests/test_arith.py @@ -2,8 +2,9 @@ import unittest import llvm from llvm.core import (Module, Type, Builder) from llvm.ee import EngineBuilder -from .support import TestCase, tests, BITS +from .support import TestCase, tests, skip_if, skip_if_not_64bits +@skip_if(llvm.version < (3, 3)) class TestArith(TestCase): ''' Test basic arithmetic support with LLVM MCJIT @@ -51,23 +52,21 @@ class TestArith(TestCase): def test_mul(self): self.template('mul', 'fmul') + @skip_if_not_64bits def test_div(self): - if BITS == 32: - print('skipped test for div') - print('known failure due to unresolved external symbol __udivdi3') - return + ''' + known failure due to unresolved external symbol __udivdi3 + ''' self.template('udiv', None) # 'fdiv') + @skip_if_not_64bits def test_rem(self): - if BITS == 32: - print('skipped test for rem') - print('known failure due to unresolved external symbol __umoddi3') - return + ''' + known failure due to unresolved external symbol __umoddi3 + ''' self.template('urem', None) # 'frem') -if llvm.version >= (3, 3): - # MCJIT is broken in 3.2 - tests.append(TestArith) +tests.append(TestArith) if __name__ == '__main__': unittest.main() diff --git a/llvm/tests/test_cpu_support.py b/llvm/tests/test_cpu_support.py index 3ec9c9c..7874706 100644 --- a/llvm/tests/test_cpu_support.py +++ b/llvm/tests/test_cpu_support.py @@ -5,8 +5,10 @@ from llvm.core import (Module, Type, Function, Builder, from llvm.ee import EngineBuilder import llvm.core as lc import llvm.ee as le -from .support import TestCase, tests +from llvm.workaround.avx_support import detect_avx_support +from .support import TestCase, tests, skip_if_not_intel_cpu, skip_if +@skip_if_not_intel_cpu class TestCPUSupport(TestCase): def _build_test_module(self): @@ -73,15 +75,12 @@ class TestCPUSupport(TestCase): print('disable mattrs', mattrs) self._template(mattrs) + @skip_if(not detect_avx_support(), msg="no AVX support") def test_cpu_support6(self): features = [] - from llvm.workaround.avx_support import detect_avx_support - if not detect_avx_support(): - print('Skipping: no AVX') - else: - mattrs = ','.join(map(lambda s: '-%s' % s, features)) - print('disable mattrs', mattrs) - self._template(mattrs) + mattrs = ','.join(map(lambda s: '-%s' % s, features)) + print('disable mattrs', mattrs) + self._template(mattrs) tests.append(TestCPUSupport) diff --git a/llvm/tests/test_intel_native_asm.py b/llvm/tests/test_intel_native_asm.py new file mode 100644 index 0000000..088413a --- /dev/null +++ b/llvm/tests/test_intel_native_asm.py @@ -0,0 +1,31 @@ +import sys +import os +import unittest +from llvm.core import Builder, Module, Type +import llvm.core as lc +from .support import TestCase, skip_if_not_intel_cpu, isolated_tests + +@skip_if_not_intel_cpu +class TestNativeAsm(TestCase): + + def test_asm(self): + m = Module.new('module1') + + foo = m.add_function(Type.function(Type.int(), + [Type.int(), Type.int()]), + name="foo") + bldr = Builder.new(foo.append_basic_block('entry')) + x = bldr.add(foo.args[0], foo.args[1]) + bldr.ret(x) + + att_syntax = m.to_native_assembly() + os.environ["LLVMPY_OPTIONS"] = "-x86-asm-syntax=intel" + lc.parse_environment_options(sys.argv[0], "LLVMPY_OPTIONS") + intel_syntax = m.to_native_assembly() + + self.assertNotEqual(att_syntax, intel_syntax) + +isolated_tests.append(__name__) + +if __name__ == '__main__': + unittest.main() diff --git a/llvm/tests/test_native.py b/llvm/tests/test_native.py index d7afea9..63eab5a 100644 --- a/llvm/tests/test_native.py +++ b/llvm/tests/test_native.py @@ -43,9 +43,9 @@ class TestNative(TestCase): self.assertEqual(s, 0xab) def test_assembly(self): - if sys.platform == 'darwin': - # skip this test on MacOSX for now - return + # if sys.platform == 'darwin': + # # skip this test on MacOSX for now + # return m = self._make_module() output = m.to_native_assembly() @@ -60,9 +60,9 @@ class TestNative(TestCase): self._compile(src) def test_object(self): - if sys.platform == 'darwin': - # skip this test on MacOSX for now - return + # if sys.platform == 'darwin': + # # skip this test on MacOSX for now + # return m = self._make_module() output = m.to_native_object() diff --git a/llvm/tests/test_struct_args.py b/llvm/tests/test_struct_args.py index 8683a6e..5e48ea8 100644 --- a/llvm/tests/test_struct_args.py +++ b/llvm/tests/test_struct_args.py @@ -6,6 +6,9 @@ from ctypes import Structure, c_float, c_double, c_uint8, CFUNCTYPE from llvm import core as lc from llvm import ee as le +from .support import (skip_if_win32, skip_if_not_win32, skip_if_not_32bits, + skip_if_not_64bits, skip_if_not_intel_cpu, TestCase) + class TwoDoubleOneByte(Structure): _fields_ = ('x', c_double), ('y', c_double), ('z', c_uint8) @@ -30,32 +33,9 @@ class OneByte(Structure): def __repr__(self): return '' % (self.x,) -TM = le.TargetMachine.new() -POINTER_BITSIZE = TM.target_data.pointer_size * 8 - -def skip_if_not_64bits(fn): - if POINTER_BITSIZE == 64: - return fn - -def skip_if_not_32bits(fn): - if POINTER_BITSIZE == 32: - return fn - -def skip_if_not_system_v(cls): - if not sys.platform.startswith('win32'): - return cls - -def skip_if_not_win32(cls): - if sys.platform.startswith('win32'): - return cls - -class FloatTestMixin(object): - def assertClose(self, got, expect): - rel = abs(got - expect) / float(expect) - self.assertTrue(rel < 1e-6, 'relative error = %f' % rel) - -@skip_if_not_system_v -class TestStructSystemVABI(unittest.TestCase, FloatTestMixin): +@skip_if_not_intel_cpu +@skip_if_win32 +class TestStructSystemVABI(TestCase): ''' Non microsoft convention ''' @@ -169,8 +149,6 @@ class TestStructSystemVABI(unittest.TestCase, FloatTestMixin): float_type = lc.Type.float() struct_type = lc.Type.vector(float_type, 2) - print('ABI size', - TM.target_data.abi_size(lc.Type.struct([float_type, float_type]))) func_type = lc.Type.function(struct_type, [struct_type]) func = m.add_function(func_type, name='foo') @@ -370,11 +348,11 @@ class TestStructSystemVABI(unittest.TestCase, FloatTestMixin): self.assertEqual(arg.x * arg.x, ret.x) -if not sys.platform.startswith('win32'): - tests.append(TestStructSystemVABI) +tests.append(TestStructSystemVABI) +@skip_if_not_intel_cpu @skip_if_not_win32 -class TestStructMicrosoftABI(unittest.TestCase, FloatTestMixin): +class TestStructMicrosoftABI(TestCase): ''' Microsoft convention ''' @@ -701,10 +679,7 @@ class TestStructMicrosoftABI(unittest.TestCase, FloatTestMixin): self.assertClose(arg.x / arg.y, ret.y) self.assertEqual(arg.z, ret.z) - - -if sys.platform.startswith('win32'): - tests.append(TestStructMicrosoftABI) +tests.append(TestStructMicrosoftABI) if __name__ == "__main__": unittest.main() diff --git a/llvm/tests/test_target_machines.py b/llvm/tests/test_target_machines.py index e7ea7de..2bbe76e 100644 --- a/llvm/tests/test_target_machines.py +++ b/llvm/tests/test_target_machines.py @@ -2,7 +2,15 @@ import unittest import llvm.core as lc import llvm.ee as le from llvm.core import Type, Builder -from .support import TestCase, tests +from .support import TestCase, tests, skip_if + +# Check PTX backend +if le.initialize_target('PTX', noraise=True): + PTX_ARCH = 'ptx64' +elif le.initialize_target('NVPTX', noraise=True): + PTX_ARCH = 'nvptx64' +else: + PTX_ARCH = None class TestTargetMachines(TestCase): '''Exercise target machines @@ -20,14 +28,9 @@ class TestTargetMachines(TestCase): self.assertIn('foo', tm.emit_assembly(m)) self.assertTrue(le.get_host_cpu_name()) + @skip_if(not PTX_ARCH, msg='LLVM is not compiled with PTX enabled') def test_ptx(self): - if le.initialize_target('PTX', noraise=True): - arch = 'ptx64' - elif le.initialize_target('NVPTX', noraise=True): - arch = 'nvptx64' - else: - return # skip this test - + arch = PTX_ARCH print(arch) m, func = self._build_module() func.calling_convention = lc.CC_PTX_KERNEL # set calling conv @@ -51,9 +54,6 @@ class TestTargetMachines(TestCase): m.verify() return m, func - def _build_bad_archname(self): - with self.assertRaises(RuntimeError): - le.TargetMachine.lookup("ain't no arch name") tests.append(TestTargetMachines)