| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import unicode_literals |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
import sys, math |
| 12 |
from cffi import FFI |
| 13 |
from extra_tests.cffi_tests.support import is_musl |
| 14 |
|
| 15 |
|
| 16 |
lib_m = "m" |
| 17 |
if sys.platform == 'win32': |
| 18 |
|
| 19 |
import distutils.ccompiler |
| 20 |
if distutils.ccompiler.get_default_compiler() == 'msvc': |
| 21 |
lib_m = 'msvcrt' |
| 22 |
elif is_musl: |
| 23 |
lib_m = 'c' |
| 24 |
|
| 25 |
|
| 26 |
def test_cast(): |
| 27 |
ffi = FFI() |
| 28 |
assert int(ffi.cast("int", 3.14)) == 3 |
| 29 |
|
| 30 |
def test_new(): |
| 31 |
ffi = FFI() |
| 32 |
assert ffi.new("int[]", [3, 4, 5])[2] == 5 |
| 33 |
|
| 34 |
def test_typeof(): |
| 35 |
ffi = FFI() |
| 36 |
tp = ffi.typeof("int[51]") |
| 37 |
assert tp.length == 51 |
| 38 |
|
| 39 |
def test_sizeof(): |
| 40 |
ffi = FFI() |
| 41 |
assert ffi.sizeof("int[51]") == 51 * 4 |
| 42 |
|
| 43 |
def test_alignof(): |
| 44 |
ffi = FFI() |
| 45 |
assert ffi.alignof("int[51]") == 4 |
| 46 |
|
| 47 |
def test_getctype(): |
| 48 |
ffi = FFI() |
| 49 |
assert ffi.getctype("int**") == "int * *" |
| 50 |
assert type(ffi.getctype("int**")) is str |
| 51 |
|
| 52 |
def test_cdef(): |
| 53 |
ffi = FFI() |
| 54 |
ffi.cdef("typedef int foo_t[50];") |
| 55 |
|
| 56 |
def test_offsetof(): |
| 57 |
ffi = FFI() |
| 58 |
ffi.cdef("typedef struct { int x, y; } foo_t;") |
| 59 |
assert ffi.offsetof("foo_t", "y") == 4 |
| 60 |
|
| 61 |
def test_enum(): |
| 62 |
ffi = FFI() |
| 63 |
ffi.cdef("enum foo_e { AA, BB, CC };") |
| 64 |
x = ffi.cast("enum foo_e", 1) |
| 65 |
assert int(ffi.cast("int", x)) == 1 |
| 66 |
|
| 67 |
def test_dlopen(): |
| 68 |
ffi = FFI() |
| 69 |
ffi.cdef("double sin(double x);") |
| 70 |
m = ffi.dlopen(lib_m) |
| 71 |
x = m.sin(1.23) |
| 72 |
assert x == math.sin(1.23) |
| 73 |
|
| 74 |
def test_verify(): |
| 75 |
ffi = FFI() |
| 76 |
ffi.cdef("double test_verify_1(double x);") |
| 77 |
lib = ffi.verify("double test_verify_1(double x) { return x * 42.0; }") |
| 78 |
assert lib.test_verify_1(-1.5) == -63.0 |
| 79 |
|
| 80 |
def test_callback(): |
| 81 |
ffi = FFI() |
| 82 |
cb = ffi.callback("int(int)", |
| 83 |
lambda x: x + 42) |
| 84 |
assert cb(5) == 47 |