struct conversion tests
authorRobert Bradshaw <robertwb@math.washington.edu>
Wed, 8 Oct 2008 19:01:57 +0000 (12:01 -0700)
committerRobert Bradshaw <robertwb@math.washington.edu>
Wed, 8 Oct 2008 19:01:57 +0000 (12:01 -0700)
tests/run/struct_conversion.pyx [new file with mode: 0644]

diff --git a/tests/run/struct_conversion.pyx b/tests/run/struct_conversion.pyx
new file mode 100644 (file)
index 0000000..3fd12ef
--- /dev/null
@@ -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