77_builtin_func_2.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. assert callable(A) is True # type
  140. assert callable(A()) is True # instance with __call__
  141. assert callable(A.__call__) is True # bound method
  142. assert callable(A.__init__) is True # bound method
  143. assert callable(print) is True # builtin function
  144. assert callable(isinstance) is True # builtin function
  145. assert id(0) is None
  146. assert id(2**62) is None
  147. # test issubclass
  148. assert issubclass(int, int) is True
  149. assert issubclass(int, object) is True
  150. assert issubclass(object, int) is False
  151. assert issubclass(object, object) is True
  152. assert issubclass(int, type) is False
  153. assert issubclass(type, type) is True
  154. assert issubclass(float, int) is False
  155. def f(a, b):
  156. c = a
  157. del a
  158. return sum([b, c])
  159. assert f(1, 2) == 3
  160. # /************ module time ************/
  161. import time
  162. # test time.time
  163. assert type(time.time()) is float
  164. local_t = time.localtime()
  165. assert type(local_t.tm_year) is int
  166. assert type(local_t.tm_mon) is int
  167. assert type(local_t.tm_mday) is int
  168. assert type(local_t.tm_hour) is int
  169. assert type(local_t.tm_min) is int
  170. assert type(local_t.tm_sec) is int
  171. assert type(local_t.tm_wday) is int
  172. assert type(local_t.tm_yday) is int
  173. assert type(local_t.tm_isdst) is int
  174. # test time.sleep
  175. time.sleep(0.1)
  176. # test time.localtime
  177. assert type(time.localtime()) is time.struct_time
  178. # test min/max
  179. assert min(1, 2) == 1
  180. assert min(1, 2, 3) == 1
  181. assert min([1, 2]) == 1
  182. assert min([1, 2], key=lambda x: -x) == 2
  183. assert max(1, 2) == 2
  184. assert max(1, 2, 3) == 3
  185. assert max([1, 2]) == 2
  186. assert max([1, 2, 3], key=lambda x: -x) == 1
  187. assert min([
  188. (3, 1),
  189. (1, 2),
  190. (1, 3),
  191. (1, 4),
  192. ]) == (1, 2)
  193. assert min(1, 2) == 1
  194. assert max(1, 2) == 2