| 1 |
import gc |
| 2 |
from rpython.rlib.rweaklist import RWeakListMixin, INITIAL_SIZE |
| 3 |
|
| 4 |
|
| 5 |
class A(object): |
| 6 |
pass |
| 7 |
|
| 8 |
|
| 9 |
def test_simple(): |
| 10 |
a1 = A(); a2 = A() |
| 11 |
wlist = RWeakListMixin(); wlist.initialize() |
| 12 |
i = wlist.add_handle(a1) |
| 13 |
assert i == INITIAL_SIZE - 1 |
| 14 |
i = wlist.reserve_next_handle_index() |
| 15 |
assert i == INITIAL_SIZE - 2 |
| 16 |
wlist.store_handle(i, a2) |
| 17 |
assert wlist.fetch_handle(INITIAL_SIZE - 1) is a1 |
| 18 |
assert wlist.fetch_handle(INITIAL_SIZE - 2) is a2 |
| 19 |
|
| 20 |
del a2 |
| 21 |
for i in range(5): |
| 22 |
gc.collect() |
| 23 |
if wlist.fetch_handle(INITIAL_SIZE - 2) is None: |
| 24 |
break |
| 25 |
else: |
| 26 |
raise AssertionError("second handle() did not disappear") |
| 27 |
assert wlist.fetch_handle(INITIAL_SIZE - 1) is a1 |
| 28 |
|
| 29 |
def test_reuse(): |
| 30 |
alist = [A() for i in range(200)] |
| 31 |
wlist = RWeakListMixin(); wlist.initialize() |
| 32 |
mapping = [] |
| 33 |
for i in range(200): |
| 34 |
j = wlist.reserve_next_handle_index() |
| 35 |
mapping.append(j) |
| 36 |
wlist.store_handle(j, alist[i]) |
| 37 |
|
| 38 |
del alist[1::2] |
| 39 |
del alist[1::2] |
| 40 |
del alist[1::2] |
| 41 |
del alist[1::2] |
| 42 |
del alist[1::2] |
| 43 |
for i in range(5): |
| 44 |
gc.collect() |
| 45 |
|
| 46 |
for i, j in enumerate(mapping): |
| 47 |
a = wlist.fetch_handle(j) |
| 48 |
if i % 32 == 0: |
| 49 |
assert a is alist[i // 32] |
| 50 |
else: |
| 51 |
assert a is None |
| 52 |
|
| 53 |
maximum = -1 |
| 54 |
for i in range(200): |
| 55 |
j = wlist.reserve_next_handle_index() |
| 56 |
assert wlist.fetch_handle(j) is None |
| 57 |
maximum = max(maximum, j) |
| 58 |
wlist.store_handle(j, A()) |
| 59 |
assert maximum < 256 |