99_builtin_func.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. # 未完全测试准确性-----------------------------------------------
  161. # 116: 392: _vm->bind_method<0>("int", "bit_length", [](VM* vm, ArgsView args) {
  162. # #####: 393: i64 x = _CAST(i64, args[0]);
  163. # #####: 394: if(x < 0) x = -x;
  164. # -: 395: int bits = 0;
  165. # #####: 396: while(x){ x >>= 1; bits++; }
  166. # #####: 397: return VAR(bits);
  167. # -: 398: });
  168. # test int.bit_length:
  169. assert type(int.bit_length(100)) is int
  170. assert type(10//11) is int
  171. assert type(11%2) is int
  172. try:
  173. float('asad')
  174. print('未能拦截错误, 在测试 float')
  175. exit(1)
  176. except:
  177. pass
  178. try:
  179. float([])
  180. print('未能拦截错误, 在测试 float')
  181. exit(1)
  182. except:
  183. pass
  184. # /************ str ************/
  185. # test str.__rmul__:
  186. assert type(12 * '12') is str
  187. # 未完全测试准确性-----------------------------------------------
  188. # 116: 554: _vm->bind_method<1>("str", "index", [](VM* vm, ArgsView args) {
  189. # #####: 555: const Str& self = _CAST(Str&, args[0]);
  190. # #####: 556: const Str& sub = CAST(Str&, args[1]);
  191. # #####: 557: int index = self.index(sub);
  192. # #####: 558: if(index == -1) vm->ValueError("substring not found");
  193. # #####: 559: return VAR(index);
  194. # #####: 560: });
  195. # test str.index:
  196. assert type('25363546'.index('63')) is int
  197. try:
  198. '25363546'.index('err')
  199. print('未能拦截错误, 在测试 str.index')
  200. exit(1)
  201. except:
  202. pass
  203. # 未完全测试准确性-----------------------------------------------
  204. # 116: 562: _vm->bind_method<1>("str", "find", [](VM* vm, ArgsView args) {
  205. # #####: 563: const Str& self = _CAST(Str&, args[0]);
  206. # #####: 564: const Str& sub = CAST(Str&, args[1]);
  207. # #####: 565: return VAR(self.index(sub));
  208. # -: 566: });
  209. # test str.find:
  210. assert type('25363546'.find('63')) is int
  211. assert type('25363546'.find('err')) is int
  212. # /************ list ************/
  213. # 未完全测试准确性-----------------------------------------------
  214. # 174: 615: _vm->bind_constructor<-1>("list", [](VM* vm, ArgsView args) {
  215. # 29: 616: if(args.size() == 1+0) return VAR(List());
  216. # 29: 617: if(args.size() == 1+1){
  217. # 29: 618: return vm->py_list(args[1]);
  218. # -: 619: }
  219. # #####: 620: vm->TypeError("list() takes 0 or 1 arguments");
  220. # #####: 621: return vm->None;
  221. # 29: 622: });
  222. # test list:
  223. try:
  224. list(1,2)
  225. print('未能拦截错误, 在测试 list')
  226. exit(1)
  227. except:
  228. pass
  229. # 未完全测试准确性----------------------------------------------
  230. # 116: 648: _vm->bind_method<1>("list", "index", [](VM* vm, ArgsView args) {
  231. # #####: 649: List& self = _CAST(List&, args[0]);
  232. # #####: 650: PyObject* obj = args[1];
  233. # #####: 651: for(int i=0; i<self.size(); i++){
  234. # #####: 652: if(vm->py_eq(self[i], obj)) return VAR(i);
  235. # -: 653: }
  236. # #####: 654: vm->ValueError(_CAST(Str&, vm->py_repr(obj)) + " is not in list");
  237. # #####: 655: return vm->None;
  238. # #####: 656: });
  239. # test list.index:
  240. assert type([1,2,3,4,5].index(4)) is int
  241. try:
  242. [1,2,3,4,5].index(6)
  243. print('未能拦截错误, 在测试 list.index')
  244. exit(1)
  245. except:
  246. pass
  247. # 未完全测试准确性----------------------------------------------
  248. # 118: 658: _vm->bind_method<1>("list", "remove", [](VM* vm, ArgsView args) {
  249. # 1: 659: List& self = _CAST(List&, args[0]);
  250. # 1: 660: PyObject* obj = args[1];
  251. # 2: 661: for(int i=0; i<self.size(); i++){
  252. # 2: 662: if(vm->py_eq(self[i], obj)){
  253. # 1: 663: self.erase(i);
  254. # 1: 664: return vm->None;
  255. # -: 665: }
  256. # -: 666: }
  257. # #####: 667: vm->ValueError(_CAST(Str&, vm->py_repr(obj)) + " is not in list");
  258. # #####: 668: return vm->None;
  259. # 1: 669: });
  260. # test list.remove:
  261. try:
  262. [1,2,3,4,5].remove(6)
  263. print('未能拦截错误, 在测试 list.remove')
  264. exit(1)
  265. except:
  266. pass
  267. # 未完全测试准确性----------------------------------------------
  268. # 2536: 671: _vm->bind_method<-1>("list", "pop", [](VM* vm, ArgsView args) {
  269. # 1210: 672: List& self = _CAST(List&, args[0]);
  270. # 1210: 673: if(args.size() == 1+0){
  271. # 1208: 674: if(self.empty()) vm->IndexError("pop from empty list");
  272. # 1208: 675: return self.popx_back();
  273. # -: 676: }
  274. # 2: 677: if(args.size() == 1+1){
  275. # 2: 678: int index = CAST(int, args[1]);
  276. # 2: 679: index = vm->normalized_index(index, self.size());
  277. # 2: 680: PyObject* ret = self[index];
  278. # 2: 681: self.erase(index);
  279. # -: 682: return ret;
  280. # -: 683: }
  281. # #####: 684: vm->TypeError("pop() takes at most 1 argument");
  282. # #####: 685: return vm->None;
  283. # 1210: 686: });
  284. # test list.pop:
  285. try:
  286. [1,2,3,4,5].pop(1,2,3,4)
  287. print('未能拦截错误, 在测试 list.pop')
  288. exit(1)
  289. except:
  290. pass
  291. # 未完全测试准确性-----------------------------------------------
  292. # test list.__rmul__:
  293. assert type(12 * [12]) is list
  294. # /************ tuple ************/
  295. # 未完全测试准确性-----------------------------------------------
  296. # 180: 783: _vm->bind_constructor<-1>("tuple", [](VM* vm, ArgsView args) {
  297. # 32: 784: if(args.size() == 1+0) return VAR(Tuple(0));
  298. # 32: 785: if(args.size() == 1+1){
  299. # 32: 786: List list = CAST(List, vm->py_list(args[1]));
  300. # 32: 787: return VAR(Tuple(std::move(list)));
  301. # 32: 788: }
  302. # #####: 789: vm->TypeError("tuple() takes at most 1 argument");
  303. # #####: 790: return vm->None;
  304. # 32: 791: });
  305. # -: 792:
  306. # test tuple:
  307. try:
  308. tuple(1,2)
  309. print('未能拦截错误, 在测试 tuple')
  310. exit(1)
  311. except:
  312. pass
  313. assert (1,2,3).__contains__(5) == False
  314. assert (1,2,2,3,3,3).count(3) == 3
  315. assert (1,2,2,3,3,3).count(0) == 0
  316. assert repr(True) == 'True'
  317. assert repr(False) == 'False'
  318. assert True & True == 1
  319. assert True | True == 1
  320. assert (True ^ True) == 0
  321. assert (True == True) == 1
  322. assert type(hash(bytes([0x41, 0x42, 0x43]))) is int
  323. # 未完全测试准确性-----------------------------------------------
  324. # test bytes.__repr__:
  325. assert type(repr(bytes([0x41, 0x42, 0x43]))) is str
  326. # /************ slice ************/
  327. # 未完全测试准确性-----------------------------------------------
  328. # 116: 953: _vm->bind_constructor<4>("slice", [](VM* vm, ArgsView args) {
  329. # #####: 954: return VAR(Slice(args[1], args[2], args[3]));
  330. # -: 955: });
  331. # test slice:
  332. assert type(slice(0.1, 0.2, 0.3)) is slice
  333. # 未完全测试准确性-----------------------------------------------
  334. # 116: 1529: bind_property(_t(tp_slice), "start", [](VM* vm, ArgsView args){
  335. # #####: 1530: return CAST(Slice&, args[0]).start;
  336. # -: 1531: });
  337. # 116: 1532: bind_property(_t(tp_slice), "stop", [](VM* vm, ArgsView args){
  338. # #####: 1533: return CAST(Slice&, args[0]).stop;
  339. # -: 1534: });
  340. # 116: 1535: bind_property(_t(tp_slice), "step", [](VM* vm, ArgsView args){
  341. # #####: 1536: return CAST(Slice&, args[0]).step;
  342. # -: 1537: });
  343. s = slice(1, 2, 3)
  344. assert type(s) is slice
  345. assert s.start == 1
  346. assert s.stop == 2
  347. assert s.step == 3
  348. assert slice.__dict__['start'].__signature__ == 'start'
  349. # 未完全测试准确性-----------------------------------------------
  350. # test slice.__repr__
  351. assert type(repr(slice(1,1,1))) is str
  352. # /************ mappingproxy ************/
  353. # 未完全测试准确性-----------------------------------------------
  354. # 116: 968: _vm->bind_method<0>("mappingproxy", "keys", [](VM* vm, ArgsView args) {
  355. # #####: 969: MappingProxy& self = _CAST(MappingProxy&, args[0]);
  356. # #####: 970: List keys;
  357. # #####: 971: for(StrName name : self.attr().keys()) keys.push_back(VAR(name.sv()));
  358. # #####: 972: return VAR(std::move(keys));
  359. # #####: 973: });
  360. # test mappingproxy.keys:
  361. class A():
  362. def __init__(self):
  363. self.a = 10
  364. def method(self):
  365. pass
  366. my_mappingproxy = A().__dict__
  367. assert type(my_mappingproxy.keys()) is list
  368. # 未完全测试准确性-----------------------------------------------
  369. # 116: 975: _vm->bind_method<0>("mappingproxy", "values", [](VM* vm, ArgsView args) {
  370. # #####: 976: MappingProxy& self = _CAST(MappingProxy&, args[0]);
  371. # #####: 977: List values;
  372. # #####: 978: for(auto& item : self.attr().items()) values.push_back(item.second);
  373. # #####: 979: return VAR(std::move(values));
  374. # #####: 980: });
  375. # test mappingproxy.values:
  376. class A():
  377. def __init__(self):
  378. self.a = 10
  379. def method(self):
  380. pass
  381. my_mappingproxy = A().__dict__
  382. assert type(my_mappingproxy.values()) is list
  383. class A():
  384. def __init__(self):
  385. self.a = 10
  386. def method(self):
  387. pass
  388. my_mappingproxy = A().__dict__
  389. assert type(len(my_mappingproxy)) is int
  390. class A():
  391. def __init__(self):
  392. self.a = 10
  393. def method(self):
  394. pass
  395. my_mappingproxy = A().__dict__
  396. try:
  397. hash(my_mappingproxy)
  398. print('未能拦截错误, 在测试 mappingproxy.__hash__')
  399. exit(1)
  400. except TypeError:
  401. pass
  402. a = hash(object()) # object is hashable
  403. a = hash(A()) # A is hashable
  404. class B:
  405. def __eq__(self, o): return True
  406. try:
  407. hash(B())
  408. print('未能拦截错误, 在测试 B.__hash__')
  409. exit(1)
  410. except TypeError:
  411. pass
  412. # 未完全测试准确性-----------------------------------------------
  413. # test mappingproxy.__repr__:
  414. class A():
  415. def __init__(self):
  416. self.a = 10
  417. def method(self):
  418. pass
  419. my_mappingproxy = A().__dict__
  420. assert type(repr(my_mappingproxy)) is str
  421. # /************ dict ************/
  422. # 未完全测试准确性-----------------------------------------------
  423. # 202: 1033: _vm->bind_method<-1>("dict", "__init__", [](VM* vm, ArgsView args){
  424. # 43: 1034: if(args.size() == 1+0) return vm->None;
  425. # 42: 1035: if(args.size() == 1+1){
  426. # 42: 1036: auto _lock = vm->heap.gc_scope_lock();
  427. # 42: 1037: Dict& self = _CAST(Dict&, args[0]);
  428. # 42: 1038: List& list = CAST(List&, args[1]);
  429. # 165: 1039: for(PyObject* item : list){
  430. # 123: 1040: Tuple& t = CAST(Tuple&, item);
  431. # 123: 1041: if(t.size() != 2){
  432. # #####: 1042: vm->ValueError("dict() takes an iterable of tuples (key, value)");
  433. # #####: 1043: return vm->None;
  434. # -: 1044: }
  435. # 123: 1045: self.set(t[0], t[1]);
  436. # 246: 1046: }
  437. # 42: 1047: return vm->None;
  438. # 42: 1048: }
  439. # #####: 1049: vm->TypeError("dict() takes at most 1 argument");
  440. # #####: 1050: return vm->None;
  441. # 43: 1051: });
  442. # test dict:
  443. assert type(dict([(1,2)])) is dict
  444. try:
  445. dict([(1, 2, 3)])
  446. print('未能拦截错误, 在测试 dict')
  447. exit(1)
  448. except:
  449. pass
  450. try:
  451. dict([(1, 2)], 1)
  452. print('未能拦截错误, 在测试 dict')
  453. exit(1)
  454. except:
  455. pass
  456. try:
  457. hash(dict([(1,2)]))
  458. print('未能拦截错误, 在测试 dict.__hash__')
  459. exit(1)
  460. except:
  461. pass
  462. # test dict.__iter__
  463. for k in {1:2, 2:3, 3:4}:
  464. assert k in [1,2,3]
  465. # 未完全测试准确性-----------------------------------------------
  466. # 166: 1098: _vm->bind_method<-1>("dict", "get", [](VM* vm, ArgsView args) {
  467. # 25: 1099: Dict& self = _CAST(Dict&, args[0]);
  468. # 25: 1100: if(args.size() == 1+1){
  469. # #####: 1101: PyObject* ret = self.try_get(args[1]);
  470. # #####: 1102: if(ret != nullptr) return ret;
  471. # #####: 1103: return vm->None;
  472. # 25: 1104: }else if(args.size() == 1+2){
  473. # 25: 1105: PyObject* ret = self.try_get(args[1]);
  474. # 25: 1106: if(ret != nullptr) return ret;
  475. # 19: 1107: return args[2];
  476. # -: 1108: }
  477. # #####: 1109: vm->TypeError("get() takes at most 2 arguments");
  478. # #####: 1110: return vm->None;
  479. # 25: 1111: });
  480. # test dict.get
  481. assert {1:2, 3:4}.get(1) == 2
  482. assert {1:2, 3:4}.get(2) is None
  483. assert {1:2, 3:4}.get(20, 100) == 100
  484. try:
  485. {1:2, 3:4}.get(1,1, 1)
  486. print('未能拦截错误, 在测试 dict.get')
  487. exit(1)
  488. except:
  489. pass
  490. # 未完全测试准确性-----------------------------------------------
  491. # test dict.__repr__
  492. assert type(repr({1:2, 3:4})) is str
  493. # /************ property ************/
  494. class A():
  495. def __init__(self):
  496. self._name = '123'
  497. @property
  498. def value(self):
  499. return 2
  500. def get_name(self):
  501. '''
  502. doc string 1
  503. '''
  504. return self._name
  505. def set_name(self, val):
  506. '''
  507. doc string 2
  508. '''
  509. self._name = val
  510. assert A().value == 2
  511. assert A.__dict__['value'].__signature__ == ''
  512. A.name = property(A.get_name, A.set_name, "name: str")
  513. assert A.__dict__['name'].__signature__ == 'name: str'
  514. try:
  515. property(A.get_name, A.set_name, 1)
  516. print('未能拦截错误, 在测试 property')
  517. exit(1)
  518. except:
  519. pass
  520. class Vector2:
  521. def __init__(self) -> None:
  522. self._x = 0
  523. @property
  524. def x(self):
  525. return self._x
  526. @x.setter
  527. def x(self, val):
  528. self._x = val
  529. v = Vector2()
  530. assert v.x == 0
  531. v.x = 10
  532. assert v.x == 10
  533. # /************ module timeit ************/
  534. import timeit
  535. def aaa():
  536. for i in range(10):
  537. for j in range(10):
  538. pass
  539. assert type(timeit.timeit(aaa, 2)) is float
  540. # function.__doc__
  541. def aaa():
  542. '12345'
  543. pass
  544. assert type(aaa.__doc__) is str
  545. # function.__signature__
  546. def aaa():
  547. pass
  548. assert type(aaa.__signature__) is str
  549. # /************ module time ************/
  550. import time
  551. # 未完全测试准确性-----------------------------------------------
  552. # 116: 1267: vm->bind_func<1>(mod, "sleep", [](VM* vm, ArgsView args) {
  553. # #####: 1268: f64 seconds = CAST_F(args[0]);
  554. # #####: 1269: auto begin = std::chrono::system_clock::now();
  555. # #####: 1270: while(true){
  556. # #####: 1271: auto now = std::chrono::system_clock::now();
  557. # #####: 1272: f64 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - begin).count() / 1000.0;
  558. # #####: 1273: if(elapsed >= seconds) break;
  559. # #####: 1274: }
  560. # #####: 1275: return vm->None;
  561. # #####: 1276: });
  562. # test time.time
  563. assert type(time.time()) is float
  564. local_t = time.localtime()
  565. assert type(local_t.tm_year) is int
  566. assert type(local_t.tm_mon) is int
  567. assert type(local_t.tm_mday) is int
  568. assert type(local_t.tm_hour) is int
  569. assert type(local_t.tm_min) is int
  570. assert type(local_t.tm_sec) is int
  571. assert type(local_t.tm_wday) is int
  572. assert type(local_t.tm_yday) is int
  573. assert type(local_t.tm_isdst) is int
  574. # 未完全测试准确性-----------------------------------------------
  575. # 116: 1267: vm->bind_func<1>(mod, "sleep", [](VM* vm, ArgsView args) {
  576. # #####: 1268: f64 seconds = CAST_F(args[0]);
  577. # #####: 1269: auto begin = std::chrono::system_clock::now();
  578. # #####: 1270: while(true){
  579. # #####: 1271: auto now = std::chrono::system_clock::now();
  580. # #####: 1272: f64 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - begin).count() / 1000.0;
  581. # #####: 1273: if(elapsed >= seconds) break;
  582. # #####: 1274: }
  583. # #####: 1275: return vm->None;
  584. # #####: 1276: });
  585. # test time.sleep
  586. time.sleep(0.1)
  587. # 未完全测试准确性-----------------------------------------------
  588. # 116: 1278: vm->bind_func<0>(mod, "localtime", [](VM* vm, ArgsView args) {
  589. # #####: 1279: auto now = std::chrono::system_clock::now();
  590. # #####: 1280: std::time_t t = std::chrono::system_clock::to_time_t(now);
  591. # #####: 1281: std::tm* tm = std::localtime(&t);
  592. # #####: 1282: Dict d(vm);
  593. # #####: 1283: d.set(VAR("tm_year"), VAR(tm->tm_year + 1900));
  594. # #####: 1284: d.set(VAR("tm_mon"), VAR(tm->tm_mon + 1));
  595. # #####: 1285: d.set(VAR("tm_mday"), VAR(tm->tm_mday));
  596. # #####: 1286: d.set(VAR("tm_hour"), VAR(tm->tm_hour));
  597. # #####: 1287: d.set(VAR("tm_min"), VAR(tm->tm_min));
  598. # #####: 1288: d.set(VAR("tm_sec"), VAR(tm->tm_sec + 1));
  599. # #####: 1289: d.set(VAR("tm_wday"), VAR((tm->tm_wday + 6) % 7));
  600. # #####: 1290: d.set(VAR("tm_yday"), VAR(tm->tm_yday + 1));
  601. # #####: 1291: d.set(VAR("tm_isdst"), VAR(tm->tm_isdst));
  602. # #####: 1292: return VAR(std::move(d));
  603. # #####: 1293: });
  604. # 58: 1294:}
  605. # test time.localtime
  606. assert type(time.localtime()) is time.struct_time
  607. # test min/max
  608. assert min(1, 2) == 1
  609. assert min(1, 2, 3) == 1
  610. assert min([1, 2]) == 1
  611. assert min([1, 2], key=lambda x: -x) == 2
  612. assert max(1, 2) == 2
  613. assert max(1, 2, 3) == 3
  614. assert max([1, 2]) == 2
  615. assert max([1, 2, 3], key=lambda x: -x) == 1
  616. assert min([
  617. (1, 2),
  618. (1, 3),
  619. (1, 4),
  620. ]) == (1, 2)
  621. assert min(1, 2) == 1
  622. assert max(1, 2) == 2
  623. # test callable
  624. assert callable(lambda: 1) is True # function
  625. assert callable(1) is False # int
  626. assert callable(object) is True # type
  627. assert callable(object()) is False
  628. assert callable([].append) is True # bound method
  629. assert callable([].__getitem__) is True # bound method
  630. class A:
  631. def __init__(self):
  632. pass
  633. def __call__(self):
  634. pass
  635. assert callable(A) is True # type
  636. assert callable(A()) is True # instance with __call__
  637. assert callable(A.__call__) is True # bound method
  638. assert callable(A.__init__) is True # bound method
  639. assert callable(print) is True # builtin function
  640. assert callable(isinstance) is True # builtin function
  641. assert id(0) is None
  642. assert id(2**62) is not None
  643. # test issubclass
  644. assert issubclass(int, int) is True
  645. assert issubclass(int, object) is True
  646. assert issubclass(object, int) is False
  647. assert issubclass(object, object) is True
  648. assert issubclass(int, type) is False
  649. assert issubclass(type, type) is True
  650. assert issubclass(float, int) is False