ยปCore Development>Code coverage>Lib/ctypes/test/test_bitfields.py

Python code coverage for Lib/ctypes/test/test_bitfields.py

#countcontent
1n/afrom ctypes import *
2n/afrom ctypes.test import need_symbol
3n/aimport unittest
4n/aimport os
5n/a
6n/aimport _ctypes_test
7n/a
8n/aclass BITS(Structure):
9n/a _fields_ = [("A", c_int, 1),
10n/a ("B", c_int, 2),
11n/a ("C", c_int, 3),
12n/a ("D", c_int, 4),
13n/a ("E", c_int, 5),
14n/a ("F", c_int, 6),
15n/a ("G", c_int, 7),
16n/a ("H", c_int, 8),
17n/a ("I", c_int, 9),
18n/a
19n/a ("M", c_short, 1),
20n/a ("N", c_short, 2),
21n/a ("O", c_short, 3),
22n/a ("P", c_short, 4),
23n/a ("Q", c_short, 5),
24n/a ("R", c_short, 6),
25n/a ("S", c_short, 7)]
26n/a
27n/afunc = CDLL(_ctypes_test.__file__).unpack_bitfields
28n/afunc.argtypes = POINTER(BITS), c_char
29n/a
30n/a##for n in "ABCDEFGHIMNOPQRS":
31n/a## print n, hex(getattr(BITS, n).size), getattr(BITS, n).offset
32n/a
33n/aclass C_Test(unittest.TestCase):
34n/a
35n/a def test_ints(self):
36n/a for i in range(512):
37n/a for name in "ABCDEFGHI":
38n/a b = BITS()
39n/a setattr(b, name, i)
40n/a self.assertEqual(getattr(b, name), func(byref(b), name.encode('ascii')))
41n/a
42n/a def test_shorts(self):
43n/a for i in range(256):
44n/a for name in "MNOPQRS":
45n/a b = BITS()
46n/a setattr(b, name, i)
47n/a self.assertEqual(getattr(b, name), func(byref(b), name.encode('ascii')))
48n/a
49n/asigned_int_types = (c_byte, c_short, c_int, c_long, c_longlong)
50n/aunsigned_int_types = (c_ubyte, c_ushort, c_uint, c_ulong, c_ulonglong)
51n/aint_types = unsigned_int_types + signed_int_types
52n/a
53n/aclass BitFieldTest(unittest.TestCase):
54n/a
55n/a def test_longlong(self):
56n/a class X(Structure):
57n/a _fields_ = [("a", c_longlong, 1),
58n/a ("b", c_longlong, 62),
59n/a ("c", c_longlong, 1)]
60n/a
61n/a self.assertEqual(sizeof(X), sizeof(c_longlong))
62n/a x = X()
63n/a x.a, x.b, x.c = -1, 7, -1
64n/a self.assertEqual((x.a, x.b, x.c), (-1, 7, -1))
65n/a
66n/a def test_ulonglong(self):
67n/a class X(Structure):
68n/a _fields_ = [("a", c_ulonglong, 1),
69n/a ("b", c_ulonglong, 62),
70n/a ("c", c_ulonglong, 1)]
71n/a
72n/a self.assertEqual(sizeof(X), sizeof(c_longlong))
73n/a x = X()
74n/a self.assertEqual((x.a, x.b, x.c), (0, 0, 0))
75n/a x.a, x.b, x.c = 7, 7, 7
76n/a self.assertEqual((x.a, x.b, x.c), (1, 7, 1))
77n/a
78n/a def test_signed(self):
79n/a for c_typ in signed_int_types:
80n/a class X(Structure):
81n/a _fields_ = [("dummy", c_typ),
82n/a ("a", c_typ, 3),
83n/a ("b", c_typ, 3),
84n/a ("c", c_typ, 1)]
85n/a self.assertEqual(sizeof(X), sizeof(c_typ)*2)
86n/a
87n/a x = X()
88n/a self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, 0, 0, 0))
89n/a x.a = -1
90n/a self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, -1, 0, 0))
91n/a x.a, x.b = 0, -1
92n/a self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, 0, -1, 0))
93n/a
94n/a
95n/a def test_unsigned(self):
96n/a for c_typ in unsigned_int_types:
97n/a class X(Structure):
98n/a _fields_ = [("a", c_typ, 3),
99n/a ("b", c_typ, 3),
100n/a ("c", c_typ, 1)]
101n/a self.assertEqual(sizeof(X), sizeof(c_typ))
102n/a
103n/a x = X()
104n/a self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, 0, 0, 0))
105n/a x.a = -1
106n/a self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, 7, 0, 0))
107n/a x.a, x.b = 0, -1
108n/a self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, 0, 7, 0))
109n/a
110n/a
111n/a def fail_fields(self, *fields):
112n/a return self.get_except(type(Structure), "X", (),
113n/a {"_fields_": fields})
114n/a
115n/a def test_nonint_types(self):
116n/a # bit fields are not allowed on non-integer types.
117n/a result = self.fail_fields(("a", c_char_p, 1))
118n/a self.assertEqual(result, (TypeError, 'bit fields not allowed for type c_char_p'))
119n/a
120n/a result = self.fail_fields(("a", c_void_p, 1))
121n/a self.assertEqual(result, (TypeError, 'bit fields not allowed for type c_void_p'))
122n/a
123n/a if c_int != c_long:
124n/a result = self.fail_fields(("a", POINTER(c_int), 1))
125n/a self.assertEqual(result, (TypeError, 'bit fields not allowed for type LP_c_int'))
126n/a
127n/a result = self.fail_fields(("a", c_char, 1))
128n/a self.assertEqual(result, (TypeError, 'bit fields not allowed for type c_char'))
129n/a
130n/a class Dummy(Structure):
131n/a _fields_ = []
132n/a
133n/a result = self.fail_fields(("a", Dummy, 1))
134n/a self.assertEqual(result, (TypeError, 'bit fields not allowed for type Dummy'))
135n/a
136n/a @need_symbol('c_wchar')
137n/a def test_c_wchar(self):
138n/a result = self.fail_fields(("a", c_wchar, 1))
139n/a self.assertEqual(result,
140n/a (TypeError, 'bit fields not allowed for type c_wchar'))
141n/a
142n/a def test_single_bitfield_size(self):
143n/a for c_typ in int_types:
144n/a result = self.fail_fields(("a", c_typ, -1))
145n/a self.assertEqual(result, (ValueError, 'number of bits invalid for bit field'))
146n/a
147n/a result = self.fail_fields(("a", c_typ, 0))
148n/a self.assertEqual(result, (ValueError, 'number of bits invalid for bit field'))
149n/a
150n/a class X(Structure):
151n/a _fields_ = [("a", c_typ, 1)]
152n/a self.assertEqual(sizeof(X), sizeof(c_typ))
153n/a
154n/a class X(Structure):
155n/a _fields_ = [("a", c_typ, sizeof(c_typ)*8)]
156n/a self.assertEqual(sizeof(X), sizeof(c_typ))
157n/a
158n/a result = self.fail_fields(("a", c_typ, sizeof(c_typ)*8 + 1))
159n/a self.assertEqual(result, (ValueError, 'number of bits invalid for bit field'))
160n/a
161n/a def test_multi_bitfields_size(self):
162n/a class X(Structure):
163n/a _fields_ = [("a", c_short, 1),
164n/a ("b", c_short, 14),
165n/a ("c", c_short, 1)]
166n/a self.assertEqual(sizeof(X), sizeof(c_short))
167n/a
168n/a class X(Structure):
169n/a _fields_ = [("a", c_short, 1),
170n/a ("a1", c_short),
171n/a ("b", c_short, 14),
172n/a ("c", c_short, 1)]
173n/a self.assertEqual(sizeof(X), sizeof(c_short)*3)
174n/a self.assertEqual(X.a.offset, 0)
175n/a self.assertEqual(X.a1.offset, sizeof(c_short))
176n/a self.assertEqual(X.b.offset, sizeof(c_short)*2)
177n/a self.assertEqual(X.c.offset, sizeof(c_short)*2)
178n/a
179n/a class X(Structure):
180n/a _fields_ = [("a", c_short, 3),
181n/a ("b", c_short, 14),
182n/a ("c", c_short, 14)]
183n/a self.assertEqual(sizeof(X), sizeof(c_short)*3)
184n/a self.assertEqual(X.a.offset, sizeof(c_short)*0)
185n/a self.assertEqual(X.b.offset, sizeof(c_short)*1)
186n/a self.assertEqual(X.c.offset, sizeof(c_short)*2)
187n/a
188n/a
189n/a def get_except(self, func, *args, **kw):
190n/a try:
191n/a func(*args, **kw)
192n/a except Exception as detail:
193n/a return detail.__class__, str(detail)
194n/a
195n/a def test_mixed_1(self):
196n/a class X(Structure):
197n/a _fields_ = [("a", c_byte, 4),
198n/a ("b", c_int, 4)]
199n/a if os.name == "nt":
200n/a self.assertEqual(sizeof(X), sizeof(c_int)*2)
201n/a else:
202n/a self.assertEqual(sizeof(X), sizeof(c_int))
203n/a
204n/a def test_mixed_2(self):
205n/a class X(Structure):
206n/a _fields_ = [("a", c_byte, 4),
207n/a ("b", c_int, 32)]
208n/a self.assertEqual(sizeof(X), alignment(c_int)+sizeof(c_int))
209n/a
210n/a def test_mixed_3(self):
211n/a class X(Structure):
212n/a _fields_ = [("a", c_byte, 4),
213n/a ("b", c_ubyte, 4)]
214n/a self.assertEqual(sizeof(X), sizeof(c_byte))
215n/a
216n/a def test_mixed_4(self):
217n/a class X(Structure):
218n/a _fields_ = [("a", c_short, 4),
219n/a ("b", c_short, 4),
220n/a ("c", c_int, 24),
221n/a ("d", c_short, 4),
222n/a ("e", c_short, 4),
223n/a ("f", c_int, 24)]
224n/a # MSVC does NOT combine c_short and c_int into one field, GCC
225n/a # does (unless GCC is run with '-mms-bitfields' which
226n/a # produces code compatible with MSVC).
227n/a if os.name == "nt":
228n/a self.assertEqual(sizeof(X), sizeof(c_int) * 4)
229n/a else:
230n/a self.assertEqual(sizeof(X), sizeof(c_int) * 2)
231n/a
232n/a def test_anon_bitfields(self):
233n/a # anonymous bit-fields gave a strange error message
234n/a class X(Structure):
235n/a _fields_ = [("a", c_byte, 4),
236n/a ("b", c_ubyte, 4)]
237n/a class Y(Structure):
238n/a _anonymous_ = ["_"]
239n/a _fields_ = [("_", X)]
240n/a
241n/a @need_symbol('c_uint32')
242n/a def test_uint32(self):
243n/a class X(Structure):
244n/a _fields_ = [("a", c_uint32, 32)]
245n/a x = X()
246n/a x.a = 10
247n/a self.assertEqual(x.a, 10)
248n/a x.a = 0xFDCBA987
249n/a self.assertEqual(x.a, 0xFDCBA987)
250n/a
251n/a @need_symbol('c_uint64')
252n/a def test_uint64(self):
253n/a class X(Structure):
254n/a _fields_ = [("a", c_uint64, 64)]
255n/a x = X()
256n/a x.a = 10
257n/a self.assertEqual(x.a, 10)
258n/a x.a = 0xFEDCBA9876543211
259n/a self.assertEqual(x.a, 0xFEDCBA9876543211)
260n/a
261n/a @need_symbol('c_uint32')
262n/a def test_uint32_swap_little_endian(self):
263n/a # Issue #23319
264n/a class Little(LittleEndianStructure):
265n/a _fields_ = [("a", c_uint32, 24),
266n/a ("b", c_uint32, 4),
267n/a ("c", c_uint32, 4)]
268n/a b = bytearray(4)
269n/a x = Little.from_buffer(b)
270n/a x.a = 0xabcdef
271n/a x.b = 1
272n/a x.c = 2
273n/a self.assertEqual(b, b'\xef\xcd\xab\x21')
274n/a
275n/a @need_symbol('c_uint32')
276n/a def test_uint32_swap_big_endian(self):
277n/a # Issue #23319
278n/a class Big(BigEndianStructure):
279n/a _fields_ = [("a", c_uint32, 24),
280n/a ("b", c_uint32, 4),
281n/a ("c", c_uint32, 4)]
282n/a b = bytearray(4)
283n/a x = Big.from_buffer(b)
284n/a x.a = 0xabcdef
285n/a x.b = 1
286n/a x.c = 2
287n/a self.assertEqual(b, b'\xab\xcd\xef\x12')
288n/a
289n/aif __name__ == "__main__":
290n/a unittest.main()