• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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            self.assertEqual(typ.value.offset, 1)
31            o = typ()
32            o.value = 4
33            self.assertEqual(o.value, 4)
34
35    def test_swapped(self):
36        for typ in byteswapped_structures:
37            self.assertEqual(typ.value.offset, 1)
38            o = typ()
39            o.value = 4
40            self.assertEqual(o.value, 4)
41
42if __name__ == '__main__':
43    unittest.main()
44