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