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

« back to all changes in this revision

Viewing changes to pypy/rpython/ootypesystem/test/test_ooann.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
import py
 
2
from pypy.rpython.ootypesystem.ootype import *
 
3
from pypy.annotation import model as annmodel
 
4
from pypy.objspace.flow import FlowObjSpace
 
5
from pypy.annotation.annrpython import RPythonAnnotator
 
6
import exceptions
 
7
from pypy.rpython.ootypesystem import ooregistry # side effects
 
8
 
 
9
 
 
10
def test_simple_new():
 
11
    C = Instance("test", ROOT, {'a': Signed})
 
12
    
 
13
    def oof():
 
14
        c = new(C)
 
15
        c.a = 5
 
16
        return c.a
 
17
 
 
18
    a = RPythonAnnotator()
 
19
    s = a.build_types(oof, [])
 
20
    #a.translator.view()
 
21
 
 
22
    assert s.knowntype == int
 
23
 
 
24
def test_simple_instanceof():
 
25
    C = Instance("test", ROOT, {'a': Signed})
 
26
    
 
27
    def oof():
 
28
        c = new(C)
 
29
        return instanceof(c, C)
 
30
 
 
31
    a = RPythonAnnotator()
 
32
    s = a.build_types(oof, [])
 
33
    #a.translator.view()
 
34
 
 
35
    assert s.knowntype == bool
 
36
 
 
37
def test_simple_null():
 
38
    I = Instance("test", ROOT, {'a': Signed})
 
39
    
 
40
    def oof():
 
41
        i = null(I)
 
42
        return i
 
43
 
 
44
    a = RPythonAnnotator()
 
45
    s = a.build_types(oof, [])
 
46
    #a.translator.view()
 
47
 
 
48
    assert s == annmodel.SomeOOInstance(I)
 
49
 
 
50
def test_simple_classof():
 
51
    I = Instance("test", ROOT, {'a': Signed})
 
52
    
 
53
    def oof():
 
54
        i = new(I)
 
55
        return classof(i)
 
56
 
 
57
    a = RPythonAnnotator()
 
58
    s = a.build_types(oof, [])
 
59
    #a.translator.view()
 
60
 
 
61
    assert s == annmodel.SomeOOClass(I)
 
62
 
 
63
def test_subclassof():
 
64
    I = Instance("test", ROOT, {'a': Signed})
 
65
    I1 = Instance("test1", I) 
 
66
    
 
67
    def oof():
 
68
        i = new(I)
 
69
        i1 = new(I1)
 
70
        return subclassof(classof(i1), classof(i))
 
71
 
 
72
    a = RPythonAnnotator()
 
73
    s = a.build_types(oof, [])
 
74
    #a.translator.view()
 
75
 
 
76
    assert s == annmodel.SomeBool()
 
77
 
 
78
def test_simple_runtimenew():
 
79
    I = Instance("test", ROOT, {'a': Signed})
 
80
    
 
81
    def oof():
 
82
        i = new(I)
 
83
        c = classof(i)
 
84
        i2 = runtimenew(c)
 
85
        return i2
 
86
 
 
87
    a = RPythonAnnotator()
 
88
    s = a.build_types(oof, [])
 
89
    #a.translator.view()
 
90
 
 
91
    assert s == annmodel.SomeOOInstance(I)
 
92
 
 
93
def test_complex_runtimenew():
 
94
    I = Instance("test", ROOT, {'a': Signed})
 
95
    J = Instance("test2", I, {'b': Signed})
 
96
    K = Instance("test2", I, {'b': Signed})
 
97
    
 
98
    def oof(x):
 
99
        k = new(K)
 
100
        j = new(J)
 
101
        if x:
 
102
            c = classof(k)
 
103
        else:
 
104
            c = classof(j)
 
105
        i = runtimenew(c)
 
106
        return i
 
107
 
 
108
    a = RPythonAnnotator()
 
109
    s = a.build_types(oof, [bool])
 
110
    #a.translator.view()
 
111
 
 
112
    assert s == annmodel.SomeOOInstance(I)
 
113
 
 
114
def test_method():
 
115
    C = Instance("test", ROOT, {"a": (Signed, 3)})
 
116
 
 
117
    M = Meth([C], Signed)
 
118
    def m_(self, other):
 
119
       return self.a + other.a
 
120
    m = meth(M, _name="m", _callable=m_)
 
121
 
 
122
    addMethods(C, {"m": m})
 
123
 
 
124
    def oof():
 
125
        c = new(C)
 
126
        return c.m(c)
 
127
    
 
128
    a = RPythonAnnotator()
 
129
    s = a.build_types(oof, [])
 
130
    # a.translator.view()
 
131
 
 
132
    assert s.knowntype == int
 
133
 
 
134
def test_unionof():
 
135
    C1 = Instance("C1", ROOT)
 
136
    C2 = Instance("C2", C1)
 
137
    C3 = Instance("C3", C1)
 
138
 
 
139
    def oof(f):
 
140
        if f:
 
141
            c = new(C2)
 
142
        else:
 
143
            c = new(C3)
 
144
        return c
 
145
 
 
146
    a = RPythonAnnotator()
 
147
    s = a.build_types(oof, [bool])
 
148
    #a.translator.view()
 
149
 
 
150
    assert s == annmodel.SomeOOInstance(C1)
 
151
 
 
152
def test_static_method():
 
153
    F = StaticMethod([Signed, Signed], Signed)
 
154
    def f_(a, b):
 
155
       return a+b
 
156
    f = static_meth(F, "f", _callable=f_)
 
157
 
 
158
    def oof():
 
159
        return f(2,3)
 
160
 
 
161
    a = RPythonAnnotator()
 
162
    s = a.build_types(oof, [])
 
163
    #a.translator.view()
 
164
 
 
165
    assert s.knowntype == int
 
166
 
 
167
def test_null_static_method():
 
168
    F = StaticMethod([Signed, Signed], Signed)
 
169
 
 
170
    def oof():
 
171
        return null(F)
 
172
 
 
173
    a = RPythonAnnotator()
 
174
    s = a.build_types(oof, [])
 
175
    
 
176
    assert s == annmodel.SomeOOStaticMeth(F)
 
177
 
 
178
def test_truth_value():
 
179
    C = Instance("C", ROOT)
 
180
    def oof(f):
 
181
        if f:
 
182
            c = new(C)
 
183
        else:
 
184
            c = null(C)
 
185
        return not c
 
186
 
 
187
    a = RPythonAnnotator()
 
188
    s = a.build_types(oof, [bool])
 
189
    assert isinstance(s, annmodel.SomeBool)
 
190
    assert not s.is_constant()
 
191
 
 
192
def test_list():
 
193
    L = List(Signed)
 
194
    def oof():
 
195
        l = new(L)
 
196
        l._ll_resize(42)
 
197
        return l
 
198
 
 
199
    a = RPythonAnnotator()
 
200
    s = a.build_types(oof, [])
 
201
    #a.translator.view()
 
202
 
 
203
    assert s == annmodel.SomeOOInstance(L)
 
204
 
 
205
def test_string():
 
206
    def oof():
 
207
        return new(String)
 
208
 
 
209
    a = RPythonAnnotator()
 
210
    s = a.build_types(oof, [])
 
211
    assert s == annmodel.SomeOOInstance(String)
 
212
 
 
213
def test_nullstring():
 
214
    def oof(b):
 
215
        if b:
 
216
            return 'foo'
 
217
        else:
 
218
            return None
 
219
 
 
220
    a = RPythonAnnotator()
 
221
    s = a.build_types(oof, [bool])
 
222
    assert s == annmodel.SomeString(can_be_None=True)
 
223
 
 
224
def test_oostring():
 
225
    def oof():
 
226
        return oostring
 
227
 
 
228
    a = RPythonAnnotator()
 
229
    s = a.build_types(oof, [])
 
230
    assert isinstance(s, annmodel.SomeBuiltin)
 
231
 
 
232
def test_ooparse_int():
 
233
    def oof(n, b):
 
234
        return ooparse_int(oostring(n, b), b)
 
235
 
 
236
    a = RPythonAnnotator()
 
237
    s = a.build_types(oof, [int, int])
 
238
    assert isinstance(s, annmodel.SomeInteger)
 
239
 
 
240
def test_overloaded_meth():
 
241
    C = Instance("test", ROOT, {},
 
242
                 {'foo': overload(meth(Meth([Float], Void)),
 
243
                                  meth(Meth([Signed], Signed)),
 
244
                                  meth(Meth([], Float)))})
 
245
    def fn1():
 
246
        return new(C).foo(42.5)
 
247
    def fn2():
 
248
        return new(C).foo(42)
 
249
    def fn3():
 
250
        return new(C).foo()
 
251
    a = RPythonAnnotator()
 
252
    assert a.build_types(fn1, []) is annmodel.s_None
 
253
    assert isinstance(a.build_types(fn2, []), annmodel.SomeInteger)
 
254
    assert isinstance(a.build_types(fn3, []), annmodel.SomeFloat)
 
255
 
 
256
def test_bad_overload():
 
257
    def fn():
 
258
        C = Instance("test", ROOT, {},
 
259
                     {'foo': overload(meth(Meth([Signed], Void)),
 
260
                                      meth(Meth([Signed], Signed)))})
 
261
    py.test.raises(TypeError, fn)
 
262
 
 
263
 
 
264
def test_overload_reannotate():
 
265
    C = Instance("test", ROOT, {},
 
266
                 {'foo': overload(meth(Meth([Signed], Signed)),
 
267
                                  meth(Meth([Float], Float)))})
 
268
    def f():
 
269
        c = new(C)
 
270
        mylist = [42]
 
271
        a = c.foo(mylist[0])
 
272
        mylist.append(42.5)
 
273
        return a
 
274
    a = RPythonAnnotator()
 
275
    assert isinstance(a.build_types(f, []), annmodel.SomeFloat)
 
276
    
 
277
def test_overload_reannotate_unrelated():
 
278
    py.test.skip("Maybe we want this to work")
 
279
    # this test fails because the result type of c.foo(mylist[0])
 
280
    # changes completely after the list has been modified. We should
 
281
    # handle this case, but it's far from trival.
 
282
    C = Instance("test", ROOT, {},
 
283
                 {'foo': overload(meth(Meth([Signed], Void)),
 
284
                                  meth(Meth([Float], Float)))})
 
285
    def f():
 
286
        c = new(C)
 
287
        mylist = [42]
 
288
        a = c.foo(mylist[0])
 
289
        mylist.append(42.5)
 
290
        return a
 
291
    a = RPythonAnnotator()
 
292
    assert isinstance(a.build_types(f, []), annmodel.SomeFloat)
 
293
 
 
294
def test_overload_upcast():
 
295
    C = Instance("base", ROOT, {}, {
 
296
        'foo': overload(meth(Meth([], Void)),
 
297
                        meth(Meth([ROOT], Signed)))})
 
298
    def f():
 
299
        c = new(C)
 
300
        return c.foo(c)
 
301
    a = RPythonAnnotator()
 
302
    assert isinstance(a.build_types(f, []), annmodel.SomeInteger)
 
303
 
 
304
def test_overload_upcast_fail():
 
305
    C = Instance("base", ROOT, {}, {})
 
306
    C._add_methods({
 
307
        'foo': overload(meth(Meth([], Signed)),
 
308
                        meth(Meth([ROOT, C], Signed)),
 
309
                        meth(Meth([C, ROOT], Signed)))})
 
310
    def f():
 
311
        c = new(C)
 
312
        return c.foo(c)
 
313
    a = RPythonAnnotator()
 
314
    py.test.raises(TypeError, a.build_types, f, [])