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