99_builtin_func.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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 hash(0) == 0
  80. assert hash(123) == 123
  81. assert hash(-456) == -456
  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. l = []
  118. for i in range(128):
  119. l.append(f'{i} {chr(i)}')
  120. assert l == ['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 type(bin(1234)) is str
  122. # test __repr__:
  123. class A():
  124. def __init__(self):
  125. self.attr = 0
  126. repr(A())
  127. # 未完全测试准确性-----------------------------------------------
  128. # 33600: 318: _vm->bind_constructor<-1>("range", [](VM* vm, ArgsView args) {
  129. # 16742: 319: args._begin += 1; // skip cls
  130. # 16742: 320: Range r;
  131. # 16742: 321: switch (args.size()) {
  132. # 8735: 322: case 1: r.stop = CAST(i64, args[0]); break;
  133. # 3867: 323: case 2: r.start = CAST(i64, args[0]); r.stop = CAST(i64, args[1]); break;
  134. # 4140: 324: case 3: r.start = CAST(i64, args[0]); r.stop = CAST(i64, args[1]); r.step = CAST(i64, args[2]); break;
  135. # #####: 325: default: vm->TypeError("expected 1-3 arguments, got " + std::to_string(args.size()));
  136. # #####: 326: }
  137. # 33484: 327: return VAR(r);
  138. # 16742: 328: });
  139. # -: 329:
  140. # test range:
  141. try:
  142. range(1,2,3,4)
  143. print('未能拦截错误, 在测试 range')
  144. exit(1)
  145. except:
  146. pass
  147. # /************ int ************/
  148. try:
  149. int('asad')
  150. print('未能拦截错误, 在测试 int')
  151. exit(1)
  152. except:
  153. pass
  154. try:
  155. int(123, 16)
  156. print('未能拦截错误, 在测试 int')
  157. exit(1)
  158. except:
  159. pass
  160. assert type(10//11) is int
  161. assert type(11%2) is int
  162. try:
  163. float('asad')
  164. print('未能拦截错误, 在测试 float')
  165. exit(1)
  166. except:
  167. pass
  168. try:
  169. float([])
  170. print('未能拦截错误, 在测试 float')
  171. exit(1)
  172. except:
  173. pass
  174. # /************ str ************/
  175. # test str.__rmul__:
  176. assert type(12 * '12') is str
  177. # 未完全测试准确性-----------------------------------------------
  178. # test str.index:
  179. assert type('25363546'.index('63')) is int
  180. try:
  181. '25363546'.index('err')
  182. print('未能拦截错误, 在测试 str.index')
  183. exit(1)
  184. except:
  185. pass
  186. # 未完全测试准确性-----------------------------------------------
  187. # test str.find:
  188. assert '25363546'.find('63') == 3
  189. assert '25363546'.find('err') == -1
  190. # /************ list ************/
  191. try:
  192. list(1,2)
  193. print('未能拦截错误, 在测试 list')
  194. exit(1)
  195. except:
  196. pass
  197. # 未完全测试准确性----------------------------------------------
  198. # test list.index:
  199. assert type([1,2,3,4,5].index(4)) is int
  200. try:
  201. [1,2,3,4,5].index(6)
  202. print('未能拦截错误, 在测试 list.index')
  203. exit(1)
  204. except:
  205. pass
  206. # 未完全测试准确性----------------------------------------------
  207. # test list.remove:
  208. try:
  209. [1,2,3,4,5].remove(6)
  210. print('未能拦截错误, 在测试 list.remove')
  211. exit(1)
  212. except:
  213. pass
  214. # 未完全测试准确性----------------------------------------------
  215. # test list.pop:
  216. try:
  217. [1,2,3,4,5].pop(1,2,3,4)
  218. print('未能拦截错误, 在测试 list.pop')
  219. exit(1)
  220. except:
  221. pass
  222. # 未完全测试准确性-----------------------------------------------
  223. # test list.__rmul__:
  224. assert type(12 * [12]) is list
  225. # /************ tuple ************/
  226. # 未完全测试准确性-----------------------------------------------
  227. # 180: 783: _vm->bind_constructor<-1>("tuple", [](VM* vm, ArgsView args) {
  228. # 32: 784: if(args.size() == 1+0) return VAR(Tuple(0));
  229. # 32: 785: if(args.size() == 1+1){
  230. # 32: 786: List list = CAST(List, vm->py_list(args[1]));
  231. # 32: 787: return VAR(Tuple(std::move(list)));
  232. # 32: 788: }
  233. # #####: 789: vm->TypeError("tuple() takes at most 1 argument");
  234. # #####: 790: return vm->None;
  235. # 32: 791: });
  236. # -: 792:
  237. # test tuple:
  238. try:
  239. tuple(1,2)
  240. print('未能拦截错误, 在测试 tuple')
  241. exit(1)
  242. except:
  243. pass
  244. assert (1,2,3).__contains__(5) == False
  245. assert (1,2,2,3,3,3).count(3) == 3
  246. assert (1,2,2,3,3,3).count(0) == 0
  247. assert repr(True) == 'True'
  248. assert repr(False) == 'False'
  249. assert True & True == 1
  250. assert True | True == 1
  251. assert (True ^ True) == 0
  252. assert (True == True) == 1
  253. assert type(hash(bytes([0x41, 0x42, 0x43]))) is int
  254. # 未完全测试准确性-----------------------------------------------
  255. # test bytes.__repr__:
  256. assert type(repr(bytes([0x41, 0x42, 0x43]))) is str
  257. # /************ slice ************/
  258. # 未完全测试准确性-----------------------------------------------
  259. # 116: 953: _vm->bind_constructor<4>("slice", [](VM* vm, ArgsView args) {
  260. # #####: 954: return VAR(Slice(args[1], args[2], args[3]));
  261. # -: 955: });
  262. # test slice:
  263. assert type(slice(0.1, 0.2, 0.3)) is slice
  264. # 未完全测试准确性-----------------------------------------------
  265. # 116: 1529: bind_property(_t(tp_slice), "start", [](VM* vm, ArgsView args){
  266. # #####: 1530: return CAST(Slice&, args[0]).start;
  267. # -: 1531: });
  268. # 116: 1532: bind_property(_t(tp_slice), "stop", [](VM* vm, ArgsView args){
  269. # #####: 1533: return CAST(Slice&, args[0]).stop;
  270. # -: 1534: });
  271. # 116: 1535: bind_property(_t(tp_slice), "step", [](VM* vm, ArgsView args){
  272. # #####: 1536: return CAST(Slice&, args[0]).step;
  273. # -: 1537: });
  274. s = slice(1, 2, 3)
  275. assert type(s) is slice
  276. assert s.start == 1
  277. assert s.stop == 2
  278. assert s.step == 3
  279. assert slice.__dict__['start'].__signature__ == 'start'
  280. # 未完全测试准确性-----------------------------------------------
  281. # test slice.__repr__
  282. assert type(repr(slice(1,1,1))) is str
  283. # /************ mappingproxy ************/
  284. # test mappingproxy.keys:
  285. class A():
  286. def __init__(self):
  287. self.a = 10
  288. def method(self):
  289. pass
  290. my_mappingproxy = A().__dict__
  291. assert type(my_mappingproxy.keys()) is list
  292. # 未完全测试准确性-----------------------------------------------
  293. # test mappingproxy.values:
  294. class A():
  295. def __init__(self):
  296. self.a = 10
  297. def method(self):
  298. pass
  299. my_mappingproxy = A().__dict__
  300. assert type(my_mappingproxy.values()) is list
  301. class A():
  302. def __init__(self):
  303. self.a = 10
  304. def method(self):
  305. pass
  306. my_mappingproxy = A().__dict__
  307. assert type(len(my_mappingproxy)) is int
  308. class A():
  309. def __init__(self):
  310. self.a = 10
  311. def method(self):
  312. pass
  313. my_mappingproxy = A().__dict__
  314. try:
  315. hash(my_mappingproxy)
  316. print('未能拦截错误, 在测试 mappingproxy.__hash__')
  317. exit(1)
  318. except TypeError:
  319. pass
  320. a = hash(object()) # object is hashable
  321. a = hash(A()) # A is hashable
  322. class B:
  323. def __eq__(self, o): return True
  324. try:
  325. hash(B())
  326. print('未能拦截错误, 在测试 B.__hash__')
  327. exit(1)
  328. except TypeError:
  329. pass
  330. # 未完全测试准确性-----------------------------------------------
  331. # test mappingproxy.__repr__:
  332. class A():
  333. def __init__(self):
  334. self.a = 10
  335. def method(self):
  336. pass
  337. my_mappingproxy = A().__dict__
  338. assert type(repr(my_mappingproxy)) is str
  339. # /************ dict ************/
  340. # 未完全测试准确性-----------------------------------------------
  341. # test dict:
  342. assert type(dict([(1,2)])) is dict
  343. try:
  344. dict([(1, 2, 3)])
  345. print('未能拦截错误, 在测试 dict')
  346. exit(1)
  347. except:
  348. pass
  349. try:
  350. dict([(1, 2)], 1)
  351. print('未能拦截错误, 在测试 dict')
  352. exit(1)
  353. except:
  354. pass
  355. try:
  356. hash(dict([(1,2)]))
  357. print('未能拦截错误, 在测试 dict.__hash__')
  358. exit(1)
  359. except:
  360. pass
  361. # test dict.__iter__
  362. for k in {1:2, 2:3, 3:4}:
  363. assert k in [1,2,3]
  364. # 未完全测试准确性-----------------------------------------------
  365. # test dict.get
  366. assert {1:2, 3:4}.get(1) == 2
  367. assert {1:2, 3:4}.get(2) is None
  368. assert {1:2, 3:4}.get(20, 100) == 100
  369. try:
  370. {1:2, 3:4}.get(1,1, 1)
  371. print('未能拦截错误, 在测试 dict.get')
  372. exit(1)
  373. except:
  374. pass
  375. # 未完全测试准确性-----------------------------------------------
  376. # test dict.__repr__
  377. assert type(repr({1:2, 3:4})) is str
  378. # /************ property ************/
  379. class A():
  380. def __init__(self):
  381. self._name = '123'
  382. @property
  383. def value(self):
  384. return 2
  385. def get_name(self):
  386. '''
  387. doc string 1
  388. '''
  389. return self._name
  390. def set_name(self, val):
  391. '''
  392. doc string 2
  393. '''
  394. self._name = val
  395. assert A().value == 2
  396. assert A.__dict__['value'].__signature__ == ''
  397. A.name = property(A.get_name, A.set_name, "name: str")
  398. assert A.__dict__['name'].__signature__ == 'name: str'
  399. try:
  400. property(A.get_name, A.set_name, 1)
  401. print('未能拦截错误, 在测试 property')
  402. exit(1)
  403. except:
  404. pass
  405. class Vector2:
  406. def __init__(self) -> None:
  407. self._x = 0
  408. @property
  409. def x(self):
  410. return self._x
  411. @x.setter
  412. def x(self, val):
  413. self._x = val
  414. v = Vector2()
  415. assert v.x == 0
  416. v.x = 10
  417. assert v.x == 10
  418. # function.__doc__
  419. def aaa():
  420. '12345'
  421. pass
  422. assert type(aaa.__doc__) is str
  423. # function.__signature__
  424. def aaa():
  425. pass
  426. assert type(aaa.__signature__) is str
  427. # /************ module time ************/
  428. import time
  429. # test time.time
  430. assert type(time.time()) is float
  431. local_t = time.localtime()
  432. assert type(local_t.tm_year) is int
  433. assert type(local_t.tm_mon) is int
  434. assert type(local_t.tm_mday) is int
  435. assert type(local_t.tm_hour) is int
  436. assert type(local_t.tm_min) is int
  437. assert type(local_t.tm_sec) is int
  438. assert type(local_t.tm_wday) is int
  439. assert type(local_t.tm_yday) is int
  440. assert type(local_t.tm_isdst) is int
  441. # test time.sleep
  442. time.sleep(0.1)
  443. # test time.localtime
  444. assert type(time.localtime()) is time.struct_time
  445. # test min/max
  446. assert min(1, 2) == 1
  447. assert min(1, 2, 3) == 1
  448. assert min([1, 2]) == 1
  449. assert min([1, 2], key=lambda x: -x) == 2
  450. assert max(1, 2) == 2
  451. assert max(1, 2, 3) == 3
  452. assert max([1, 2]) == 2
  453. assert max([1, 2, 3], key=lambda x: -x) == 1
  454. assert min([
  455. (1, 2),
  456. (1, 3),
  457. (1, 4),
  458. ]) == (1, 2)
  459. assert min(1, 2) == 1
  460. assert max(1, 2) == 2
  461. # test callable
  462. assert callable(lambda: 1) is True # function
  463. assert callable(1) is False # int
  464. assert callable(object) is True # type
  465. assert callable(object()) is False
  466. assert callable([].append) is True # bound method
  467. assert callable([].__getitem__) is True # bound method
  468. class A:
  469. def __init__(self):
  470. pass
  471. def __call__(self):
  472. pass
  473. assert callable(A) is True # type
  474. assert callable(A()) is True # instance with __call__
  475. assert callable(A.__call__) is True # bound method
  476. assert callable(A.__init__) is True # bound method
  477. assert callable(print) is True # builtin function
  478. assert callable(isinstance) is True # builtin function
  479. assert id(0) is None
  480. assert id(2**62) is not None
  481. # test issubclass
  482. assert issubclass(int, int) is True
  483. assert issubclass(int, object) is True
  484. assert issubclass(object, int) is False
  485. assert issubclass(object, object) is True
  486. assert issubclass(int, type) is False
  487. assert issubclass(type, type) is True
  488. assert issubclass(float, int) is False
  489. def f(a, b):
  490. c = a
  491. del a
  492. return sum([b, c])
  493. assert f(1, 2) == 3