Ejemplo n.º 1
0
def test_invalid_free_up(scenario):
    hd = HopscotchDict()

    if scenario == "full_wraps":
        hd._resize(16)
        for i in range(12, 125, 16):
            hd[i] = f"test_invalid_free_up_{i}"

        with pytest.raises(RuntimeError):
            hd._free_up(12)

    elif scenario == "full":
        for i in range(1, 257, 32):
            hd[i] = f"test_invalid_free_up_{i}"

        with pytest.raises(RuntimeError):
            hd._free_up(1)

    elif scenario == "last_distant":
        hd._resize(32)

        hd[8] = "test_invalid_free_up_8"
        hd[9] = "test_invalid_free_up_9"
        hd[40] = "test_invalid_free_up_40"

        del hd[40]
        del hd[9]

        for i in range(1, 257, 32):
            hd[i] = f"test_invalid_free_up_{i}"

        with pytest.raises(RuntimeError):
            hd._free_up(1)
Ejemplo n.º 2
0
def test_valid_free_up(scenario):
    hd = HopscotchDict()

    if scenario == "unnecessary":
        with pytest.raises(ValueError):
            hd._free_up(-1)

        with pytest.raises(ValueError):
            hd._free_up(10)

        for i in range(2, 7):
            hd[i] = f"test_valid_free_up_{i}"

        hd._free_up(0)

        # Nothing was stored at the index, so nothing to do
        data_idx, neighbors = hd._get_lookup_index_info(0)
        assert data_idx == hd.FREE_ENTRY
        assert len(neighbors) == 0

        hd._free_up(4)

        # The index has an open neighbor, so nothing to do
        data_idx, neighbors = hd._get_lookup_index_info(4)
        assert data_idx == 2
        assert neighbors == [4]

    elif scenario == "far":
        for i in range(1, 11):
            hd[i] = f"test_valid_free_up_{i}"

        # Entry at index 4 moves to 11
        hd._free_up(1)

        data_idx, neighbors = hd._get_lookup_index_info(1)

        # The entry at index 1 will not move as a neighbor will open up first
        assert data_idx == 0
        assert neighbors == [1]

        data_idx, neighbors = hd._get_lookup_index_info(4)

        # Index 4 in _lookup_table should be empty
        assert data_idx == hd.FREE_ENTRY
        assert neighbors == [11]

        data_idx, neighbors = hd._get_lookup_index_info(11)

        # Index 11 in _lookup_table should point to index 3 in other lists
        assert data_idx == 3
        assert len(neighbors) == 0

    elif scenario == "displaced":
        hd._resize(16)

        for i in range(14, 63, 16):
            hd[i] = f"test_valid_free_up_{i}"

        for i in range(2, 9):
            hd[i] = f"test_valid_free_up_{i}"

        del hd[30]

        assert hd._get_lookup_index_info(1) == (3, [])
        assert hd._get_lookup_index_info(15) == (hd.FREE_ENTRY, [])

        hd._free_up(1)

        assert hd._get_lookup_index_info(1) == (hd.FREE_ENTRY, [])
        assert hd._get_lookup_index_info(15) == (3, [])