1 # Test properties of bool promised by PEP 285 2 3 import unittest 4 from test import test_support 5 6 import os 7 8 class 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 with test_support.check_py3k_warnings(): 184 self.assertIs(callable(len), True) 185 self.assertIs(callable(1), False) 186 187 def test_isinstance(self): 188 self.assertIs(isinstance(True, bool), True) 189 self.assertIs(isinstance(False, bool), True) 190 self.assertIs(isinstance(True, int), True) 191 self.assertIs(isinstance(False, int), True) 192 self.assertIs(isinstance(1, bool), False) 193 self.assertIs(isinstance(0, bool), False) 194 195 def test_issubclass(self): 196 self.assertIs(issubclass(bool, int), True) 197 self.assertIs(issubclass(int, bool), False) 198 199 def test_haskey(self): 200 self.assertIs(1 in {}, False) 201 self.assertIs(1 in {1:1}, True) 202 with test_support.check_py3k_warnings(): 203 self.assertIs({}.has_key(1), False) 204 self.assertIs({1:1}.has_key(1), True) 205 206 def test_string(self): 207 self.assertIs("xyz".endswith("z"), True) 208 self.assertIs("xyz".endswith("x"), False) 209 self.assertIs("xyz0123".isalnum(), True) 210 self.assertIs("@#$%".isalnum(), False) 211 self.assertIs("xyz".isalpha(), True) 212 self.assertIs("@#$%".isalpha(), False) 213 self.assertIs("0123".isdigit(), True) 214 self.assertIs("xyz".isdigit(), False) 215 self.assertIs("xyz".islower(), True) 216 self.assertIs("XYZ".islower(), False) 217 self.assertIs(" ".isspace(), True) 218 self.assertIs("XYZ".isspace(), False) 219 self.assertIs("X".istitle(), True) 220 self.assertIs("x".istitle(), False) 221 self.assertIs("XYZ".isupper(), True) 222 self.assertIs("xyz".isupper(), False) 223 self.assertIs("xyz".startswith("x"), True) 224 self.assertIs("xyz".startswith("z"), False) 225 226 if test_support.have_unicode: 227 self.assertIs(unicode("xyz", 'ascii').endswith(unicode("z", 'ascii')), True) 228 self.assertIs(unicode("xyz", 'ascii').endswith(unicode("x", 'ascii')), False) 229 self.assertIs(unicode("xyz0123", 'ascii').isalnum(), True) 230 self.assertIs(unicode("@#$%", 'ascii').isalnum(), False) 231 self.assertIs(unicode("xyz", 'ascii').isalpha(), True) 232 self.assertIs(unicode("@#$%", 'ascii').isalpha(), False) 233 self.assertIs(unicode("0123", 'ascii').isdecimal(), True) 234 self.assertIs(unicode("xyz", 'ascii').isdecimal(), False) 235 self.assertIs(unicode("0123", 'ascii').isdigit(), True) 236 self.assertIs(unicode("xyz", 'ascii').isdigit(), False) 237 self.assertIs(unicode("xyz", 'ascii').islower(), True) 238 self.assertIs(unicode("XYZ", 'ascii').islower(), False) 239 self.assertIs(unicode("0123", 'ascii').isnumeric(), True) 240 self.assertIs(unicode("xyz", 'ascii').isnumeric(), False) 241 self.assertIs(unicode(" ", 'ascii').isspace(), True) 242 self.assertIs(unicode("XYZ", 'ascii').isspace(), False) 243 self.assertIs(unicode("X", 'ascii').istitle(), True) 244 self.assertIs(unicode("x", 'ascii').istitle(), False) 245 self.assertIs(unicode("XYZ", 'ascii').isupper(), True) 246 self.assertIs(unicode("xyz", 'ascii').isupper(), False) 247 self.assertIs(unicode("xyz", 'ascii').startswith(unicode("x", 'ascii')), True) 248 self.assertIs(unicode("xyz", 'ascii').startswith(unicode("z", 'ascii')), False) 249 250 def test_boolean(self): 251 self.assertEqual(True & 1, 1) 252 self.assertNotIsInstance(True & 1, bool) 253 self.assertIs(True & True, True) 254 255 self.assertEqual(True | 1, 1) 256 self.assertNotIsInstance(True | 1, bool) 257 self.assertIs(True | True, True) 258 259 self.assertEqual(True ^ 1, 0) 260 self.assertNotIsInstance(True ^ 1, bool) 261 self.assertIs(True ^ True, False) 262 263 def test_fileclosed(self): 264 try: 265 f = file(test_support.TESTFN, "w") 266 self.assertIs(f.closed, False) 267 f.close() 268 self.assertIs(f.closed, True) 269 finally: 270 os.remove(test_support.TESTFN) 271 272 def test_types(self): 273 # types are always true. 274 for t in [bool, complex, dict, file, float, int, list, long, object, 275 set, str, tuple, type]: 276 self.assertIs(bool(t), True) 277 278 def test_operator(self): 279 import operator 280 self.assertIs(operator.truth(0), False) 281 self.assertIs(operator.truth(1), True) 282 with test_support.check_py3k_warnings(): 283 self.assertIs(operator.isCallable(0), False) 284 self.assertIs(operator.isCallable(len), True) 285 self.assertIs(operator.isNumberType(None), False) 286 self.assertIs(operator.isNumberType(0), True) 287 self.assertIs(operator.not_(1), False) 288 self.assertIs(operator.not_(0), True) 289 self.assertIs(operator.isSequenceType(0), False) 290 self.assertIs(operator.isSequenceType([]), True) 291 self.assertIs(operator.contains([], 1), False) 292 self.assertIs(operator.contains([1], 1), True) 293 self.assertIs(operator.isMappingType(1), False) 294 self.assertIs(operator.isMappingType({}), True) 295 self.assertIs(operator.lt(0, 0), False) 296 self.assertIs(operator.lt(0, 1), True) 297 self.assertIs(operator.is_(True, True), True) 298 self.assertIs(operator.is_(True, False), False) 299 self.assertIs(operator.is_not(True, True), False) 300 self.assertIs(operator.is_not(True, False), True) 301 302 def test_marshal(self): 303 import marshal 304 self.assertIs(marshal.loads(marshal.dumps(True)), True) 305 self.assertIs(marshal.loads(marshal.dumps(False)), False) 306 307 def test_pickle(self): 308 import pickle 309 self.assertIs(pickle.loads(pickle.dumps(True)), True) 310 self.assertIs(pickle.loads(pickle.dumps(False)), False) 311 self.assertIs(pickle.loads(pickle.dumps(True, True)), True) 312 self.assertIs(pickle.loads(pickle.dumps(False, True)), False) 313 314 def test_cpickle(self): 315 import cPickle 316 self.assertIs(cPickle.loads(cPickle.dumps(True)), True) 317 self.assertIs(cPickle.loads(cPickle.dumps(False)), False) 318 self.assertIs(cPickle.loads(cPickle.dumps(True, True)), True) 319 self.assertIs(cPickle.loads(cPickle.dumps(False, True)), False) 320 321 def test_mixedpickle(self): 322 import pickle, cPickle 323 self.assertIs(pickle.loads(cPickle.dumps(True)), True) 324 self.assertIs(pickle.loads(cPickle.dumps(False)), False) 325 self.assertIs(pickle.loads(cPickle.dumps(True, True)), True) 326 self.assertIs(pickle.loads(cPickle.dumps(False, True)), False) 327 328 self.assertIs(cPickle.loads(pickle.dumps(True)), True) 329 self.assertIs(cPickle.loads(pickle.dumps(False)), False) 330 self.assertIs(cPickle.loads(pickle.dumps(True, True)), True) 331 self.assertIs(cPickle.loads(pickle.dumps(False, True)), False) 332 333 def test_picklevalues(self): 334 import pickle, cPickle 335 336 # Test for specific backwards-compatible pickle values 337 self.assertEqual(pickle.dumps(True), "I01\n.") 338 self.assertEqual(pickle.dumps(False), "I00\n.") 339 self.assertEqual(cPickle.dumps(True), "I01\n.") 340 self.assertEqual(cPickle.dumps(False), "I00\n.") 341 self.assertEqual(pickle.dumps(True, True), "I01\n.") 342 self.assertEqual(pickle.dumps(False, True), "I00\n.") 343 self.assertEqual(cPickle.dumps(True, True), "I01\n.") 344 self.assertEqual(cPickle.dumps(False, True), "I00\n.") 345 346 def test_convert_to_bool(self): 347 # Verify that TypeError occurs when bad things are returned 348 # from __nonzero__(). This isn't really a bool test, but 349 # it's related. 350 check = lambda o: self.assertRaises(TypeError, bool, o) 351 class Foo(object): 352 def __nonzero__(self): 353 return self 354 check(Foo()) 355 356 class Bar(object): 357 def __nonzero__(self): 358 return "Yes" 359 check(Bar()) 360 361 class Baz(int): 362 def __nonzero__(self): 363 return self 364 check(Baz()) 365 366 367 def test_main(): 368 test_support.run_unittest(BoolTest) 369 370 if __name__ == "__main__": 371 test_main() 372