1import sys, unittest 2from ctypes import * 3 4structures = [] 5byteswapped_structures = [] 6 7 8if sys.byteorder == "little": 9 SwappedStructure = BigEndianStructure 10else: 11 SwappedStructure = LittleEndianStructure 12 13for typ in [c_short, c_int, c_long, c_longlong, 14 c_float, c_double, 15 c_ushort, c_uint, c_ulong, c_ulonglong]: 16 class X(Structure): 17 _pack_ = 1 18 _fields_ = [("pad", c_byte), 19 ("value", typ)] 20 class Y(SwappedStructure): 21 _pack_ = 1 22 _fields_ = [("pad", c_byte), 23 ("value", typ)] 24 structures.append(X) 25 byteswapped_structures.append(Y) 26 27class TestStructures(unittest.TestCase): 28 def test_native(self): 29 for typ in structures: 30## print typ.value 31 self.assertEqual(typ.value.offset, 1) 32 o = typ() 33 o.value = 4 34 self.assertEqual(o.value, 4) 35 36 def test_swapped(self): 37 for typ in byteswapped_structures: 38## print >> sys.stderr, typ.value 39 self.assertEqual(typ.value.offset, 1) 40 o = typ() 41 o.value = 4 42 self.assertEqual(o.value, 4) 43 44if __name__ == '__main__': 45 unittest.main() 46