/test3/test_leaks.py

http://gmpy.googlecode.com/ · Python · 79 lines · 63 code · 13 blank · 3 comment · 10 complexity · 4e3d50464948aaa87ae9d4b57b75fc34 MD5 · raw file

  1. from __future__ import print_function
  2. import gmpy2
  3. import fractions
  4. import decimal
  5. MPZ = gmpy2.mpz
  6. MPQ = gmpy2.mpq
  7. MPF = gmpy2.mpf
  8. FR = fractions.Fraction
  9. DC = decimal.Decimal
  10. int_vals = []
  11. int_vals.extend([1, 2, 3, -10, 256, 23**65])
  12. int_vals.extend([MPZ(1), MPZ(2), MPZ(3), MPZ(-10), MPZ(23**65)])
  13. float_vals = []
  14. float_vals.extend([1.23, -3.124159, 0.0234, float("nan")])
  15. float_vals.extend([MPF(1.23), MPF("-3.124159"), MPF("0.0234")])
  16. frac_vals = []
  17. frac_vals.extend([FR(1,2), FR(-7,2345)])
  18. frac_vals.extend([MPQ(1,2), MPQ(-7,2345)])
  19. all_vals = int_vals + float_vals + frac_vals
  20. def test_leaks1(bits = 80, chunk = 150, terms = 20):
  21. """Test gmpy2.pack and gmpy2.unpack."""
  22. for t in range(2, terms):
  23. for b in range(1, bits):
  24. # Test with all bits set to 1.
  25. v = [ 2**b - 1 ] * t
  26. for c in range(b, chunk):
  27. temp = gmpy2.pack(v, c)
  28. u = gmpy2.unpack(temp, c)
  29. assert u == v, (v, temp, u, (t, b, c))
  30. def test_leaks2():
  31. """Test binary operations."""
  32. def test_binary(a, b):
  33. t = a + b
  34. t = a - b
  35. t = a * b
  36. try:
  37. t = a / b
  38. except:
  39. pass
  40. try:
  41. t = a // b
  42. except:
  43. pass
  44. try:
  45. t = divmod(a, b)
  46. except:
  47. pass
  48. t = gmpy2.add(a, b)
  49. t = gmpy2.sub(a, b)
  50. t = gmpy2.mul(a, b)
  51. t = gmpy2.div(a, b)
  52. t = gmpy2.agm(a, b)
  53. t = gmpy2.atan2(a, b)
  54. for x in all_vals:
  55. for y in all_vals:
  56. test_binary(x, y)
  57. test_binary(y, x)
  58. def main(count = 100):
  59. print("Testing for memory leaks by repeating a set of calculations.")
  60. print("This test may take a few minutes.")
  61. for i in range(count):
  62. print("Pass: {0}".format(i))
  63. test_leaks2()
  64. print("Test successful.")
  65. if __name__ == "__main__":
  66. main()