/ test / test_mpz_args.py
test_mpz_args.py
 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  
 5  import sys
 6  import gmpy2
 7  
 8  if sys.version.startswith('3'):
 9      intTypes = (int,)
10  else:
11      intTypes = (int, long)
12  
13  
14  valueList = [0, 1, 2, 3, 4, 5]
15  
16  for power in (14, 15, 16, 29, 30, 31, 32, 45, 48, 60, 64, 75, 90, 96, 105, 120, 128):
17      for i in (-2, -1, 0, 1, 2):
18          valueList.append(2**power + i)
19  
20  valueList.append('123456789012345678901234567890')
21  valueList.append('10000000000000000000000000000000000000000000000000000000000000000')
22  
23  testValues = []
24  mpzValues = []
25  for i in valueList:
26      for t in intTypes:
27          testValues.append(t(i))
28          testValues.append(-t(i))
29      mpzValues.append(gmpy2.mpz(i))
30      mpzValues.append(-gmpy2.mpz(i))
31  
32  testValues.extend(mpzValues)
33  
34  def test():
35      for i in testValues:
36          for z in mpzValues:
37              # Test all permutations of addition
38              assert int(i) + int(z) == i + z, (repr(i), repr(z))
39              assert int(z) + int(i) == z + i, (repr(i), repr(z))
40  
41              # Test all permutations of subtraction
42              assert int(i) - int(z) == i - z, (repr(i), repr(z))
43              assert int(z) - int(i) == z - i, (repr(i), repr(z))
44  
45              # Test all permutations of multiplication
46              assert int(i) * int(z) == i * z, (repr(i), repr(z))
47              assert int(z) * int(i) == z * i, (repr(i), repr(z))
48  
49              # Test all permutations of floor division
50              if z != 0:
51                  assert int(i) // int(z) == i // z, (repr(i), repr(z))
52                  assert int(i) % int(z) == i % z, (repr(i), repr(z))
53                  assert divmod(int(i), int(z)) == divmod(i, z), (repr(i), repr(z))
54  
55              if i!=0:
56                  assert int(z) // int(i) == z // i, (repr(i), repr(z))
57                  assert int(z) % int(i) == z % i, (repr(i), repr(z))
58                  assert divmod(int(z), int(i)) == divmod(z,i), (repr(i), repr(z))
59      return True
60              
61  if __name__ == "__main__":
62      print("Testing combinations of mpz and integer operations.")
63      print("This test may take a few minutes.")
64      test()
65      print("Test successful.")