Exemple #1
0
 def __init__(self, capacity=16, source=None):
     """Construct."""
     if source is not None:
         self._hash_map = hash_map.HashMap(
             capacity, ((element, PRESENT) for element in source)
         )
     else:
         self._hash_map = hash_map.HashMap(capacity)
def test_construct_capacity(capacity, raises):
    """
    GIVEN initial capacity and whether ValueError should be raised
    WHEN a HashMap is constructed with the capacity
    THEN ValueError is raised when expected raises is True.
    """
    if raises:
        with pytest.raises(ValueError):
            hash_map.HashMap(capacity)
    else:
        hash_map.HashMap(capacity)
def mocked_buckets_hash_map():
    """HashMap with mocked buckets and _calculate_index."""
    capacity = 16
    test_hash_map = hash_map.HashMap(capacity)
    for idx in range(capacity):
        test_hash_map._buckets[idx] = mock.MagicMock()
    test_hash_map._calculate_index = mock.MagicMock()
    return test_hash_map
def test_delete_missing():
    """
    GIVEN empty hash map
    WHEN delete is called with a key
    THEN KeyError is raised.
    """
    test_hash_map = hash_map.HashMap()

    with pytest.raises(KeyError):
        test_hash_map.delete("key 1")
def test_set_get_missing():
    """
    GIVEN empty hash map
    WHEN get is called with a key
    THEN KeyError is raised.
    """
    test_hash_map = hash_map.HashMap()

    with pytest.raises(KeyError):
        test_hash_map.get("key 1")
def test_exists_missing():
    """
    GIVEN empty hash map
    WHEN exists is called with a key
    THEN False is returned.
    """
    test_hash_map = hash_map.HashMap()

    exists = test_hash_map.exists("key 1")

    assert exists is False
def test_size(actions, expected_size):
    """
    GIVEN empty hash map, actions to modify the hash map and expected size
    WHEN the actions are performed on the map
    THEN the map has the expected size.
    """
    test_hash_map = hash_map.HashMap()
    for operation, args in actions:
        getattr(test_hash_map, operation)(*args)

    assert test_hash_map.size == expected_size
def test_clone_capacity_copied():
    """
    GIVEN empty hash map and capacity of the hash map
    WHEN hash map is cloned
    THEN the cloned map has the same capacity.
    """
    capacity = 16
    test_hash_map = hash_map.HashMap(capacity)

    cloned_hash_map = test_hash_map.clone()

    assert cloned_hash_map.capacity == capacity
def test_calculate_index(key, capacity, expected_index):
    """
    GIVEN key, capacity of HashMap and expected index
    WHEN HashMap is constructed with the capacity and _calculate_index is called with
        the key
    THEN the expected index is returned.
    """
    test_hash_map = hash_map.HashMap(capacity)

    index = test_hash_map._calculate_index(key)

    assert index == expected_index
def test_construct_source(elements):
    """
    GIVEN list of key value tuples
    WHEN hash map is constructed with list as source
    THEN map contains each element in the list.
    """
    test_hash_map = hash_map.HashMap(source=elements)

    element_set = set(iter(test_hash_map))
    assert len(element_set) == len(elements)
    for element in elements:
        assert element in element_set
def test_size_delete_raises():
    """
    GIVEN hash map with a single element
    WHEN delete is called with a different key than the element
    THEN size is not decremented.
    """
    test_hash_map = hash_map.HashMap()
    test_hash_map.set_("key 1", "value 1")

    with pytest.raises(KeyError):
        test_hash_map.delete("key 2")

    assert test_hash_map.size == 1
def test_delete_present():
    """
    GIVEN empty hash map and key and value
    WHEN set is called with the key and value and delete is called with the key
    THEN the key no longer exists in the hash map.
    """
    test_hash_map = hash_map.HashMap()
    key = "key 1"

    test_hash_map.set_(key, "value 1")
    test_hash_map.delete(key)

    assert test_hash_map.exists(key) is False
def test_exists_present():
    """
    GIVEN empty hash map and key and value
    WHEN set is called with the key and value and exists is called with the key
    THEN True is returned.
    """
    test_hash_map = hash_map.HashMap()
    key = "key 1"

    test_hash_map.set_(key, "value 1")
    exists = test_hash_map.exists(key)

    assert exists is True
def test_clone_elements(elements):
    """
    GIVEN empty hash map and elements to add to it
    WHEN hash map is constructed with list as source and cloned
    THEN cloned map contains each element in the list.
    """
    test_hash_map = hash_map.HashMap(source=elements)

    cloned_hash_map = test_hash_map.clone()

    element_set = set(iter(cloned_hash_map))
    assert len(element_set) == len(elements)
    for element in elements:
        assert element in element_set
def test_iterate(elements):
    """
    GIVEN empty hash map and elements to set
    WHEN elements are set and the map is iterated over
    THEN all elements are returned.
    """
    test_hash_map = hash_map.HashMap()
    for element in elements:
        test_hash_map.set_(*element)

    element_set = set(iter(test_hash_map))
    assert len(element_set) == len(elements)
    for element in elements:
        assert element in element_set
def test_clear(elements):
    """
    GIVEN empty hash map and elements to set
    WHEN elements are set and clear is called
    THEN the map has zero size and iterating over it returns an empty list.
    """
    test_hash_map = hash_map.HashMap()
    for element in elements:
        test_hash_map.set_(*element)

    test_hash_map.clear()

    assert test_hash_map.size == 0
    assert list(iter(test_hash_map)) == []
def test_set_get():
    """
    GIVEN empty hash map and key and value
    WHEN set is called with the key and value and get is called with the key
    THEN the value is returned.
    """
    test_hash_map = hash_map.HashMap()
    key = "key 1"
    value = "value 1"

    test_hash_map.set_(key, value)
    return_value = test_hash_map.get(key)

    assert return_value == value
def test_clone_not_same():
    """
    GIVEN empty hash map
    WHEN hash map is cloned
    THEN the cloned map is not the same object and contains different buckets.
    """
    test_hash_map = hash_map.HashMap()

    cloned_hash_map = test_hash_map.clone()

    assert id(cloned_hash_map) != id(test_hash_map)
    for original_bucket, cloned_bucket in zip(
        test_hash_map._buckets, cloned_hash_map._buckets
    ):
        assert id(original_bucket) != id(cloned_bucket)
def test_resize_down(initial_capacity, initial_size, expected_capacity):
    """
    GIVEN empty hash map, initial capacity, initial number of elements and
        expected capacity
    WHEN the initial size is added to the map and then one is removed
    THEN the capacity is the expected final capacity and all key value pairs except the
        removed pair are still in the map.
    """
    elements = [(f"key {idx + 1}", f"value {idx + 1}") for idx in range(initial_size)]

    test_hash_map = hash_map.HashMap(initial_capacity, elements)
    test_hash_map.delete(elements[-1][0])

    assert test_hash_map.capacity == expected_capacity
    assert len(test_hash_map._buckets) == expected_capacity
    assert test_hash_map.size == initial_size - 1
    element_set = set(iter(test_hash_map))
    assert len(element_set) == len(elements[:-1])
    for element in elements[:-1]:
        assert element in element_set
def test_resize_up(initial_capacity, initial_size, expected_capacity):
    """
    GIVEN empty hash map, initial capacity, initial number of elements and
        expected capacity
    WHEN the initial size plus one key and value is added to the map
    THEN the capacity is the expected final capacity and all added key value pairs are
        still in the map.
    """
    elements = [
        (f"key {idx + 1}", f"value {idx + 1}") for idx in range(initial_size + 1)
    ]

    test_hash_map = hash_map.HashMap(initial_capacity, elements)

    assert test_hash_map.capacity == expected_capacity
    assert len(test_hash_map._buckets) == expected_capacity
    assert test_hash_map.size == initial_size + 1
    element_set = set(iter(test_hash_map))
    assert len(element_set) == len(elements)
    for element in elements:
        assert element in element_set