/test3/test_mpz_args.py

http://gmpy.googlecode.com/ · Python · 61 lines · 41 code · 13 blank · 7 comment · 10 complexity · adbfcb9ee0bab4ee8d4635a516f9b859 MD5 · raw file

  1. # Test a wide variety of input values to the commonly used mpz operations.
  2. # This test should be run whenever optimizations are made to the handling of
  3. # arguments.
  4. import sys
  5. import gmpy2 as gmpy
  6. if sys.version.startswith('3'):
  7. intTypes = (int,)
  8. else:
  9. intTypes = (int, long)
  10. def writeln(s):
  11. sys.stdout.write(s+'\n')
  12. valueList = [0, 1, 2, 3, 4, 5]
  13. for power in (15, 16, 30, 32, 45, 48, 60, 64, 75, 90, 96, 105, 120, 128):
  14. for i in (-2, -1, 0, 1, 2):
  15. valueList.append(2**power + i)
  16. valueList.append('123456789012345678901234567890')
  17. valueList.append('10000000000000000000000000000000000000000000000000000000000000000')
  18. testValues = []
  19. mpzValues = []
  20. for i in valueList:
  21. for t in intTypes:
  22. testValues.append(t(i))
  23. testValues.append(-t(i))
  24. mpzValues.append(gmpy.mpz(i))
  25. mpzValues.append(-gmpy.mpz(i))
  26. testValues.extend(mpzValues)
  27. for i in testValues:
  28. for z in mpzValues:
  29. # Test all permutations of addition
  30. assert int(i)+int(z) == i+z, (repr(i),repr(z))
  31. assert int(z)+int(i) == z+i, (repr(i),repr(z))
  32. # Test all permutations of subtraction
  33. assert int(i)-int(z) == i-z, (repr(i),repr(z))
  34. assert int(z)-int(i) == z-i, (repr(i),repr(z))
  35. # Test all permutations of multiplication
  36. assert int(i)*int(z) == i*z, (repr(i),repr(z))
  37. assert int(z)*int(i) == z*i, (repr(i),repr(z))
  38. # Test all permutations of division
  39. if z!=0:
  40. temp = int(i)//int(z)
  41. assert int(i)//int(z) == i//z, (repr(i),repr(z))
  42. assert int(i)%int(z) == i%z, (repr(i),repr(z))
  43. assert divmod(int(i),int(z)) == divmod(i,z), (repr(i),repr(z))
  44. if i!=0:
  45. temp = int(z)//int(i)
  46. assert int(z)//int(i) == z//i, (repr(i),repr(z))
  47. assert int(z)%int(i) == z%i, (repr(i),repr(z))
  48. assert divmod(int(z),int(i)) == divmod(z,i), (repr(i),repr(z))