File: test_rweaklist.py

package info (click to toggle)
pypy 2.4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 86,992 kB
  • ctags: 170,715
  • sloc: python: 1,030,417; ansic: 43,437; cpp: 5,241; asm: 5,169; sh: 458; makefile: 408; xml: 231; lisp: 45
file content (72 lines) | stat: -rw-r--r-- 1,834 bytes parent folder | download
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
import gc
from rpython.rlib.rweaklist import RWeakListMixin, _reduced_value as reduced_value


def test_reduced_value():
    assert reduced_value(0) == 0
    assert reduced_value(1) == 0
    assert reduced_value(2) == 1
    assert reduced_value(3) == 0
    assert reduced_value(4) == 2
    assert reduced_value(5) == 1
    assert reduced_value(6) == 3
    assert reduced_value(7) == 0
    assert reduced_value(8) == 4
    assert reduced_value(9) == 2
    assert reduced_value(10) == 5
    assert reduced_value(11) == 1


class A(object):
    pass


def test_simple():
    a1 = A(); a2 = A()
    wlist = RWeakListMixin(); wlist.initialize()
    i = wlist.add_handle(a1)
    assert i == 0
    i = wlist.reserve_next_handle_index()
    assert i == 1
    wlist.store_handle(i, a2)
    assert wlist.fetch_handle(0) is a1
    assert wlist.fetch_handle(1) is a2
    #
    del a2
    for i in range(5):
        gc.collect()
        if wlist.fetch_handle(1) is None:
            break
    else:
        raise AssertionError("handle(1) did not disappear")
    assert wlist.fetch_handle(0) is a1

def test_reuse():
    alist = [A() for i in range(200)]
    wlist = RWeakListMixin(); wlist.initialize()
    for i in range(200):
        j = wlist.reserve_next_handle_index()
        assert j == i
        wlist.store_handle(i, alist[i])
    #
    del alist[1::2]
    del alist[1::2]
    del alist[1::2]
    del alist[1::2]
    del alist[1::2]
    for i in range(5):
        gc.collect()
    #
    for i in range(200):
        a = wlist.fetch_handle(i)
        if i % 32 == 0:
            assert a is alist[i // 32]
        else:
            assert a is None
    #
    maximum = -1
    for i in range(200):
        j = wlist.reserve_next_handle_index()
        maximum = max(maximum, j)
        wlist.store_handle(j, A())
    assert maximum <= 240