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

Python code coverage for Lib/test/test_structmembers.py

#countcontent
1n/aimport unittest
2n/afrom test import support
3n/a
4n/a# Skip this test if the _testcapi module isn't available.
5n/asupport.import_module('_testcapi')
6n/afrom _testcapi import _test_structmembersType, \
7n/a CHAR_MAX, CHAR_MIN, UCHAR_MAX, \
8n/a SHRT_MAX, SHRT_MIN, USHRT_MAX, \
9n/a INT_MAX, INT_MIN, UINT_MAX, \
10n/a LONG_MAX, LONG_MIN, ULONG_MAX, \
11n/a LLONG_MAX, LLONG_MIN, ULLONG_MAX, \
12n/a PY_SSIZE_T_MAX, PY_SSIZE_T_MIN
13n/a
14n/ats=_test_structmembersType(False, # T_BOOL
15n/a 1, # T_BYTE
16n/a 2, # T_UBYTE
17n/a 3, # T_SHORT
18n/a 4, # T_USHORT
19n/a 5, # T_INT
20n/a 6, # T_UINT
21n/a 7, # T_LONG
22n/a 8, # T_ULONG
23n/a 23, # T_PYSSIZET
24n/a 9.99999,# T_FLOAT
25n/a 10.1010101010, # T_DOUBLE
26n/a "hi" # T_STRING_INPLACE
27n/a )
28n/a
29n/aclass ReadWriteTests(unittest.TestCase):
30n/a
31n/a def test_bool(self):
32n/a ts.T_BOOL = True
33n/a self.assertEqual(ts.T_BOOL, True)
34n/a ts.T_BOOL = False
35n/a self.assertEqual(ts.T_BOOL, False)
36n/a self.assertRaises(TypeError, setattr, ts, 'T_BOOL', 1)
37n/a
38n/a def test_byte(self):
39n/a ts.T_BYTE = CHAR_MAX
40n/a self.assertEqual(ts.T_BYTE, CHAR_MAX)
41n/a ts.T_BYTE = CHAR_MIN
42n/a self.assertEqual(ts.T_BYTE, CHAR_MIN)
43n/a ts.T_UBYTE = UCHAR_MAX
44n/a self.assertEqual(ts.T_UBYTE, UCHAR_MAX)
45n/a
46n/a def test_short(self):
47n/a ts.T_SHORT = SHRT_MAX
48n/a self.assertEqual(ts.T_SHORT, SHRT_MAX)
49n/a ts.T_SHORT = SHRT_MIN
50n/a self.assertEqual(ts.T_SHORT, SHRT_MIN)
51n/a ts.T_USHORT = USHRT_MAX
52n/a self.assertEqual(ts.T_USHORT, USHRT_MAX)
53n/a
54n/a def test_int(self):
55n/a ts.T_INT = INT_MAX
56n/a self.assertEqual(ts.T_INT, INT_MAX)
57n/a ts.T_INT = INT_MIN
58n/a self.assertEqual(ts.T_INT, INT_MIN)
59n/a ts.T_UINT = UINT_MAX
60n/a self.assertEqual(ts.T_UINT, UINT_MAX)
61n/a
62n/a def test_long(self):
63n/a ts.T_LONG = LONG_MAX
64n/a self.assertEqual(ts.T_LONG, LONG_MAX)
65n/a ts.T_LONG = LONG_MIN
66n/a self.assertEqual(ts.T_LONG, LONG_MIN)
67n/a ts.T_ULONG = ULONG_MAX
68n/a self.assertEqual(ts.T_ULONG, ULONG_MAX)
69n/a
70n/a def test_py_ssize_t(self):
71n/a ts.T_PYSSIZET = PY_SSIZE_T_MAX
72n/a self.assertEqual(ts.T_PYSSIZET, PY_SSIZE_T_MAX)
73n/a ts.T_PYSSIZET = PY_SSIZE_T_MIN
74n/a self.assertEqual(ts.T_PYSSIZET, PY_SSIZE_T_MIN)
75n/a
76n/a @unittest.skipUnless(hasattr(ts, "T_LONGLONG"), "long long not present")
77n/a def test_longlong(self):
78n/a ts.T_LONGLONG = LLONG_MAX
79n/a self.assertEqual(ts.T_LONGLONG, LLONG_MAX)
80n/a ts.T_LONGLONG = LLONG_MIN
81n/a self.assertEqual(ts.T_LONGLONG, LLONG_MIN)
82n/a
83n/a ts.T_ULONGLONG = ULLONG_MAX
84n/a self.assertEqual(ts.T_ULONGLONG, ULLONG_MAX)
85n/a
86n/a ## make sure these will accept a plain int as well as a long
87n/a ts.T_LONGLONG = 3
88n/a self.assertEqual(ts.T_LONGLONG, 3)
89n/a ts.T_ULONGLONG = 4
90n/a self.assertEqual(ts.T_ULONGLONG, 4)
91n/a
92n/a def test_bad_assignments(self):
93n/a integer_attributes = [
94n/a 'T_BOOL',
95n/a 'T_BYTE', 'T_UBYTE',
96n/a 'T_SHORT', 'T_USHORT',
97n/a 'T_INT', 'T_UINT',
98n/a 'T_LONG', 'T_ULONG',
99n/a 'T_PYSSIZET'
100n/a ]
101n/a if hasattr(ts, 'T_LONGLONG'):
102n/a integer_attributes.extend(['T_LONGLONG', 'T_ULONGLONG'])
103n/a
104n/a # issue8014: this produced 'bad argument to internal function'
105n/a # internal error
106n/a for nonint in None, 3.2j, "full of eels", {}, []:
107n/a for attr in integer_attributes:
108n/a self.assertRaises(TypeError, setattr, ts, attr, nonint)
109n/a
110n/a def test_inplace_string(self):
111n/a self.assertEqual(ts.T_STRING_INPLACE, "hi")
112n/a self.assertRaises(TypeError, setattr, ts, "T_STRING_INPLACE", "s")
113n/a self.assertRaises(TypeError, delattr, ts, "T_STRING_INPLACE")
114n/a
115n/a
116n/aclass TestWarnings(unittest.TestCase):
117n/a
118n/a def test_byte_max(self):
119n/a with support.check_warnings(('', RuntimeWarning)):
120n/a ts.T_BYTE = CHAR_MAX+1
121n/a
122n/a def test_byte_min(self):
123n/a with support.check_warnings(('', RuntimeWarning)):
124n/a ts.T_BYTE = CHAR_MIN-1
125n/a
126n/a def test_ubyte_max(self):
127n/a with support.check_warnings(('', RuntimeWarning)):
128n/a ts.T_UBYTE = UCHAR_MAX+1
129n/a
130n/a def test_short_max(self):
131n/a with support.check_warnings(('', RuntimeWarning)):
132n/a ts.T_SHORT = SHRT_MAX+1
133n/a
134n/a def test_short_min(self):
135n/a with support.check_warnings(('', RuntimeWarning)):
136n/a ts.T_SHORT = SHRT_MIN-1
137n/a
138n/a def test_ushort_max(self):
139n/a with support.check_warnings(('', RuntimeWarning)):
140n/a ts.T_USHORT = USHRT_MAX+1
141n/a
142n/a
143n/aif __name__ == "__main__":
144n/a unittest.main()