77_builtin_func.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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 Exception('未能拦截错误')
  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:
  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. try:
  44. t = TestSuperNoParent()
  45. print('未能拦截错误')
  46. exit(1)
  47. except:
  48. pass
  49. try:
  50. t = TestSuperNoBaseMethod()
  51. print('未能拦截错误')
  52. exit(1)
  53. except:
  54. pass
  55. class B():
  56. pass
  57. class C():
  58. def method(self):
  59. super(C, self).method()
  60. class D():
  61. def method(self):
  62. super(B, self).method()
  63. try:
  64. c = C()
  65. c.method()
  66. print('未能拦截错误')
  67. exit(1)
  68. except:
  69. pass
  70. try:
  71. d = D()
  72. d.method()
  73. print('未能拦截错误')
  74. exit(1)
  75. except:
  76. pass
  77. # test hash:
  78. # 测试整数类型的输入
  79. assert type(hash(0)) is int
  80. assert type(hash(123)) is int
  81. assert type(hash(-456)) is int
  82. # 测试字符串类型的输入
  83. assert type(hash("hello")) is int
  84. # 测试浮点数类型的输入
  85. assert type(hash(3.14)) is int
  86. assert type(hash(-2.71828)) is int
  87. # 测试边界情况
  88. # assert type(hash(None)) is int
  89. assert hash(True) == 1
  90. assert hash(False) == 0
  91. # 测试元组
  92. assert type(hash((4, 5, 6, (1234,1122), 2.3983, 'abcd'))) is int
  93. # 测试自定义类和对象的输入
  94. class A():
  95. pass
  96. a = A()
  97. assert type(hash(A)) is int
  98. assert type(hash(a)) is int
  99. # 测试函数的输入
  100. def f():
  101. pass
  102. assert type(hash(a)) is int
  103. # 测试不可哈希对象
  104. try:
  105. hash({1:1})
  106. print('未能拦截错误')
  107. exit(1)
  108. except:
  109. pass
  110. try:
  111. hash([1])
  112. print('未能拦截错误')
  113. exit(1)
  114. except:
  115. pass
  116. # test chr
  117. actual = []
  118. for i in range(128):
  119. actual.append(f'{i} {chr(i)}')
  120. 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']
  121. assert len(actual) == len(expected)
  122. for i in range(len(actual)):
  123. assert (actual[i] == expected[i]), (actual[i], expected[i])
  124. # assert type(bin(1234)) is str
  125. # test __repr__:
  126. class A():
  127. def __init__(self):
  128. self.attr = 0
  129. repr(A())
  130. try:
  131. range(1,2,3,4)
  132. print('未能拦截错误, 在测试 range')
  133. exit(1)
  134. except:
  135. pass
  136. # /************ int ************/
  137. try:
  138. int('asad')
  139. print('未能拦截错误, 在测试 int')
  140. exit(1)
  141. except:
  142. pass
  143. try:
  144. int(123, 16)
  145. print('未能拦截错误, 在测试 int')
  146. exit(1)
  147. except:
  148. pass
  149. assert type(10//11) is int
  150. assert type(11%2) is int
  151. try:
  152. float('asad')
  153. print('未能拦截错误, 在测试 float')
  154. exit(1)
  155. except:
  156. pass
  157. try:
  158. float([])
  159. print('未能拦截错误, 在测试 float')
  160. exit(1)
  161. except:
  162. pass
  163. # /************ str ************/
  164. # test str.__rmul__:
  165. assert type(12 * '12') is str
  166. # 未完全测试准确性-----------------------------------------------
  167. # test str.index:
  168. assert type('25363546'.index('63')) is int
  169. try:
  170. '25363546'.index('err')
  171. print('未能拦截错误, 在测试 str.index')
  172. exit(1)
  173. except:
  174. pass
  175. # 未完全测试准确性-----------------------------------------------
  176. # test str.find:
  177. assert '25363546'.find('63') == 3
  178. assert '25363546'.find('err') == -1
  179. # /************ list ************/
  180. try:
  181. list(1,2)
  182. print('未能拦截错误, 在测试 list')
  183. exit(1)
  184. except:
  185. pass
  186. # 未完全测试准确性----------------------------------------------
  187. # test list.index:
  188. assert type([1,2,3,4,5].index(4)) is int
  189. try:
  190. [1,2,3,4,5].index(6)
  191. print('未能拦截错误, 在测试 list.index')
  192. exit(1)
  193. except:
  194. pass
  195. # 未完全测试准确性----------------------------------------------
  196. # test list.remove:
  197. try:
  198. [1,2,3,4,5].remove(6)
  199. print('未能拦截错误, 在测试 list.remove')
  200. exit(1)
  201. except:
  202. pass
  203. # 未完全测试准确性----------------------------------------------
  204. # test list.pop:
  205. try:
  206. [1,2,3,4,5].pop(1,2,3,4)
  207. print('未能拦截错误, 在测试 list.pop')
  208. exit(1)
  209. except:
  210. pass
  211. # 未完全测试准确性-----------------------------------------------
  212. # test list.__rmul__:
  213. assert type(12 * [12]) is list
  214. # /************ tuple ************/
  215. # test tuple:
  216. try:
  217. tuple(1,2)
  218. print('未能拦截错误, 在测试 tuple')
  219. exit(1)
  220. except:
  221. pass
  222. assert [1,2,2,3,3,3].count(3) == 3
  223. assert [1,2,2,3,3,3].count(0) == 0
  224. assert 3 in (1, 3, 4)
  225. assert 5 not in (1, 3, 4)
  226. assert repr(True) == 'True'
  227. assert repr(False) == 'False'
  228. assert True & True == True
  229. assert True | False == True
  230. assert (True ^ True) == False
  231. assert (True == True) == True
  232. assert type(hash(bytes([0x41, 0x42, 0x43]))) is int
  233. # 未完全测试准确性-----------------------------------------------
  234. # test bytes.__repr__:
  235. assert type(repr(bytes([0x41, 0x42, 0x43]))) is str
  236. # /************ slice ************/
  237. assert type(slice(0.1, 0.2, 0.3)) is slice
  238. # 未完全测试准确性-----------------------------------------------
  239. # 116: 1529: bind_property(_t(tp_slice), "start", [](VM* vm, ArgsView args){
  240. # #####: 1530: return CAST(Slice&, args[0]).start;
  241. # -: 1531: });
  242. # 116: 1532: bind_property(_t(tp_slice), "stop", [](VM* vm, ArgsView args){
  243. # #####: 1533: return CAST(Slice&, args[0]).stop;
  244. # -: 1534: });
  245. # 116: 1535: bind_property(_t(tp_slice), "step", [](VM* vm, ArgsView args){
  246. # #####: 1536: return CAST(Slice&, args[0]).step;
  247. # -: 1537: });
  248. s = slice(1, 2, 3)
  249. assert type(s) is slice
  250. assert s.start == 1
  251. assert s.stop == 2
  252. assert s.step == 3
  253. # 未完全测试准确性-----------------------------------------------
  254. # test slice.__repr__
  255. assert type(repr(slice(1,1,1))) is str
  256. # /************ namedict ************/
  257. # # test namedict.keys:
  258. # class A():
  259. # def __init__(self):
  260. # self.a = 10
  261. # def method(self):
  262. # pass
  263. # my_namedict = A().__dict__
  264. # assert type(my_namedict.keys()) is list
  265. # # test namedict.values:
  266. # class A():
  267. # def __init__(self):
  268. # self.a = 10
  269. # def method(self):
  270. # pass
  271. # my_namedict = A().__dict__
  272. # assert type(my_namedict.values()) is list
  273. # class A():
  274. # def __init__(self):
  275. # self.a = 10
  276. # def method(self):
  277. # pass
  278. # my_namedict = A().__dict__
  279. # assert type(len(my_namedict)) is int
  280. class A():
  281. def __init__(self):
  282. self.a = 10
  283. def method(self):
  284. pass
  285. my_namedict = A().__dict__
  286. try:
  287. hash(my_namedict)
  288. print('未能拦截错误, 在测试 namedict.__hash__')
  289. exit(1)
  290. except TypeError:
  291. pass
  292. a = hash(object()) # object is hashable
  293. a = hash(A()) # A is hashable
  294. class B:
  295. def __eq__(self, o): return True
  296. def __ne__(self, o): return False
  297. try:
  298. hash(B())
  299. print('未能拦截错误, 在测试 B.__hash__')
  300. exit(1)
  301. except TypeError:
  302. pass
  303. # 未完全测试准确性-----------------------------------------------
  304. # test namedict.__repr__:
  305. class A():
  306. def __init__(self):
  307. self.a = 10
  308. def method(self):
  309. pass
  310. my_namedict = A().__dict__
  311. assert type(repr(my_namedict)) is str
  312. # /************ dict ************/
  313. # 未完全测试准确性-----------------------------------------------
  314. # test dict:
  315. assert type(dict([(1,2)])) is dict
  316. try:
  317. dict([(1, 2, 3)])
  318. print('未能拦截错误, 在测试 dict')
  319. exit(1)
  320. except:
  321. pass
  322. try:
  323. dict([(1, 2)], 1)
  324. print('未能拦截错误, 在测试 dict')
  325. exit(1)
  326. except:
  327. pass
  328. try:
  329. hash(dict([(1,2)]))
  330. print('未能拦截错误, 在测试 dict.__hash__')
  331. exit(1)
  332. except:
  333. pass
  334. # test dict.__iter__
  335. for k in {1:2, 2:3, 3:4}.keys():
  336. assert k in [1,2,3]
  337. # 未完全测试准确性-----------------------------------------------
  338. # test dict.get
  339. assert {1:2, 3:4}.get(1) == 2
  340. assert {1:2, 3:4}.get(2) is None
  341. assert {1:2, 3:4}.get(20, 100) == 100
  342. try:
  343. {1:2, 3:4}.get(1,1, 1)
  344. print('未能拦截错误, 在测试 dict.get')
  345. exit(1)
  346. except:
  347. pass
  348. # 未完全测试准确性-----------------------------------------------
  349. # test dict.__repr__
  350. assert type(repr({1:2, 3:4})) is str
  351. # /************ property ************/
  352. class A():
  353. def __init__(self):
  354. self._name = '123'
  355. @property
  356. def value(self):
  357. return 2
  358. def get_name(self):
  359. '''
  360. doc string 1
  361. '''
  362. return self._name
  363. def set_name(self, val):
  364. '''
  365. doc string 2
  366. '''
  367. self._name = val
  368. assert A().value == 2
  369. A.name = property(A.get_name, A.set_name)
  370. class Vector2:
  371. def __init__(self) -> None:
  372. self._x = 0
  373. @property
  374. def x(self):
  375. return self._x
  376. @x.setter
  377. def x(self, val):
  378. self._x = val
  379. v = Vector2()
  380. assert v.x == 0
  381. v.x = 10
  382. assert v.x == 10
  383. # function.__doc__
  384. def aaa():
  385. '12345'
  386. pass
  387. assert aaa.__doc__ == '12345'
  388. # test callable
  389. assert callable(lambda: 1) is True # function
  390. assert callable(1) is False # int
  391. assert callable(object) is True # type
  392. assert callable(object()) is False
  393. assert callable([].append) is True # bound method
  394. assert callable([].__getitem__) is True # bound method
  395. class A:
  396. def __init__(self):
  397. pass
  398. def __call__(self):
  399. pass
  400. assert callable(A) is True # type
  401. assert callable(A()) is True # instance with __call__
  402. assert callable(A.__call__) is True # bound method
  403. assert callable(A.__init__) is True # bound method
  404. assert callable(print) is True # builtin function
  405. assert callable(isinstance) is True # builtin function
  406. assert id(0) is None
  407. assert id(2**62) is None
  408. # test issubclass
  409. assert issubclass(int, int) is True
  410. assert issubclass(int, object) is True
  411. assert issubclass(object, int) is False
  412. assert issubclass(object, object) is True
  413. assert issubclass(int, type) is False
  414. assert issubclass(type, type) is True
  415. assert issubclass(float, int) is False
  416. def f(a, b):
  417. c = a
  418. del a
  419. return sum([b, c])
  420. assert f(1, 2) == 3
  421. # /************ module time ************/
  422. import time
  423. # test time.time
  424. assert type(time.time()) is float
  425. local_t = time.localtime()
  426. assert type(local_t.tm_year) is int
  427. assert type(local_t.tm_mon) is int
  428. assert type(local_t.tm_mday) is int
  429. assert type(local_t.tm_hour) is int
  430. assert type(local_t.tm_min) is int
  431. assert type(local_t.tm_sec) is int
  432. assert type(local_t.tm_wday) is int
  433. assert type(local_t.tm_yday) is int
  434. assert type(local_t.tm_isdst) is int
  435. # test time.sleep
  436. time.sleep(0.1)
  437. # test time.localtime
  438. assert type(time.localtime()) is time.struct_time
  439. # test min/max
  440. assert min(1, 2) == 1
  441. assert min(1, 2, 3) == 1
  442. assert min([1, 2]) == 1
  443. assert min([1, 2], key=lambda x: -x) == 2
  444. assert max(1, 2) == 2
  445. assert max(1, 2, 3) == 3
  446. assert max([1, 2]) == 2
  447. assert max([1, 2, 3], key=lambda x: -x) == 1
  448. assert min([
  449. (3, 1),
  450. (1, 2),
  451. (1, 3),
  452. (1, 4),
  453. ]) == (1, 2)
  454. assert min(1, 2) == 1
  455. assert max(1, 2) == 2
  456. exit()
  457. dir_int = dir(int)
  458. assert dir_int[:4] == ['__add__', '__and__', '__base__', '__eq__']