~ubuntu-branches/ubuntu/karmic/python3.0/karmic

« back to all changes in this revision

Viewing changes to Lib/test/test_hash.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-02-16 17:18:23 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20090216171823-1d5cm5qnnjvmnzzm
Tags: 3.0.1-0ubuntu1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
        # for 64-bit platforms
34
34
        self.same_hash(int(2**31), float(2**31))
35
35
        self.same_hash(int(-2**63), float(-2**63))
 
36
        self.same_hash(int(2**63), float(2**63))
36
37
 
37
38
    def test_coerced_floats(self):
38
39
        self.same_hash(int(1.23e300), float(1.23e300))
55
56
    def __ne__(self, other):
56
57
        return self is not other
57
58
 
58
 
class OnlyCmp(object):
59
 
    def __cmp__(self, other):
60
 
        return cmp(id(self), id(other))
61
 
 
62
59
class InheritedHashWithEquality(FixedHash, OnlyEquality): pass
63
60
class InheritedHashWithInequality(FixedHash, OnlyInequality): pass
64
 
class InheritedHashWithCmp(FixedHash, OnlyCmp): pass
65
61
 
66
62
class NoHash(object):
67
63
    __hash__ = None
74
70
    fixed_expected = [FixedHash(),
75
71
                      InheritedHashWithEquality(),
76
72
                      InheritedHashWithInequality(),
77
 
                      InheritedHashWithCmp(),
78
73
                      ]
79
74
    error_expected = [NoHash(),
80
75
                      OnlyEquality(),
103
98
            self.assertFalse(isinstance(obj, Hashable), repr(obj))
104
99
 
105
100
 
 
101
# Issue #4701: Check that some builtin types are correctly hashable
 
102
class DefaultIterSeq(object):
 
103
    seq = range(10)
 
104
    def __len__(self):
 
105
        return len(self.seq)
 
106
    def __getitem__(self, index):
 
107
        return self.seq[index]
 
108
 
 
109
class HashBuiltinsTestCase(unittest.TestCase):
 
110
    hashes_to_check = [range(10),
 
111
                       enumerate(range(10)),
 
112
                       iter(DefaultIterSeq()),
 
113
                       iter(lambda: 0, 0),
 
114
                      ]
 
115
 
 
116
    def test_hashes(self):
 
117
        _default_hash = object.__hash__
 
118
        for obj in self.hashes_to_check:
 
119
            self.assertEqual(hash(obj), _default_hash(obj))
 
120
 
106
121
def test_main():
107
122
    support.run_unittest(HashEqualityTestCase,
108
 
                         HashInheritanceTestCase)
 
123
                              HashInheritanceTestCase,
 
124
                              HashBuiltinsTestCase)
109
125
 
110
126
 
111
127
if __name__ == "__main__":