def _test_free_for_lo_hi(self, lowest, highest):
        start = lowest
        end = highest + 1

        pool = IdPool(lowest, highest)

        # Exhaust the pool
        id_list1 = []
        for i in range(start, end):
            id = pool.alloc()
            id_list1.append(id)

        # Return everything to the pool
        for id in id_list1:
            pool.free(id)

        # Verify that nothing is used in the pool
        assert len(pool._used) == 0

        # Exhaust the pool
        id_list2 = []
        for i in range(start, end):
            id = pool.alloc()
            id_list2.append(id)

        # Verify that the same ID values came back as last time
        assert set(id_list1) == set(id_list2)

        # Verify that the pool is exhausted
        with pytest.raises(ValueError):
            pool.alloc()
def test_init_error_1():
    """
    Test invalid IdPool.__init__() with lowest > highest.
    """

    with pytest.raises(ValueError):
        IdPool(7, 6)
    def test_invalid_free_error_2(self):

        pool = IdPool(5, 5)

        pool.free_if_allocated(4)  # not in range (= not allocated)

        pool.free_if_allocated(5)  # in range but not allocated

        pool.free_if_allocated(6)  # not in range (= not allocated)
    def _test_exhausting_for_lo_hi(self, lowest, highest):
        start = lowest
        end = highest + 1

        pool = IdPool(lowest, highest)

        # Exhaust the pool
        id_list = []
        for i in range(start, end):
            id = pool.alloc()
            id_list.append(id)

        # Verify uniqueness of the ID values
        id_set = set(id_list)
        assert len(id_set) == len(id_list)

        # Verify that the pool is exhausted
        with pytest.raises(ValueError):
            pool.alloc()
    def test_invalid_free_error_1(self):

        pool = IdPool(5, 5)

        with pytest.raises(ValueError):
            pool.free(4)  # not in range

        with pytest.raises(ValueError):
            pool.free(5)  # in range but not allocated

        with pytest.raises(ValueError):
            pool.free(6)  # not in range
def test_invalid_free_error_2():
    """
    Test invalid IdPool.free_if_allocated() in some variations.
    """

    pool = IdPool(5, 5)

    pool.free_if_allocated(4)  # not in range (= not allocated)

    pool.free_if_allocated(5)  # in range but not allocated

    pool.free_if_allocated(6)  # not in range (= not allocated)
def test_invalid_free_error_1():
    """
    Test invalid IdPool.free() in some variations.
    """

    pool = IdPool(5, 5)

    with pytest.raises(ValueError):
        pool.free(4)  # not in range

    with pytest.raises(ValueError):
        pool.free(5)  # in range but not allocated

    with pytest.raises(ValueError):
        pool.free(6)  # not in range
def _test_exhausting_for_lo_hi(lowest, highest):
    """
    Internal helper function that tests exhausting an IdPool.
    """

    start = lowest
    end = highest + 1

    pool = IdPool(lowest, highest)

    # Exhaust the pool
    id_list = []
    for _ in range(start, end):
        _id = pool.alloc()
        id_list.append(_id)

    # Verify uniqueness of the ID values
    id_set = set(id_list)
    assert len(id_set) == len(id_list)

    # Verify that the pool is exhausted
    with pytest.raises(ValueError):
        pool.alloc()
def _test_free_for_lo_hi(lowest, highest):
    """
    Internal helper function that tests exhausting, freeing, and again
    exhausting an IdPool.
    """

    start = lowest
    end = highest + 1

    pool = IdPool(lowest, highest)

    # Exhaust the pool
    id_list1 = []
    for _ in range(start, end):
        _id = pool.alloc()
        id_list1.append(_id)

    # Return everything to the pool
    for _id in id_list1:
        pool.free(_id)

    # Verify that nothing is used in the pool
    assert len(pool._used) == 0  # pylint: disable=protected-access

    # Exhaust the pool
    id_list2 = []
    for _ in range(start, end):
        _id = pool.alloc()
        id_list2.append(_id)

    # Verify that the same ID values came back as last time
    assert set(id_list1) == set(id_list2)

    # Verify that the pool is exhausted
    with pytest.raises(ValueError):
        pool.alloc()
    def test_init_error_1(self):

        with pytest.raises(ValueError):
            IdPool(7, 6)