TST: buffer: add additional buffer access tests for packed structs related to 3f599a8c
authorPauli Virtanen <pav@iki.fi>
Sat, 2 Apr 2011 10:08:19 +0000 (12:08 +0200)
committerDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Sat, 16 Apr 2011 16:16:34 +0000 (18:16 +0200)
tests/run/bufaccess.pyx

index 6f26cf8fbf626013e9d6a8656fed2a2e205c32a3..393d3fe5e0f89b8fc588d1695d4a12a3e07b4245 100644 (file)
@@ -1281,6 +1281,16 @@ cdef struct NestedStruct:
     SmallStruct y
     int z
 
+cdef packed struct PackedStruct:
+    char a
+    int b
+
+cdef struct NestedPackedStruct:
+    char a
+    int b
+    PackedStruct sub
+    int c
+
 cdef class MyStructMockBuffer(MockBuffer):
     cdef int write(self, char* buf, object value) except -1:
         cdef MyStruct* s
@@ -1301,6 +1311,26 @@ cdef class NestedStructMockBuffer(MockBuffer):
     cdef get_itemsize(self): return sizeof(NestedStruct)
     cdef get_default_format(self): return b"2T{ii}i"
 
+cdef class PackedStructMockBuffer(MockBuffer):
+    cdef int write(self, char* buf, object value) except -1:
+        cdef PackedStruct* s
+        s = <PackedStruct*>buf;
+        s.a, s.b = value
+        return 0
+
+    cdef get_itemsize(self): return sizeof(PackedStruct)
+    cdef get_default_format(self): return b"^ci"
+
+cdef class NestedPackedStructMockBuffer(MockBuffer):
+    cdef int write(self, char* buf, object value) except -1:
+        cdef NestedPackedStruct* s
+        s = <NestedPackedStruct*>buf;
+        s.a, s.b, s.sub.a, s.sub.b, s.c = value
+        return 0
+
+    cdef get_itemsize(self): return sizeof(NestedPackedStruct)
+    cdef get_default_format(self): return b"ci^ci@i"
+
 @testcase
 def basic_struct(object[MyStruct] buf):
     """
@@ -1325,6 +1355,35 @@ def nested_struct(object[NestedStruct] buf):
     """
     print buf[0].x.a, buf[0].x.b, buf[0].y.a, buf[0].y.b, buf[0].z
 
+@testcase
+def packed_struct(object[PackedStruct] buf):
+    """
+    See also buffmt.pyx
+
+    >>> packed_struct(PackedStructMockBuffer(None, [(1, 2)]))
+    1 2
+    >>> packed_struct(PackedStructMockBuffer(None, [(1, 2)], format="T{c^i}"))
+    1 2
+    >>> packed_struct(PackedStructMockBuffer(None, [(1, 2)], format="T{c=i}"))
+    1 2
+
+    """
+    print buf[0].a, buf[0].b
+
+@testcase
+def nested_packed_struct(object[NestedPackedStruct] buf):
+    """
+    See also buffmt.pyx
+
+    >>> nested_packed_struct(NestedPackedStructMockBuffer(None, [(1, 2, 3, 4, 5)]))
+    1 2 3 4 5
+    >>> nested_packed_struct(NestedPackedStructMockBuffer(None, [(1, 2, 3, 4, 5)], format="ci^ci@i"))
+    1 2 3 4 5
+    >>> nested_packed_struct(NestedPackedStructMockBuffer(None, [(1, 2, 3, 4, 5)], format="^c@i^ci@i"))
+    1 2 3 4 5
+    """
+    print buf[0].a, buf[0].b, buf[0].sub.a, buf[0].sub.b, buf[0].c
+
 cdef struct LongComplex:
     long double real
     long double imag