@@ -42,14 +42,14 @@ def get(self, key):
4242 if self ._keys [hash_ ] is self ._empty :
4343 # That key was never assigned
4444 return None
45- elif self ._keys [hash_ ] == key and self . _values [ hash_ ] :
45+ elif self ._keys [hash_ ] == key :
4646 # key found
4747 return self ._values [hash_ ]
48- elif initial_hash == hash_ :
49- # table is full and wrapped around
50- return None
5148
5249 hash_ = self .rehash (hash_ )
50+ if initial_hash == hash_ :
51+ # table is full and wrapped around
52+ return None
5353
5454 def hash (self , key ):
5555 return key % self .size
@@ -72,3 +72,27 @@ def test_one_entry(self):
7272 m = HashTable (10 )
7373 m .put (1 , '1' )
7474 self .assertEqual ('1' , m .get (1 ))
75+
76+ def test_add_entry_bigger_than_table_size (self ):
77+ m = HashTable (10 )
78+ m .put (11 , '1' )
79+ self .assertEqual ('1' , m .get (11 ))
80+
81+ def test_get_none_if_key_missing_and_hash_collision (self ):
82+ m = HashTable (10 )
83+ m .put (1 , '1' )
84+ self .assertEqual (None , m .get (11 ))
85+
86+ def test_two_entries_with_same_hash (self ):
87+ m = HashTable (10 )
88+ m .put (1 , '1' )
89+ m .put (11 , '11' )
90+ self .assertEqual ('1' , m .get (1 ))
91+ self .assertEqual ('11' , m .get (11 ))
92+
93+ def test_get_on_full_table_does_halts (self ):
94+ # and does not search forever
95+ m = HashTable (10 )
96+ for i in range (10 , 20 ):
97+ m .put (i , i )
98+ self .assertEqual (None , m .get (1 ))
0 commit comments