diff options
| author | Armin Rigo <arigo@tunes.org> | 2012-08-04 17:46:09 +0200 |
|---|---|---|
| committer | Armin Rigo <arigo@tunes.org> | 2012-08-04 17:46:09 +0200 |
| commit | e2024b64eb37c607c0742caa3bb1af682e7855f5 (patch) | |
| tree | 87499ea81aab472fb763bd2f5c146223217ee0ce /testing | |
| parent | 599300811cc1015773147b625d20f3e033865e2f (diff) | |
| download | cffi-e2024b64eb37c607c0742caa3bb1af682e7855f5.tar.gz | |
Merge the two verifiers into two VEngine classes. There is still a little bit
of code duplication but not too much.
Diffstat (limited to 'testing')
| -rw-r--r-- | testing/test_verify.py | 22 | ||||
| -rw-r--r-- | testing/test_vgen.py | 12 | ||||
| -rw-r--r-- | testing/test_vgen2.py | 13 | ||||
| -rw-r--r-- | testing/test_zdistutils.py | 277 |
4 files changed, 195 insertions, 129 deletions
diff --git a/testing/test_verify.py b/testing/test_verify.py index e9f5d74..13e7192 100644 --- a/testing/test_verify.py +++ b/testing/test_verify.py @@ -15,6 +15,19 @@ def setup_module(): cffi.verifier.cleanup_tmpdir() +def test_module_type(): + import cffi.verifier + ffi = FFI() + lib = ffi.verify() + if hasattr(lib, '_cffi_python_module'): + print 'verify got a PYTHON module' + if hasattr(lib, '_cffi_generic_module'): + print 'verify got a GENERIC module' + expected_generic = (cffi.verifier._FORCE_GENERIC_ENGINE or + '__pypy__' in sys.builtin_module_names) + assert hasattr(lib, '_cffi_python_module') == (not expected_generic) + assert hasattr(lib, '_cffi_generic_module') == expected_generic + def test_missing_function(): ffi = FFI() ffi.cdef("void some_completely_unknown_function();") @@ -474,11 +487,14 @@ def test_access_callback(): lib.cb = my_callback assert lib.foo(4) == 887 -def test_cannot_verify_with_ctypes(): +def test_ctypes_backend_forces_generic_engine(): from cffi.backend_ctypes import CTypesBackend ffi = FFI(backend=CTypesBackend()) - ffi.cdef("int a;") - py.test.raises(NotImplementedError, ffi.verify, "int a;") + ffi.cdef("int func(int a);") + lib = ffi.verify("int func(int a) { return a * 42; }") + assert not hasattr(lib, '_cffi_python_module') + assert hasattr(lib, '_cffi_generic_module') + assert lib.func(100) == 4200 def test_call_with_struct_ptr(): ffi = FFI() diff --git a/testing/test_vgen.py b/testing/test_vgen.py new file mode 100644 index 0000000..1a7e05d --- /dev/null +++ b/testing/test_vgen.py @@ -0,0 +1,12 @@ +import cffi.verifier +from .test_verify import * + + +def setup_module(): + cffi.verifier.cleanup_tmpdir() + cffi.verifier._FORCE_GENERIC_ENGINE = True + # Runs all tests with _FORCE_GENERIC_ENGINE = True, to make sure we + # also test vengine_gen.py. + +def teardown_module(): + cffi.verifier._FORCE_GENERIC_ENGINE = False diff --git a/testing/test_vgen2.py b/testing/test_vgen2.py new file mode 100644 index 0000000..34147c8 --- /dev/null +++ b/testing/test_vgen2.py @@ -0,0 +1,13 @@ +import cffi.verifier +from .test_vgen import * + +# This test file runs normally after test_vgen. We only clean up the .c +# sources, to check that it also works when we have only the .so. The +# tests should run much faster than test_vgen. + +def setup_module(): + cffi.verifier.cleanup_tmpdir(keep_so=True) + cffi.verifier._FORCE_GENERIC_ENGINE = True + +def teardown_module(): + cffi.verifier._FORCE_GENERIC_ENGINE = False diff --git a/testing/test_zdistutils.py b/testing/test_zdistutils.py index 7272ab5..92b5dd8 100644 --- a/testing/test_zdistutils.py +++ b/testing/test_zdistutils.py @@ -1,134 +1,159 @@ -import os, imp, math, StringIO, random +import sys, os, imp, math, StringIO, random import py from cffi import FFI, FFIError -from cffi.verifier import Verifier +from cffi.verifier import Verifier, _locate_engine_class from testing.udir import udir -def test_write_source(): - ffi = FFI() - ffi.cdef("double sin(double x);") - csrc = '/*hi there!*/\n#include <math.h>\n' - v = Verifier(ffi, csrc) - v.write_source() - with file(v.sourcefilename, 'r') as f: - data = f.read() - assert csrc in data - -def test_write_source_explicit_filename(): - ffi = FFI() - ffi.cdef("double sin(double x);") - csrc = '/*hi there!*/\n#include <math.h>\n' - v = Verifier(ffi, csrc) - v.sourcefilename = filename = str(udir.join('write_source.c')) - v.write_source() - assert filename == v.sourcefilename - with file(filename, 'r') as f: - data = f.read() - assert csrc in data - -def test_write_source_to_file_obj(): - ffi = FFI() - ffi.cdef("double sin(double x);") - csrc = '/*hi there!*/\n#include <math.h>\n' - v = Verifier(ffi, csrc) - f = StringIO.StringIO() - v.write_source(file=f) - assert csrc in f.getvalue() - -def test_compile_module(): - ffi = FFI() - ffi.cdef("double sin(double x);") - csrc = '/*hi there!*/\n#include <math.h>\n' - v = Verifier(ffi, csrc) - v.compile_module() - assert v.get_module_name().startswith('_cffi_') - if v.generates_python_module(): - mod = imp.load_dynamic(v.get_module_name(), v.modulefilename) - assert hasattr(mod, '_cffi_setup') - -def test_compile_module_explicit_filename(): - ffi = FFI() - ffi.cdef("double sin(double x);") - csrc = '/*hi there!2*/\n#include <math.h>\n' - v = Verifier(ffi, csrc) - v.modulefilename = filename = str(udir.join('test_compile_module.so')) - v.compile_module() - assert filename == v.modulefilename - assert v.get_module_name() == 'test_compile_module' - if v.generates_python_module(): - mod = imp.load_dynamic(v.get_module_name(), v.modulefilename) - assert hasattr(mod, '_cffi_setup') - -def test_name_from_checksum_of_cdef(): - names = [] - for csrc in ['double', 'double', 'float']: +class DistUtilsTest(object): + + def test_locate_engine_class(self): + cls = _locate_engine_class(FFI(), self.generic) + if self.generic: + # asked for the generic engine, which must not generate a + # CPython extension module + assert not cls._gen_python_module + else: + # asked for the CPython engine: check that we got it, unless + # we are running on top of PyPy, where the generic engine is + # always better + if '__pypy__' not in sys.builtin_module_names: + assert cls._gen_python_module + + def test_write_source(self): ffi = FFI() - ffi.cdef("%s sin(double x);" % csrc) - v = Verifier(ffi, "#include <math.h>") - names.append(v.get_module_name()) - assert names[0] == names[1] != names[2] - -def test_name_from_checksum_of_csrc(): - names = [] - for csrc in ['123', '123', '1234']: + ffi.cdef("double sin(double x);") + csrc = '/*hi there!*/\n#include <math.h>\n' + v = Verifier(ffi, csrc, force_generic_engine=self.generic) + v.write_source() + with file(v.sourcefilename, 'r') as f: + data = f.read() + assert csrc in data + + def test_write_source_explicit_filename(self): ffi = FFI() ffi.cdef("double sin(double x);") - v = Verifier(ffi, csrc) - names.append(v.get_module_name()) - assert names[0] == names[1] != names[2] - -def test_load_library(): - ffi = FFI() - ffi.cdef("double sin(double x);") - csrc = '/*hi there!3*/\n#include <math.h>\n' - v = Verifier(ffi, csrc) - library = v.load_library() - assert library.sin(12.3) == math.sin(12.3) - -def test_verifier_args(): - ffi = FFI() - ffi.cdef("double sin(double x);") - csrc = '/*hi there!4*/#include "test_verifier_args.h"\n' - udir.join('test_verifier_args.h').write('#include <math.h>\n') - v = Verifier(ffi, csrc, include_dirs=[str(udir)]) - library = v.load_library() - assert library.sin(12.3) == math.sin(12.3) - -def test_verifier_object_from_ffi(): - ffi = FFI() - ffi.cdef("double sin(double x);") - csrc = "/*6*/\n#include <math.h>" - lib = ffi.verify(csrc) - assert lib.sin(12.3) == math.sin(12.3) - assert isinstance(ffi.verifier, Verifier) - with file(ffi.verifier.sourcefilename, 'r') as f: - data = f.read() - assert csrc in data - -def test_extension_object(): - ffi = FFI() - ffi.cdef("double sin(double x);") - csrc = '''/*7*/ -#include <math.h> -#ifndef TEST_EXTENSION_OBJECT -# error "define_macros missing" -#endif -''' - lib = ffi.verify(csrc, define_macros=[('TEST_EXTENSION_OBJECT', '1')]) - assert lib.sin(12.3) == math.sin(12.3) - v = ffi.verifier - ext = v.get_extension() - assert str(ext.__class__) == 'distutils.extension.Extension' - assert ext.sources == [v.sourcefilename] - assert ext.name == v.get_module_name() - assert ext.define_macros == [('TEST_EXTENSION_OBJECT', '1')] - -def test_extension_forces_write_source(): - ffi = FFI() - ffi.cdef("double sin(double x);") - csrc = '/*hi there!%r*/\n#include <math.h>\n' % random.random() - v = Verifier(ffi, csrc) - assert not os.path.exists(v.sourcefilename) - v.get_extension() - assert os.path.exists(v.sourcefilename) + csrc = '/*hi there!*/\n#include <math.h>\n' + v = Verifier(ffi, csrc, force_generic_engine=self.generic) + v.sourcefilename = filename = str(udir.join('write_source.c')) + v.write_source() + assert filename == v.sourcefilename + with file(filename, 'r') as f: + data = f.read() + assert csrc in data + + def test_write_source_to_file_obj(self): + ffi = FFI() + ffi.cdef("double sin(double x);") + csrc = '/*hi there!*/\n#include <math.h>\n' + v = Verifier(ffi, csrc, force_generic_engine=self.generic) + f = StringIO.StringIO() + v.write_source(file=f) + assert csrc in f.getvalue() + + def test_compile_module(self): + ffi = FFI() + ffi.cdef("double sin(double x);") + csrc = '/*hi there!*/\n#include <math.h>\n' + v = Verifier(ffi, csrc, force_generic_engine=self.generic) + v.compile_module() + assert v.get_module_name().startswith('_cffi_') + if v.generates_python_module(): + mod = imp.load_dynamic(v.get_module_name(), v.modulefilename) + assert hasattr(mod, '_cffi_setup') + + def test_compile_module_explicit_filename(self): + ffi = FFI() + ffi.cdef("double sin(double x);") + csrc = '/*hi there!2*/\n#include <math.h>\n' + v = Verifier(ffi, csrc, force_generic_engine=self.generic) + v.modulefilename = filename = str(udir.join('test_compile_module.so')) + v.compile_module() + assert filename == v.modulefilename + assert v.get_module_name() == 'test_compile_module' + if v.generates_python_module(): + mod = imp.load_dynamic(v.get_module_name(), v.modulefilename) + assert hasattr(mod, '_cffi_setup') + + def test_name_from_checksum_of_cdef(self): + names = [] + for csrc in ['double', 'double', 'float']: + ffi = FFI() + ffi.cdef("%s sin(double x);" % csrc) + v = Verifier(ffi, "#include <math.h>", + force_generic_engine=self.generic) + names.append(v.get_module_name()) + assert names[0] == names[1] != names[2] + + def test_name_from_checksum_of_csrc(self): + names = [] + for csrc in ['123', '123', '1234']: + ffi = FFI() + ffi.cdef("double sin(double x);") + v = Verifier(ffi, csrc, force_generic_engine=self.generic) + names.append(v.get_module_name()) + assert names[0] == names[1] != names[2] + + def test_load_library(self): + ffi = FFI() + ffi.cdef("double sin(double x);") + csrc = '/*hi there!3*/\n#include <math.h>\n' + v = Verifier(ffi, csrc, force_generic_engine=self.generic) + library = v.load_library() + assert library.sin(12.3) == math.sin(12.3) + + def test_verifier_args(self): + ffi = FFI() + ffi.cdef("double sin(double x);") + csrc = '/*hi there!4*/#include "test_verifier_args.h"\n' + udir.join('test_verifier_args.h').write('#include <math.h>\n') + v = Verifier(ffi, csrc, include_dirs=[str(udir)], + force_generic_engine=self.generic) + library = v.load_library() + assert library.sin(12.3) == math.sin(12.3) + + def test_verifier_object_from_ffi(self): + ffi = FFI() + ffi.cdef("double sin(double x);") + csrc = "/*6*/\n#include <math.h>" + lib = ffi.verify(csrc, force_generic_engine=self.generic) + assert lib.sin(12.3) == math.sin(12.3) + assert isinstance(ffi.verifier, Verifier) + with file(ffi.verifier.sourcefilename, 'r') as f: + data = f.read() + assert csrc in data + + def test_extension_object(self): + ffi = FFI() + ffi.cdef("double sin(double x);") + csrc = '''/*7*/ + #include <math.h> + #ifndef TEST_EXTENSION_OBJECT + # error "define_macros missing" + #endif + ''' + lib = ffi.verify(csrc, define_macros=[('TEST_EXTENSION_OBJECT', '1')], + force_generic_engine=self.generic) + assert lib.sin(12.3) == math.sin(12.3) + v = ffi.verifier + ext = v.get_extension() + assert str(ext.__class__) == 'distutils.extension.Extension' + assert ext.sources == [v.sourcefilename] + assert ext.name == v.get_module_name() + assert ext.define_macros == [('TEST_EXTENSION_OBJECT', '1')] + + def test_extension_forces_write_source(self): + ffi = FFI() + ffi.cdef("double sin(double x);") + csrc = '/*hi there!%r*/\n#include <math.h>\n' % random.random() + v = Verifier(ffi, csrc, force_generic_engine=self.generic) + assert not os.path.exists(v.sourcefilename) + v.get_extension() + assert os.path.exists(v.sourcefilename) + + +class TestDistUtilsCPython(DistUtilsTest): + generic = False + +class TestDistUtilsGeneric(DistUtilsTest): + generic = True |
