~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/objspace/std/test/test_dictmultiobject.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
8
 
 
9
class TestW_DictMultiObject(test_dictobject.TestW_DictObject):
 
10
    def setup_class(cls):
 
11
        cls.space = gettestobjspace(**{"objspace.std.withmultidict": True})
 
12
 
 
13
class AppTest_DictMultiObject(test_dictobject.AppTest_DictObject):
 
14
    def setup_class(cls):
 
15
        cls.space = gettestobjspace(**{"objspace.std.withmultidict": True})
 
16
 
 
17
    def test_len_iter(self):
 
18
        d = {1: 2, 3: 4}
 
19
        i = iter(d)
 
20
        assert len(i) == 2
 
21
        x = i.next()
 
22
        assert len(i) == 1
 
23
        y = i.next()
 
24
        assert len(i) == 0
 
25
        l = [x, y]
 
26
        l.sort()
 
27
        assert l == [1, 3]
 
28
        raises(StopIteration, i.next)
 
29
        raises(StopIteration, i.next)
 
30
 
 
31
class TestW_DictSharing(test_dictobject.TestW_DictObject):
 
32
    def setup_class(cls):
 
33
        cls.space = gettestobjspace(**{"objspace.std.withsharingdict": True})
 
34
 
 
35
class AppTest_DictSharing(test_dictobject.AppTest_DictObject):
 
36
    def setup_class(cls):
 
37
        cls.space = gettestobjspace(**{"objspace.std.withsharingdict": True})
 
38
 
 
39
    def test_values_does_not_share(self):
 
40
        class A(object):
 
41
            pass
 
42
        a = A()
 
43
        a.abc = 12
 
44
        l = a.__dict__.values()
 
45
        assert l == [12]
 
46
        l[0] = 24
 
47
        assert a.abc == 12
 
48
 
 
49
    def test_items(self):
 
50
        class A(object):
 
51
            pass
 
52
        a = A()
 
53
        a.abc = 12
 
54
        a.__dict__.items() == [("abc", 12)]
 
55
 
 
56
 
 
57
class TestW_DictSmall(test_dictobject.TestW_DictObject):
 
58
    def setup_class(cls):
 
59
        cls.space = gettestobjspace(**{"objspace.std.withsmalldicts": True})
 
60
 
 
61
class AppTest_DictSmall(test_dictobject.AppTest_DictObject):
 
62
    def setup_class(cls):
 
63
        cls.space = gettestobjspace(**{"objspace.std.withsmalldicts": True})
 
64
 
 
65
class C: pass
 
66
 
 
67
class FakeSpace(test_dictobject.FakeSpace):
 
68
    def str_w(self, string):
 
69
        assert isinstance(string, str)
 
70
        return string
 
71
 
 
72
    def wrap(self, obj):
 
73
        return obj
 
74
 
 
75
    def isinstance(self, obj, klass):
 
76
        return isinstance(obj, klass)
 
77
 
 
78
    def newtuple(self, l):
 
79
        return tuple(l)
 
80
 
 
81
    w_StopIteration = StopIteration
 
82
    w_None = None
 
83
 
 
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
 
90
 
 
91
    def test_stressdict(self):
 
92
        from random import randint
 
93
        d = self.space.DictObjectCls(self.space)
 
94
        N = 10000
 
95
        pydict = {}
 
96
        for i in range(N):
 
97
            x = randint(-N, N)
 
98
            setitem__DictMulti_ANY_ANY(self.space, d, x, i)
 
99
            pydict[x] = i
 
100
        for x in pydict:
 
101
            assert pydict[x] == getitem__DictMulti_ANY(self.space, d, x)
 
102
 
 
103
class TestRDictImplementation:
 
104
    ImplementionClass = RDictImplementation
 
105
    DevolvedClass = RDictImplementation
 
106
    EmptyClass = EmptyDictImplementation
 
107
    DefaultDictImpl = RDictImplementation
 
108
 
 
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()
 
117
 
 
118
    def get_impl(self):
 
119
        "Needs to be empty, or one entry with key self.string"
 
120
        return self.ImplementionClass(self.space)
 
121
 
 
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
 
126
 
 
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
 
131
 
 
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)
 
142
 
 
143
    def test_keys(self):
 
144
        self.impl.setitem(self.string, 1000)
 
145
        self.impl.setitem(self.string2, 2000)
 
146
        keys = self.impl.keys()
 
147
        keys.sort()
 
148
        assert keys == [self.string, self.string2]
 
149
 
 
150
    def test_values(self):
 
151
        self.impl.setitem(self.string, 1000)
 
152
        self.impl.setitem(self.string2, 2000)
 
153
        values = self.impl.values()
 
154
        values.sort()
 
155
        assert values == [1000, 2000]
 
156
 
 
157
    def test_items(self):
 
158
        self.impl.setitem(self.string, 1000)
 
159
        self.impl.setitem(self.string2, 2000)
 
160
        items = self.impl.items()
 
161
        items.sort()
 
162
        assert items == zip([self.string, self.string2], [1000, 2000])
 
163
 
 
164
    def test_iterkeys(self):
 
165
        self.impl.setitem(self.string, 1000)
 
166
        self.impl.setitem(self.string2, 2000)
 
167
        iteratorimplementation = self.impl.iterkeys()
 
168
        keys = []
 
169
        while 1:
 
170
            key = iteratorimplementation.next()
 
171
            if key is None:
 
172
                break
 
173
            keys.append(key)
 
174
        keys.sort()
 
175
        assert keys == [self.string, self.string2]
 
176
 
 
177
    def test_itervalues(self):
 
178
        self.impl.setitem(self.string, 1000)
 
179
        self.impl.setitem(self.string2, 2000)
 
180
        iteratorimplementation = self.impl.itervalues()
 
181
        values = []
 
182
        while 1:
 
183
            value = iteratorimplementation.next()
 
184
            if value is None:
 
185
                break
 
186
            values.append(value)
 
187
        values.sort()
 
188
        assert values == [1000, 2000]
 
189
 
 
190
    def test_iteritems(self):
 
191
        self.impl.setitem(self.string, 1000)
 
192
        self.impl.setitem(self.string2, 2000)
 
193
        iteratorimplementation = self.impl.iteritems()
 
194
        items = []
 
195
        while 1:
 
196
            item = iteratorimplementation.next()
 
197
            if item is None:
 
198
                break
 
199
            items.append(item)
 
200
        items.sort()
 
201
        assert items == zip([self.string, self.string2], [1000, 2000])
 
202
 
 
203
    def test_devolve(self):
 
204
        impl = self.impl
 
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)
 
209
 
 
210
class TestStrDictImplementation(TestRDictImplementation):
 
211
    ImplementionClass = StrDictImplementation
 
212
 
 
213
class TestSmallDictImplementation(TestRDictImplementation):
 
214
    ImplementionClass = SmallDictImplementation
 
215
 
 
216
    def get_impl(self):
 
217
        return self.ImplementionClass(self.space, self.string, self.string2)
 
218
 
 
219
class TestMeasuringDictImplementation(TestRDictImplementation):
 
220
    ImplementionClass = MeasuringDictImplementation
 
221
    DevolvedClass = MeasuringDictImplementation
 
222
    EmptyClass = MeasuringDictImplementation
 
223
 
 
224
class TestSmallStrDictImplementation(TestRDictImplementation):
 
225
    ImplementionClass = SmallStrDictImplementation
 
226
 
 
227
    def get_impl(self):
 
228
        return self.ImplementionClass(self.space, self.string, self.string2)