PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_1_0/Src/Tests/test_math.py

#
Python | 284 lines | 225 code | 41 blank | 18 comment | 15 complexity | df0e509608000c45feaaf36e74a122e2 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  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 lib.assert_util import *
  16. if is_cli:
  17. from System import Int64, Byte, Int16
  18. Assert("pypypypypy" == 5 * "py")
  19. Assert("pypypypypy" == "py" * 5)
  20. Assert("pypypypy" == 2 * "py" * 2)
  21. Assert(['py', 'py', 'py'] == ['py'] * 3)
  22. Assert(['py', 'py', 'py'] == 3 * ['py'])
  23. Assert(['py', 'py', 'py', 'py', 'py', 'py', 'py', 'py', 'py'] == 3 * ['py'] * 3)
  24. Assert(3782452410 > 0)
  25. # Complex tests
  26. Assert((2+4j)/(1+1j) == (3+1j))
  27. Assert((2+10j)/4.0 == (0.5+2.5j))
  28. AreEqual(1j ** 2, (-1.0+0j))
  29. AreEqual(pow(1j, 2), (-1.0+0j))
  30. AreEqual(1+0j, 1)
  31. AreEqual(1+0j, 1.0)
  32. AreEqual(1+0j, 1L)
  33. AreEqual((1+1j)/1L, (1+1j))
  34. AreEqual((1j) + 1L, (1+1j))
  35. if is_cli: AreEqual((1j) + Int64(), 1j)
  36. AssertError(TypeError, (lambda:(1+1j)+[]))
  37. AreEqual( (12j//5j), (2+0j))
  38. AreEqual( (12.0+0j) // (5.0+0j), (2+0j) )
  39. AreEqual( (12+0j) // (0+3j), 0j)
  40. AreEqual( (0+12j) // (3+0j), 0j)
  41. AssertError(TypeError, (lambda:2j // "astring"))
  42. AssertError(ZeroDivisionError, (lambda:3.0 // (0j)))
  43. AreEqual ( 3.0 // (2j), 0j)
  44. AreEqual( 12 // (3j), 0j)
  45. AreEqual( 25 % (5+3j), (10-9j))
  46. AreEqual((12+3j)/3L, (4+1j))
  47. AreEqual(3j - 5L, -5+3j)
  48. if is_cli: AreEqual(3j - Int64(), 3j)
  49. AssertError(TypeError, (lambda:3j-[]))
  50. if is_cli: AreEqual(pow(5j, Int64()), (1+0j))
  51. AreEqual(pow(5j, 0L), (1+0j))
  52. AssertError(TypeError, (lambda:pow(5j, [])))
  53. if is_cli: AreEqual(5j * Int64(), 0)
  54. AreEqual(5j * 3L, 15j)
  55. AssertError(TypeError, (lambda:(5j*[])))
  56. AreEqual(pow(2, 1000000000, 2147483647 + 10), 511677409)
  57. AreEqual(pow(2, 2147483647*2147483647, 2147483647*2147483647), 297528129805479806)
  58. for i in range(1, 100, 7):
  59. l = long(i)
  60. Assert(type(i) == int)
  61. Assert(type(l) == long)
  62. for exp in [17, 2863, 234857, 1435435, 234636554, 2147483647]:
  63. lexp = long(exp)
  64. Assert(type(exp) == int)
  65. Assert(type(lexp) == long)
  66. for mod in [7, 5293, 23745, 232474276, 534634665, 2147483647]:
  67. lmod = long(mod)
  68. Assert(type(mod) == int)
  69. Assert(type(lmod) == long)
  70. ir = pow(i, exp, mod)
  71. lr = pow(l, lexp, lmod)
  72. AreEqual(ir, lr)
  73. ir = pow(i, 0, mod)
  74. lr = pow(l, 0L, lmod)
  75. AreEqual(ir, 1)
  76. AreEqual(lr, 1)
  77. AssertError(ValueError, pow, i, exp, 0)
  78. AssertError(ValueError, pow, l, lexp, 0L)
  79. class powtest:
  80. def __pow__(self, exp, mod = None):
  81. return ("powtest.__pow__", exp, mod)
  82. def __rpow__(self, exp):
  83. return ("powtest.__rpow__", exp)
  84. AreEqual(pow(powtest(), 1, 2), ("powtest.__pow__", 1, 2))
  85. AreEqual(pow(powtest(), 3), ("powtest.__pow__", 3, None))
  86. AreEqual(powtest() ** 4, ("powtest.__pow__", 4, None))
  87. AreEqual(5 ** powtest(), ("powtest.__rpow__", 5))
  88. AreEqual(pow(7, powtest()), ("powtest.__rpow__", 7))
  89. AssertError(TypeError, pow, 1, powtest(), 7)
  90. # Extensible Float tests
  91. class XFloat(float): pass
  92. AreEqual(XFloat(3.14), 3.14)
  93. Assert(XFloat(3.14) < 4.0)
  94. Assert(XFloat(3.14) > 3.0)
  95. Assert(XFloat(3.14) < XFloat(4.0))
  96. Assert(XFloat(3.14) > XFloat(3.0))
  97. Assert(0xabcdef01 + (0xabcdef01<<32)+(0xabcdef01<<64) == 0xabcdef01abcdef01abcdef01)
  98. Assert(round(-5.5) == (-6.0))
  99. Assert(round(-5.0) == (-5.0))
  100. Assert(round(-4.5) == (-5.0))
  101. Assert(round(-4.0) == (-4.0))
  102. Assert(round(-3.5) == (-4.0))
  103. Assert(round(-3.0) == (-3.0))
  104. Assert(round(-2.5) == (-3.0))
  105. Assert(round(-2.0) == (-2.0))
  106. Assert(round(-1.5) == (-2.0))
  107. Assert(round(-1.0) == (-1.0))
  108. Assert(round(-0.5) == (-1.0))
  109. Assert(round(0.0) == (0.0))
  110. Assert(round(0.5) == (1.0))
  111. Assert(round(1.0) == (1.0))
  112. Assert(round(1.5) == (2.0))
  113. Assert(round(2.0) == (2.0))
  114. Assert(round(2.5) == (3.0))
  115. Assert(round(3.0) == (3.0))
  116. Assert(round(3.5) == (4.0))
  117. Assert(round(4.0) == (4.0))
  118. Assert(round(4.5) == (5.0))
  119. Assert(round(5.0) == (5.0))
  120. x = ('a', 'b', 'c')
  121. y = x
  122. y *= 3
  123. z = x
  124. z += x
  125. z += x
  126. Assert(y == z)
  127. Assert(1 << 32 == 4294967296L)
  128. Assert(2 << 32 == (1 << 32) << 1)
  129. Assert(((1 << 16) << 16) << 16 == 1 << 48)
  130. Assert(((1 << 16) << 16) << 16 == 281474976710656L)
  131. for i in [1, 10, 42, 1000000000, 34141235135135135, 13523525234523452345235235234523, 100000000000000000000000000000000000000]:
  132. Assert(~i == -i - 1)
  133. Assert(7 ** 5 == 7*7*7*7*7)
  134. Assert(7L ** 5L == 7L*7L*7L*7L*7L)
  135. Assert(7 ** 5L == 7*7*7*7*7)
  136. Assert(7L ** 5 == 7L*7L*7L*7L*7L)
  137. Assert(1 ** 735293857239475 == 1)
  138. Assert(0 ** 735293857239475 == 0)
  139. # cpython tries to compute this, takes a long time to finish
  140. if is_cli:
  141. AssertError(ValueError, (lambda: 10 ** 735293857239475))
  142. Assert(2 ** 3.0 == 8.0)
  143. Assert(2.0 ** 3 == 8.0)
  144. Assert(4 ** 0.5 == 2.0)
  145. nums = [3, 2.3, (2+1j), (2-1j), 1j, (-1j), 1]
  146. for x in nums:
  147. for y in nums:
  148. z = x ** y
  149. AreEqual(7.1//2.1, 3.0)
  150. AreEqual(divmod(5.0, 2), (2.0,1.0))
  151. AreEqual(divmod(5,2), (2,1))
  152. AreEqual(divmod(-5,2), (-3,1))
  153. AreEqual(True | False, True)
  154. AreEqual(True | 4, 5)
  155. AreEqual(True & 3, 1)
  156. AreEqual(True + 3, 4)
  157. AreEqual(True - 10, -9)
  158. AreEqual(True * 8, 8)
  159. AreEqual(True / 3, 0)
  160. AreEqual(True ** 5, 1)
  161. AreEqual(True % 2, 1)
  162. AreEqual(True << 4, 1 << 4)
  163. AreEqual(True >> 2, 0)
  164. AreEqual(True ^ 3, 2)
  165. if is_cli:
  166. a = Byte()
  167. AreEqual(type(a), Byte)
  168. AreEqual(a, 0)
  169. b = a + Byte(1)
  170. AreEqual(b, 1)
  171. AreEqual(type(b), Byte)
  172. bprime = b * Byte(10)
  173. AreEqual(type(bprime), Byte)
  174. d = a + Byte(255)
  175. AreEqual(type(d), Byte)
  176. c = b + Byte(255)
  177. AreEqual(c, 256)
  178. AreEqual(type(c), Int16)
  179. Assert(not (20 == False))
  180. Assert(not (20 == None))
  181. Assert(not (False == 20))
  182. Assert(not (None == 20))
  183. Assert(not (20 == 'a'))
  184. Assert(not ('a' == 20))
  185. Assert(not (2.5 == None))
  186. Assert(not (20 == (2,3)))
  187. AreEqual(long(1234793454934), 1234793454934)
  188. AreEqual(4 ** -2, 0.0625)
  189. AreEqual(4L ** -2, 0.0625)
  190. AssertError(ZeroDivisionError, (lambda: (0 ** -1)))
  191. AssertError(ZeroDivisionError, (lambda: (0.0 ** -1)))
  192. AssertError(ZeroDivisionError, (lambda: (0 ** -1.0)))
  193. AssertError(ZeroDivisionError, (lambda: (0.0 ** -1.0)))
  194. AssertError(ZeroDivisionError, (lambda: (False ** -1)))
  195. AssertError(ZeroDivisionError, (lambda: (0L ** -(2 ** 65))))
  196. AssertError(ZeroDivisionError, (lambda: (0j ** -1)))
  197. AssertError(ZeroDivisionError, (lambda: (0j ** 1j)))
  198. def test_extensible_math():
  199. operators = ['__add__', '__sub__', '__pow__', '__mul__', '__div__', '__floordiv__', '__truediv__', '__mod__']
  200. opSymbol = ['+', '-', '**', '*', '/', '//', '/', '%']
  201. types = []
  202. for baseType in [(int, (100,2)), (long, (100L, 2L)), (float, (100.0, 2.0))]:
  203. # (complex, (100+0j, 2+0j)) - cpython doesn't call reverse ops for complex ?
  204. class prototype(baseType[0]):
  205. for op in operators:
  206. exec '''def %s(self, other):
  207. global opCalled
  208. opCalled.append('%s')
  209. return super(self.__class__, self).%s(other)
  210. def %s(self, other):
  211. global opCalled
  212. opCalled.append('%s')
  213. return super(self.__class__, self).%s(other)''' % (op, op, op, op[:2] + 'r' + op[2:], op[:2] + 'r' + op[2:], op[:2] + 'r' + op[2:])
  214. types.append( (prototype, baseType[1]) )
  215. global opCalled
  216. opCalled = []
  217. for op in opSymbol:
  218. for typeInfo in types:
  219. ex = typeInfo[0](typeInfo[1][0])
  220. ey = typeInfo[0](typeInfo[1][1])
  221. nx = typeInfo[0].__bases__[0](typeInfo[1][0])
  222. ny = typeInfo[0].__bases__[0](typeInfo[1][1])
  223. #print 'nx %s ey' % op, type(nx), type(ey)
  224. res1 = eval('nx %s ey' % op)
  225. res2 = eval('nx %s ny' % op)
  226. AreEqual(res1, res2)
  227. AreEqual(len(opCalled), 1)
  228. opCalled = []
  229. #print 'ex %s ny' % op, type(ex), type(ny)
  230. res1 = eval('ex %s ny' % op)
  231. res2 = eval('nx %s ny' % op)
  232. AreEqual(res1, res2)
  233. AreEqual(len(opCalled), 1)
  234. opCalled = []
  235. test_extensible_math()