PageRenderTime 29ms CodeModel.GetById 22ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/test3/test_mpz_args.py

http://gmpy.googlecode.com/
Python | 61 lines | 41 code | 13 blank | 7 comment | 15 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
 5import sys
 6import gmpy2 as gmpy
 7
 8if sys.version.startswith('3'):
 9    intTypes = (int,)
10else:
11    intTypes = (int, long)
12
13def writeln(s):
14    sys.stdout.write(s+'\n')
15
16valueList = [0, 1, 2, 3, 4, 5]
17
18for power in (15, 16, 30, 32, 45, 48, 60, 64, 75, 90, 96, 105, 120, 128):
19    for i in (-2, -1, 0, 1, 2):
20        valueList.append(2**power + i)
21
22valueList.append('123456789012345678901234567890')
23valueList.append('10000000000000000000000000000000000000000000000000000000000000000')
24
25testValues = []
26mpzValues = []
27for i in valueList:
28    for t in intTypes:
29        testValues.append(t(i))
30        testValues.append(-t(i))
31    mpzValues.append(gmpy.mpz(i))
32    mpzValues.append(-gmpy.mpz(i))
33
34testValues.extend(mpzValues)
35
36for i in testValues:
37    for z in mpzValues:
38        # Test all permutations of addition
39        assert int(i)+int(z) == i+z, (repr(i),repr(z))
40        assert int(z)+int(i) == z+i, (repr(i),repr(z))
41
42        # Test all permutations of subtraction
43        assert int(i)-int(z) == i-z, (repr(i),repr(z))
44        assert int(z)-int(i) == z-i, (repr(i),repr(z))
45
46        # Test all permutations of multiplication
47        assert int(i)*int(z) == i*z, (repr(i),repr(z))
48        assert int(z)*int(i) == z*i, (repr(i),repr(z))
49
50        # Test all permutations of division
51        if z!=0:
52            temp = int(i)//int(z)
53            assert int(i)//int(z) == i//z, (repr(i),repr(z))
54            assert int(i)%int(z) == i%z, (repr(i),repr(z))
55            assert divmod(int(i),int(z)) == divmod(i,z), (repr(i),repr(z))
56
57        if i!=0:
58            temp = int(z)//int(i)
59            assert int(z)//int(i) == z//i, (repr(i),repr(z))
60            assert int(z)%int(i) == z%i, (repr(i),repr(z))
61            assert divmod(int(z),int(i)) == divmod(z,i), (repr(i),repr(z))