blob: c5a1f1f2c2b29f93611349023d03ebaece212a21 [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
Mark Dickinsonb820d7f2016-08-22 12:24:46 +010099 self.assertEqual(True%1, 0)
100 self.assertIsNot(True%1, False)
101 self.assertEqual(True%2, 1)
102 self.assertIsNot(True%2, True)
103 self.assertEqual(False%1, 0)
104 self.assertIsNot(False%1, False)
105
Walter Dörwalddbcede52003-04-25 10:22:01 +0000106 for b in False, True:
107 for i in 0, 1, 2:
108 self.assertEqual(b**i, int(b)**i)
109 self.assertIsNot(b**i, bool(int(b)**i))
Guido van Rossum5f820362002-04-03 23:01:45 +0000110
Walter Dörwalddbcede52003-04-25 10:22:01 +0000111 for a in False, True:
112 for b in False, True:
113 self.assertIs(a&b, bool(int(a)&int(b)))
114 self.assertIs(a|b, bool(int(a)|int(b)))
115 self.assertIs(a^b, bool(int(a)^int(b)))
116 self.assertEqual(a&int(b), int(a)&int(b))
117 self.assertIsNot(a&int(b), bool(int(a)&int(b)))
118 self.assertEqual(a|int(b), int(a)|int(b))
119 self.assertIsNot(a|int(b), bool(int(a)|int(b)))
120 self.assertEqual(a^int(b), int(a)^int(b))
121 self.assertIsNot(a^int(b), bool(int(a)^int(b)))
122 self.assertEqual(int(a)&b, int(a)&int(b))
123 self.assertIsNot(int(a)&b, bool(int(a)&int(b)))
124 self.assertEqual(int(a)|b, int(a)|int(b))
125 self.assertIsNot(int(a)|b, bool(int(a)|int(b)))
126 self.assertEqual(int(a)^b, int(a)^int(b))
127 self.assertIsNot(int(a)^b, bool(int(a)^int(b)))
Guido van Rossum5f820362002-04-03 23:01:45 +0000128
Walter Dörwalddbcede52003-04-25 10:22:01 +0000129 self.assertIs(1==1, True)
130 self.assertIs(1==0, False)
131 self.assertIs(0<1, True)
132 self.assertIs(1<0, False)
133 self.assertIs(0<=0, True)
134 self.assertIs(1<=0, False)
135 self.assertIs(1>0, True)
136 self.assertIs(1>1, False)
137 self.assertIs(1>=1, True)
138 self.assertIs(0>=1, False)
139 self.assertIs(0!=1, True)
140 self.assertIs(0!=0, False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000141
Walter Dörwalddbcede52003-04-25 10:22:01 +0000142 x = [1]
143 self.assertIs(x is x, True)
144 self.assertIs(x is not x, False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000145
Walter Dörwalddbcede52003-04-25 10:22:01 +0000146 self.assertIs(1 in x, True)
147 self.assertIs(0 in x, False)
148 self.assertIs(1 not in x, False)
149 self.assertIs(0 not in x, True)
Guido van Rossum5f820362002-04-03 23:01:45 +0000150
Walter Dörwalddbcede52003-04-25 10:22:01 +0000151 x = {1: 2}
152 self.assertIs(x is x, True)
153 self.assertIs(x is not x, False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000154
Walter Dörwalddbcede52003-04-25 10:22:01 +0000155 self.assertIs(1 in x, True)
156 self.assertIs(0 in x, False)
157 self.assertIs(1 not in x, False)
158 self.assertIs(0 not in x, True)
Guido van Rossum5f820362002-04-03 23:01:45 +0000159
Walter Dörwalddbcede52003-04-25 10:22:01 +0000160 self.assertIs(not True, False)
161 self.assertIs(not False, True)
Guido van Rossum5f820362002-04-03 23:01:45 +0000162
Walter Dörwalddbcede52003-04-25 10:22:01 +0000163 def test_convert(self):
164 self.assertRaises(TypeError, bool, 42, 42)
165 self.assertIs(bool(10), True)
166 self.assertIs(bool(1), True)
167 self.assertIs(bool(-1), True)
168 self.assertIs(bool(0), False)
169 self.assertIs(bool("hello"), True)
170 self.assertIs(bool(""), False)
171 self.assertIs(bool(), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000172
Serhiy Storchaka58d23e62017-03-06 00:53:39 +0200173 def test_keyword_args(self):
Serhiy Storchaka2e564242017-03-06 17:01:06 +0200174 with self.assertRaisesRegex(TypeError, 'keyword argument'):
175 bool(x=10)
Serhiy Storchaka58d23e62017-03-06 00:53:39 +0200176
Martin v. Löwisff398c62007-08-14 21:57:32 +0000177 def test_format(self):
178 self.assertEqual("%d" % False, "0")
179 self.assertEqual("%d" % True, "1")
180 self.assertEqual("%x" % False, "0")
181 self.assertEqual("%x" % True, "1")
182
Walter Dörwalddbcede52003-04-25 10:22:01 +0000183 def test_hasattr(self):
184 self.assertIs(hasattr([], "append"), True)
185 self.assertIs(hasattr([], "wobble"), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000186
Walter Dörwalddbcede52003-04-25 10:22:01 +0000187 def test_callable(self):
Antoine Pitroue71362d2010-11-27 22:00:11 +0000188 self.assertIs(callable(len), True)
189 self.assertIs(callable(1), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000190
Walter Dörwalddbcede52003-04-25 10:22:01 +0000191 def test_isinstance(self):
192 self.assertIs(isinstance(True, bool), True)
193 self.assertIs(isinstance(False, bool), True)
194 self.assertIs(isinstance(True, int), True)
195 self.assertIs(isinstance(False, int), True)
196 self.assertIs(isinstance(1, bool), False)
197 self.assertIs(isinstance(0, bool), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000198
Walter Dörwalddbcede52003-04-25 10:22:01 +0000199 def test_issubclass(self):
200 self.assertIs(issubclass(bool, int), True)
201 self.assertIs(issubclass(int, bool), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000202
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000203 def test_contains(self):
204 self.assertIs(1 in {}, False)
205 self.assertIs(1 in {1:1}, True)
Guido van Rossum5f820362002-04-03 23:01:45 +0000206
Walter Dörwalddbcede52003-04-25 10:22:01 +0000207 def test_string(self):
208 self.assertIs("xyz".endswith("z"), True)
209 self.assertIs("xyz".endswith("x"), False)
210 self.assertIs("xyz0123".isalnum(), True)
211 self.assertIs("@#$%".isalnum(), False)
212 self.assertIs("xyz".isalpha(), True)
213 self.assertIs("@#$%".isalpha(), False)
214 self.assertIs("0123".isdigit(), True)
215 self.assertIs("xyz".isdigit(), False)
216 self.assertIs("xyz".islower(), True)
217 self.assertIs("XYZ".islower(), False)
Walter Dörwald20badb32007-05-05 15:57:18 +0000218 self.assertIs("0123".isdecimal(), True)
219 self.assertIs("xyz".isdecimal(), False)
220 self.assertIs("0123".isnumeric(), True)
221 self.assertIs("xyz".isnumeric(), False)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000222 self.assertIs(" ".isspace(), True)
Walter Dörwald9bc47122007-05-05 16:04:46 +0000223 self.assertIs("\xa0".isspace(), True)
224 self.assertIs("\u3000".isspace(), True)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000225 self.assertIs("XYZ".isspace(), False)
226 self.assertIs("X".istitle(), True)
227 self.assertIs("x".istitle(), False)
228 self.assertIs("XYZ".isupper(), True)
229 self.assertIs("xyz".isupper(), False)
230 self.assertIs("xyz".startswith("x"), True)
231 self.assertIs("xyz".startswith("z"), False)
Guido van Rossum5f820362002-04-03 23:01:45 +0000232
Walter Dörwalddbcede52003-04-25 10:22:01 +0000233 def test_boolean(self):
234 self.assertEqual(True & 1, 1)
Ezio Melottie9615932010-01-24 19:26:24 +0000235 self.assertNotIsInstance(True & 1, bool)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000236 self.assertIs(True & True, True)
Guido van Rossum5f820362002-04-03 23:01:45 +0000237
Walter Dörwalddbcede52003-04-25 10:22:01 +0000238 self.assertEqual(True | 1, 1)
Ezio Melottie9615932010-01-24 19:26:24 +0000239 self.assertNotIsInstance(True | 1, bool)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000240 self.assertIs(True | True, True)
Guido van Rossume2763392002-04-05 19:30:08 +0000241
Walter Dörwalddbcede52003-04-25 10:22:01 +0000242 self.assertEqual(True ^ 1, 0)
Ezio Melottie9615932010-01-24 19:26:24 +0000243 self.assertNotIsInstance(True ^ 1, bool)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000244 self.assertIs(True ^ True, False)
245
246 def test_fileclosed(self):
247 try:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000248 f = open(support.TESTFN, "w")
Walter Dörwalddbcede52003-04-25 10:22:01 +0000249 self.assertIs(f.closed, False)
250 f.close()
251 self.assertIs(f.closed, True)
252 finally:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000253 os.remove(support.TESTFN)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000254
Antoine Pitrou503ab332010-03-30 18:56:19 +0000255 def test_types(self):
256 # types are always true.
257 for t in [bool, complex, dict, float, int, list, object,
258 set, str, tuple, type]:
259 self.assertIs(bool(t), True)
260
Walter Dörwalddbcede52003-04-25 10:22:01 +0000261 def test_operator(self):
262 import operator
263 self.assertIs(operator.truth(0), False)
264 self.assertIs(operator.truth(1), True)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000265 self.assertIs(operator.not_(1), False)
266 self.assertIs(operator.not_(0), True)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000267 self.assertIs(operator.contains([], 1), False)
268 self.assertIs(operator.contains([1], 1), True)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000269 self.assertIs(operator.lt(0, 0), False)
270 self.assertIs(operator.lt(0, 1), True)
271 self.assertIs(operator.is_(True, True), True)
272 self.assertIs(operator.is_(True, False), False)
273 self.assertIs(operator.is_not(True, True), False)
274 self.assertIs(operator.is_not(True, False), True)
275
276 def test_marshal(self):
277 import marshal
Walter Dörwald90437c02003-05-01 13:12:34 +0000278 self.assertIs(marshal.loads(marshal.dumps(True)), True)
279 self.assertIs(marshal.loads(marshal.dumps(False)), False)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000280
281 def test_pickle(self):
282 import pickle
Serhiy Storchakabad12572014-12-15 14:03:42 +0200283 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
284 self.assertIs(pickle.loads(pickle.dumps(True, proto)), True)
285 self.assertIs(pickle.loads(pickle.dumps(False, proto)), False)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000286
Walter Dörwalddbcede52003-04-25 10:22:01 +0000287 def test_picklevalues(self):
Walter Dörwalddbcede52003-04-25 10:22:01 +0000288 # Test for specific backwards-compatible pickle values
Guido van Rossumbf12cdb2006-08-17 20:24:18 +0000289 import pickle
Guido van Rossumc4afa102007-05-15 19:52:52 +0000290 self.assertEqual(pickle.dumps(True, protocol=0), b"I01\n.")
291 self.assertEqual(pickle.dumps(False, protocol=0), b"I00\n.")
292 self.assertEqual(pickle.dumps(True, protocol=1), b"I01\n.")
293 self.assertEqual(pickle.dumps(False, protocol=1), b"I00\n.")
294 self.assertEqual(pickle.dumps(True, protocol=2), b'\x80\x02\x88.')
295 self.assertEqual(pickle.dumps(False, protocol=2), b'\x80\x02\x89.')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000296
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000297 def test_convert_to_bool(self):
298 # Verify that TypeError occurs when bad things are returned
Jack Diederich4dafcc42006-11-28 19:15:13 +0000299 # from __bool__(). This isn't really a bool test, but
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000300 # it's related.
301 check = lambda o: self.assertRaises(TypeError, bool, o)
302 class Foo(object):
Jack Diederich4dafcc42006-11-28 19:15:13 +0000303 def __bool__(self):
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000304 return self
305 check(Foo())
Tim Peters478c1052003-06-29 05:46:54 +0000306
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000307 class Bar(object):
Jack Diederich4dafcc42006-11-28 19:15:13 +0000308 def __bool__(self):
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000309 return "Yes"
310 check(Bar())
Tim Peters478c1052003-06-29 05:46:54 +0000311
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000312 class Baz(int):
Jack Diederich4dafcc42006-11-28 19:15:13 +0000313 def __bool__(self):
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000314 return self
315 check(Baz())
Tim Peters478c1052003-06-29 05:46:54 +0000316
Jack Diederich4dafcc42006-11-28 19:15:13 +0000317 # __bool__() must return a bool not an int
318 class Spam(int):
319 def __bool__(self):
320 return 1
321 check(Spam())
322
323 class Eggs:
324 def __len__(self):
325 return -1
326 self.assertRaises(ValueError, bool, Eggs())
327
Serhiy Storchakaea36c942016-05-12 10:37:58 +0300328 def test_from_bytes(self):
329 self.assertIs(bool.from_bytes(b'\x00'*8, 'big'), False)
330 self.assertIs(bool.from_bytes(b'abcd', 'little'), True)
331
Jack Diederich4dafcc42006-11-28 19:15:13 +0000332 def test_sane_len(self):
333 # this test just tests our assumptions about __len__
334 # this will start failing if __len__ changes assertions
335 for badval in ['illegal', -1, 1 << 32]:
336 class A:
337 def __len__(self):
338 return badval
339 try:
340 bool(A())
Guido van Rossumb940e112007-01-10 16:19:56 +0000341 except (Exception) as e_bool:
342 try:
343 len(A())
344 except (Exception) as e_len:
345 self.assertEqual(str(e_bool), str(e_len))
Jeremy Hylton7ff55e62003-06-27 17:40:16 +0000346
Guido van Rossum97c1adf2016-08-18 09:22:23 -0700347 def test_blocked(self):
348 class A:
349 __bool__ = None
350 self.assertRaises(TypeError, bool, A())
351
352 class B:
353 def __len__(self):
354 return 10
355 __bool__ = None
356 self.assertRaises(TypeError, bool, B())
357
Benjamin Peterson81b21d72011-05-25 09:21:46 -0500358 def test_real_and_imag(self):
359 self.assertEqual(True.real, 1)
360 self.assertEqual(True.imag, 0)
361 self.assertIs(type(True.real), int)
362 self.assertIs(type(True.imag), int)
363 self.assertEqual(False.real, 0)
364 self.assertEqual(False.imag, 0)
365 self.assertIs(type(False.real), int)
366 self.assertIs(type(False.imag), int)
367
Walter Dörwalddbcede52003-04-25 10:22:01 +0000368def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000369 support.run_unittest(BoolTest)
Walter Dörwalddbcede52003-04-25 10:22:01 +0000370
371if __name__ == "__main__":
372 test_main()