File: test_rweakvaldict.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 (146 lines) | stat: -rw-r--r-- 3,942 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import py
from rpython.rlib import rgc
from rpython.rlib.rweakref import RWeakValueDictionary
from rpython.rtyper.test.test_llinterp import interpret

class X(object):
    pass

class Y(X):
    pass


def make_test(loop=100, keyclass=str):
    if keyclass is str:
        make_key = str
        keys = ["abc", "def", "ghi", "hello"]
    elif keyclass is int:
        make_key = int
        keys = [123, 456, 789, 1234]

    def g(d):
        assert d.get(keys[3]) is None
        x1 = X(); x2 = X(); x3 = X()
        d.set(keys[0], x1)
        d.set(keys[1], x2)
        d.set(keys[2], x3)
        assert d.get(keys[0]) is x1
        assert d.get(keys[1]) is x2
        assert d.get(keys[2]) is x3
        assert d.get(keys[3]) is None
        return x1, x3    # x2 dies
    def f():
        d = RWeakValueDictionary(keyclass, X)
        x1, x3 = g(d)
        rgc.collect(); rgc.collect()
        assert d.get(keys[0]) is x1
        assert d.get(keys[1]) is None
        assert d.get(keys[2]) is x3
        assert d.get(keys[3]) is None
        d.set(keys[0], None)
        assert d.get(keys[0]) is None
        assert d.get(keys[1]) is None
        assert d.get(keys[2]) is x3
        assert d.get(keys[3]) is None
        # resizing should also work
        for i in range(loop):
            d.set(make_key(i), x1)
        for i in range(loop):
            assert d.get(make_key(i)) is x1
        assert d.get(keys[0]) is None
        assert d.get(keys[1]) is None
        assert d.get(keys[2]) is x3
        assert d.get(keys[3]) is None
        # a subclass
        y = Y()
        d.set(keys[3], y)
        assert d.get(keys[3]) is y
        # storing a lot of Nones
        for i in range(loop, loop*2-5):
            d.set(make_key(1000 + i), x1)
        for i in range(loop):
            d.set(make_key(i), None)
        for i in range(loop):
            assert d.get(make_key(i)) is None
        assert d.get(keys[0]) is None
        assert d.get(keys[1]) is None
        assert d.get(keys[2]) is x3
        assert d.get(keys[3]) is y
        for i in range(loop, loop*2-5):
            assert d.get(make_key(1000 + i)) is x1
    return f

def test_RWeakValueDictionary():
    make_test()()

def test_RWeakValueDictionary_int():
    make_test(keyclass=int)()

def test_rpython_RWeakValueDictionary():
    interpret(make_test(loop=12), [])

def test_rpython_RWeakValueDictionary_int():
    interpret(make_test(loop=12, keyclass=int), [])

def test_rpython_prebuilt():
    d = RWeakValueDictionary(str, X)
    living = [X() for i in range(8)]
    for i in range(8):
        d.set(str(i), living[i])
    #
    def f():
        x = X()
        for i in range(8, 13):
            d.set(str(i), x)
        for i in range(0, 8):
            assert d.get(str(i)) is living[i]
        for i in range(8, 13):
            assert d.get(str(i)) is x
        assert d.get("foobar") is None
    #
    f()
    interpret(f, [])

def test_rpython_merge_RWeakValueDictionary():
    empty = RWeakValueDictionary(str, X)
    def f(n):
        x = X()
        if n:
            d = empty
        else:
            d = RWeakValueDictionary(str, X)
            d.set("a", x)
        return d.get("a") is x
    assert f(0)
    assert interpret(f, [0])
    assert not f(1)
    assert not interpret(f, [1])


def test_rpython_merge_RWeakValueDictionary2():
    class A(object):
        def __init__(self):
            self.d = RWeakValueDictionary(str, A)
        def f(self, key):
            a = A()
            self.d.set(key, a)
            return a
    empty = A()
    def f(x):
        a = A()
        if x:
            a = empty
        a2 = a.f("a")
        assert a.d.get("a") is a2
    f(0)
    interpret(f, [0])
    f(1)
    interpret(f, [1])

    def g(x):
        if x:
            d = RWeakValueDictionary(str, X)
        else:
            d = RWeakValueDictionary(str, Y)
        d.set("x", X())
    py.test.raises(Exception, interpret, g, [1])