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

Python code coverage for Lib/test/test_bool.py

#countcontent
1n/a# Test properties of bool promised by PEP 285
2n/a
3n/aimport unittest
4n/afrom test import support
5n/a
6n/aimport os
7n/a
8n/aclass BoolTest(unittest.TestCase):
9n/a
10n/a def test_subclass(self):
11n/a try:
12n/a class C(bool):
13n/a pass
14n/a except TypeError:
15n/a pass
16n/a else:
17n/a self.fail("bool should not be subclassable")
18n/a
19n/a self.assertRaises(TypeError, int.__new__, bool, 0)
20n/a
21n/a def test_print(self):
22n/a try:
23n/a fo = open(support.TESTFN, "w")
24n/a print(False, True, file=fo)
25n/a fo.close()
26n/a fo = open(support.TESTFN, "r")
27n/a self.assertEqual(fo.read(), 'False True\n')
28n/a finally:
29n/a fo.close()
30n/a os.remove(support.TESTFN)
31n/a
32n/a def test_repr(self):
33n/a self.assertEqual(repr(False), 'False')
34n/a self.assertEqual(repr(True), 'True')
35n/a self.assertEqual(eval(repr(False)), False)
36n/a self.assertEqual(eval(repr(True)), True)
37n/a
38n/a def test_str(self):
39n/a self.assertEqual(str(False), 'False')
40n/a self.assertEqual(str(True), 'True')
41n/a
42n/a def test_int(self):
43n/a self.assertEqual(int(False), 0)
44n/a self.assertIsNot(int(False), False)
45n/a self.assertEqual(int(True), 1)
46n/a self.assertIsNot(int(True), True)
47n/a
48n/a def test_float(self):
49n/a self.assertEqual(float(False), 0.0)
50n/a self.assertIsNot(float(False), False)
51n/a self.assertEqual(float(True), 1.0)
52n/a self.assertIsNot(float(True), True)
53n/a
54n/a def test_math(self):
55n/a self.assertEqual(+False, 0)
56n/a self.assertIsNot(+False, False)
57n/a self.assertEqual(-False, 0)
58n/a self.assertIsNot(-False, False)
59n/a self.assertEqual(abs(False), 0)
60n/a self.assertIsNot(abs(False), False)
61n/a self.assertEqual(+True, 1)
62n/a self.assertIsNot(+True, True)
63n/a self.assertEqual(-True, -1)
64n/a self.assertEqual(abs(True), 1)
65n/a self.assertIsNot(abs(True), True)
66n/a self.assertEqual(~False, -1)
67n/a self.assertEqual(~True, -2)
68n/a
69n/a self.assertEqual(False+2, 2)
70n/a self.assertEqual(True+2, 3)
71n/a self.assertEqual(2+False, 2)
72n/a self.assertEqual(2+True, 3)
73n/a
74n/a self.assertEqual(False+False, 0)
75n/a self.assertIsNot(False+False, False)
76n/a self.assertEqual(False+True, 1)
77n/a self.assertIsNot(False+True, True)
78n/a self.assertEqual(True+False, 1)
79n/a self.assertIsNot(True+False, True)
80n/a self.assertEqual(True+True, 2)
81n/a
82n/a self.assertEqual(True-True, 0)
83n/a self.assertIsNot(True-True, False)
84n/a self.assertEqual(False-False, 0)
85n/a self.assertIsNot(False-False, False)
86n/a self.assertEqual(True-False, 1)
87n/a self.assertIsNot(True-False, True)
88n/a self.assertEqual(False-True, -1)
89n/a
90n/a self.assertEqual(True*1, 1)
91n/a self.assertEqual(False*1, 0)
92n/a self.assertIsNot(False*1, False)
93n/a
94n/a self.assertEqual(True/1, 1)
95n/a self.assertIsNot(True/1, True)
96n/a self.assertEqual(False/1, 0)
97n/a self.assertIsNot(False/1, False)
98n/a
99n/a self.assertEqual(True%1, 0)
100n/a self.assertIsNot(True%1, False)
101n/a self.assertEqual(True%2, 1)
102n/a self.assertIsNot(True%2, True)
103n/a self.assertEqual(False%1, 0)
104n/a self.assertIsNot(False%1, False)
105n/a
106n/a for b in False, True:
107n/a for i in 0, 1, 2:
108n/a self.assertEqual(b**i, int(b)**i)
109n/a self.assertIsNot(b**i, bool(int(b)**i))
110n/a
111n/a for a in False, True:
112n/a for b in False, True:
113n/a self.assertIs(a&b, bool(int(a)&int(b)))
114n/a self.assertIs(a|b, bool(int(a)|int(b)))
115n/a self.assertIs(a^b, bool(int(a)^int(b)))
116n/a self.assertEqual(a&int(b), int(a)&int(b))
117n/a self.assertIsNot(a&int(b), bool(int(a)&int(b)))
118n/a self.assertEqual(a|int(b), int(a)|int(b))
119n/a self.assertIsNot(a|int(b), bool(int(a)|int(b)))
120n/a self.assertEqual(a^int(b), int(a)^int(b))
121n/a self.assertIsNot(a^int(b), bool(int(a)^int(b)))
122n/a self.assertEqual(int(a)&b, int(a)&int(b))
123n/a self.assertIsNot(int(a)&b, bool(int(a)&int(b)))
124n/a self.assertEqual(int(a)|b, int(a)|int(b))
125n/a self.assertIsNot(int(a)|b, bool(int(a)|int(b)))
126n/a self.assertEqual(int(a)^b, int(a)^int(b))
127n/a self.assertIsNot(int(a)^b, bool(int(a)^int(b)))
128n/a
129n/a self.assertIs(1==1, True)
130n/a self.assertIs(1==0, False)
131n/a self.assertIs(0<1, True)
132n/a self.assertIs(1<0, False)
133n/a self.assertIs(0<=0, True)
134n/a self.assertIs(1<=0, False)
135n/a self.assertIs(1>0, True)
136n/a self.assertIs(1>1, False)
137n/a self.assertIs(1>=1, True)
138n/a self.assertIs(0>=1, False)
139n/a self.assertIs(0!=1, True)
140n/a self.assertIs(0!=0, False)
141n/a
142n/a x = [1]
143n/a self.assertIs(x is x, True)
144n/a self.assertIs(x is not x, False)
145n/a
146n/a self.assertIs(1 in x, True)
147n/a self.assertIs(0 in x, False)
148n/a self.assertIs(1 not in x, False)
149n/a self.assertIs(0 not in x, True)
150n/a
151n/a x = {1: 2}
152n/a self.assertIs(x is x, True)
153n/a self.assertIs(x is not x, False)
154n/a
155n/a self.assertIs(1 in x, True)
156n/a self.assertIs(0 in x, False)
157n/a self.assertIs(1 not in x, False)
158n/a self.assertIs(0 not in x, True)
159n/a
160n/a self.assertIs(not True, False)
161n/a self.assertIs(not False, True)
162n/a
163n/a def test_convert(self):
164n/a self.assertRaises(TypeError, bool, 42, 42)
165n/a self.assertIs(bool(10), True)
166n/a self.assertIs(bool(1), True)
167n/a self.assertIs(bool(-1), True)
168n/a self.assertIs(bool(0), False)
169n/a self.assertIs(bool("hello"), True)
170n/a self.assertIs(bool(""), False)
171n/a self.assertIs(bool(), False)
172n/a
173n/a def test_format(self):
174n/a self.assertEqual("%d" % False, "0")
175n/a self.assertEqual("%d" % True, "1")
176n/a self.assertEqual("%x" % False, "0")
177n/a self.assertEqual("%x" % True, "1")
178n/a
179n/a def test_hasattr(self):
180n/a self.assertIs(hasattr([], "append"), True)
181n/a self.assertIs(hasattr([], "wobble"), False)
182n/a
183n/a def test_callable(self):
184n/a self.assertIs(callable(len), True)
185n/a self.assertIs(callable(1), False)
186n/a
187n/a def test_isinstance(self):
188n/a self.assertIs(isinstance(True, bool), True)
189n/a self.assertIs(isinstance(False, bool), True)
190n/a self.assertIs(isinstance(True, int), True)
191n/a self.assertIs(isinstance(False, int), True)
192n/a self.assertIs(isinstance(1, bool), False)
193n/a self.assertIs(isinstance(0, bool), False)
194n/a
195n/a def test_issubclass(self):
196n/a self.assertIs(issubclass(bool, int), True)
197n/a self.assertIs(issubclass(int, bool), False)
198n/a
199n/a def test_contains(self):
200n/a self.assertIs(1 in {}, False)
201n/a self.assertIs(1 in {1:1}, True)
202n/a
203n/a def test_string(self):
204n/a self.assertIs("xyz".endswith("z"), True)
205n/a self.assertIs("xyz".endswith("x"), False)
206n/a self.assertIs("xyz0123".isalnum(), True)
207n/a self.assertIs("@#$%".isalnum(), False)
208n/a self.assertIs("xyz".isalpha(), True)
209n/a self.assertIs("@#$%".isalpha(), False)
210n/a self.assertIs("0123".isdigit(), True)
211n/a self.assertIs("xyz".isdigit(), False)
212n/a self.assertIs("xyz".islower(), True)
213n/a self.assertIs("XYZ".islower(), False)
214n/a self.assertIs("0123".isdecimal(), True)
215n/a self.assertIs("xyz".isdecimal(), False)
216n/a self.assertIs("0123".isnumeric(), True)
217n/a self.assertIs("xyz".isnumeric(), False)
218n/a self.assertIs(" ".isspace(), True)
219n/a self.assertIs("\xa0".isspace(), True)
220n/a self.assertIs("\u3000".isspace(), True)
221n/a self.assertIs("XYZ".isspace(), False)
222n/a self.assertIs("X".istitle(), True)
223n/a self.assertIs("x".istitle(), False)
224n/a self.assertIs("XYZ".isupper(), True)
225n/a self.assertIs("xyz".isupper(), False)
226n/a self.assertIs("xyz".startswith("x"), True)
227n/a self.assertIs("xyz".startswith("z"), False)
228n/a
229n/a def test_boolean(self):
230n/a self.assertEqual(True & 1, 1)
231n/a self.assertNotIsInstance(True & 1, bool)
232n/a self.assertIs(True & True, True)
233n/a
234n/a self.assertEqual(True | 1, 1)
235n/a self.assertNotIsInstance(True | 1, bool)
236n/a self.assertIs(True | True, True)
237n/a
238n/a self.assertEqual(True ^ 1, 0)
239n/a self.assertNotIsInstance(True ^ 1, bool)
240n/a self.assertIs(True ^ True, False)
241n/a
242n/a def test_fileclosed(self):
243n/a try:
244n/a f = open(support.TESTFN, "w")
245n/a self.assertIs(f.closed, False)
246n/a f.close()
247n/a self.assertIs(f.closed, True)
248n/a finally:
249n/a os.remove(support.TESTFN)
250n/a
251n/a def test_types(self):
252n/a # types are always true.
253n/a for t in [bool, complex, dict, float, int, list, object,
254n/a set, str, tuple, type]:
255n/a self.assertIs(bool(t), True)
256n/a
257n/a def test_operator(self):
258n/a import operator
259n/a self.assertIs(operator.truth(0), False)
260n/a self.assertIs(operator.truth(1), True)
261n/a self.assertIs(operator.not_(1), False)
262n/a self.assertIs(operator.not_(0), True)
263n/a self.assertIs(operator.contains([], 1), False)
264n/a self.assertIs(operator.contains([1], 1), True)
265n/a self.assertIs(operator.lt(0, 0), False)
266n/a self.assertIs(operator.lt(0, 1), True)
267n/a self.assertIs(operator.is_(True, True), True)
268n/a self.assertIs(operator.is_(True, False), False)
269n/a self.assertIs(operator.is_not(True, True), False)
270n/a self.assertIs(operator.is_not(True, False), True)
271n/a
272n/a def test_marshal(self):
273n/a import marshal
274n/a self.assertIs(marshal.loads(marshal.dumps(True)), True)
275n/a self.assertIs(marshal.loads(marshal.dumps(False)), False)
276n/a
277n/a def test_pickle(self):
278n/a import pickle
279n/a for proto in range(pickle.HIGHEST_PROTOCOL + 1):
280n/a self.assertIs(pickle.loads(pickle.dumps(True, proto)), True)
281n/a self.assertIs(pickle.loads(pickle.dumps(False, proto)), False)
282n/a
283n/a def test_picklevalues(self):
284n/a # Test for specific backwards-compatible pickle values
285n/a import pickle
286n/a self.assertEqual(pickle.dumps(True, protocol=0), b"I01\n.")
287n/a self.assertEqual(pickle.dumps(False, protocol=0), b"I00\n.")
288n/a self.assertEqual(pickle.dumps(True, protocol=1), b"I01\n.")
289n/a self.assertEqual(pickle.dumps(False, protocol=1), b"I00\n.")
290n/a self.assertEqual(pickle.dumps(True, protocol=2), b'\x80\x02\x88.')
291n/a self.assertEqual(pickle.dumps(False, protocol=2), b'\x80\x02\x89.')
292n/a
293n/a def test_convert_to_bool(self):
294n/a # Verify that TypeError occurs when bad things are returned
295n/a # from __bool__(). This isn't really a bool test, but
296n/a # it's related.
297n/a check = lambda o: self.assertRaises(TypeError, bool, o)
298n/a class Foo(object):
299n/a def __bool__(self):
300n/a return self
301n/a check(Foo())
302n/a
303n/a class Bar(object):
304n/a def __bool__(self):
305n/a return "Yes"
306n/a check(Bar())
307n/a
308n/a class Baz(int):
309n/a def __bool__(self):
310n/a return self
311n/a check(Baz())
312n/a
313n/a # __bool__() must return a bool not an int
314n/a class Spam(int):
315n/a def __bool__(self):
316n/a return 1
317n/a check(Spam())
318n/a
319n/a class Eggs:
320n/a def __len__(self):
321n/a return -1
322n/a self.assertRaises(ValueError, bool, Eggs())
323n/a
324n/a def test_from_bytes(self):
325n/a self.assertIs(bool.from_bytes(b'\x00'*8, 'big'), False)
326n/a self.assertIs(bool.from_bytes(b'abcd', 'little'), True)
327n/a
328n/a def test_sane_len(self):
329n/a # this test just tests our assumptions about __len__
330n/a # this will start failing if __len__ changes assertions
331n/a for badval in ['illegal', -1, 1 << 32]:
332n/a class A:
333n/a def __len__(self):
334n/a return badval
335n/a try:
336n/a bool(A())
337n/a except (Exception) as e_bool:
338n/a try:
339n/a len(A())
340n/a except (Exception) as e_len:
341n/a self.assertEqual(str(e_bool), str(e_len))
342n/a
343n/a def test_blocked(self):
344n/a class A:
345n/a __bool__ = None
346n/a self.assertRaises(TypeError, bool, A())
347n/a
348n/a class B:
349n/a def __len__(self):
350n/a return 10
351n/a __bool__ = None
352n/a self.assertRaises(TypeError, bool, B())
353n/a
354n/a def test_real_and_imag(self):
355n/a self.assertEqual(True.real, 1)
356n/a self.assertEqual(True.imag, 0)
357n/a self.assertIs(type(True.real), int)
358n/a self.assertIs(type(True.imag), int)
359n/a self.assertEqual(False.real, 0)
360n/a self.assertEqual(False.imag, 0)
361n/a self.assertIs(type(False.real), int)
362n/a self.assertIs(type(False.imag), int)
363n/a
364n/adef test_main():
365n/a support.run_unittest(BoolTest)
366n/a
367n/aif __name__ == "__main__":
368n/a test_main()