Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. ### This file is grabbed from Python's Lib/test/test_math.py
  2. ###---------------------------------------------------------
  3. # Python test set -- math module
  4. # XXXX Should not do tests around zero only
  5.  
  6. eps = 0.00001
  7. #print('math module, testing with eps ', eps)
  8. import math
  9.  
  10. def testit(name, value, expected):
  11.     if abs(value-expected) > eps:
  12.         msg = name + " returned " + str(value) + " expected " + str(expected)
  13.         raise msg
  14.  
  15. #print 'constants'
  16. testit('pi', math.pi, 3.1415926)
  17. testit('e', math.e, 2.7182818)
  18.  
  19. #print 'acos'
  20. testit('acos(-1)', math.acos(-1), math.pi)
  21. testit('acos(0)', math.acos(0), math.pi/2)
  22. testit('acos(1)', math.acos(1), 0)
  23.  
  24. #print 'asin'
  25. testit('asin(-1)', math.asin(-1), -math.pi/2)
  26. testit('asin(0)', math.asin(0), 0)
  27. testit('asin(1)', math.asin(1), math.pi/2)
  28.  
  29. #print 'atan'
  30. testit('atan(-1)', math.atan(-1), -math.pi/4)
  31. testit('atan(0)', math.atan(0), 0)
  32. testit('atan(1)', math.atan(1), math.pi/4)
  33.  
  34. #print 'atan2'
  35. testit('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
  36. testit('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
  37. testit('atan2(0, 1)', math.atan2(0, 1), 0)
  38. testit('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
  39. testit('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
  40.  
  41. #print 'ceil'
  42. testit('ceil(0.5)', math.ceil(0.5), 1)
  43. testit('ceil(1.0)', math.ceil(1.0), 1)
  44. testit('ceil(1.5)', math.ceil(1.5), 2)
  45. testit('ceil(-0.5)', math.ceil(-0.5), 0)
  46. testit('ceil(-1.0)', math.ceil(-1.0), -1)
  47. testit('ceil(-1.5)', math.ceil(-1.5), -1)
  48.  
  49. #print 'cos'
  50. testit('cos(-pi/2)', math.cos(-math.pi/2), 0)
  51. testit('cos(0)', math.cos(0), 1)
  52. testit('cos(pi/2)', math.cos(math.pi/2), 0)
  53. testit('cos(pi)', math.cos(math.pi), -1)
  54.  
  55. #print 'cosh'
  56. testit('cosh(0)', math.cosh(0), 1)
  57. testit('cosh(2)-2*(cosh(1)**2)', math.cosh(2)-2*(math.cosh(1)**2), -1) # Thanks to Lambert
  58.  
  59. #print 'degrees'
  60. testit('degrees(pi)', math.degrees(math.pi), 180.0)
  61. testit('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
  62. testit('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
  63.  
  64. #print 'exp'
  65. testit('exp(-1)', math.exp(-1), 1/math.e)
  66. testit('exp(0)', math.exp(0), 1)
  67. testit('exp(1)', math.exp(1), math.e)
  68.  
  69. #print 'fabs'
  70. testit('fabs(-1)', math.fabs(-1), 1)
  71. testit('fabs(0)', math.fabs(0), 0)
  72. testit('fabs(1)', math.fabs(1), 1)
  73.  
  74. #print 'floor'
  75. testit('floor(0.5)', math.floor(0.5), 0)
  76. testit('floor(1.0)', math.floor(1.0), 1)
  77. testit('floor(1.5)', math.floor(1.5), 1)
  78. testit('floor(-0.5)', math.floor(-0.5), -1)
  79. testit('floor(-1.0)', math.floor(-1.0), -1)
  80. testit('floor(-1.5)', math.floor(-1.5), -2)
  81.  
  82. #print 'fmod'
  83. testit('fmod(10,1)', math.fmod(10,1), 0)
  84. testit('fmod(10,0.5)', math.fmod(10,0.5), 0)
  85. testit('fmod(10,1.5)', math.fmod(10,1.5), 1)
  86. testit('fmod(-10,1)', math.fmod(-10,1), 0)
  87. testit('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
  88. testit('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
  89.  
  90. #print 'frexp'
  91. def testfrexp(name, value, expected):
  92.     mant = value[0]
  93.     exp  = value[1]
  94.     emant = expected[0]
  95.     eexp  = expected[1]
  96.     if abs(mant-emant) > eps or exp != eexp:
  97.         raise '%s returned (%f, %f), expected (%f, %f)'%\
  98.               (name, mant, exp, emant,eexp)
  99.  
  100. testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
  101. testfrexp('frexp(0)', math.frexp(0), (0, 0))
  102. testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
  103. testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
  104.  
  105. #print 'hypot'
  106. testit('hypot(0,0)', math.hypot(0,0), 0)
  107. testit('hypot(3,4)', math.hypot(3,4), 5)
  108.  
  109. #print 'ldexp'
  110. testit('ldexp(0,1)', math.ldexp(0,1), 0)
  111. testit('ldexp(1,1)', math.ldexp(1,1), 2)
  112. testit('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
  113. testit('ldexp(-1,1)', math.ldexp(-1,1), -2)
  114.  
  115. #print 'log'
  116. testit('log(1/e)', math.log(1/math.e), -1)
  117. testit('log(1)', math.log(1), 0)
  118. testit('log(e)', math.log(math.e), 1)
  119. testit('log(32,2)', math.log(32,2), 5)
  120. testit('log(10**40, 10)', math.log(10**40, 10), 40)
  121. testit('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
  122.  
  123. #print 'log10'
  124. testit('log10(0.1)', math.log10(0.1), -1)
  125. testit('log10(1)', math.log10(1), 0)
  126. testit('log10(10)', math.log10(10), 1)
  127.  
  128. #print 'modf'
  129. def testmodf(name, value, expected):
  130.     v1 = value[0]
  131.     v2 = value[1]
  132.     e1 = expected[0]
  133.     e2 = expected[1]
  134.     if abs(v1-e1) > eps or abs(v2-e2):
  135.         raise '%s returned (%f, %f), expected (%f, %f)'%\
  136.               (name, v1,v2, e1,e2)
  137.  
  138. testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
  139. testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
  140.  
  141. #print 'pow'
  142. testit('pow(0,1)', math.pow(0,1), 0)
  143. testit('pow(1,0)', math.pow(1,0), 1)
  144. testit('pow(2,1)', math.pow(2,1), 2)
  145. testit('pow(2,-1)', math.pow(2,-1), 0.5)
  146.  
  147. #print 'radians'
  148. testit('radians(180)', math.radians(180), math.pi)
  149. testit('radians(90)', math.radians(90), math.pi/2)
  150. testit('radians(-45)', math.radians(-45), -math.pi/4)
  151.  
  152. #print 'sin'
  153. testit('sin(0)', math.sin(0), 0)
  154. testit('sin(pi/2)', math.sin(math.pi/2), 1)
  155. testit('sin(-pi/2)', math.sin(-math.pi/2), -1)
  156.  
  157. #print 'sinh'
  158. testit('sinh(0)', math.sinh(0), 0)
  159. testit('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
  160. testit('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
  161.  
  162. #print 'sqrt'
  163. testit('sqrt(0)', math.sqrt(0), 0)
  164. testit('sqrt(1)', math.sqrt(1), 1)
  165. testit('sqrt(4)', math.sqrt(4), 2)
  166.  
  167. #print 'tan'
  168. testit('tan(0)', math.tan(0), 0)
  169. testit('tan(pi/4)', math.tan(math.pi/4), 1)
  170. testit('tan(-pi/4)', math.tan(-math.pi/4), -1)
  171.  
  172. #print 'tanh'
  173. testit('tanh(0)', math.tanh(0), 0)
  174. testit('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
  175.  
  176. #print("OK: math module test pass")
  177.