77_builtin_func_2.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # 未完全测试准确性-----------------------------------------------
  2. # 116: 1529: bind_property(_t(tp_slice), "start", [](VM* vm, ArgsView args){
  3. # #####: 1530: return CAST(Slice&, args[0]).start;
  4. # -: 1531: });
  5. # 116: 1532: bind_property(_t(tp_slice), "stop", [](VM* vm, ArgsView args){
  6. # #####: 1533: return CAST(Slice&, args[0]).stop;
  7. # -: 1534: });
  8. # 116: 1535: bind_property(_t(tp_slice), "step", [](VM* vm, ArgsView args){
  9. # #####: 1536: return CAST(Slice&, args[0]).step;
  10. # -: 1537: });
  11. s = slice(1, 2, 3)
  12. assert type(s) is slice
  13. assert s.start == 1
  14. assert s.stop == 2
  15. assert s.step == 3
  16. # 未完全测试准确性-----------------------------------------------
  17. # test slice.__repr__
  18. assert type(repr(slice(1,1,1))) is str
  19. class A():
  20. def __init__(self):
  21. self.a = 10
  22. def method(self):
  23. pass
  24. my_namedict = A().__dict__
  25. try:
  26. hash(my_namedict)
  27. print('未能拦截错误, 在测试 namedict.__hash__')
  28. exit(1)
  29. except TypeError:
  30. pass
  31. a = hash(object()) # object is hashable
  32. a = hash(A()) # A is hashable
  33. class B:
  34. def __eq__(self, o): return True
  35. def __ne__(self, o): return False
  36. try:
  37. hash(B())
  38. print('未能拦截错误, 在测试 B.__hash__')
  39. exit(1)
  40. except TypeError:
  41. pass
  42. # 未完全测试准确性-----------------------------------------------
  43. # test namedict.__repr__:
  44. class A():
  45. def __init__(self):
  46. self.a = 10
  47. def method(self):
  48. pass
  49. my_namedict = A().__dict__
  50. assert type(repr(my_namedict)) is str
  51. # /************ dict ************/
  52. # 未完全测试准确性-----------------------------------------------
  53. # test dict:
  54. assert type(dict([(1,2)])) is dict
  55. try:
  56. dict([(1, 2, 3)])
  57. print('未能拦截错误, 在测试 dict')
  58. exit(1)
  59. except ValueError:
  60. pass
  61. try:
  62. dict([(1, 2)], 1)
  63. print('未能拦截错误, 在测试 dict')
  64. exit(1)
  65. except TypeError:
  66. pass
  67. try:
  68. hash(dict([(1,2)]))
  69. print('未能拦截错误, 在测试 dict.__hash__')
  70. exit(1)
  71. except TypeError:
  72. pass
  73. # test dict.__iter__
  74. for k in {1:2, 2:3, 3:4}.keys():
  75. assert k in [1,2,3]
  76. # 未完全测试准确性-----------------------------------------------
  77. # test dict.get
  78. assert {1:2, 3:4}.get(1) == 2
  79. assert {1:2, 3:4}.get(2) is None
  80. assert {1:2, 3:4}.get(20, 100) == 100
  81. try:
  82. {1:2, 3:4}.get(1,1, 1)
  83. print('未能拦截错误, 在测试 dict.get')
  84. exit(1)
  85. except TypeError:
  86. pass
  87. # 未完全测试准确性-----------------------------------------------
  88. # test dict.__repr__
  89. assert type(repr({1:2, 3:4})) is str
  90. # /************ property ************/
  91. class A():
  92. def __init__(self):
  93. self._name = '123'
  94. @property
  95. def value(self):
  96. return 2
  97. def get_name(self):
  98. '''
  99. doc string 1
  100. '''
  101. return self._name
  102. def set_name(self, val):
  103. '''
  104. doc string 2
  105. '''
  106. self._name = val
  107. assert A().value == 2
  108. A.name = property(A.get_name, A.set_name)
  109. class Vector2:
  110. def __init__(self) -> None:
  111. self._x = 0
  112. @property
  113. def x(self):
  114. return self._x
  115. @x.setter
  116. def x(self, val):
  117. self._x = val
  118. v = Vector2()
  119. assert v.x == 0
  120. v.x = 10
  121. assert v.x == 10
  122. # function.__doc__
  123. def aaa():
  124. '12345'
  125. pass
  126. assert aaa.__doc__ == '12345'
  127. # test callable
  128. assert callable(lambda: 1) is True # function
  129. assert callable(1) is False # int
  130. assert callable(object) is True # type
  131. assert callable(object()) is False
  132. assert callable([].append) is True # bound method
  133. assert callable([].__getitem__) is True # bound method
  134. class A:
  135. def __init__(self):
  136. pass
  137. def __call__(self):
  138. pass
  139. @staticmethod
  140. def staticmethod():
  141. pass
  142. @classmethod
  143. def classmethod(cls):
  144. pass
  145. assert callable(A) is True # type
  146. assert callable(A()) is True # instance with __call__
  147. assert callable(A.__call__) is True # bound method
  148. assert callable(A.__init__) is True # bound method
  149. assert callable(print) is True # builtin function
  150. assert callable(isinstance) is True # builtin function
  151. assert callable(A.staticmethod) is True # staticmethod
  152. assert callable(A.classmethod) is True # classmethod
  153. assert id(0) is None
  154. assert id(2**62) is None
  155. # test issubclass
  156. assert issubclass(int, int) is True
  157. assert issubclass(int, object) is True
  158. assert issubclass(object, int) is False
  159. assert issubclass(object, object) is True
  160. assert issubclass(int, type) is False
  161. assert issubclass(type, type) is True
  162. assert issubclass(float, int) is False
  163. def f(a, b):
  164. c = a
  165. del a
  166. return sum([b, c])
  167. assert f(1, 2) == 3
  168. # /************ module time ************/
  169. import time
  170. # test time.time
  171. assert type(time.time()) is float
  172. local_t = time.localtime()
  173. assert type(local_t.tm_year) is int
  174. assert type(local_t.tm_mon) is int
  175. assert type(local_t.tm_mday) is int
  176. assert type(local_t.tm_hour) is int
  177. assert type(local_t.tm_min) is int
  178. assert type(local_t.tm_sec) is int
  179. assert type(local_t.tm_wday) is int
  180. assert type(local_t.tm_yday) is int
  181. assert type(local_t.tm_isdst) is int
  182. # test time.sleep
  183. time.sleep(0.1)
  184. # test time.localtime
  185. assert type(time.localtime()) is time.struct_time
  186. # test min/max
  187. assert min(1, 2) == 1
  188. assert min(1, 2, 3) == 1
  189. assert min([1, 2]) == 1
  190. assert min([1, 2], key=lambda x: -x) == 2
  191. assert max(1, 2) == 2
  192. assert max(1, 2, 3) == 3
  193. assert max([1, 2]) == 2
  194. assert max([1, 2, 3], key=lambda x: -x) == 1
  195. assert min([
  196. (3, 1),
  197. (1, 2),
  198. (1, 3),
  199. (1, 4),
  200. ]) == (1, 2)
  201. assert min(1, 2) == 1
  202. assert max(1, 2) == 2
  203. def fn(): pass
  204. assert repr(fn).startswith('<function fn at')
  205. assert repr(round) == '<nativefunc object>'