Staging
v0.5.1
https://github.com/python/cpython
Raw File
Tip revision: 3556b2db57ab3b17b3d4c6b28cdc604a3e0168e8 authored by Anthony Baxter on 27 April 2006, 02:13:13 UTC
2.5a2
Tip revision: 3556b2d
test_unaligned_structures.py
import sys, unittest
from ctypes import *

structures = []
byteswapped_structures = []


if sys.byteorder == "little":
    SwappedStructure = BigEndianStructure
else:
    SwappedStructure = LittleEndianStructure

for typ in [c_short, c_int, c_long, c_longlong,
            c_float, c_double,
            c_ushort, c_uint, c_ulong, c_ulonglong]:
    class X(Structure):
        _pack_ = 1
        _fields_ = [("pad", c_byte),
                    ("value", typ)]
    class Y(SwappedStructure):
        _pack_ = 1
        _fields_ = [("pad", c_byte),
                    ("value", typ)]
    structures.append(X)
    byteswapped_structures.append(Y)

class TestStructures(unittest.TestCase):
    def test_native(self):
        for typ in structures:
##            print typ.value
            self.failUnlessEqual(typ.value.offset, 1)
            o = typ()
            o.value = 4
            self.failUnlessEqual(o.value, 4)

    def test_swapped(self):
        for typ in byteswapped_structures:
##            print >> sys.stderr, typ.value
            self.failUnlessEqual(typ.value.offset, 1)
            o = typ()
            o.value = 4
            self.failUnlessEqual(o.value, 4)

if __name__ == '__main__':
    unittest.main()
back to top