def test_Wconversion_floating():
lib = _Wconversion("float sin(double);",
"#include <math.h>", libraries=lib_m)
res = lib.sin(1.23)
assert res != math.sin(1.23) # not exact, because of double->float
assert abs(res - math.sin(1.23)) < 1E-5
python类h()的实例源码
def test_Wconversion_float2int():
_Wconversion("int sinf(float);",
"#include <math.h>", libraries=lib_m)
def test_Wconversion_double2int():
_Wconversion("int sin(double);",
"#include <math.h>", libraries=lib_m)
def test_rounding_1():
ffi = FFI()
ffi.cdef("double sinf(float x);")
lib = ffi.verify('#include <math.h>', libraries=lib_m)
res = lib.sinf(1.23)
assert res != math.sin(1.23) # not exact, because of double->float
assert abs(res - math.sin(1.23)) < 1E-5
def test_rounding_2():
ffi = FFI()
ffi.cdef("double sin(float x);")
lib = ffi.verify('#include <math.h>', libraries=lib_m)
res = lib.sin(1.23)
assert res != math.sin(1.23) # not exact, because of double->float
assert abs(res - math.sin(1.23)) < 1E-5
def test_strlen_approximate():
lib = _Wconversion("int strlen(char *s);",
"#include <string.h>")
assert lib.strlen(b"hi there!") == 9
def test_strlen_array_of_char():
ffi = FFI()
ffi.cdef("size_t strlen(char[]);")
lib = ffi.verify("#include <string.h>")
assert lib.strlen(b"hello") == 5
def test_longdouble():
ffi = FFI()
ffi.cdef("long double sinl(long double x);")
lib = ffi.verify('#include <math.h>', libraries=lib_m)
for input in [1.23,
ffi.cast("double", 1.23),
ffi.cast("long double", 1.23)]:
x = lib.sinl(input)
assert repr(x).startswith("<cdata 'long double'")
assert (float(x) - math.sin(1.23)) < 1E-10
def test_funcptr_as_argument():
ffi = FFI()
ffi.cdef("""
void qsort(void *base, size_t nel, size_t width,
int (*compar)(const void *, const void *));
""")
ffi.verify("#include <stdlib.h>")
def test_func_as_argument():
ffi = FFI()
ffi.cdef("""
void qsort(void *base, size_t nel, size_t width,
int compar(const void *, const void *));
""")
ffi.verify("#include <stdlib.h>")
def test_function_typedef():
ffi = FFI()
ffi.cdef("""
typedef double func_t(double);
func_t sin;
""")
lib = ffi.verify('#include <math.h>', libraries=lib_m)
assert lib.sin(1.23) == math.sin(1.23)
def test_return_partial_struct():
ffi = FFI()
ffi.cdef("""
typedef struct { int x; ...; } foo_t;
foo_t foo(void);
""")
lib = ffi.verify("""
typedef struct { int y, x; } foo_t;
foo_t foo(void) { foo_t r = { 45, 81 }; return r; }
""")
h = lib.foo()
assert ffi.sizeof(h) == 2 * ffi.sizeof("int")
assert h.x == 81
def test_relative_to():
import tempfile, os
from testing.udir import udir
tmpdir = tempfile.mkdtemp(dir=str(udir))
ffi = FFI()
ffi.cdef("int foo(int);")
f = open(os.path.join(tmpdir, 'foo.h'), 'w')
f.write("int foo(int a) { return a + 42; }\n")
f.close()
lib = ffi.verify('#include "foo.h"',
include_dirs=['.'],
relative_to=os.path.join(tmpdir, 'x'))
assert lib.foo(100) == 142
def test_string_to_voidp_arg():
ffi = FFI()
ffi.cdef("int myfunc(void *);")
lib = ffi.verify("int myfunc(void *p) { return ((signed char *)p)[0]; }")
res = lib.myfunc(b"hi!")
assert res == ord(b"h")
p = ffi.new("char[]", b"gah")
res = lib.myfunc(p)
assert res == ord(b"g")
res = lib.myfunc(ffi.cast("void *", p))
assert res == ord(b"g")
res = lib.myfunc(ffi.cast("int *", p))
assert res == ord(b"g")
def test_write_source(self):
ffi = FFI()
ffi.cdef("double sin(double x);")
csrc = '/*hi there %s!*/\n#include <math.h>\n' % self
v = Verifier(ffi, csrc, force_generic_engine=self.generic,
libraries=[self.lib_m])
v.write_source()
with open(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);")
csrc = '/*hi there %s!*/\n#include <math.h>\n' % self
v = Verifier(ffi, csrc, force_generic_engine=self.generic,
libraries=[self.lib_m])
v.sourcefilename = filename = str(udir.join('write_source.c'))
v.write_source()
assert filename == v.sourcefilename
with open(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 %s!*/\n#include <math.h>\n' % self
v = Verifier(ffi, csrc, force_generic_engine=self.generic,
libraries=[self.lib_m])
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
f = 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 %s!*/\n#include <math.h>\n' % self
v = Verifier(ffi, csrc, force_generic_engine=self.generic,
libraries=[self.lib_m])
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 %s!2*/\n#include <math.h>\n' % self
v = Verifier(ffi, csrc, force_generic_engine=self.generic,
libraries=[self.lib_m])
basename = self.__class__.__name__ + 'test_compile_module'
v.modulefilename = filename = str(udir.join(basename + '.so'))
v.compile_module()
assert filename == v.modulefilename
assert v.get_module_name() == basename
if v.generates_python_module():
mod = imp.load_dynamic(v.get_module_name(), v.modulefilename)
assert hasattr(mod, '_cffi_setup')
def test_load_library(self):
ffi = FFI()
ffi.cdef("double sin(double x);")
csrc = '/*hi there %s!3*/\n#include <math.h>\n' % self
v = Verifier(ffi, csrc, force_generic_engine=self.generic,
libraries=[self.lib_m])
library = v.load_library()
assert library.sin(12.3) == math.sin(12.3)