Exemplo n.º 1
0
def test_hashtable():
    """
    1. Adding a key/value to your hashtable results in the value being in the data structure
    2. Retrieving based on a key returns the value stored
    3. Successfully returns null for a key that does not exist in the hashtable
    4. Successfully handle a collision within the hashtable
    5. Successfully retrieve a value from a bucket within the hashtable that has a collision
    6. Successfully hash a key to an in-range value
    """

    hashtable = HashTable()

    hashtable.add('reema', 5)

    assert hashtable.contains('reema')

    assert hashtable.get('reema') == 5

    assert hashtable.get('nokey') == None

    hashtable.add('collision', 4)

    assert hashtable.contains('collision')

    assert hashtable.get('collision') == 4

    assert hashtable.hash('key')
Exemplo n.º 2
0
def test_get():
    """
    2.Retrieving based on a key returns the value stored
    3.Successfully returns null for a key that does not exist in the hashtable
    """
    ht = HashTable(1024)
    ht.add('five', 5)
    assert ht.get('five') == 5
    assert ht.get('tiger') == None
Exemplo n.º 3
0
def test_hashtable():
    hashtable = HashTable()

    hashtable.set("darkblue", 1)
    hashtable.set("salmon", 2)
    hashtable.set("hohoj", 3)

    assert hashtable.get("darkblue") == 1
    assert hashtable.get("salmon") == 2
    assert hashtable.get("empty") == None

    assert hashtable.keys() == ["darkblue", "salmon", "hohoj"]
    assert hashtable.values() == [1, 2, 3]
Exemplo n.º 4
0
def test_add_multiple_hash_pass():
    hashtable = HashTable()
    hashtable.add('hadi', 45)
    hashtable.add('ahmad', 33)
    hashtable.add('moe', 11)
    actual = hashtable.get('moe')
    expected = 11
    assert actual == expected
Exemplo n.º 5
0
def test_add_multiple_collision():
    """  
    4.Successfully handle a collision within the hashtable
    5.Successfully retrieve a value from a bucket within the hashtable that has a collision
    6.Successfully retrieve a value from a bucket within the hashtable that has a collision
    """
    ht = HashTable(1024)
    ht.add('one', 1)
    ht.add('two', 2)
    ht.add('two', 3)
    assert ht.contains('one') == True
    assert ht.contains('two') == True
    assert ht.get('two') == 2
Exemplo n.º 6
0
def test_get():
    hashtable = HashTable()
    hashtable.add('glisten', 'glitter')
    expected = 'glitter'
    actual = hashtable.get('glisten')
    assert actual == expected
def test_single_hash_fail():
    hashtable = HashTable()
    hashtable.add('roger', 45)
    actual = hashtable.get('roger')
    expected = 44
    assert actual != expected
def test_single_hash_pass():
    hashtable = HashTable()
    hashtable.add('roger', 45)
    actual = hashtable.get('roger')
    expected = 45
    assert actual == expected
Exemplo n.º 9
0
def test_single_hash_fail():
    hashtable = HashTable()
    hashtable.add('hadi', 40)
    actual = hashtable.get('hadi')
    expected = 45
    assert actual != expected
def test_get():
    ht = HashTable()
    ht.add('milk', 'coookies')
    actual = ht.get('milk')
    expected = 'coookies'
    assert actual == expected
Exemplo n.º 11
0
class TestHashTable(TestCase):
    def setUp(self):
        self.hash_table = HashTable()

    def test_attributes(self):
        self.assertEqual(4, len(self.hash_table.keys))
        self.assertEqual(4, len(self.hash_table.values))
        self.assertEqual(4, self.hash_table.max_capacity)

    def test_add_with_available_space(self):
        self.hash_table.add("test_key_1", "test_value_1")
        self.assertEqual(1, self.hash_table.actual_length)
        self.assertEqual(4, self.hash_table.max_capacity)
        self.assertEqual("test_value_1", self.hash_table["test_key_1"])

    def test_add_with_no_available_space__expect_to_resize(self):
        for number in range(1, self.hash_table.max_capacity + 1):
            self.hash_table.add(f'test_key_{number}', f'test_value_{number}')

        self.assertEqual(4, self.hash_table.actual_length)
        self.assertEqual(4, self.hash_table.max_capacity)

        # Here we overload the dict and it should resize
        self.hash_table.add(f'test_key_5', f'test_value_5')
        self.assertEqual(5, self.hash_table.actual_length)
        self.assertEqual(8, self.hash_table.max_capacity)
        self.assertIn('test_key_5', self.hash_table.keys)

    def test_value_is_replaced_when_key_exist(self):
        self.hash_table.add('test_key', 'test_value')
        self.assertEqual('test_value', self.hash_table['test_key'])
        self.hash_table['test_key'] = 'new_value'
        self.assertEqual('new_value', self.hash_table['test_key'])

    def test_get_with_existing_key(self):
        self.hash_table.add('test_key', 'test_value')
        self.assertEqual('test_value', self.hash_table.get('test_key'))

    def test_get_with_not_existing_key(self):
        self.hash_table.add("test_key", "test_value")
        self.assertIsNone(self.hash_table.get("not existing"))

    def test_get_with_not_existing_key_with_default_value(self):
        self.hash_table.add("test_key", "value")
        self.assertEqual("DEFAULT",
                         self.hash_table.get("not existing", "DEFAULT"))

    def test_representation(self):
        self.hash_table.add("test_key", "test_value")
        self.assertEqual('{test_key: test_value}', str(self.hash_table))

    def test_collision_set_next_available_index(self):
        self.hash_table["name"] = "Peter"
        self.assertEqual(1, self.hash_table.keys.index("name"))
        # collision with index 1
        self.hash_table["age"] = 25
        self.assertEqual(2, self.hash_table.keys.index("age"))

    def test_collision_set_next_available_index_at_0(self):
        self.hash_table["name"] = "Peter"
        self.assertEqual(1, self.hash_table.keys.index("name"))
        # collision with index 1
        self.hash_table["age"] = 25
        self.assertEqual(2, self.hash_table.keys.index("age"))
        self.hash_table["work"] = "Some title"
        self.assertEqual(3, self.hash_table.keys.index("work"))

        # Go back to index 0 because no other available
        self.hash_table["eyes color"] = "blue"
        self.assertEqual(0, self.hash_table.keys.index("eyes color"))