Add test skipping to disable arch specific tests conditionally

This commit is contained in:
Siu Kwan Lam 2013-10-05 13:37:04 +08:00
commit a82e876f93
8 changed files with 130 additions and 79 deletions

View file

@ -5,11 +5,11 @@ import unittest
import subprocess import subprocess
import llvm import llvm
tests = [] tests = [] # stores unittest.TestCase objects
# Isolated tests # Isolated tests
# Tests that affect process-wide settings # Tests that affect process-wide settings
isolated_tests = [] isolated_tests = [] # stores modue name
def run(verbosity=1): def run(verbosity=1):
@ -26,7 +26,8 @@ def run(verbosity=1):
suite = unittest.TestSuite() suite = unittest.TestSuite()
for cls in tests: 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, # The default stream fails in IPython qtconsole on Windows,
# so just using sys.stdout # so just using sys.stdout
@ -44,8 +45,10 @@ def run(verbosity=1):
for test in isolated_tests: for test in isolated_tests:
print(('testing %s' % test).center(80)) 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 return testresult

View file

@ -1,14 +1,20 @@
from __future__ import print_function, division
import sys import sys
import platform
import unittest import unittest
import contextlib import contextlib
import types
from llvm.tests import tests, isolated_tests # re-expose symbol from llvm.tests import tests, isolated_tests # re-expose symbol
IS_PY3K = sys.version_info[0] >= 3 IS_PY3K = sys.version_info[0] >= 3
BITS = tuple.__itemsize__ * 8 BITS = tuple.__itemsize__ * 8
OS = sys.platform
MACHINE = platform.machine()
INTEL_CPUS = 'i386', 'x86_64'
if sys.version_info[:2] <= (2, 6): if sys.version_info[:2] <= (2, 6):
# create custom TestCase # create custom TestCase
class TestCase(unittest.TestCase): class _TestCase(unittest.TestCase):
def assertIn(self, item, container): def assertIn(self, item, container):
self.assertTrue(item in 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) raise self.failureException("Did not raise %s" % exc)
else: 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')

View file

@ -2,8 +2,9 @@ import unittest
import llvm import llvm
from llvm.core import (Module, Type, Builder) from llvm.core import (Module, Type, Builder)
from llvm.ee import EngineBuilder 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): class TestArith(TestCase):
''' '''
Test basic arithmetic support with LLVM MCJIT Test basic arithmetic support with LLVM MCJIT
@ -51,23 +52,21 @@ class TestArith(TestCase):
def test_mul(self): def test_mul(self):
self.template('mul', 'fmul') self.template('mul', 'fmul')
@skip_if_not_64bits
def test_div(self): def test_div(self):
if BITS == 32: '''
print('skipped test for div') known failure due to unresolved external symbol __udivdi3
print('known failure due to unresolved external symbol __udivdi3') '''
return
self.template('udiv', None) # 'fdiv') self.template('udiv', None) # 'fdiv')
@skip_if_not_64bits
def test_rem(self): def test_rem(self):
if BITS == 32: '''
print('skipped test for rem') known failure due to unresolved external symbol __umoddi3
print('known failure due to unresolved external symbol __umoddi3') '''
return
self.template('urem', None) # 'frem') self.template('urem', None) # 'frem')
if llvm.version >= (3, 3): tests.append(TestArith)
# MCJIT is broken in 3.2
tests.append(TestArith)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -5,8 +5,10 @@ from llvm.core import (Module, Type, Function, Builder,
from llvm.ee import EngineBuilder from llvm.ee import EngineBuilder
import llvm.core as lc import llvm.core as lc
import llvm.ee as le 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): class TestCPUSupport(TestCase):
def _build_test_module(self): def _build_test_module(self):
@ -73,15 +75,12 @@ class TestCPUSupport(TestCase):
print('disable mattrs', mattrs) print('disable mattrs', mattrs)
self._template(mattrs) self._template(mattrs)
@skip_if(not detect_avx_support(), msg="no AVX support")
def test_cpu_support6(self): def test_cpu_support6(self):
features = [] features = []
from llvm.workaround.avx_support import detect_avx_support mattrs = ','.join(map(lambda s: '-%s' % s, features))
if not detect_avx_support(): print('disable mattrs', mattrs)
print('Skipping: no AVX') self._template(mattrs)
else:
mattrs = ','.join(map(lambda s: '-%s' % s, features))
print('disable mattrs', mattrs)
self._template(mattrs)
tests.append(TestCPUSupport) tests.append(TestCPUSupport)

View file

@ -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()

View file

@ -43,9 +43,9 @@ class TestNative(TestCase):
self.assertEqual(s, 0xab) self.assertEqual(s, 0xab)
def test_assembly(self): def test_assembly(self):
if sys.platform == 'darwin': # if sys.platform == 'darwin':
# skip this test on MacOSX for now # # skip this test on MacOSX for now
return # return
m = self._make_module() m = self._make_module()
output = m.to_native_assembly() output = m.to_native_assembly()
@ -60,9 +60,9 @@ class TestNative(TestCase):
self._compile(src) self._compile(src)
def test_object(self): def test_object(self):
if sys.platform == 'darwin': # if sys.platform == 'darwin':
# skip this test on MacOSX for now # # skip this test on MacOSX for now
return # return
m = self._make_module() m = self._make_module()
output = m.to_native_object() output = m.to_native_object()

View file

@ -6,6 +6,9 @@ from ctypes import Structure, c_float, c_double, c_uint8, CFUNCTYPE
from llvm import core as lc from llvm import core as lc
from llvm import ee as le 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): class TwoDoubleOneByte(Structure):
_fields_ = ('x', c_double), ('y', c_double), ('z', c_uint8) _fields_ = ('x', c_double), ('y', c_double), ('z', c_uint8)
@ -30,32 +33,9 @@ class OneByte(Structure):
def __repr__(self): def __repr__(self):
return '<x=%d>' % (self.x,) return '<x=%d>' % (self.x,)
TM = le.TargetMachine.new() @skip_if_not_intel_cpu
POINTER_BITSIZE = TM.target_data.pointer_size * 8 @skip_if_win32
class TestStructSystemVABI(TestCase):
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):
''' '''
Non microsoft convention Non microsoft convention
''' '''
@ -169,8 +149,6 @@ class TestStructSystemVABI(unittest.TestCase, FloatTestMixin):
float_type = lc.Type.float() float_type = lc.Type.float()
struct_type = lc.Type.vector(float_type, 2) 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_type = lc.Type.function(struct_type, [struct_type])
func = m.add_function(func_type, name='foo') 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) 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 @skip_if_not_win32
class TestStructMicrosoftABI(unittest.TestCase, FloatTestMixin): class TestStructMicrosoftABI(TestCase):
''' '''
Microsoft convention Microsoft convention
''' '''
@ -701,10 +679,7 @@ class TestStructMicrosoftABI(unittest.TestCase, FloatTestMixin):
self.assertClose(arg.x / arg.y, ret.y) self.assertClose(arg.x / arg.y, ret.y)
self.assertEqual(arg.z, ret.z) self.assertEqual(arg.z, ret.z)
tests.append(TestStructMicrosoftABI)
if sys.platform.startswith('win32'):
tests.append(TestStructMicrosoftABI)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View file

@ -2,7 +2,15 @@ import unittest
import llvm.core as lc import llvm.core as lc
import llvm.ee as le import llvm.ee as le
from llvm.core import Type, Builder 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): class TestTargetMachines(TestCase):
'''Exercise target machines '''Exercise target machines
@ -20,14 +28,9 @@ class TestTargetMachines(TestCase):
self.assertIn('foo', tm.emit_assembly(m)) self.assertIn('foo', tm.emit_assembly(m))
self.assertTrue(le.get_host_cpu_name()) self.assertTrue(le.get_host_cpu_name())
@skip_if(not PTX_ARCH, msg='LLVM is not compiled with PTX enabled')
def test_ptx(self): def test_ptx(self):
if le.initialize_target('PTX', noraise=True): arch = PTX_ARCH
arch = 'ptx64'
elif le.initialize_target('NVPTX', noraise=True):
arch = 'nvptx64'
else:
return # skip this test
print(arch) print(arch)
m, func = self._build_module() m, func = self._build_module()
func.calling_convention = lc.CC_PTX_KERNEL # set calling conv func.calling_convention = lc.CC_PTX_KERNEL # set calling conv
@ -51,9 +54,6 @@ class TestTargetMachines(TestCase):
m.verify() m.verify()
return m, func return m, func
def _build_bad_archname(self):
with self.assertRaises(RuntimeError):
le.TargetMachine.lookup("ain't no arch name")
tests.append(TestTargetMachines) tests.append(TestTargetMachines)