1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
import pytest
from pyroute2.common import AddrPool
def test_alloc_aligned():
ap = AddrPool(minaddr=1, maxaddr=1024)
for i in range(1024):
ap.alloc()
with pytest.raises(KeyError):
ap.alloc()
def test_alloc_odd():
ap = AddrPool(minaddr=1, maxaddr=1020)
for i in range(1020):
ap.alloc()
with pytest.raises(KeyError):
ap.alloc()
def test_reverse():
ap = AddrPool(minaddr=1, maxaddr=1024, reverse=True)
for i in range(512):
assert ap.alloc() > ap.alloc()
def test_free():
ap = AddrPool(minaddr=1, maxaddr=1024)
f = ap.alloc()
ap.free(f)
def test_free_fail():
ap = AddrPool(minaddr=1, maxaddr=1024)
with pytest.raises(KeyError):
ap.free(0)
def test_free_reverse_fail():
ap = AddrPool(minaddr=1, maxaddr=1024, reverse=True)
with pytest.raises(KeyError):
ap.free(0)
def test_locate():
ap = AddrPool()
f = ap.alloc()
base1, bit1, is_allocated1 = ap.locate(f)
base2, bit2, is_allocated2 = ap.locate(f + 1)
assert base1 == base2
assert bit2 == bit1 + 1
assert is_allocated1
assert not is_allocated2
assert ap.allocated == 1
def test_setaddr_allocated():
ap = AddrPool()
f = ap.alloc()
base, bit, is_allocated = ap.locate(f + 1)
assert not is_allocated
assert ap.allocated == 1
ap.setaddr(f + 1, 'allocated')
base, bit, is_allocated = ap.locate(f + 1)
assert is_allocated
assert ap.allocated == 2
ap.free(f + 1)
base, bit, is_allocated = ap.locate(f + 1)
assert not is_allocated
assert ap.allocated == 1
def test_setaddr_free():
ap = AddrPool()
f = ap.alloc()
base, bit, is_allocated = ap.locate(f + 1)
assert not is_allocated
assert ap.allocated == 1
ap.setaddr(f + 1, 'free')
base, bit, is_allocated = ap.locate(f + 1)
assert not is_allocated
assert ap.allocated == 1
ap.setaddr(f, 'free')
base, bit, is_allocated = ap.locate(f)
assert not is_allocated
assert ap.allocated == 0
with pytest.raises(KeyError):
ap.free(f)
|