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

« back to all changes in this revision

Viewing changes to pypy/objspace/std/test/test_intobject.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 sys
 
2
from pypy.objspace.std import intobject as iobj
 
3
from pypy.objspace.std.objspace import FailedToImplement
 
4
from pypy.rlib.rarithmetic import r_uint
 
5
from pypy.rlib.rbigint import rbigint
 
6
 
 
7
 
 
8
class TestW_IntObject:
 
9
 
 
10
    def _longshiftresult(self, x):
 
11
        """ calculate an overflowing shift """
 
12
        n = 1
 
13
        l = long(x)
 
14
        while 1:
 
15
            ires = x << n
 
16
            lres = l << n
 
17
            if type(ires) is long or lres != ires:
 
18
                return n
 
19
            n += 1
 
20
 
 
21
    def _unwrap_nonimpl(self, func, *args, **kwds):
 
22
        """ make sure that the expected exception occours, and unwrap it """
 
23
        try:
 
24
            res = func(*args, **kwds)
 
25
            raise Exception, "should have failed but returned '%s'!" %repr(res)
 
26
        except FailedToImplement, arg:
 
27
            return arg.w_type
 
28
 
 
29
    def test_int_w(self):
 
30
        assert self.space.int_w(self.space.wrap(42)) == 42
 
31
 
 
32
    def test_uint_w(self):
 
33
        space = self.space
 
34
        assert space.uint_w(space.wrap(42)) == 42
 
35
        assert isinstance(space.uint_w(space.wrap(42)), r_uint)
 
36
        space.raises_w(space.w_ValueError, space.uint_w, space.wrap(-1))
 
37
 
 
38
    def test_bigint_w(self):
 
39
        space = self.space
 
40
        assert isinstance(space.bigint_w(space.wrap(42)), rbigint)
 
41
        assert space.bigint_w(space.wrap(42)).eq(rbigint.fromint(42))
 
42
        
 
43
    def test_repr(self):
 
44
        x = 1
 
45
        f1 = iobj.W_IntObject(x)
 
46
        result = iobj.repr__Int(self.space, f1)
 
47
        assert self.space.unwrap(result) == repr(x)
 
48
 
 
49
    def test_str(self):
 
50
        x = 12345
 
51
        f1 = iobj.W_IntObject(x)
 
52
        result = iobj.str__Int(self.space, f1)
 
53
        assert self.space.unwrap(result) == str(x)
 
54
 
 
55
    def test_hash(self):
 
56
        x = 42
 
57
        f1 = iobj.W_IntObject(x)
 
58
        result = iobj.hash__Int(self.space, f1)
 
59
        assert result.intval == hash(x)
 
60
 
 
61
    def test_compare(self):
 
62
        import operator
 
63
        optab = ['lt', 'le', 'eq', 'ne', 'gt', 'ge']
 
64
        for x in (-10, -1, 0, 1, 2, 1000, sys.maxint):
 
65
            for y in (-sys.maxint-1, -11, -9, -2, 0, 1, 3, 1111, sys.maxint):
 
66
                for op in optab:
 
67
                    wx = iobj.W_IntObject(x)
 
68
                    wy = iobj.W_IntObject(y)
 
69
                    res = getattr(operator, op)(x, y)
 
70
                    method = getattr(iobj, '%s__Int_Int' % op)
 
71
                    myres = method(self.space, wx, wy)
 
72
                    assert self.space.unwrap(myres) == res
 
73
                    
 
74
    def test_add(self):
 
75
        x = 1
 
76
        y = 2
 
77
        f1 = iobj.W_IntObject(x)
 
78
        f2 = iobj.W_IntObject(y)
 
79
        result = iobj.add__Int_Int(self.space, f1, f2)
 
80
        assert result.intval == x+y
 
81
        x = sys.maxint
 
82
        y = 1
 
83
        f1 = iobj.W_IntObject(x)
 
84
        f2 = iobj.W_IntObject(y)
 
85
        assert self.space.w_OverflowError == (
 
86
                          self._unwrap_nonimpl(iobj.add__Int_Int, self.space, f1, f2))
 
87
 
 
88
    def test_sub(self):
 
89
        x = 1
 
90
        y = 2
 
91
        f1 = iobj.W_IntObject(x)
 
92
        f2 = iobj.W_IntObject(y)
 
93
        result = iobj.sub__Int_Int(self.space, f1, f2)
 
94
        assert result.intval == x-y
 
95
        x = sys.maxint
 
96
        y = -1
 
97
        f1 = iobj.W_IntObject(x)
 
98
        f2 = iobj.W_IntObject(y)
 
99
        assert self.space.w_OverflowError == (
 
100
                          self._unwrap_nonimpl(iobj.sub__Int_Int, self.space, f1, f2))
 
101
 
 
102
    def test_mul(self):
 
103
        x = 2
 
104
        y = 3
 
105
        f1 = iobj.W_IntObject(x)
 
106
        f2 = iobj.W_IntObject(y)
 
107
        result = iobj.mul__Int_Int(self.space, f1, f2)
 
108
        assert result.intval == x*y
 
109
        x = -sys.maxint-1
 
110
        y = -1
 
111
        f1 = iobj.W_IntObject(x)
 
112
        f2 = iobj.W_IntObject(y)
 
113
        assert self.space.w_OverflowError == (
 
114
                          self._unwrap_nonimpl(iobj.mul__Int_Int, self.space, f1, f2))
 
115
 
 
116
    def test_div(self):
 
117
        for i in range(10):
 
118
            res = i//3
 
119
            f1 = iobj.W_IntObject(i)
 
120
            f2 = iobj.W_IntObject(3)
 
121
            result = iobj.div__Int_Int(self.space, f1, f2)
 
122
            assert result.intval == res
 
123
        x = -sys.maxint-1
 
124
        y = -1
 
125
        f1 = iobj.W_IntObject(x)
 
126
        f2 = iobj.W_IntObject(y)
 
127
        assert self.space.w_OverflowError == (
 
128
                          self._unwrap_nonimpl(iobj.div__Int_Int, self.space, f1, f2))
 
129
 
 
130
    def test_mod(self):
 
131
        x = 1
 
132
        y = 2
 
133
        f1 = iobj.W_IntObject(x)
 
134
        f2 = iobj.W_IntObject(y)
 
135
        v = iobj.mod__Int_Int(self.space, f1, f2)
 
136
        assert v.intval == x % y
 
137
        # not that mod cannot overflow
 
138
 
 
139
    def test_divmod(self):
 
140
        x = 1
 
141
        y = 2
 
142
        f1 = iobj.W_IntObject(x)
 
143
        f2 = iobj.W_IntObject(y)
 
144
        ret = iobj.divmod__Int_Int(self.space, f1, f2)
 
145
        v, w = self.space.unwrap(ret)
 
146
        assert (v, w) == divmod(x, y)
 
147
        x = -sys.maxint-1
 
148
        y = -1
 
149
        f1 = iobj.W_IntObject(x)
 
150
        f2 = iobj.W_IntObject(y)
 
151
        assert self.space.w_OverflowError == (
 
152
                          self._unwrap_nonimpl(iobj.divmod__Int_Int, self.space, f1, f2))
 
153
 
 
154
    def test_pow_iii(self):
 
155
        x = 10
 
156
        y = 2
 
157
        z = 13
 
158
        f1 = iobj.W_IntObject(x)
 
159
        f2 = iobj.W_IntObject(y)
 
160
        f3 = iobj.W_IntObject(z)
 
161
        v = iobj.pow__Int_Int_Int(self.space, f1, f2, f3)
 
162
        assert v.intval == pow(x, y, z)
 
163
        f1, f2, f3 = [iobj.W_IntObject(i) for i in (10, -1, 42)]
 
164
        self.space.raises_w(self.space.w_TypeError,
 
165
                            iobj.pow__Int_Int_Int,
 
166
                            self.space, f1, f2, f3)
 
167
        f1, f2, f3 = [iobj.W_IntObject(i) for i in (10, 5, 0)]
 
168
        self.space.raises_w(self.space.w_ValueError,
 
169
                            iobj.pow__Int_Int_Int,
 
170
                            self.space, f1, f2, f3)
 
171
 
 
172
    def test_pow_iin(self):
 
173
        x = 10
 
174
        y = 2
 
175
        f1 = iobj.W_IntObject(x)
 
176
        f2 = iobj.W_IntObject(y)
 
177
        v = iobj.pow__Int_Int_None(self.space, f1, f2, self.space.w_None)
 
178
        assert v.intval == x ** y
 
179
        f1, f2 = [iobj.W_IntObject(i) for i in (10, 20)]
 
180
        assert self.space.w_OverflowError == (
 
181
                          self._unwrap_nonimpl(iobj.pow__Int_Int_None, self.space, f1, f2, self.space.w_None))
 
182
 
 
183
    def test_neg(self):
 
184
        x = 42
 
185
        f1 = iobj.W_IntObject(x)
 
186
        v = iobj.neg__Int(self.space, f1)
 
187
        assert v.intval == -x
 
188
        x = -sys.maxint-1
 
189
        f1 = iobj.W_IntObject(x)
 
190
        assert self.space.w_OverflowError == (
 
191
                          self._unwrap_nonimpl(iobj.neg__Int, self.space, f1))
 
192
 
 
193
    def test_pos(self):
 
194
        x = 42
 
195
        f1 = iobj.W_IntObject(x)
 
196
        v = iobj.pos__Int(self.space, f1)
 
197
        assert v.intval == +x
 
198
        x = -42
 
199
        f1 = iobj.W_IntObject(x)
 
200
        v = iobj.pos__Int(self.space, f1)
 
201
        assert v.intval == +x
 
202
 
 
203
    def test_abs(self):
 
204
        x = 42
 
205
        f1 = iobj.W_IntObject(x)
 
206
        v = iobj.abs__Int(self.space, f1)
 
207
        assert v.intval == abs(x)
 
208
        x = -42
 
209
        f1 = iobj.W_IntObject(x)
 
210
        v = iobj.abs__Int(self.space, f1)
 
211
        assert v.intval == abs(x)
 
212
        x = -sys.maxint-1
 
213
        f1 = iobj.W_IntObject(x)
 
214
        assert self.space.w_OverflowError == (
 
215
                          self._unwrap_nonimpl(iobj.abs__Int, self.space, f1))
 
216
 
 
217
    def test_invert(self):
 
218
        x = 42
 
219
        f1 = iobj.W_IntObject(x)
 
220
        v = iobj.invert__Int(self.space, f1)
 
221
        assert v.intval == ~x
 
222
 
 
223
    def test_lshift(self):
 
224
        x = 12345678
 
225
        y = 2
 
226
        f1 = iobj.W_IntObject(x)
 
227
        f2 = iobj.W_IntObject(y)
 
228
        v = iobj.lshift__Int_Int(self.space, f1, f2)
 
229
        assert v.intval == x << y
 
230
        y = self._longshiftresult(x)
 
231
        f1 = iobj.W_IntObject(x)
 
232
        f2 = iobj.W_IntObject(y)
 
233
        assert self.space.w_OverflowError == (
 
234
                          self._unwrap_nonimpl(iobj.lshift__Int_Int, self.space, f1, f2))
 
235
 
 
236
    def test_rshift(self):
 
237
        x = 12345678
 
238
        y = 2
 
239
        f1 = iobj.W_IntObject(x)
 
240
        f2 = iobj.W_IntObject(y)
 
241
        v = iobj.rshift__Int_Int(self.space, f1, f2)
 
242
        assert v.intval == x >> y
 
243
 
 
244
    def test_and(self):
 
245
        x = 12345678
 
246
        y = 2
 
247
        f1 = iobj.W_IntObject(x)
 
248
        f2 = iobj.W_IntObject(y)
 
249
        v = iobj.and__Int_Int(self.space, f1, f2)
 
250
        assert v.intval == x & y
 
251
 
 
252
    def test_xor(self):
 
253
        x = 12345678
 
254
        y = 2
 
255
        f1 = iobj.W_IntObject(x)
 
256
        f2 = iobj.W_IntObject(y)
 
257
        v = iobj.xor__Int_Int(self.space, f1, f2)
 
258
        assert v.intval == x ^ y
 
259
 
 
260
    def test_or(self):
 
261
        x = 12345678
 
262
        y = 2
 
263
        f1 = iobj.W_IntObject(x)
 
264
        f2 = iobj.W_IntObject(y)
 
265
        v = iobj.or__Int_Int(self.space, f1, f2)
 
266
        assert v.intval == x | y
 
267
 
 
268
    def test_int(self):
 
269
        f1 = iobj.W_IntObject(1)
 
270
        result = iobj.int__Int(self.space, f1)
 
271
        assert result == f1
 
272
 
 
273
##    def test_long(self):
 
274
##        x = 1
 
275
##        f1 = iobj.W_IntObject(x)
 
276
##        result = iobj.int_long(self.space, f1)
 
277
##        self.assertEquals(self.space.unwrap(result), long(x))
 
278
 
 
279
##    def test_float(self):
 
280
##        x = 1
 
281
##        f1 = iobj.W_IntObject(x)
 
282
##        result = iobj.int_float(self.space, f1)
 
283
##        self.assertEquals(self.space.unwrap(result), float(x))
 
284
 
 
285
    def test_oct(self):
 
286
        x = 012345
 
287
        f1 = iobj.W_IntObject(x)
 
288
        result = iobj.oct__Int(self.space, f1)
 
289
        assert self.space.unwrap(result) == oct(x)
 
290
 
 
291
    def test_hex(self):
 
292
        x = 0x12345
 
293
        f1 = iobj.W_IntObject(x)
 
294
        result = iobj.hex__Int(self.space, f1)
 
295
        assert self.space.unwrap(result) == hex(x)
 
296
 
 
297
class AppTestInt:
 
298
 
 
299
    def test_int_callable(self):
 
300
        assert 43 == int(43)
 
301
 
 
302
    def test_int_string(self):
 
303
        assert 42 == int("42")
 
304
        assert 10000000000 == long("10000000000")
 
305
 
 
306
    def test_int_unicode(self):
 
307
        assert 42 == int(unicode('42'))
 
308
 
 
309
    def test_int_float(self):
 
310
        assert 4 == int(4.2)
 
311
 
 
312
    def test_int_str_repr(self):
 
313
        assert "42" == str(42)
 
314
        assert "42" == repr(42)
 
315
        raises(ValueError, int, '0x2A')
 
316
        
 
317
    def test_int_two_param(self):
 
318
        assert 42 == int('0x2A', 0)
 
319
        assert 42 == int('2A', 16)
 
320
        assert 42 == int('42', 10)
 
321
        raises(TypeError, int, 1, 10)
 
322
        raises(TypeError, int, '5', '9')
 
323
 
 
324
    def test_shift_zeros(self):
 
325
        assert (1 << 0) == 1
 
326
        assert (1 >> 0) == 1
 
327
 
 
328
    def test_overflow(self):
 
329
        import sys
 
330
        n = sys.maxint + 1
 
331
        assert isinstance(n, long)
 
332
 
 
333
    def test_pow(self):
 
334
        assert pow(2, -10) == 1/1024.
 
335
 
 
336
    def test_int_w_long_arg(self):
 
337
        assert int(10000000000) == 10000000000L
 
338
        assert int("10000000000") == 10000000000l
 
339
        raises(ValueError, int, "10000000000JUNK")
 
340
        raises(ValueError, int, "10000000000JUNK", 10)
 
341
 
 
342
    def test_int_subclass_ctr(self):
 
343
        import sys
 
344
        class j(int):
 
345
            pass
 
346
        assert j(100) == 100
 
347
        assert isinstance(j(100),j)
 
348
        assert j(100L) == 100
 
349
        assert j("100") == 100
 
350
        assert j("100",2) == 4
 
351
        assert isinstance(j("100",2),j)
 
352
        raises(OverflowError,j,sys.maxint+1)
 
353
        raises(OverflowError,j,str(sys.maxint+1))
 
354
 
 
355
    def test_int_subclass_ops(self):
 
356
        import sys
 
357
        class j(int):
 
358
            def __add__(self, other):
 
359
                return "add."
 
360
            def __iadd__(self, other):
 
361
                return "iadd."
 
362
            def __sub__(self, other):
 
363
                return "sub."
 
364
            def __isub__(self, other):
 
365
                return "isub."
 
366
            def __mul__(self, other):
 
367
                return "mul."
 
368
            def __imul__(self, other):
 
369
                return "imul."
 
370
            def __lshift__(self, other):
 
371
                return "lshift."
 
372
            def __ilshift__(self, other):
 
373
                return "ilshift."
 
374
        assert j(100) +  5   == "add."
 
375
        assert j(100) +  str == "add."
 
376
        assert j(100) -  5   == "sub."
 
377
        assert j(100) -  str == "sub."
 
378
        assert j(100) *  5   == "mul."
 
379
        assert j(100) *  str == "mul."
 
380
        assert j(100) << 5   == "lshift."
 
381
        assert j(100) << str == "lshift."
 
382
        assert (5 +  j(100),  type(5 +  j(100))) == (     105, int)
 
383
        assert (5 -  j(100),  type(5 -  j(100))) == (     -95, int)
 
384
        assert (5 *  j(100),  type(5 *  j(100))) == (     500, int)
 
385
        assert (5 << j(100),  type(5 << j(100))) == (5 << 100, long)
 
386
        assert (j(100) >> 2,  type(j(100) >> 2)) == (      25, int)
 
387
 
 
388
    def test_special_int(self):
 
389
        class a:
 
390
            def __int__(self): 
 
391
                self.ar = True 
 
392
                return None
 
393
        inst = a()
 
394
        raises(TypeError, int, inst) 
 
395
        assert inst.ar == True 
 
396
 
 
397
        class b: 
 
398
            pass 
 
399
        raises((AttributeError,TypeError), int, b()) 
 
400
 
 
401
    def test_special_long(self):
 
402
        class a:
 
403
            def __long__(self): 
 
404
                self.ar = True 
 
405
                return None
 
406
        inst = a()
 
407
        raises(TypeError, long, inst) 
 
408
        assert inst.ar == True 
 
409
 
 
410
        class b: 
 
411
            pass 
 
412
        raises((AttributeError,TypeError), long, b()) 
 
413
 
 
414
    def test_getnewargs(self):
 
415
        assert  0 .__getnewargs__() == (0,)
 
416
 
 
417
class AppTestIntOptimizedAdd(AppTestInt):
 
418
    def setup_class(cls):
 
419
        from pypy.conftest import gettestobjspace
 
420
        cls.space = gettestobjspace(**{"objspace.std.optimized_int_add": True})