Debian Codegraph @

rpython/rlib/test/test_rweakkeydict.py

raw symbols SCIP
1 import py
2 from rpython.annotator.model import UnionError
3 from rpython.rlib import rgc
4 from rpython.rlib.rweakref import RWeakKeyDictionary
5 from rpython.rtyper.test.test_llinterp import interpret
6
7 class KX(object):
8 pass
9
10 class KY(KX):
11 pass
12
13 class VX(object):
14 pass
15
16 class VY(VX):
17 pass
18
19
20 def make_test(loop=100, prebuilt=None):
21 def g(d):
22 assert d.get(KX()) is None
23 assert d.get(KY()) is None
24 k1 = KX(); k2 = KX(); k3 = KX()
25 v1 = VX(); v2 = VX(); v3 = VX()
26 d.set(k1, v1)
27 d.set(k2, v2)
28 d.set(k3, v3)
29 assert d.get(k1) is v1
30 assert d.get(k2) is v2
31 assert d.get(k3) is v3
32 assert d.get(KX()) is None
33 assert d.length() == 3
34 return k1, k3, v1, v2, v3 # k2 dies
35 def f():
36 d = prebuilt
37 if d is None:
38 d = RWeakKeyDictionary(KX, VX)
39 k1, k3, v1, v2, v3 = g(d)
40 rgc.collect(); rgc.collect()
41 assert d.get(k1) is v1
42 assert d.get(k3) is v3
43 assert d.get(k1) is not v2
44 assert d.get(k3) is not v2
45 assert d.length() == 2
46 d.set(k1, None)
47 assert d.get(k1) is None
48 assert d.get(k3) is v3
49 assert d.length() == 1
50 # resizing should also work
51 lots_of_keys = [KX() for i in range(loop)]
52 for k in lots_of_keys:
53 d.set(k, v1)
54 for k in lots_of_keys:
55 assert d.get(k) is v1
56 assert d.get(k1) is None
57 assert d.get(k3) is v3
58 assert d.length() == loop + 1
59 # a subclass
60 ky = KY()
61 vy = VY()
62 d.set(ky, vy)
63 assert d.get(ky) is vy
64 assert d.length() == loop + 2
65 # deleting by storing Nones
66 for k in lots_of_keys:
67 d.set(k, None)
68 for k in lots_of_keys:
69 assert d.get(k) is None
70 assert d.get(k1) is None
71 assert d.get(k3) is v3
72 assert d.get(ky) is vy
73 assert d.length() == 2
74 return f
75
76 def test_RWeakKeyDictionary():
77 make_test()()
78
79 def test_rpython_RWeakKeyDictionary():
80 interpret(make_test(loop=12), [])
81
82 def test_rpython_prebuilt():
83 f = make_test(loop=12, prebuilt=RWeakKeyDictionary(KX, VX))
84 interpret(f, [])
85
86 def test_rpython_merge_RWeakKeyDictionary():
87 empty = RWeakKeyDictionary(KX, VX)
88 def f(n):
89 k = KX()
90 v = VX()
91 if n:
92 d = empty
93 else:
94 d = RWeakKeyDictionary(KX, VX)
95 d.set(k, v)
96 return d.get(k) is v
97 assert f(0)
98 assert interpret(f, [0])
99 assert not f(1)
100 assert not interpret(f, [1])
101
102
103 def test_rpython_merge_RWeakKeyDictionary2():
104 class A(object):
105 def __init__(self):
106 self.d = RWeakKeyDictionary(KX, A)
107 def f(self, key):
108 a = A()
109 self.d.set(key, a)
110 return a
111 empty = A()
112 def f(x):
113 a = A()
114 if x:
115 a = empty
116 k = KX()
117 a2 = a.f(k)
118 assert a.d.get(k) is a2
119 f(0)
120 interpret(f, [0])
121 f(1)
122 interpret(f, [1])
123
124 @py.test.mark.xfail(
125 reason="may fail with AssertionError, depending on annotation order")
126 def test_rpython_merge_RWeakKeyDictionary3():
127 def g(x):
128 if x:
129 d = RWeakKeyDictionary(KX, VX)
130 else:
131 d = RWeakKeyDictionary(KY, VX)
132 d.set(KX(), VX())
133
134 with py.test.raises(UnionError):
135 interpret(g, [1])
136
137 @py.test.mark.xfail(
138 reason="may fail with AssertionError, depending on annotation order")
139 def test_rpython_merge_RWeakKeyDictionary4():
140 def g(x):
141 if x:
142 d = RWeakKeyDictionary(KX, VX)
143 else:
144 d = RWeakKeyDictionary(KX, VY)
145 d.set(KX(), VX())
146
147 with py.test.raises(UnionError):
148 interpret(g, [1])
149
150 @py.test.mark.xfail(reason="not implemented, messy")
151 def test_rpython_free_values():
152 class VXDel:
153 def __del__(self):
154 state.freed.append(1)
155 class State:
156 pass
157 state = State()
158 state.freed = []
159 #
160 def add_me():
161 k = KX()
162 v = VXDel()
163 d = RWeakKeyDictionary(KX, VXDel)
164 d.set(k, v)
165 return d
166 def f():
167 del state.freed[:]
168 d = add_me()
169 rgc.collect()
170 # we want the dictionary to be really empty here. It's hard to
171 # ensure in the current implementation after just one collect(),
172 # but at least two collects should be enough.
173 rgc.collect()
174 return len(state.freed)
175 assert f() == 1
176 assert interpret(f, []) == 1