blob: d30a3b9c0f24b2b5a6a9f25f21df886507f2cc91 [file] [log] [blame]
Guido van Rossum5f820362002-04-03 23:01:45 +00001# Test properties of bool promised by PEP 285
2
Walter Dörwalddbcede52003-04-25 10:22:01 +00003import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test import support
Walter Dörwalddbcede52003-04-25 10:22:01 +00005
Martin v. Löwis2f6d4da2002-04-14 10:22:29 +00006import os
Guido van Rossum5f820362002-04-03 23:01:45 +00007
Walter Dörwalddbcede52003-04-25 10:22:01 +00008class BoolTest(unittest.TestCase):
Guido van Rossum5f820362002-04-03 23:01:45 +00009
Walter Dörwalddbcede52003-04-25 10:22:01 +000010 def test_subclass(self):
11 try:
12 class C(bool):
13 pass
14 except TypeError:
15 pass
16 else:
17 self.fail("bool should not be subclassable")
Martin v. Löwis2f6d4da2002-04-14 10:22:29 +000018
Walter Dörwalddbcede52003-04-25 10:22:01 +000019 self.assertRaises(TypeError, int.__new__, bool, 0)
Guido van Rossum5f820362002-04-03 23:01:45 +000020
Walter Dörwalddbcede52003-04-25 10:22:01 +000021 def test_print(self):
22 try:
Benjamin Petersonee8712c2008-05-20 21:35:26 +000023 fo = open(support.TESTFN, "w")
Guido van Rossumbe19ed72007-02-09 05:37:30 +000024 print(False, True, file=fo)
Walter Dörwalddbcede52003-04-25 10:22:01 +000025 fo.close()
Benjamin Petersonee8712c2008-05-20 21:35:26 +000026 fo = open(support.TESTFN, "r")
Walter Dörwalddbcede52003-04-25 10:22:01 +000027 self.assertEqual(fo.read(), 'False True\n')
28 finally:
29 fo.close()
Benjamin Petersonee8712c2008-05-20 21:35:26 +000030 os.remove(support.TESTFN)
Guido van Rossum5f820362002-04-03 23:01:45 +000031
Walter Dörwalddbcede52003-04-25 10:22:01 +000032 def test_repr(self):
33 self.assertEqual(repr(False), 'False')
34 self.assertEqual(repr(True), 'True')
35 self.assertEqual(eval(repr(False)), False)
36 self.assertEqual(eval(repr(True)), True)
Guido van Rossum5f820362002-04-03 23:01:45 +000037
Walter Dörwalddbcede52003-04-25 10:22:01 +000038 def test_str(self):
39 self.assertEqual(str(False), 'False')
40 self.assertEqual(str(True), 'True')
Guido van Rossum5f820362002-04-03 23:01:45 +000041
Walter Dörwalddbcede52003-04-25 10:22:01 +000042 def test_int(self):
43 self.assertEqual(int(False), 0)
44 self.assertIsNot(int(False), False)
45 self.assertEqual(int(True), 1)
46 self.assertIsNot(int(True), True)
Guido van Rossum5f820362002-04-03 23:01:45 +000047
Antoine Pitrou503ab332010-03-30 18:56:19 +000048 def test_float(self):
49 self.assertEqual(float(False), 0.0)
50 self.assertIsNot(float(False), False)
51 self.assertEqual(float(True), 1.0)
52 self.assertIsNot(float(True), True)
53
Walter Dörwalddbcede52003-04-25 10:22:01 +000054 def test_math(self):
55 self.assertEqual(+False, 0)
56 self.assertIsNot(+False, False)
57 self.assertEqual(-False, 0)
58 self.assertIsNot(-False, False)
59 self.assertEqual(abs(False), 0)
60 self.assertIsNot(abs(False), False)
61 self.assertEqual(+True, 1)
62 self.assertIsNot(+True, True)
63 self.assertEqual(-True, -1)
64 self.assertEqual(abs(True), 1)
65 self.assertIsNot(abs(True), True)
66 self.assertEqual(~False, -1)
67 self.assertEqual(~True, -2)
Guido van Rossum5f820362002-04-03 23:01:45 +000068
Walter Dörwalddbcede52003-04-25 10:22:01 +000069 self.assertEqual(False+2, 2)
70 self.assertEqual(True+2, 3)
71 self.assertEqual(2+False, 2)
72 self.assertEqual(2+True, 3)
Guido van Rossum5f820362002-04-03 23:01:45 +000073
Walter Dörwalddbcede52003-04-25 10:22:01 +000074 self.assertEqual(False+False, 0)
75 self.assertIsNot(False+False, False)
76 self.assertEqual(False+True, 1)
77 self.assertIsNot(False+True, True)
78 self.assertEqual(True+False, 1)
79 self.assertIsNot(True+False, True)
80 self.assertEqual(True+True, 2)
Guido van Rossum5f820362002-04-03 23:01:45 +000081
Walter Dörwalddbcede52003-04-25 10:22:01 +000082 self.assertEqual(True-True, 0)
83 self.assertIsNot(True-True, False)
84 self.assertEqual(False-False, 0)
85 self.assertIsNot(False-False, False)
86 self.assertEqual(True-False, 1)
87 self.assertIsNot(True-False, True)
88 self.assertEqual(False-True, -1)
Guido van Rossum5f820362002-04-03 23:01:45 +000089
Walter Dörwalddbcede52003-04-25 10:22:01 +000090 self.assertEqual(True*1, 1)
91 self.assertEqual(False*1, 0)
92 self.assertIsNot(False*1, False)
Guido van Rossum5f820362002-04-03 23:01:45 +000093
Walter Dörwalddbcede52003-04-25 10:22:01 +000094 self.assertEqual(True/1, 1)
95 self.assertIsNot(True/1, True)
96 self.assertEqual(False/1, 0)
97 self.assertIsNot(False/1, False)
Guido van Rossum5f820362002-04-03 23:01:45 +000098
Walter Dörwalddbcede52003-04-25 10:22:01 +000099 for b in False, True:
100 for i in 0, 1, 2:
101 self.assertEqual(b**i, int(b)**i)
102 self.assertIsNot(b**i, bool(int(b)**i))
Guido van Rossum5f820362002-04-03 23:01:45 +0000103
Walter Dörwalddbcede52003-04-25 10:22:01 +0000104 for a in False, True:
105 for b in False, True:
106 self.assertIs(a&b, bool(int(a)&int(b)))
107 self.assertIs(a|b, bool(int(a)|int(b)))
108 self.assertIs(a^b, bool(int(a)^int(b)))
109 self.assertEqual(a&int(b), int(a)&int(b))
110 self.assertIsNot(a&int(b), bool(int(a)&int(b)))
111 self.assertEqual(a|int(b), int(a)|int(b))
112 self.assertIsNot(a|int(b), bool(int(a)|int(b)))
113 self.assertEqual(a^int(b), int(a)^int(b))
114 self.assertIsNot(a^int(b), bool(int(a)^int(b)))
115 self.assertEqual(int(a)&b, int(a)&int(b))
116 self.assertIsNot(int(a)&b, bool(int(a)&int(b)))
117 self.assertEqual(int(a)|b, int(a)|int(b))
118 self.assertIsNot(int(a)|b, bool(int(a)|int(b)))
119 self.assertEqual(int(a)^b, int(a)^int(b))
120 self.assertIsNot(int(a)^b, bool(int(a)^int(b)))
Guido van Rossum5f820362002-04-03 23:01:45 +0000121
Walter Dörwalddbcede52003-04-25 10:22:01 +0000122 self.assertIs(1==1, True)
123 self.assertIs(1==0, False)
124 self.assertIs(0<1, True)
125 self.assertIs(1<0, False)
126 self.assertIs(0<=0, True)
127 self.assertIs(1<=0, False)
128 self.assertIs(1>0, True)
129 self.assertIs(1>1, False)
130 self.assertIs(1>=1, True)
131 self.assertIs(0>=1, False)
132 self.assertIs(0!=1, True)
133 self.assertIs(0!=0, False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000134
Walter Dörwalddbcede52003-04-25 10:22:01 +0000135 x = [1]
136 self.assertIs(x is x, True)
137 self.assertIs(x is not x, False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000138
Walter Dörwalddbcede52003-04-25 10:22:01 +0000139 self.assertIs(1 in x, True)
140 self.assertIs(0 in x, False)
141 self.assertIs(1 not in x, False)
142 self.assertIs(0 not in x, True)
Guido van Rossum5f820362002-04-03 23:01:45 +0000143
Walter Dörwalddbcede52003-04-25 10:22:01 +0000144 x = {1: 2}
145 self.assertIs(x is x, True)
146 self.assertIs(x is not x, False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000147
Walter Dörwalddbcede52003-04-25 10:22:01 +0000148 self.assertIs(1 in x, True)
149 self.assertIs(0 in x, False)
150 self.assertIs(1 not in x, False)
151 self.assertIs(0 not in x, True)
Guido van Rossum5f820362002-04-03 23:01:45 +0000152
Walter Dörwalddbcede52003-04-25 10:22:01 +0000153 self.assertIs(not True, False)
154 self.assertIs(not False, True)
Guido van Rossum5f820362002-04-03 23:01:45 +0000155
Walter Dörwalddbcede52003-04-25 10:22:01 +0000156 def test_convert(self):
157 self.assertRaises(TypeError, bool, 42, 42)
158 self.assertIs(bool(10), True)
159 self.assertIs(bool(1), True)
160 self.assertIs(bool(-1), True)
161 self.assertIs(bool(0), False)
162 self.assertIs(bool("hello"), True)
163 self.assertIs(bool(""), False)
164 self.assertIs(bool(), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000165
Martin v. Löwisff398c62007-08-14 21:57:32 +0000166 def test_format(self):
167 self.assertEqual("%d" % False, "0")
168 self.assertEqual("%d" % True, "1")
169 self.assertEqual("%x" % False, "0")
170 self.assertEqual("%x" % True, "1")
171
Walter Dörwalddbcede52003-04-25 10:22:01 +0000172 def test_hasattr(self):
173 self.assertIs(hasattr([], "append"), True)
174 self.assertIs(hasattr([], "wobble"), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000175
Walter Dörwalddbcede52003-04-25 10:22:01 +0000176 def test_callable(self):
Antoine Pitroue71362d2010-11-27 22:00:11 +0000177 self.assertIs(callable(len), True)
178 self.assertIs(callable(1), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000179
Walter Dörwalddbcede52003-04-25 10:22:01 +0000180 def test_isinstance(self):
181 self.assertIs(isinstance(True, bool), True)
182 self.assertIs(isinstance(False, bool), True)
183 self.assertIs(isinstance(True, int), True)
184 self.assertIs(isinstance(False, int), True)
185 self.assertIs(isinstance(1, bool), False)
186 self.assertIs(isinstance(0, bool), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000187
Walter Dörwalddbcede52003-04-25 10:22:01 +0000188 def test_issubclass(self):
189 self.assertIs(issubclass(bool, int), True)
190 self.assertIs(issubclass(int, bool), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000191
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000192 def test_contains(self):
193 self.assertIs(1 in {}, False)
194 self.assertIs(1 in {1:1}, True)
Guido van Rossum5f820362002-04-03 23:01:45 +0000195
Walter Dörwalddbcede52003-04-25 10:22:01 +0000196 def test_string(self):
197 self.assertIs("xyz".endswith("z"), True)
198 self.assertIs("xyz".endswith("x"), False)
199 self.assertIs("xyz0123".isalnum(), True)
200 self.assertIs("@#$%".isalnum(), False)
201 self.assertIs("xyz".isalpha(), True)
202 self.assertIs("@#$%".isalpha(), False)
203 self.assertIs("0123".isdigit(), True)
204 self.assertIs("xyz".isdigit(), False)
205 self.assertIs("xyz".islower(), True)
206 self.assertIs("XYZ".islower(), False)
Walter Dörwald20badb32007-05-05 15:57:18 +0000207 self.assertIs("0123".isdecimal(), True)
208 self.assertIs("xyz".isdecimal(), False)
209 self.assertIs("0123".isnumeric(), True)
210 self.assertIs("xyz".isnumeric(), False)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000211 self.assertIs(" ".isspace(), True)
Walter Dörwald9bc47122007-05-05 16:04:46 +0000212 self.assertIs("\xa0".isspace(), True)
213 self.assertIs("\u3000".isspace(), True)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000214 self.assertIs("XYZ".isspace(), False)
215 self.assertIs("X".istitle(), True)
216 self.assertIs("x".istitle(), False)
217 self.assertIs("XYZ".isupper(), True)
218 self.assertIs("xyz".isupper(), False)
219 self.assertIs("xyz".startswith("x"), True)
220 self.assertIs("xyz".startswith("z"), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000221
Walter Dörwalddbcede52003-04-25 10:22:01 +0000222 def test_boolean(self):
223 self.assertEqual(True & 1, 1)
Ezio Melottie9615932010-01-24 19:26:24 +0000224 self.assertNotIsInstance(True & 1, bool)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000225 self.assertIs(True & True, True)
Guido van Rossum5f820362002-04-03 23:01:45 +0000226
Walter Dörwalddbcede52003-04-25 10:22:01 +0000227 self.assertEqual(True | 1, 1)
Ezio Melottie9615932010-01-24 19:26:24 +0000228 self.assertNotIsInstance(True | 1, bool)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000229 self.assertIs(True | True, True)
Guido van Rossume2763392002-04-05 19:30:08 +0000230
Walter Dörwalddbcede52003-04-25 10:22:01 +0000231 self.assertEqual(True ^ 1, 0)
Ezio Melottie9615932010-01-24 19:26:24 +0000232 self.assertNotIsInstance(True ^ 1, bool)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000233 self.assertIs(True ^ True, False)
234
235 def test_fileclosed(self):
236 try:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000237 f = open(support.TESTFN, "w")
Walter Dörwalddbcede52003-04-25 10:22:01 +0000238 self.assertIs(f.closed, False)
239 f.close()
240 self.assertIs(f.closed, True)
241 finally:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000242 os.remove(support.TESTFN)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000243
Antoine Pitrou503ab332010-03-30 18:56:19 +0000244 def test_types(self):
245 # types are always true.
246 for t in [bool, complex, dict, float, int, list, object,
247 set, str, tuple, type]:
248 self.assertIs(bool(t), True)
249
Walter Dörwalddbcede52003-04-25 10:22:01 +0000250 def test_operator(self):
251 import operator
252 self.assertIs(operator.truth(0), False)
253 self.assertIs(operator.truth(1), True)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000254 self.assertIs(operator.not_(1), False)
255 self.assertIs(operator.not_(0), True)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000256 self.assertIs(operator.contains([], 1), False)
257 self.assertIs(operator.contains([1], 1), True)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000258 self.assertIs(operator.lt(0, 0), False)
259 self.assertIs(operator.lt(0, 1), True)
260 self.assertIs(operator.is_(True, True), True)
261 self.assertIs(operator.is_(True, False), False)
262 self.assertIs(operator.is_not(True, True), False)
263 self.assertIs(operator.is_not(True, False), True)
264
265 def test_marshal(self):
266 import marshal
Walter Dörwald90437c02003-05-01 13:12:34 +0000267 self.assertIs(marshal.loads(marshal.dumps(True)), True)
268 self.assertIs(marshal.loads(marshal.dumps(False)), False)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000269
270 def test_pickle(self):
271 import pickle
Serhiy Storchakabad12572014-12-15 14:03:42 +0200272 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
273 self.assertIs(pickle.loads(pickle.dumps(True, proto)), True)
274 self.assertIs(pickle.loads(pickle.dumps(False, proto)), False)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000275
Walter Dörwalddbcede52003-04-25 10:22:01 +0000276 def test_picklevalues(self):
Walter Dörwalddbcede52003-04-25 10:22:01 +0000277 # Test for specific backwards-compatible pickle values
Guido van Rossumbf12cdb2006-08-17 20:24:18 +0000278 import pickle
Guido van Rossumc4afa102007-05-15 19:52:52 +0000279 self.assertEqual(pickle.dumps(True, protocol=0), b"I01\n.")
280 self.assertEqual(pickle.dumps(False, protocol=0), b"I00\n.")
281 self.assertEqual(pickle.dumps(True, protocol=1), b"I01\n.")
282 self.assertEqual(pickle.dumps(False, protocol=1), b"I00\n.")
283 self.assertEqual(pickle.dumps(True, protocol=2), b'\x80\x02\x88.')
284 self.assertEqual(pickle.dumps(False, protocol=2), b'\x80\x02\x89.')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000285
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000286 def test_convert_to_bool(self):
287 # Verify that TypeError occurs when bad things are returned
Jack Diederich4dafcc42006-11-28 19:15:13 +0000288 # from __bool__(). This isn't really a bool test, but
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000289 # it's related.
290 check = lambda o: self.assertRaises(TypeError, bool, o)
291 class Foo(object):
Jack Diederich4dafcc42006-11-28 19:15:13 +0000292 def __bool__(self):
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000293 return self
294 check(Foo())
Tim Peters478c1052003-06-29 05:46:54 +0000295
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000296 class Bar(object):
Jack Diederich4dafcc42006-11-28 19:15:13 +0000297 def __bool__(self):
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000298 return "Yes"
299 check(Bar())
Tim Peters478c1052003-06-29 05:46:54 +0000300
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000301 class Baz(int):
Jack Diederich4dafcc42006-11-28 19:15:13 +0000302 def __bool__(self):
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000303 return self
304 check(Baz())
Tim Peters478c1052003-06-29 05:46:54 +0000305
Jack Diederich4dafcc42006-11-28 19:15:13 +0000306 # __bool__() must return a bool not an int
307 class Spam(int):
308 def __bool__(self):
309 return 1
310 check(Spam())
311
312 class Eggs:
313 def __len__(self):
314 return -1
315 self.assertRaises(ValueError, bool, Eggs())
316
Serhiy Storchakaea36c942016-05-12 10:37:58 +0300317 def test_from_bytes(self):
318 self.assertIs(bool.from_bytes(b'\x00'*8, 'big'), False)
319 self.assertIs(bool.from_bytes(b'abcd', 'little'), True)
320
Jack Diederich4dafcc42006-11-28 19:15:13 +0000321 def test_sane_len(self):
322 # this test just tests our assumptions about __len__
323 # this will start failing if __len__ changes assertions
324 for badval in ['illegal', -1, 1 << 32]:
325 class A:
326 def __len__(self):
327 return badval
328 try:
329 bool(A())
Guido van Rossumb940e112007-01-10 16:19:56 +0000330 except (Exception) as e_bool:
331 try:
332 len(A())
333 except (Exception) as e_len:
334 self.assertEqual(str(e_bool), str(e_len))
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000335
Benjamin Peterson81b21d72011-05-25 09:21:46 -0500336 def test_real_and_imag(self):
337 self.assertEqual(True.real, 1)
338 self.assertEqual(True.imag, 0)
339 self.assertIs(type(True.real), int)
340 self.assertIs(type(True.imag), int)
341 self.assertEqual(False.real, 0)
342 self.assertEqual(False.imag, 0)
343 self.assertIs(type(False.real), int)
344 self.assertIs(type(False.imag), int)
345
Walter Dörwalddbcede52003-04-25 10:22:01 +0000346def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000347 support.run_unittest(BoolTest)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000348
349if __name__ == "__main__":
350 test_main()