Back to index

python3.2  3.2.2
Public Member Functions
test.test_numeric_tower.HashTest Class Reference

List of all members.

Public Member Functions

def check_equal_hash
def test_bools
def test_integers
def test_binary_floats
def test_complex
def test_decimals
def test_fractions
def test_hash_normalization

Detailed Description

Definition at line 18 of file test_numeric_tower.py.


Member Function Documentation

Definition at line 19 of file test_numeric_tower.py.

00019 
00020     def check_equal_hash(self, x, y):
00021         # check both that x and y are equal and that their hashes are equal
00022         self.assertEqual(hash(x), hash(y),
00023                          "got different hashes for {!r} and {!r}".format(x, y))
00024         self.assertEqual(x, y)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 67 of file test_numeric_tower.py.

00067 
00068     def test_binary_floats(self):
00069         # check that floats hash equal to corresponding Fractions and Decimals
00070 
00071         # floats that are distinct but numerically equal should hash the same
00072         self.check_equal_hash(0.0, -0.0)
00073 
00074         # zeros
00075         self.check_equal_hash(0.0, D(0))
00076         self.check_equal_hash(-0.0, D(0))
00077         self.check_equal_hash(-0.0, D('-0.0'))
00078         self.check_equal_hash(0.0, F(0))
00079 
00080         # infinities and nans
00081         self.check_equal_hash(float('inf'), D('inf'))
00082         self.check_equal_hash(float('-inf'), D('-inf'))
00083 
00084         for _ in range(1000):
00085             x = random.random() * math.exp(random.random()*200.0 - 100.0)
00086             self.check_equal_hash(x, D.from_float(x))
00087             self.check_equal_hash(x, F.from_float(x))

Here is the call graph for this function:

Definition at line 25 of file test_numeric_tower.py.

00025 
00026     def test_bools(self):
00027         self.check_equal_hash(False, 0)
00028         self.check_equal_hash(True, 1)

Here is the call graph for this function:

Definition at line 88 of file test_numeric_tower.py.

00088 
00089     def test_complex(self):
00090         # complex numbers with zero imaginary part should hash equal to
00091         # the corresponding float
00092 
00093         test_values = [0.0, -0.0, 1.0, -1.0, 0.40625, -5136.5,
00094                        float('inf'), float('-inf')]
00095 
00096         for zero in -0.0, 0.0:
00097             for value in test_values:
00098                 self.check_equal_hash(value, complex(value, zero))

Here is the call graph for this function:

Definition at line 99 of file test_numeric_tower.py.

00099 
00100     def test_decimals(self):
00101         # check that Decimal instances that have different representations
00102         # but equal values give the same hash
00103         zeros = ['0', '-0', '0.0', '-0.0e10', '000e-10']
00104         for zero in zeros:
00105             self.check_equal_hash(D(zero), D(0))
00106 
00107         self.check_equal_hash(D('1.00'), D(1))
00108         self.check_equal_hash(D('1.00000'), D(1))
00109         self.check_equal_hash(D('-1.00'), D(-1))
00110         self.check_equal_hash(D('-1.00000'), D(-1))
00111         self.check_equal_hash(D('123e2'), D(12300))
00112         self.check_equal_hash(D('1230e1'), D(12300))
00113         self.check_equal_hash(D('12300'), D(12300))
00114         self.check_equal_hash(D('12300.0'), D(12300))
00115         self.check_equal_hash(D('12300.00'), D(12300))
00116         self.check_equal_hash(D('12300.000'), D(12300))

Here is the call graph for this function:

Definition at line 117 of file test_numeric_tower.py.

00117 
00118     def test_fractions(self):
00119         # check special case for fractions where either the numerator
00120         # or the denominator is a multiple of _PyHASH_MODULUS
00121         self.assertEqual(hash(F(1, _PyHASH_MODULUS)), _PyHASH_INF)
00122         self.assertEqual(hash(F(-1, 3*_PyHASH_MODULUS)), -_PyHASH_INF)
00123         self.assertEqual(hash(F(7*_PyHASH_MODULUS, 1)), 0)
00124         self.assertEqual(hash(F(-_PyHASH_MODULUS, 1)), 0)

Here is the call graph for this function:

Definition at line 125 of file test_numeric_tower.py.

00125 
00126     def test_hash_normalization(self):
00127         # Test for a bug encountered while changing long_hash.
00128         #
00129         # Given objects x and y, it should be possible for y's
00130         # __hash__ method to return hash(x) in order to ensure that
00131         # hash(x) == hash(y).  But hash(x) is not exactly equal to the
00132         # result of x.__hash__(): there's some internal normalization
00133         # to make sure that the result fits in a C long, and is not
00134         # equal to the invalid hash value -1.  This internal
00135         # normalization must therefore not change the result of
00136         # hash(x) for any x.
00137 
00138         class HalibutProxy:
00139             def __hash__(self):
00140                 return hash('halibut')
00141             def __eq__(self, other):
00142                 return other == 'halibut'
00143 
00144         x = {'halibut', HalibutProxy()}
00145         self.assertEqual(len(x), 1)

Here is the call graph for this function:

Definition at line 29 of file test_numeric_tower.py.

00029 
00030     def test_integers(self):
00031         # check that equal values hash equal
00032 
00033         # exact integers
00034         for i in range(-1000, 1000):
00035             self.check_equal_hash(i, float(i))
00036             self.check_equal_hash(i, D(i))
00037             self.check_equal_hash(i, F(i))
00038 
00039         # the current hash is based on reduction modulo 2**n-1 for some
00040         # n, so pay special attention to numbers of the form 2**n and 2**n-1.
00041         for i in range(100):
00042             n = 2**i - 1
00043             if n == int(float(n)):
00044                 self.check_equal_hash(n, float(n))
00045                 self.check_equal_hash(-n, -float(n))
00046             self.check_equal_hash(n, D(n))
00047             self.check_equal_hash(n, F(n))
00048             self.check_equal_hash(-n, D(-n))
00049             self.check_equal_hash(-n, F(-n))
00050 
00051             n = 2**i
00052             self.check_equal_hash(n, float(n))
00053             self.check_equal_hash(-n, -float(n))
00054             self.check_equal_hash(n, D(n))
00055             self.check_equal_hash(n, F(n))
00056             self.check_equal_hash(-n, D(-n))
00057             self.check_equal_hash(-n, F(-n))
00058 
00059         # random values of various sizes
00060         for _ in range(1000):
00061             e = random.randrange(300)
00062             n = random.randrange(-10**e, 10**e)
00063             self.check_equal_hash(n, D(n))
00064             self.check_equal_hash(n, F(n))
00065             if n == int(float(n)):
00066                 self.check_equal_hash(n, float(n))

Here is the call graph for this function:


The documentation for this class was generated from the following file: