[pypy-commit] cffi default: A list of tests for the new feature of ffi.make_verifier().

arigo noreply at buildbot.pypy.org
Sat Jul 14 14:40:11 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r642:62b922f46777
Date: 2012-07-14 14:39 +0200
http://bitbucket.org/cffi/cffi/changeset/62b922f46777/

Log:	A list of tests for the new feature of ffi.make_verifier().

diff --git a/testing/test_zdistutils.py b/testing/test_zdistutils.py
new file mode 100644
--- /dev/null
+++ b/testing/test_zdistutils.py
@@ -0,0 +1,142 @@
+import imp, math, StringIO
+import py
+from cffi import FFI, FFIError
+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 = ffi.make_verifier(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 = ffi.make_verifier(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 = ffi.make_verifier(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 = ffi.make_verifier(csrc)
+    v.compile_module()
+    assert v.modulename.startswith('_cffi_')
+    mod = imp.load_dynamic(v.modulename, 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 = ffi.make_verifier(csrc)
+    v.modulefilename = filename = str(udir.join('compile_module.so'))
+    v.compile_module()
+    assert filename == v.modulefilename
+    assert v.modulename.startswith('_cffi_')
+    mod = imp.load_dynamic(v.modulename, v.modulefilename)
+    assert hasattr(mod, '_cffi_setup')
+
+def test_name_from_md5_of_cdef():
+    names = []
+    for csrc in ['double', 'double', 'float']:
+        ffi = FFI()
+        ffi.cdef("%s sin(double x);" % csrc)
+        v = ffi.make_verifier("#include <math.h>")
+        names.append(v.modulename)
+    assert names[0] == names[1] != names[2]
+
+def test_name_from_md5_of_csrc():
+    names = []
+    for csrc in ['123', '123', '1234']:
+        ffi = FFI()
+        ffi.cdef("double sin(double x);")
+        v = ffi.make_verifier(csrc)
+        names.append(v.modulename)
+    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 = ffi.make_verifier(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 = ffi.make_verifier(csrc, include_dirs=[str(udir)])
+    library = v.load_library()
+    assert library.sin(12.3) == math.sin(12.3)
+
+def test_verifier_object_from_ffi_1():
+    ffi = FFI()
+    ffi.cdef("double sin(double x);")
+    csrc = "/*5*/\n#include <math.h>"
+    v = ffi.make_verifier(csrc)
+    library = v.load_library()
+    assert library.sin(12.3) == math.sin(12.3)
+    assert ffi.get_verifier() is v
+    with file(ffi.get_verifier().sourcefilename, 'r') as f:
+        data = f.read()
+    assert csrc in data
+
+def test_verifier_object_from_ffi_2():
+    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)
+    with file(ffi.get_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.get_verifier()
+    ext = v.get_extension()
+    assert str(ext.__class__) == 'distutils.extension.Extension'
+    assert ext.sources == [v.sourcefilename]
+    assert ext.name == v.modulename
+    assert ext.define_macros == [('TEST_EXTENSION_OBJECT', '1')]
+
+def test_caching():
+    ffi = FFI()
+    ffi.cdef("double sin(double x);")
+    py.test.raises(TypeError, ffi.make_verifier)
+    py.test.raises(FFIError, ffi.get_verifier)
+    v = ffi.make_verifier("#include <math.h>")
+    py.test.raises(FFIError, ffi.make_verifier, "foobar")
+    assert ffi.get_verifier() is v


More information about the pypy-commit mailing list