PageRenderTime 186ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/edk2/AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_bool.py

https://gitlab.com/envieidoc/Clover
Python | 371 lines | 319 code | 46 blank | 6 comment | 15 complexity | 4c5a1a3381c30c18b31282a6e45d6fbe MD5 | raw file
  1. # Test properties of bool promised by PEP 285
  2. import unittest
  3. from test import test_support
  4. import os
  5. class BoolTest(unittest.TestCase):
  6. def test_subclass(self):
  7. try:
  8. class C(bool):
  9. pass
  10. except TypeError:
  11. pass
  12. else:
  13. self.fail("bool should not be subclassable")
  14. self.assertRaises(TypeError, int.__new__, bool, 0)
  15. def test_print(self):
  16. try:
  17. fo = open(test_support.TESTFN, "wb")
  18. print >> fo, False, True
  19. fo.close()
  20. fo = open(test_support.TESTFN, "rb")
  21. self.assertEqual(fo.read(), 'False True\n')
  22. finally:
  23. fo.close()
  24. os.remove(test_support.TESTFN)
  25. def test_repr(self):
  26. self.assertEqual(repr(False), 'False')
  27. self.assertEqual(repr(True), 'True')
  28. self.assertEqual(eval(repr(False)), False)
  29. self.assertEqual(eval(repr(True)), True)
  30. def test_str(self):
  31. self.assertEqual(str(False), 'False')
  32. self.assertEqual(str(True), 'True')
  33. def test_int(self):
  34. self.assertEqual(int(False), 0)
  35. self.assertIsNot(int(False), False)
  36. self.assertEqual(int(True), 1)
  37. self.assertIsNot(int(True), True)
  38. def test_float(self):
  39. self.assertEqual(float(False), 0.0)
  40. self.assertIsNot(float(False), False)
  41. self.assertEqual(float(True), 1.0)
  42. self.assertIsNot(float(True), True)
  43. def test_long(self):
  44. self.assertEqual(long(False), 0L)
  45. self.assertIsNot(long(False), False)
  46. self.assertEqual(long(True), 1L)
  47. self.assertIsNot(long(True), True)
  48. def test_math(self):
  49. self.assertEqual(+False, 0)
  50. self.assertIsNot(+False, False)
  51. self.assertEqual(-False, 0)
  52. self.assertIsNot(-False, False)
  53. self.assertEqual(abs(False), 0)
  54. self.assertIsNot(abs(False), False)
  55. self.assertEqual(+True, 1)
  56. self.assertIsNot(+True, True)
  57. self.assertEqual(-True, -1)
  58. self.assertEqual(abs(True), 1)
  59. self.assertIsNot(abs(True), True)
  60. self.assertEqual(~False, -1)
  61. self.assertEqual(~True, -2)
  62. self.assertEqual(False+2, 2)
  63. self.assertEqual(True+2, 3)
  64. self.assertEqual(2+False, 2)
  65. self.assertEqual(2+True, 3)
  66. self.assertEqual(False+False, 0)
  67. self.assertIsNot(False+False, False)
  68. self.assertEqual(False+True, 1)
  69. self.assertIsNot(False+True, True)
  70. self.assertEqual(True+False, 1)
  71. self.assertIsNot(True+False, True)
  72. self.assertEqual(True+True, 2)
  73. self.assertEqual(True-True, 0)
  74. self.assertIsNot(True-True, False)
  75. self.assertEqual(False-False, 0)
  76. self.assertIsNot(False-False, False)
  77. self.assertEqual(True-False, 1)
  78. self.assertIsNot(True-False, True)
  79. self.assertEqual(False-True, -1)
  80. self.assertEqual(True*1, 1)
  81. self.assertEqual(False*1, 0)
  82. self.assertIsNot(False*1, False)
  83. self.assertEqual(True//1, 1)
  84. self.assertIsNot(True//1, True)
  85. self.assertEqual(False//1, 0)
  86. self.assertIsNot(False//1, False)
  87. for b in False, True:
  88. for i in 0, 1, 2:
  89. self.assertEqual(b**i, int(b)**i)
  90. self.assertIsNot(b**i, bool(int(b)**i))
  91. for a in False, True:
  92. for b in False, True:
  93. self.assertIs(a&b, bool(int(a)&int(b)))
  94. self.assertIs(a|b, bool(int(a)|int(b)))
  95. self.assertIs(a^b, bool(int(a)^int(b)))
  96. self.assertEqual(a&int(b), int(a)&int(b))
  97. self.assertIsNot(a&int(b), bool(int(a)&int(b)))
  98. self.assertEqual(a|int(b), int(a)|int(b))
  99. self.assertIsNot(a|int(b), bool(int(a)|int(b)))
  100. self.assertEqual(a^int(b), int(a)^int(b))
  101. self.assertIsNot(a^int(b), bool(int(a)^int(b)))
  102. self.assertEqual(int(a)&b, int(a)&int(b))
  103. self.assertIsNot(int(a)&b, bool(int(a)&int(b)))
  104. self.assertEqual(int(a)|b, int(a)|int(b))
  105. self.assertIsNot(int(a)|b, bool(int(a)|int(b)))
  106. self.assertEqual(int(a)^b, int(a)^int(b))
  107. self.assertIsNot(int(a)^b, bool(int(a)^int(b)))
  108. self.assertIs(1==1, True)
  109. self.assertIs(1==0, False)
  110. self.assertIs(0<1, True)
  111. self.assertIs(1<0, False)
  112. self.assertIs(0<=0, True)
  113. self.assertIs(1<=0, False)
  114. self.assertIs(1>0, True)
  115. self.assertIs(1>1, False)
  116. self.assertIs(1>=1, True)
  117. self.assertIs(0>=1, False)
  118. self.assertIs(0!=1, True)
  119. self.assertIs(0!=0, False)
  120. x = [1]
  121. self.assertIs(x is x, True)
  122. self.assertIs(x is not x, False)
  123. self.assertIs(1 in x, True)
  124. self.assertIs(0 in x, False)
  125. self.assertIs(1 not in x, False)
  126. self.assertIs(0 not in x, True)
  127. x = {1: 2}
  128. self.assertIs(x is x, True)
  129. self.assertIs(x is not x, False)
  130. self.assertIs(1 in x, True)
  131. self.assertIs(0 in x, False)
  132. self.assertIs(1 not in x, False)
  133. self.assertIs(0 not in x, True)
  134. self.assertIs(not True, False)
  135. self.assertIs(not False, True)
  136. def test_convert(self):
  137. self.assertRaises(TypeError, bool, 42, 42)
  138. self.assertIs(bool(10), True)
  139. self.assertIs(bool(1), True)
  140. self.assertIs(bool(-1), True)
  141. self.assertIs(bool(0), False)
  142. self.assertIs(bool("hello"), True)
  143. self.assertIs(bool(""), False)
  144. self.assertIs(bool(), False)
  145. def test_format(self):
  146. self.assertEqual("%d" % False, "0")
  147. self.assertEqual("%d" % True, "1")
  148. self.assertEqual("%x" % False, "0")
  149. self.assertEqual("%x" % True, "1")
  150. def test_hasattr(self):
  151. self.assertIs(hasattr([], "append"), True)
  152. self.assertIs(hasattr([], "wobble"), False)
  153. def test_callable(self):
  154. with test_support.check_py3k_warnings():
  155. self.assertIs(callable(len), True)
  156. self.assertIs(callable(1), False)
  157. def test_isinstance(self):
  158. self.assertIs(isinstance(True, bool), True)
  159. self.assertIs(isinstance(False, bool), True)
  160. self.assertIs(isinstance(True, int), True)
  161. self.assertIs(isinstance(False, int), True)
  162. self.assertIs(isinstance(1, bool), False)
  163. self.assertIs(isinstance(0, bool), False)
  164. def test_issubclass(self):
  165. self.assertIs(issubclass(bool, int), True)
  166. self.assertIs(issubclass(int, bool), False)
  167. def test_haskey(self):
  168. self.assertIs(1 in {}, False)
  169. self.assertIs(1 in {1:1}, True)
  170. with test_support.check_py3k_warnings():
  171. self.assertIs({}.has_key(1), False)
  172. self.assertIs({1:1}.has_key(1), True)
  173. def test_string(self):
  174. self.assertIs("xyz".endswith("z"), True)
  175. self.assertIs("xyz".endswith("x"), False)
  176. self.assertIs("xyz0123".isalnum(), True)
  177. self.assertIs("@#$%".isalnum(), False)
  178. self.assertIs("xyz".isalpha(), True)
  179. self.assertIs("@#$%".isalpha(), False)
  180. self.assertIs("0123".isdigit(), True)
  181. self.assertIs("xyz".isdigit(), False)
  182. self.assertIs("xyz".islower(), True)
  183. self.assertIs("XYZ".islower(), False)
  184. self.assertIs(" ".isspace(), True)
  185. self.assertIs("XYZ".isspace(), False)
  186. self.assertIs("X".istitle(), True)
  187. self.assertIs("x".istitle(), False)
  188. self.assertIs("XYZ".isupper(), True)
  189. self.assertIs("xyz".isupper(), False)
  190. self.assertIs("xyz".startswith("x"), True)
  191. self.assertIs("xyz".startswith("z"), False)
  192. if test_support.have_unicode:
  193. self.assertIs(unicode("xyz", 'ascii').endswith(unicode("z", 'ascii')), True)
  194. self.assertIs(unicode("xyz", 'ascii').endswith(unicode("x", 'ascii')), False)
  195. self.assertIs(unicode("xyz0123", 'ascii').isalnum(), True)
  196. self.assertIs(unicode("@#$%", 'ascii').isalnum(), False)
  197. self.assertIs(unicode("xyz", 'ascii').isalpha(), True)
  198. self.assertIs(unicode("@#$%", 'ascii').isalpha(), False)
  199. self.assertIs(unicode("0123", 'ascii').isdecimal(), True)
  200. self.assertIs(unicode("xyz", 'ascii').isdecimal(), False)
  201. self.assertIs(unicode("0123", 'ascii').isdigit(), True)
  202. self.assertIs(unicode("xyz", 'ascii').isdigit(), False)
  203. self.assertIs(unicode("xyz", 'ascii').islower(), True)
  204. self.assertIs(unicode("XYZ", 'ascii').islower(), False)
  205. self.assertIs(unicode("0123", 'ascii').isnumeric(), True)
  206. self.assertIs(unicode("xyz", 'ascii').isnumeric(), False)
  207. self.assertIs(unicode(" ", 'ascii').isspace(), True)
  208. self.assertIs(unicode("XYZ", 'ascii').isspace(), False)
  209. self.assertIs(unicode("X", 'ascii').istitle(), True)
  210. self.assertIs(unicode("x", 'ascii').istitle(), False)
  211. self.assertIs(unicode("XYZ", 'ascii').isupper(), True)
  212. self.assertIs(unicode("xyz", 'ascii').isupper(), False)
  213. self.assertIs(unicode("xyz", 'ascii').startswith(unicode("x", 'ascii')), True)
  214. self.assertIs(unicode("xyz", 'ascii').startswith(unicode("z", 'ascii')), False)
  215. def test_boolean(self):
  216. self.assertEqual(True & 1, 1)
  217. self.assertNotIsInstance(True & 1, bool)
  218. self.assertIs(True & True, True)
  219. self.assertEqual(True | 1, 1)
  220. self.assertNotIsInstance(True | 1, bool)
  221. self.assertIs(True | True, True)
  222. self.assertEqual(True ^ 1, 0)
  223. self.assertNotIsInstance(True ^ 1, bool)
  224. self.assertIs(True ^ True, False)
  225. def test_fileclosed(self):
  226. try:
  227. f = file(test_support.TESTFN, "w")
  228. self.assertIs(f.closed, False)
  229. f.close()
  230. self.assertIs(f.closed, True)
  231. finally:
  232. os.remove(test_support.TESTFN)
  233. def test_types(self):
  234. # types are always true.
  235. for t in [bool, complex, dict, file, float, int, list, long, object,
  236. set, str, tuple, type]:
  237. self.assertIs(bool(t), True)
  238. def test_operator(self):
  239. import operator
  240. self.assertIs(operator.truth(0), False)
  241. self.assertIs(operator.truth(1), True)
  242. with test_support.check_py3k_warnings():
  243. self.assertIs(operator.isCallable(0), False)
  244. self.assertIs(operator.isCallable(len), True)
  245. self.assertIs(operator.isNumberType(None), False)
  246. self.assertIs(operator.isNumberType(0), True)
  247. self.assertIs(operator.not_(1), False)
  248. self.assertIs(operator.not_(0), True)
  249. self.assertIs(operator.isSequenceType(0), False)
  250. self.assertIs(operator.isSequenceType([]), True)
  251. self.assertIs(operator.contains([], 1), False)
  252. self.assertIs(operator.contains([1], 1), True)
  253. self.assertIs(operator.isMappingType(1), False)
  254. self.assertIs(operator.isMappingType({}), True)
  255. self.assertIs(operator.lt(0, 0), False)
  256. self.assertIs(operator.lt(0, 1), True)
  257. self.assertIs(operator.is_(True, True), True)
  258. self.assertIs(operator.is_(True, False), False)
  259. self.assertIs(operator.is_not(True, True), False)
  260. self.assertIs(operator.is_not(True, False), True)
  261. def test_marshal(self):
  262. import marshal
  263. self.assertIs(marshal.loads(marshal.dumps(True)), True)
  264. self.assertIs(marshal.loads(marshal.dumps(False)), False)
  265. def test_pickle(self):
  266. import pickle
  267. self.assertIs(pickle.loads(pickle.dumps(True)), True)
  268. self.assertIs(pickle.loads(pickle.dumps(False)), False)
  269. self.assertIs(pickle.loads(pickle.dumps(True, True)), True)
  270. self.assertIs(pickle.loads(pickle.dumps(False, True)), False)
  271. def test_cpickle(self):
  272. import cPickle
  273. self.assertIs(cPickle.loads(cPickle.dumps(True)), True)
  274. self.assertIs(cPickle.loads(cPickle.dumps(False)), False)
  275. self.assertIs(cPickle.loads(cPickle.dumps(True, True)), True)
  276. self.assertIs(cPickle.loads(cPickle.dumps(False, True)), False)
  277. def test_mixedpickle(self):
  278. import pickle, cPickle
  279. self.assertIs(pickle.loads(cPickle.dumps(True)), True)
  280. self.assertIs(pickle.loads(cPickle.dumps(False)), False)
  281. self.assertIs(pickle.loads(cPickle.dumps(True, True)), True)
  282. self.assertIs(pickle.loads(cPickle.dumps(False, True)), False)
  283. self.assertIs(cPickle.loads(pickle.dumps(True)), True)
  284. self.assertIs(cPickle.loads(pickle.dumps(False)), False)
  285. self.assertIs(cPickle.loads(pickle.dumps(True, True)), True)
  286. self.assertIs(cPickle.loads(pickle.dumps(False, True)), False)
  287. def test_picklevalues(self):
  288. import pickle, cPickle
  289. # Test for specific backwards-compatible pickle values
  290. self.assertEqual(pickle.dumps(True), "I01\n.")
  291. self.assertEqual(pickle.dumps(False), "I00\n.")
  292. self.assertEqual(cPickle.dumps(True), "I01\n.")
  293. self.assertEqual(cPickle.dumps(False), "I00\n.")
  294. self.assertEqual(pickle.dumps(True, True), "I01\n.")
  295. self.assertEqual(pickle.dumps(False, True), "I00\n.")
  296. self.assertEqual(cPickle.dumps(True, True), "I01\n.")
  297. self.assertEqual(cPickle.dumps(False, True), "I00\n.")
  298. def test_convert_to_bool(self):
  299. # Verify that TypeError occurs when bad things are returned
  300. # from __nonzero__(). This isn't really a bool test, but
  301. # it's related.
  302. check = lambda o: self.assertRaises(TypeError, bool, o)
  303. class Foo(object):
  304. def __nonzero__(self):
  305. return self
  306. check(Foo())
  307. class Bar(object):
  308. def __nonzero__(self):
  309. return "Yes"
  310. check(Bar())
  311. class Baz(int):
  312. def __nonzero__(self):
  313. return self
  314. check(Baz())
  315. def test_main():
  316. test_support.run_unittest(BoolTest)
  317. if __name__ == "__main__":
  318. test_main()