1# Test properties of bool promised by PEP 285 2 3import unittest 4from test import test_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 fo = open(test_support.TESTFN, "wb") 24 print >> fo, False, True 25 fo.close() 26 fo = open(test_support.TESTFN, "rb") 27 self.assertEqual(fo.read(), 'False True\n') 28 finally: 29 fo.close() 30 os.remove(test_support.TESTFN) 31 32 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) 37 38 def test_str(self): 39 self.assertEqual(str(False), 'False') 40 self.assertEqual(str(True), 'True') 41 42 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) 47 48 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 54 def test_long(self): 55 self.assertEqual(long(False), 0L) 56 self.assertIsNot(long(False), False) 57 self.assertEqual(long(True), 1L) 58 self.assertIsNot(long(True), True) 59 60 def test_math(self): 61 self.assertEqual(+False, 0) 62 self.assertIsNot(+False, False) 63 self.assertEqual(-False, 0) 64 self.assertIsNot(-False, False) 65 self.assertEqual(abs(False), 0) 66 self.assertIsNot(abs(False), False) 67 self.assertEqual(+True, 1) 68 self.assertIsNot(+True, True) 69 self.assertEqual(-True, -1) 70 self.assertEqual(abs(True), 1) 71 self.assertIsNot(abs(True), True) 72 self.assertEqual(~False, -1) 73 self.assertEqual(~True, -2) 74 75 self.assertEqual(False+2, 2) 76 self.assertEqual(True+2, 3) 77 self.assertEqual(2+False, 2) 78 self.assertEqual(2+True, 3) 79 80 self.assertEqual(False+False, 0) 81 self.assertIsNot(False+False, False) 82 self.assertEqual(False+True, 1) 83 self.assertIsNot(False+True, True) 84 self.assertEqual(True+False, 1) 85 self.assertIsNot(True+False, True) 86 self.assertEqual(True+True, 2) 87 88 self.assertEqual(True-True, 0) 89 self.assertIsNot(True-True, False) 90 self.assertEqual(False-False, 0) 91 self.assertIsNot(False-False, False) 92 self.assertEqual(True-False, 1) 93 self.assertIsNot(True-False, True) 94 self.assertEqual(False-True, -1) 95 96 self.assertEqual(True*1, 1) 97 self.assertEqual(False*1, 0) 98 self.assertIsNot(False*1, False) 99 100 self.assertEqual(True//1, 1) 101 self.assertIsNot(True//1, True) 102 self.assertEqual(False//1, 0) 103 self.assertIsNot(False//1, False) 104 105 for b in False, True: 106 for i in 0, 1, 2: 107 self.assertEqual(b**i, int(b)**i) 108 self.assertIsNot(b**i, bool(int(b)**i)) 109 110 for a in False, True: 111 for b in False, True: 112 self.assertIs(a&b, bool(int(a)&int(b))) 113 self.assertIs(a|b, bool(int(a)|int(b))) 114 self.assertIs(a^b, bool(int(a)^int(b))) 115 self.assertEqual(a&int(b), int(a)&int(b)) 116 self.assertIsNot(a&int(b), bool(int(a)&int(b))) 117 self.assertEqual(a|int(b), int(a)|int(b)) 118 self.assertIsNot(a|int(b), bool(int(a)|int(b))) 119 self.assertEqual(a^int(b), int(a)^int(b)) 120 self.assertIsNot(a^int(b), bool(int(a)^int(b))) 121 self.assertEqual(int(a)&b, int(a)&int(b)) 122 self.assertIsNot(int(a)&b, bool(int(a)&int(b))) 123 self.assertEqual(int(a)|b, int(a)|int(b)) 124 self.assertIsNot(int(a)|b, bool(int(a)|int(b))) 125 self.assertEqual(int(a)^b, int(a)^int(b)) 126 self.assertIsNot(int(a)^b, bool(int(a)^int(b))) 127 128 self.assertIs(1==1, True) 129 self.assertIs(1==0, False) 130 self.assertIs(0<1, True) 131 self.assertIs(1<0, False) 132 self.assertIs(0<=0, True) 133 self.assertIs(1<=0, False) 134 self.assertIs(1>0, True) 135 self.assertIs(1>1, False) 136 self.assertIs(1>=1, True) 137 self.assertIs(0>=1, False) 138 self.assertIs(0!=1, True) 139 self.assertIs(0!=0, False) 140 141 x = [1] 142 self.assertIs(x is x, True) 143 self.assertIs(x is not x, False) 144 145 self.assertIs(1 in x, True) 146 self.assertIs(0 in x, False) 147 self.assertIs(1 not in x, False) 148 self.assertIs(0 not in x, True) 149 150 x = {1: 2} 151 self.assertIs(x is x, True) 152 self.assertIs(x is not x, False) 153 154 self.assertIs(1 in x, True) 155 self.assertIs(0 in x, False) 156 self.assertIs(1 not in x, False) 157 self.assertIs(0 not in x, True) 158 159 self.assertIs(not True, False) 160 self.assertIs(not False, True) 161 162 def test_convert(self): 163 self.assertRaises(TypeError, bool, 42, 42) 164 self.assertIs(bool(10), True) 165 self.assertIs(bool(1), True) 166 self.assertIs(bool(-1), True) 167 self.assertIs(bool(0), False) 168 self.assertIs(bool("hello"), True) 169 self.assertIs(bool(""), False) 170 self.assertIs(bool(), False) 171 172 def test_format(self): 173 self.assertEqual("%d" % False, "0") 174 self.assertEqual("%d" % True, "1") 175 self.assertEqual("%x" % False, "0") 176 self.assertEqual("%x" % True, "1") 177 178 def test_hasattr(self): 179 self.assertIs(hasattr([], "append"), True) 180 self.assertIs(hasattr([], "wobble"), False) 181 182 def test_callable(self): 183 self.assertIs(callable(len), True) 184 self.assertIs(callable(1), False) 185 186 def test_isinstance(self): 187 self.assertIs(isinstance(True, bool), True) 188 self.assertIs(isinstance(False, bool), True) 189 self.assertIs(isinstance(True, int), True) 190 self.assertIs(isinstance(False, int), True) 191 self.assertIs(isinstance(1, bool), False) 192 self.assertIs(isinstance(0, bool), False) 193 194 def test_issubclass(self): 195 self.assertIs(issubclass(bool, int), True) 196 self.assertIs(issubclass(int, bool), False) 197 198 def test_haskey(self): 199 self.assertIs(1 in {}, False) 200 self.assertIs(1 in {1:1}, True) 201 with test_support.check_py3k_warnings(): 202 self.assertIs({}.has_key(1), False) 203 self.assertIs({1:1}.has_key(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(" ".isspace(), True) 217 self.assertIs("XYZ".isspace(), False) 218 self.assertIs("X".istitle(), True) 219 self.assertIs("x".istitle(), False) 220 self.assertIs("XYZ".isupper(), True) 221 self.assertIs("xyz".isupper(), False) 222 self.assertIs("xyz".startswith("x"), True) 223 self.assertIs("xyz".startswith("z"), False) 224 225 if test_support.have_unicode: 226 self.assertIs(unicode("xyz", 'ascii').endswith(unicode("z", 'ascii')), True) 227 self.assertIs(unicode("xyz", 'ascii').endswith(unicode("x", 'ascii')), False) 228 self.assertIs(unicode("xyz0123", 'ascii').isalnum(), True) 229 self.assertIs(unicode("@#$%", 'ascii').isalnum(), False) 230 self.assertIs(unicode("xyz", 'ascii').isalpha(), True) 231 self.assertIs(unicode("@#$%", 'ascii').isalpha(), False) 232 self.assertIs(unicode("0123", 'ascii').isdecimal(), True) 233 self.assertIs(unicode("xyz", 'ascii').isdecimal(), False) 234 self.assertIs(unicode("0123", 'ascii').isdigit(), True) 235 self.assertIs(unicode("xyz", 'ascii').isdigit(), False) 236 self.assertIs(unicode("xyz", 'ascii').islower(), True) 237 self.assertIs(unicode("XYZ", 'ascii').islower(), False) 238 self.assertIs(unicode("0123", 'ascii').isnumeric(), True) 239 self.assertIs(unicode("xyz", 'ascii').isnumeric(), False) 240 self.assertIs(unicode(" ", 'ascii').isspace(), True) 241 self.assertIs(unicode("XYZ", 'ascii').isspace(), False) 242 self.assertIs(unicode("X", 'ascii').istitle(), True) 243 self.assertIs(unicode("x", 'ascii').istitle(), False) 244 self.assertIs(unicode("XYZ", 'ascii').isupper(), True) 245 self.assertIs(unicode("xyz", 'ascii').isupper(), False) 246 self.assertIs(unicode("xyz", 'ascii').startswith(unicode("x", 'ascii')), True) 247 self.assertIs(unicode("xyz", 'ascii').startswith(unicode("z", 'ascii')), False) 248 249 def test_boolean(self): 250 self.assertEqual(True & 1, 1) 251 self.assertNotIsInstance(True & 1, bool) 252 self.assertIs(True & True, True) 253 254 self.assertEqual(True | 1, 1) 255 self.assertNotIsInstance(True | 1, bool) 256 self.assertIs(True | True, True) 257 258 self.assertEqual(True ^ 1, 0) 259 self.assertNotIsInstance(True ^ 1, bool) 260 self.assertIs(True ^ True, False) 261 262 def test_fileclosed(self): 263 try: 264 f = file(test_support.TESTFN, "w") 265 self.assertIs(f.closed, False) 266 f.close() 267 self.assertIs(f.closed, True) 268 finally: 269 os.remove(test_support.TESTFN) 270 271 def test_types(self): 272 # types are always true. 273 for t in [bool, complex, dict, file, float, int, list, long, object, 274 set, str, tuple, type]: 275 self.assertIs(bool(t), True) 276 277 def test_operator(self): 278 import operator 279 self.assertIs(operator.truth(0), False) 280 self.assertIs(operator.truth(1), True) 281 with test_support.check_py3k_warnings(): 282 self.assertIs(operator.isCallable(0), False) 283 self.assertIs(operator.isCallable(len), True) 284 self.assertIs(operator.isNumberType(None), False) 285 self.assertIs(operator.isNumberType(0), True) 286 self.assertIs(operator.not_(1), False) 287 self.assertIs(operator.not_(0), True) 288 self.assertIs(operator.isSequenceType(0), False) 289 self.assertIs(operator.isSequenceType([]), True) 290 self.assertIs(operator.contains([], 1), False) 291 self.assertIs(operator.contains([1], 1), True) 292 self.assertIs(operator.isMappingType(1), False) 293 self.assertIs(operator.isMappingType({}), True) 294 self.assertIs(operator.lt(0, 0), False) 295 self.assertIs(operator.lt(0, 1), True) 296 self.assertIs(operator.is_(True, True), True) 297 self.assertIs(operator.is_(True, False), False) 298 self.assertIs(operator.is_not(True, True), False) 299 self.assertIs(operator.is_not(True, False), True) 300 301 def test_marshal(self): 302 import marshal 303 self.assertIs(marshal.loads(marshal.dumps(True)), True) 304 self.assertIs(marshal.loads(marshal.dumps(False)), False) 305 306 def test_pickle(self): 307 import pickle 308 for proto in range(pickle.HIGHEST_PROTOCOL + 1): 309 self.assertIs(pickle.loads(pickle.dumps(True, proto)), True) 310 self.assertIs(pickle.loads(pickle.dumps(False, proto)), False) 311 312 def test_cpickle(self): 313 import cPickle 314 for proto in range(cPickle.HIGHEST_PROTOCOL + 1): 315 self.assertIs(cPickle.loads(cPickle.dumps(True, proto)), True) 316 self.assertIs(cPickle.loads(cPickle.dumps(False, proto)), False) 317 318 def test_mixedpickle(self): 319 import pickle, cPickle 320 for proto in range(pickle.HIGHEST_PROTOCOL + 1): 321 self.assertIs(pickle.loads(cPickle.dumps(True, proto)), True) 322 self.assertIs(pickle.loads(cPickle.dumps(False, proto)), False) 323 self.assertIs(cPickle.loads(pickle.dumps(True, proto)), True) 324 self.assertIs(cPickle.loads(pickle.dumps(False, proto)), False) 325 326 def test_picklevalues(self): 327 import pickle, cPickle 328 329 # Test for specific backwards-compatible pickle values 330 self.assertEqual(pickle.dumps(True, protocol=0), "I01\n.") 331 self.assertEqual(pickle.dumps(False, protocol=0), "I00\n.") 332 self.assertEqual(cPickle.dumps(True, protocol=0), "I01\n.") 333 self.assertEqual(cPickle.dumps(False, protocol=0), "I00\n.") 334 self.assertEqual(pickle.dumps(True, protocol=1), "I01\n.") 335 self.assertEqual(pickle.dumps(False, protocol=1), "I00\n.") 336 self.assertEqual(cPickle.dumps(True, protocol=1), "I01\n.") 337 self.assertEqual(cPickle.dumps(False, protocol=1), "I00\n.") 338 self.assertEqual(pickle.dumps(True, protocol=2), b'\x80\x02\x88.') 339 self.assertEqual(pickle.dumps(False, protocol=2), b'\x80\x02\x89.') 340 self.assertEqual(cPickle.dumps(True, protocol=2), b'\x80\x02\x88.') 341 self.assertEqual(cPickle.dumps(False, protocol=2), b'\x80\x02\x89.') 342 343 def test_convert_to_bool(self): 344 # Verify that TypeError occurs when bad things are returned 345 # from __nonzero__(). This isn't really a bool test, but 346 # it's related. 347 check = lambda o: self.assertRaises(TypeError, bool, o) 348 class Foo(object): 349 def __nonzero__(self): 350 return self 351 check(Foo()) 352 353 class Bar(object): 354 def __nonzero__(self): 355 return "Yes" 356 check(Bar()) 357 358 class Baz(int): 359 def __nonzero__(self): 360 return self 361 check(Baz()) 362 363 364def test_main(): 365 test_support.run_unittest(BoolTest) 366 367if __name__ == "__main__": 368 test_main() 369