1
from pypy.interpreter.error import OperationError
2
from pypy.objspace.std.dictmultiobject import \
3
W_DictMultiObject, setitem__DictMulti_ANY_ANY, getitem__DictMulti_ANY, \
4
EmptyDictImplementation, RDictImplementation, StrDictImplementation, \
5
SmallDictImplementation, SmallStrDictImplementation, MeasuringDictImplementation
6
from pypy.conftest import gettestobjspace
7
from pypy.objspace.std.test import test_dictobject
9
class TestW_DictMultiObject(test_dictobject.TestW_DictObject):
11
cls.space = gettestobjspace(**{"objspace.std.withmultidict": True})
13
class AppTest_DictMultiObject(test_dictobject.AppTest_DictObject):
15
cls.space = gettestobjspace(**{"objspace.std.withmultidict": True})
17
def test_len_iter(self):
28
raises(StopIteration, i.next)
29
raises(StopIteration, i.next)
31
class TestW_DictSharing(test_dictobject.TestW_DictObject):
33
cls.space = gettestobjspace(**{"objspace.std.withsharingdict": True})
35
class AppTest_DictSharing(test_dictobject.AppTest_DictObject):
37
cls.space = gettestobjspace(**{"objspace.std.withsharingdict": True})
39
def test_values_does_not_share(self):
44
l = a.__dict__.values()
54
a.__dict__.items() == [("abc", 12)]
57
class TestW_DictSmall(test_dictobject.TestW_DictObject):
59
cls.space = gettestobjspace(**{"objspace.std.withsmalldicts": True})
61
class AppTest_DictSmall(test_dictobject.AppTest_DictObject):
63
cls.space = gettestobjspace(**{"objspace.std.withsmalldicts": True})
67
class FakeSpace(test_dictobject.FakeSpace):
68
def str_w(self, string):
69
assert isinstance(string, str)
75
def isinstance(self, obj, klass):
76
return isinstance(obj, klass)
78
def newtuple(self, l):
81
w_StopIteration = StopIteration
84
class TestDictImplementation:
85
def setup_method(self,method):
86
self.space = FakeSpace()
87
self.space.emptydictimpl = EmptyDictImplementation(self.space)
88
self.space.DictObjectCls = W_DictMultiObject
89
self.space.DefaultDictImpl = RDictImplementation
91
def test_stressdict(self):
92
from random import randint
93
d = self.space.DictObjectCls(self.space)
98
setitem__DictMulti_ANY_ANY(self.space, d, x, i)
101
assert pydict[x] == getitem__DictMulti_ANY(self.space, d, x)
103
class TestRDictImplementation:
104
ImplementionClass = RDictImplementation
105
DevolvedClass = RDictImplementation
106
EmptyClass = EmptyDictImplementation
107
DefaultDictImpl = RDictImplementation
109
def setup_method(self,method):
110
self.space = FakeSpace()
111
self.space.DictObjectCls = W_DictMultiObject
112
self.space.emptydictimpl = EmptyDictImplementation(self.space)
113
self.space.DefaultDictImpl = self.DefaultDictImpl
114
self.string = self.space.wrap("fish")
115
self.string2 = self.space.wrap("fish2")
116
self.impl = self.get_impl()
119
"Needs to be empty, or one entry with key self.string"
120
return self.ImplementionClass(self.space)
122
def test_setitem(self):
123
assert self.impl.setitem(self.string, 1000) is self.impl
124
assert self.impl.length() == 1
125
assert self.impl.get(self.string) == 1000
127
def test_setitem_str(self):
128
assert self.impl.setitem_str(self.space.str_w(self.string), 1000) is self.impl
129
assert self.impl.length() == 1
130
assert self.impl.get(self.string) == 1000
132
def test_delitem(self):
133
self.impl.setitem(self.string, 1000)
134
self.impl.setitem(self.string2, 2000)
135
assert self.impl.length() == 2
136
newimpl = self.impl.delitem(self.string)
137
assert self.impl.length() == 1
138
assert newimpl is self.impl
139
newimpl = self.impl.delitem(self.string2)
140
assert self.impl.length() == 0
141
assert isinstance(newimpl, self.EmptyClass)
144
self.impl.setitem(self.string, 1000)
145
self.impl.setitem(self.string2, 2000)
146
keys = self.impl.keys()
148
assert keys == [self.string, self.string2]
150
def test_values(self):
151
self.impl.setitem(self.string, 1000)
152
self.impl.setitem(self.string2, 2000)
153
values = self.impl.values()
155
assert values == [1000, 2000]
157
def test_items(self):
158
self.impl.setitem(self.string, 1000)
159
self.impl.setitem(self.string2, 2000)
160
items = self.impl.items()
162
assert items == zip([self.string, self.string2], [1000, 2000])
164
def test_iterkeys(self):
165
self.impl.setitem(self.string, 1000)
166
self.impl.setitem(self.string2, 2000)
167
iteratorimplementation = self.impl.iterkeys()
170
key = iteratorimplementation.next()
175
assert keys == [self.string, self.string2]
177
def test_itervalues(self):
178
self.impl.setitem(self.string, 1000)
179
self.impl.setitem(self.string2, 2000)
180
iteratorimplementation = self.impl.itervalues()
183
value = iteratorimplementation.next()
188
assert values == [1000, 2000]
190
def test_iteritems(self):
191
self.impl.setitem(self.string, 1000)
192
self.impl.setitem(self.string2, 2000)
193
iteratorimplementation = self.impl.iteritems()
196
item = iteratorimplementation.next()
201
assert items == zip([self.string, self.string2], [1000, 2000])
203
def test_devolve(self):
205
for x in xrange(100):
206
impl = impl.setitem(self.space.str_w(str(x)), x)
207
impl = impl.setitem(x, x)
208
assert isinstance(impl, self.DevolvedClass)
210
class TestStrDictImplementation(TestRDictImplementation):
211
ImplementionClass = StrDictImplementation
213
class TestSmallDictImplementation(TestRDictImplementation):
214
ImplementionClass = SmallDictImplementation
217
return self.ImplementionClass(self.space, self.string, self.string2)
219
class TestMeasuringDictImplementation(TestRDictImplementation):
220
ImplementionClass = MeasuringDictImplementation
221
DevolvedClass = MeasuringDictImplementation
222
EmptyClass = MeasuringDictImplementation
224
class TestSmallStrDictImplementation(TestRDictImplementation):
225
ImplementionClass = SmallStrDictImplementation
228
return self.ImplementionClass(self.space, self.string, self.string2)