|
1 | 1 | from ctypes import ( |
2 | 2 | c_char, c_uint32, c_uint16, c_ubyte, c_byte, alignment, sizeof, |
3 | 3 | BigEndianStructure, LittleEndianStructure, |
4 | | - BigEndianUnion, LittleEndianUnion, |
| 4 | + BigEndianUnion, LittleEndianUnion, Structure, |
5 | 5 | ) |
6 | 6 | import struct |
7 | 7 | import unittest |
@@ -69,6 +69,41 @@ class Main(base): |
69 | 69 | self.assertEqual(Main.z.offset, 8) |
70 | 70 | self.assertEqual(main.z, 7) |
71 | 71 |
|
| 72 | + def test_negative_align(self): |
| 73 | + for base in (Structure, LittleEndianStructure, BigEndianStructure): |
| 74 | + with ( |
| 75 | + self.subTest(base=base), |
| 76 | + self.assertRaisesRegex( |
| 77 | + ValueError, |
| 78 | + '_align_ must be a non-negative integer', |
| 79 | + ) |
| 80 | + ): |
| 81 | + class MyStructure(base): |
| 82 | + _align_ = -1 |
| 83 | + _fields_ = [] |
| 84 | + |
| 85 | + def test_zero_align_no_fields(self): |
| 86 | + for base in (Structure, LittleEndianStructure, BigEndianStructure): |
| 87 | + with self.subTest(base=base): |
| 88 | + class MyStructure(base): |
| 89 | + _align_ = 0 |
| 90 | + _fields_ = [] |
| 91 | + |
| 92 | + self.assertEqual(alignment(MyStructure), 1) |
| 93 | + self.assertEqual(alignment(MyStructure()), 1) |
| 94 | + |
| 95 | + def test_zero_align_with_fields(self): |
| 96 | + for base in (Structure, LittleEndianStructure, BigEndianStructure): |
| 97 | + with self.subTest(base=base): |
| 98 | + class MyStructure(base): |
| 99 | + _align_ = 0 |
| 100 | + _fields_ = [ |
| 101 | + ("x", c_ubyte), |
| 102 | + ] |
| 103 | + |
| 104 | + self.assertEqual(alignment(MyStructure), 1) |
| 105 | + self.assertEqual(alignment(MyStructure()), 1) |
| 106 | + |
72 | 107 | def test_oversized_structure(self): |
73 | 108 | data = bytearray(b"\0" * 8) |
74 | 109 | for base in (LittleEndianStructure, BigEndianStructure): |
|
0 commit comments