/IronPython_1_0/Src/Tests/compat/sbs_simple_ops.py

# · Python · 223 lines · 183 code · 26 blank · 14 comment · 21 complexity · 609d03b04b196864576794977f13c7cf MD5 · raw file

  1. #####################################################################################
  2. #
  3. # Copyright (c) Microsoft Corporation. All rights reserved.
  4. #
  5. # This source code is subject to terms and conditions of the Shared Source License
  6. # for IronPython. A copy of the license can be found in the License.html file
  7. # at the root of this distribution. If you can not locate the Shared Source License
  8. # for IronPython, please send an email to ironpy@microsoft.com.
  9. # By using this source code in any fashion, you are agreeing to be bound by
  10. # the terms of the Shared Source License for IronPython.
  11. #
  12. # You must not remove this notice, or any other, from this software.
  13. #
  14. ######################################################################################
  15. from common import *
  16. import testdata
  17. if not is_cli:
  18. import warnings
  19. warnings.filterwarnings("ignore",
  20. r'complex divmod\(\), // and % are deprecated',
  21. DeprecationWarning,
  22. r'sbs_simple_ops$'
  23. )
  24. class oldstyle:
  25. def __init__(self, value): self.value = value
  26. def __repr__(self): return "oldstyle(%s)" % self.value
  27. def __add__(self, other): return self.value + other
  28. def __sub__(self, other): return self.value - other
  29. def __mul__(self, other): return self.value * other
  30. def __div__(self, other): return self.value / other
  31. def __floordiv__(self, other): return self.value // other
  32. def __mod__(self, other): return self.value % other
  33. def __divmod__(self, other): return divmod(self.value, other)
  34. def __pow__(self, other): return self.value ** other
  35. def __lshift__(self, other): return self.value << other
  36. def __rshift__(self, other): return self.value >> other
  37. def __and__(self, other): return self.value & other
  38. def __xor__(self, other): return self.value ^ other
  39. def __or__(self, other): return self.value | other
  40. class oldstyle_reflect:
  41. def __init__(self, value): self.value = value
  42. def __repr__(self): return "oldstyle_reflect(%s)" % self.value
  43. def __radd__(self, other): return other + self.value
  44. def __rsub__(self, other): return other - self.value
  45. def __rmul__(self, other): return other * self.value
  46. def __rdiv__(self, other): return other / self.value
  47. def __rfloordiv__(self, other): return other // self.value
  48. def __rmod__(self, other): return other % self.value
  49. def __rdivmod__(self, other): return divmod(other, self.value)
  50. def __rpow__(self, other): return other ** self.value
  51. def __rlshift__(self, other): return other << self.value
  52. def __rrshift__(self, other): return other >> self.value
  53. def __rand__(self, other): return self.value & other
  54. def __rxor__(self, other): return self.value ^ other
  55. def __ror__(self, other): return self.value | other
  56. class oldstyle_inplace:
  57. def __init__(self, value): self.value = value
  58. def __repr__(self): return "oldstyle_inplace(%s)" % self.value
  59. def __iadd__(self, other): return self.value + other
  60. def __isub__(self, other): return self.value - other
  61. def __imul__(self, other): return self.value * other
  62. def __idiv__(self, other): return self.value / other
  63. def __ifloordiv__(self, other): return self.value // other
  64. def __imod__(self, other): return self.value % other
  65. def __idivmod__(self, other): return divmod(self.value, other)
  66. def __ipow__(self, other): return self.value ** other
  67. def __ilshift__(self, other): return self.value << other
  68. def __irshift__(self, other): return self.value >> other
  69. def __iand__(self, other): return self.value & other
  70. def __ixor__(self, other): return self.value ^ other
  71. def __ior__(self, other): return self.value | other
  72. class oldstyle_notdefined:
  73. def __init__(self, value): self.value = value
  74. def __repr__(self): return "oldstyle_notdefined(%s)" % self.value
  75. class newstyle(object):
  76. def __init__(self, value): self.value = value
  77. def __repr__(self): return "newstyle(%s)" % self.value
  78. def __add__(self, other): return self.value + other
  79. def __sub__(self, other): return self.value - other
  80. def __mul__(self, other): return self.value * other
  81. def __div__(self, other): return self.value / other
  82. def __floordiv__(self, other): return self.value // other
  83. def __mod__(self, other): return self.value % other
  84. def __divmod__(self, other): return divmod(self.value, other)
  85. def __pow__(self, other): return self.value ** other
  86. def __lshift__(self, other): return self.value << other
  87. def __rshift__(self, other): return self.value >> other
  88. def __and__(self, other): return self.value & other
  89. def __xor__(self, other): return self.value ^ other
  90. def __or__(self, other): return self.value | other
  91. class newstyle_reflect(object):
  92. def __init__(self, value): self.value = value
  93. def __repr__(self): return "newstyle_reflect(%s)" % self.value
  94. def __radd__(self, other): return other + self.value
  95. def __rsub__(self, other): return other - self.value
  96. def __rmul__(self, other): return other * self.value
  97. def __rdiv__(self, other): return other / self.value
  98. def __rfloordiv__(self, other): return other // self.value
  99. def __rmod__(self, other): return other % self.value
  100. def __rdivmod__(self, other): return divmod(other, self.value)
  101. def __rpow__(self, other): return other ** self.value
  102. def __rlshift__(self, other): return other << self.value
  103. def __rrshift__(self, other): return other >> self.value
  104. def __rand__(self, other): return self.value & other
  105. def __rxor__(self, other): return self.value ^ other
  106. def __ror__(self, other): return self.value | other
  107. class newstyle_inplace(object):
  108. def __init__(self, value): self.value = value
  109. def __repr__(self): return "newstyle_inplace(%s)" % self.value
  110. def __iadd__(self, other): return self.value + other
  111. def __isub__(self, other): return self.value - other
  112. def __imul__(self, other): return self.value * other
  113. def __idiv__(self, other): return self.value / other
  114. def __ifloordiv__(self, other): return self.value // other
  115. def __imod__(self, other): return self.value % other
  116. def __idivmod__(self, other): return divmod(self.value, other)
  117. def __ipow__(self, other): return self.value ** other
  118. def __ilshift__(self, other): return self.value << other
  119. def __irshift__(self, other): return self.value >> other
  120. def __iand__(self, other): return self.value & other
  121. def __ixor__(self, other): return self.value ^ other
  122. def __ior__(self, other): return self.value | other
  123. class newstyle_notdefined(object):
  124. def __init__(self, value): self.value = value
  125. def __repr__(self): return "newstyle_notdefined(%s)" % self.value
  126. import sys
  127. class common(object):
  128. def normal(self, leftc, rightc):
  129. for a in leftc:
  130. for b in rightc:
  131. for op in ["+", "-", "*", "/", "//", "%", "**", "<<", ">>", "&", "^", "|"]:
  132. try:
  133. printwith("case", a, op, b, type(a), type(b))
  134. printwithtype(eval("a %s b" % op))
  135. except:
  136. printwith("same", sys.exc_type)
  137. def clone_list(self, l):
  138. l2 = []
  139. for x in l:
  140. if x is newstyle_inplace:
  141. l2.append(newstyle_inplace(x.value))
  142. elif x is oldstyle_inplace:
  143. l2.append(oldstyle_inplace(x.value))
  144. else :
  145. l2.append(x)
  146. return l2
  147. def inplace(self, leftc, rightc):
  148. rc = self.clone_list(rightc)
  149. for b in rc:
  150. for op in ["+", "-", "*", "//", "%", "**", "<<", ">>", "&", "^", "|"]:
  151. lc = self.clone_list(leftc)
  152. for a in lc:
  153. try:
  154. line = "a %s= b" % op
  155. printwith("case", "%s %s= %s" % (a, op, b), type(a), type(b))
  156. exec line
  157. printwithtype(a)
  158. except:
  159. printwith("same", sys.exc_type)
  160. class ops_simple(common):
  161. def __init__(self):
  162. self.collection = testdata.merge_lists(
  163. [None],
  164. testdata.list_bool,
  165. testdata.list_int,
  166. testdata.list_float,
  167. testdata.list_long[:-1], # the last number is very long
  168. testdata.list_complex,
  169. testdata.list_myint,
  170. testdata.list_myfloat,
  171. testdata.list_mylong,
  172. testdata.list_mycomplex,
  173. testdata.get_Int64_Byte(),
  174. )
  175. self.collection_oldstyle = [oldstyle(x) for x in self.collection]
  176. self.collection_oldstyle_reflect = [oldstyle_reflect(x) for x in self.collection]
  177. self.collection_oldstyle_notdefined = [oldstyle_notdefined(x) for x in self.collection]
  178. self.collection_newstyle = [newstyle(x) for x in self.collection]
  179. self.collection_newstyle_reflect = [newstyle_reflect(x) for x in self.collection]
  180. self.collection_newstyle_notdefined = [newstyle_notdefined(x) for x in self.collection]
  181. self.collection_oldstyle_inplace = [oldstyle_inplace(x) for x in self.collection]
  182. self.collection_newstyle_inplace = [newstyle_inplace(x) for x in self.collection]
  183. def test_normal(self): super(ops_simple, self).normal(self.collection, self.collection)
  184. def test_normal_oc_left(self): super(ops_simple, self).normal(self.collection_oldstyle, self.collection)
  185. def test_normal_oc_right(self): super(ops_simple, self).normal(self.collection, self.collection_oldstyle)
  186. def test_normal_nc_left(self): super(ops_simple, self).normal(self.collection_newstyle, self.collection)
  187. def test_normal_nc_right(self): super(ops_simple, self).normal(self.collection, self.collection_newstyle)
  188. def test_reflect_oc_right(self): super(ops_simple, self).normal(self.collection, self.collection_oldstyle_reflect)
  189. def test_reflect_nc_right(self): super(ops_simple, self).normal(self.collection, self.collection_newstyle_reflect)
  190. def test_oc_notdefined(self): super(ops_simple, self).normal(self.collection_oldstyle_notdefined, self.collection)
  191. def test_nc_notdefined(self): super(ops_simple, self).normal(self.collection_newstyle_notdefined, self.collection)
  192. def test_oc_notdefined_oc_reflect(self): super(ops_simple, self).normal(self.collection_oldstyle_notdefined, self.collection_oldstyle_reflect)
  193. def test_nc_notdefined_nc_reflect(self): super(ops_simple, self).normal(self.collection_newstyle_notdefined, self.collection_newstyle_reflect)
  194. def test_inplace(self): super(ops_simple, self).inplace(self.collection, self.collection)
  195. def test_inplace_ol(self): super(ops_simple, self).inplace(self.collection_oldstyle_inplace, self.collection)
  196. def test_inplace_nl(self): super(ops_simple, self).inplace(self.collection_newstyle_inplace, self.collection)
  197. runtests(ops_simple)