pure.pyx 2.43 KB
Newer Older
Stefan Behnel's avatar
Stefan Behnel committed
1
__doc__ = u"""
2 3 4 5 6 7
>>> test_sizeof()
True
True
True
True
True
8

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
>>> test_declare(100)
(100, 100)
>>> test_declare(100.5)
(100, 100)
>>> test_declare(None)
Traceback (most recent call last):
...
TypeError: an integer is required

>>> test_cast(1.5)
1
>>> test_cast(None)
Traceback (most recent call last):
...
TypeError: a float is required

>>> test_address(39)
39

>>> test_locals(5)
True
30

31 32 33 34 35 36 37 38
>>> test_struct(389, 1.64493)
(389, 1.64493)

>>> test_imports()
True
"""

import cython
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

def test_sizeof():
    x = cython.declare(cython.bint)
    print sizeof(x) == sizeof(cython.bint)
    print sizeof(cython.char) <= sizeof(cython.short) <= sizeof(cython.int) <= sizeof(cython.long) <= sizeof(cython.longlong)
    print sizeof(cython.uint) == sizeof(cython.int)
    print sizeof(cython.p_int) == sizeof(cython.p_double)
    if cython.compiled:
        print sizeof(cython.char) < sizeof(cython.longlong)
    else:
        print sizeof(cython.char) == 1

def test_declare(n):
    x = cython.declare(cython.int)
    y = cython.declare(cython.int, n)
    if cython.compiled:
        cython.declare(xx=cython.int, yy=cython.long)
56
        i = sizeof(xx)
57 58 59 60 61 62 63 64 65 66 67 68 69
    ptr = cython.declare(cython.p_int, cython.address(y))
    return y, ptr[0]
    
@cython.locals(x=cython.double, n=cython.int)
def test_cast(x):
    n = cython.cast(cython.int, x)
    return n
    
@cython.locals(x=cython.int, y=cython.p_int)
def test_address(x):
    y = cython.address(x)
    return y[0]

70 71
@cython.locals(x=cython.int)
@cython.locals(y=cython.bint)
72 73 74 75 76 77 78 79 80 81 82 83
def test_locals(x):
    y = x
    return y
    

MyUnion = cython.union(n=cython.int, x=cython.double)
MyStruct = cython.struct(is_integral=cython.bint, data=MyUnion)
MyStruct2 = cython.typedef(MyStruct[2])

def test_struct(n, x):
    a = cython.declare(MyStruct2)
    a[0] = MyStruct(True, data=MyUnion(n=n))
84
    a[1] = MyStruct(is_integral=False, data={'x': x})
85 86 87 88
    return a[0].data.n, a[1].data.x

import cython as cy
from cython import declare, cast, locals, address, typedef, p_void, compiled
89
from cython import declare as my_declare, locals as my_locals, p_void as my_void_star, typedef as my_typedef, compiled as my_compiled
90 91 92

@my_locals(a=cython.p_void)
def test_imports():
93 94 95 96 97
    a = cython.NULL
    b = declare(p_void, cython.NULL)
    c = my_declare(my_void_star, cython.NULL)
    d = cy.declare(cy.p_void, cython.NULL)
    return a == d and compiled and my_compiled
98 99 100 101

MyStruct3 = typedef(MyStruct[3])
MyStruct4 = my_typedef(MyStruct[4])
MyStruct5 = cy.typedef(MyStruct[5])