1
0

770_builtin_func_1.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. # test super:
  2. class TestSuperBase():
  3. def __init__(self):
  4. self.base_attr = 1
  5. def base_method(self):
  6. return self.base_attr
  7. def error(self):
  8. raise RuntimeError('未能拦截错误')
  9. class TestSuperChild1(TestSuperBase):
  10. def __init__(self):
  11. super(TestSuperChild1, self).__init__()
  12. def child_method(self):
  13. return super(TestSuperChild1, self).base_method()
  14. def error_handling(self):
  15. try:
  16. super(TestSuperChild1, self).error()
  17. except RuntimeError:
  18. pass
  19. class TestSuperChild2(TestSuperBase):
  20. pass
  21. test_base = TestSuperBase()
  22. # 测试属性
  23. assert test_base.base_attr == 1
  24. # 测试方法
  25. assert test_base.base_method() == 1
  26. test_child1 = TestSuperChild1()
  27. # 测试继承的属性
  28. assert test_child1.base_attr == 1
  29. # 测试继承的方法
  30. assert test_child1.base_method() == 1
  31. # 测试子类添加的方法
  32. assert test_child1.child_method() == 1
  33. # 测试子类的错误拦截
  34. test_child1.error_handling()
  35. test_child2 = TestSuperChild2()
  36. # 测试继承的属性
  37. assert test_child2.base_attr == 1
  38. # 测试继承的方法
  39. assert test_child2.base_method() == 1
  40. class TestSuperNoBaseMethod(TestSuperBase):
  41. def __init__(self):
  42. super(TestSuperNoBaseMethod, self).append(1)
  43. class TestSuperNoParent():
  44. def method(self):
  45. super(TestSuperNoParent, self).method()
  46. try:
  47. t = TestSuperNoParent().method()
  48. print('未能拦截错误2')
  49. exit(2)
  50. except AttributeError:
  51. pass
  52. try:
  53. t = TestSuperNoBaseMethod()
  54. print('未能拦截错误3')
  55. exit(3)
  56. except AttributeError:
  57. pass
  58. class B():
  59. pass
  60. class C():
  61. def method(self):
  62. super(C, self).method()
  63. class D():
  64. def method(self):
  65. super(B, self).method()
  66. try:
  67. c = C()
  68. c.method()
  69. print('未能拦截错误4')
  70. exit(4)
  71. except AttributeError:
  72. pass
  73. try:
  74. d = D()
  75. d.method()
  76. print('未能拦截错误5')
  77. exit(5)
  78. except TypeError:
  79. pass
  80. # test hash:
  81. # 测试整数类型的输入
  82. assert type(hash(0)) is int
  83. assert type(hash(123)) is int
  84. assert type(hash(-456)) is int
  85. # 测试字符串类型的输入
  86. assert type(hash("hello")) is int
  87. # 测试浮点数类型的输入
  88. assert type(hash(3.14)) is int
  89. assert type(hash(-2.71828)) is int
  90. # 测试边界情况
  91. # assert type(hash(None)) is int
  92. assert hash(True) == 1
  93. assert hash(False) == 0
  94. # 测试元组
  95. assert type(hash((4, 5, 6, (1234,1122), 2.3983, 'abcd'))) is int
  96. # 测试自定义类和对象的输入
  97. class A():
  98. pass
  99. a = A()
  100. assert type(hash(A)) is int
  101. assert type(hash(a)) is int
  102. # 测试函数的输入
  103. def f():
  104. pass
  105. assert type(hash(a)) is int
  106. # 测试不可哈希对象
  107. try:
  108. hash({1:1})
  109. print('未能拦截错误6')
  110. exit(6)
  111. except TypeError:
  112. pass
  113. try:
  114. hash([1])
  115. print('未能拦截错误7')
  116. exit(7)
  117. except TypeError:
  118. pass
  119. # test chr
  120. actual = []
  121. for i in range(128):
  122. actual.append(f'{i} {chr(i)}')
  123. expected = ['0 \x00', '1 \x01', '2 \x02', '3 \x03', '4 \x04', '5 \x05', '6 \x06', '7 \x07', '8 \x08', '9 \t', '10 \n', '11 \x0b', '12 \x0c', '13 \r', '14 \x0e', '15 \x0f', '16 \x10', '17 \x11', '18 \x12', '19 \x13', '20 \x14', '21 \x15', '22 \x16', '23 \x17', '24 \x18', '25 \x19', '26 \x1a', '27 \x1b', '28 \x1c', '29 \x1d', '30 \x1e', '31 \x1f', '32 ', '33 !', '34 "', '35 #', '36 $', '37 %', '38 &', "39 '", '40 (', '41 )', '42 *', '43 +', '44 ,', '45 -', '46 .', '47 /', '48 0', '49 1', '50 2', '51 3', '52 4', '53 5', '54 6', '55 7', '56 8', '57 9', '58 :', '59 ;', '60 <', '61 =', '62 >', '63 ?', '64 @', '65 A', '66 B', '67 C', '68 D', '69 E', '70 F', '71 G', '72 H', '73 I', '74 J', '75 K', '76 L', '77 M', '78 N', '79 O', '80 P', '81 Q', '82 R', '83 S', '84 T', '85 U', '86 V', '87 W', '88 X', '89 Y', '90 Z', '91 [', '92 \\', '93 ]', '94 ^', '95 _', '96 `', '97 a', '98 b', '99 c', '100 d', '101 e', '102 f', '103 g', '104 h', '105 i', '106 j', '107 k', '108 l', '109 m', '110 n', '111 o', '112 p', '113 q', '114 r', '115 s', '116 t', '117 u', '118 v', '119 w', '120 x', '121 y', '122 z', '123 {', '124 |', '125 }', '126 ~', '127 \x7f']
  124. assert len(actual) == len(expected)
  125. for i in range(len(actual)):
  126. assert (actual[i] == expected[i]), (actual[i], expected[i])
  127. # assert type(bin(1234)) is str
  128. # test __repr__:
  129. class A():
  130. def __init__(self):
  131. self.attr = 0
  132. repr(A())
  133. try:
  134. range(1,2,3,4)
  135. print('未能拦截错误8, 在测试 range')
  136. exit(8)
  137. except TypeError:
  138. pass
  139. # /************ int ************/
  140. try:
  141. int('asad')
  142. print('未能拦截错误9, 在测试 int')
  143. exit(9)
  144. except ValueError:
  145. pass
  146. try:
  147. int(123, 16)
  148. print('未能拦截错误10, 在测试 int')
  149. exit(10)
  150. except TypeError:
  151. pass
  152. assert type(10//11) is int
  153. assert type(11%2) is int
  154. try:
  155. float('asad')
  156. print('未能拦截错误11, 在测试 float')
  157. exit(11)
  158. except ValueError:
  159. pass
  160. try:
  161. float([])
  162. print('未能拦截错误12, 在测试 float')
  163. exit(12)
  164. except TypeError:
  165. pass
  166. # /************ str ************/
  167. # test str.__rmul__:
  168. assert type(12 * '12') is str
  169. # 未完全测试准确性-----------------------------------------------
  170. # test str.index:
  171. assert type('25363546'.index('63')) is int
  172. try:
  173. '25363546'.index('err')
  174. print('未能拦截错误13, 在测试 str.index')
  175. exit(13)
  176. except ValueError as e:
  177. assert str(e) == "substring not found"
  178. # 未完全测试准确性-----------------------------------------------
  179. # test str.find:
  180. assert '25363546'.find('63') == 3
  181. assert '25363546'.find('err') == -1
  182. # /************ list ************/
  183. try:
  184. list(1,2)
  185. print('未能拦截错误14, 在测试 list')
  186. exit(14)
  187. except TypeError:
  188. pass
  189. # 未完全测试准确性----------------------------------------------
  190. # test list.index:
  191. assert type([1,2,3,4,5].index(4)) is int
  192. try:
  193. [1,2,3,4,5].index(6)
  194. print('未能拦截错误15, 在测试 list.index')
  195. exit(15)
  196. except ValueError as e:
  197. assert str(e) == "list.index(x): x not in list"
  198. # 未完全测试准确性----------------------------------------------
  199. # test list.remove:
  200. try:
  201. [1,2,3,4,5].remove(6)
  202. print('未能拦截错误16, 在测试 list.remove')
  203. exit(16)
  204. except ValueError as e:
  205. assert str(e) == "list.remove(x): x not in list"
  206. # 未完全测试准确性----------------------------------------------
  207. # test list.pop:
  208. try:
  209. [1,2,3,4,5].pop(1,2,3,4)
  210. print('未能拦截错误17, 在测试 list.pop')
  211. exit(17)
  212. except TypeError:
  213. pass
  214. # 未完全测试准确性-----------------------------------------------
  215. # test list.__rmul__:
  216. assert type(12 * [12]) is list
  217. # /************ tuple ************/
  218. # test tuple:
  219. try:
  220. tuple(1,2)
  221. print('未能拦截错误18, 在测试 tuple')
  222. exit(18)
  223. except TypeError:
  224. pass
  225. assert [1,2,2,3,3,3].count(3) == 3
  226. assert [1,2,2,3,3,3].count(0) == 0
  227. assert 3 in (1, 3, 4)
  228. assert 5 not in (1, 3, 4)
  229. assert repr(True) == 'True'
  230. assert repr(False) == 'False'
  231. assert True & True == True
  232. assert True | False == True
  233. assert (True ^ True) == False
  234. assert (True == True) == True
  235. assert type(hash(bytes([0x41, 0x42, 0x43]))) is int
  236. # 未完全测试准确性-----------------------------------------------
  237. # test bytes.__repr__:
  238. assert type(repr(bytes([0x41, 0x42, 0x43]))) is str
  239. # /************ slice ************/
  240. assert type(slice(0.1, 0.2, 0.3)) is slice