From: Robert Bradshaw Date: Wed, 8 Oct 2008 19:01:57 +0000 (-0700) Subject: struct conversion tests X-Git-Tag: 0.9.9.2.beta~52 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=d19f62521c2dde3d37ccd639090fde505b8cf177;p=cython.git struct conversion tests --- diff --git a/tests/run/struct_conversion.pyx b/tests/run/struct_conversion.pyx new file mode 100644 index 00000000..3fd12ef4 --- /dev/null +++ b/tests/run/struct_conversion.pyx @@ -0,0 +1,60 @@ +__doc__ = """ + >>> test_constructor(1,2,255) + {'y': 2.0, 'x': 1.0, 'color': 255} + >>> test_constructor(1,None,255) + Traceback (most recent call last): + ... + TypeError: a float is required + + >>> test_constructor_kwds(1.25, 2.5, 128) + {'y': 2.5, 'x': 1.25, 'color': 128} + >>> test_constructor_kwds(1.25, 2.5, None) + Traceback (most recent call last): + ... + TypeError: an integer is required + + >>> test_dict_construction(4, 5, 64) + {'y': 5.0, 'x': 4.0, 'color': 64} + >>> test_dict_construction("foo", 5, 64) + Traceback (most recent call last): + ... + TypeError: a float is required + + >>> test_pointers(100, 2.71828) + 100 + 2.71828 + True +""" + +cdef struct Point: + double x + double y + int color + +def test_constructor(x, y, color): + cdef Point p = Point(x, y, color) + return p + +def test_constructor_kwds(x, y, color): + cdef Point p = Point(x=x, y=y, color=color) + return p + +def test_dict_construction(x, y, color): + cdef Point p = {color: color, x: x, y: y} + return p + +cdef union int_or_float: + int n + double x + +cdef struct with_pointers: + bint is_integral + int_or_float data + void* ptr + +def test_pointers(int n, double x): + cdef with_pointers a = [True, {n: n}, NULL] + cdef with_pointers b = with_pointers(False, {x: x}, NULL) + print a.data.n + print b.data.x + print a.ptr == b.ptr == NULL \ No newline at end of file