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