MCPcopy Index your code
hub / github.com/python/cpython / test_hash_method

Method test_hash_method

Lib/test/test_decimal.py:1910–1986  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1908 self.assertEqual(id(dc), id(d))
1909
1910 def test_hash_method(self):
1911
1912 Decimal = self.decimal.Decimal
1913 localcontext = self.decimal.localcontext
1914
1915 def hashit(d):
1916 a = hash(d)
1917 b = d.__hash__()
1918 self.assertEqual(a, b)
1919 return a
1920
1921 #just that it's hashable
1922 hashit(Decimal(23))
1923 hashit(Decimal('Infinity'))
1924 hashit(Decimal('-Infinity'))
1925 hashit(Decimal('nan123'))
1926 hashit(Decimal('-NaN'))
1927
1928 test_values = [Decimal(sign*(2**m + n))
1929 for m in [0, 14, 15, 16, 17, 30, 31,
1930 32, 33, 61, 62, 63, 64, 65, 66]
1931 for n in range(-10, 10)
1932 for sign in [-1, 1]]
1933 test_values.extend([
1934 Decimal("-1"), # ==> -2
1935 Decimal("-0"), # zeros
1936 Decimal("0.00"),
1937 Decimal("-0.000"),
1938 Decimal("0E10"),
1939 Decimal("-0E12"),
1940 Decimal("10.0"), # negative exponent
1941 Decimal("-23.00000"),
1942 Decimal("1230E100"), # positive exponent
1943 Decimal("-4.5678E50"),
1944 # a value for which hash(n) != hash(n % (2**64-1))
1945 # in Python pre-2.6
1946 Decimal(2**64 + 2**32 - 1),
1947 # selection of values which fail with the old (before
1948 # version 2.6) long.__hash__
1949 Decimal("1.634E100"),
1950 Decimal("90.697E100"),
1951 Decimal("188.83E100"),
1952 Decimal("1652.9E100"),
1953 Decimal("56531E100"),
1954 ])
1955
1956 # check that hash(d) == hash(int(d)) for integral values
1957 for value in test_values:
1958 self.assertEqual(hashit(value), hash(int(value)))
1959
1960 # check that the hashes of a Decimal float match when they
1961 # represent exactly the same values
1962 test_strings = ['inf', '-Inf', '0.0', '-.0e1',
1963 '34.0', '2.5', '112390.625', '-0.515625']
1964 for s in test_strings:
1965 f = float(s)
1966 d = Decimal(s)
1967 self.assertEqual(hashit(d), hash(f))

Callers

nothing calls this directly

Calls 4

DecimalClass · 0.85
localcontextFunction · 0.85
extendMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected