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

« back to all changes in this revision

Viewing changes to pypy/objspace/std/test/test_smallintobject.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, py
 
2
 
 
3
#from pypy.objspace.std.model import WITHSMALLINT
 
4
#if not WITHSMALLINT:
 
5
#    py.test.skip("WITHSMALLINT is not enabled")
 
6
 
 
7
from pypy.objspace.std.inttype import wrapint
 
8
from pypy.objspace.std.objspace import FailedToImplement
 
9
from pypy.rlib.rarithmetic import r_uint
 
10
 
 
11
from pypy.conftest import gettestobjspace
 
12
 
 
13
class TestW_IntObject:
 
14
 
 
15
    def setup_class(cls):
 
16
        cls.space = gettestobjspace(**{"objspace.std.withsmallint": True})
 
17
 
 
18
    def test_int_w(self):
 
19
        assert self.space.int_w(self.space.wrap(42)) == 42
 
20
 
 
21
    def test_uint_w(self):
 
22
        space = self.space
 
23
        assert space.uint_w(space.wrap(42)) == 42
 
24
        assert isinstance(space.uint_w(space.wrap(42)), r_uint)
 
25
        space.raises_w(space.w_ValueError, space.uint_w, space.wrap(-1))
 
26
        
 
27
    def test_repr(self):
 
28
        x = 1
 
29
        f1 = wrapint(self.space, x)
 
30
        result = self.space.repr(f1)
 
31
        assert self.space.unwrap(result) == repr(x)
 
32
 
 
33
    def test_str(self):
 
34
        x = 12345
 
35
        f1 = wrapint(self.space, x)
 
36
        result = self.space.str(f1)
 
37
        assert self.space.unwrap(result) == str(x)
 
38
 
 
39
    def test_hash(self):
 
40
        x = 42
 
41
        f1 = wrapint(self.space, x)
 
42
        result = self.space.hash(f1)
 
43
        assert result.intval == hash(x)
 
44
 
 
45
    def test_compare(self):
 
46
        import operator
 
47
        optab = ['lt', 'le', 'eq', 'ne', 'gt', 'ge']
 
48
        for x in (-10, -1, 0, 1, 2, 1000, sys.maxint):
 
49
            for y in (-sys.maxint-1, -11, -9, -2, 0, 1, 3, 1111, sys.maxint):
 
50
                for op in optab:
 
51
                    wx = wrapint(self.space, x)
 
52
                    wy = wrapint(self.space, y)
 
53
                    res = getattr(operator, op)(x, y)
 
54
                    method = getattr(self.space, op)
 
55
                    myres = method(wx, wy)
 
56
                    assert self.space.unwrap(myres) == res
 
57
                    
 
58
    def test_add(self):
 
59
        for x in [1, 100, sys.maxint // 2 - 50,
 
60
                  sys.maxint // 2, sys.maxint - 1000, sys.maxint]:
 
61
            for y in [1, 100, sys.maxint // 2 - 50,
 
62
                      sys.maxint // 2, sys.maxint - 1000, sys.maxint]:
 
63
                f1 = wrapint(self.space, x)
 
64
                f2 = wrapint(self.space, y)
 
65
                result = self.space.unwrap(self.space.add(f1, f2))
 
66
                assert result == x+y and type(result) == type(x+y)
 
67
 
 
68
    def test_sub(self):
 
69
        for x in [1, 100, sys.maxint // 2 - 50,
 
70
                  sys.maxint // 2, sys.maxint - 1000, sys.maxint]:
 
71
            for y in [1, 100, sys.maxint // 2 - 50,
 
72
                      sys.maxint // 2, sys.maxint - 1000, sys.maxint]:
 
73
                f1 = wrapint(self.space, x)
 
74
                f2 = wrapint(self.space, y)
 
75
                result = self.space.unwrap(self.space.sub(f1, f2))
 
76
                assert result == x-y and type(result) == type(x-y)
 
77
 
 
78
    def test_mul(self):
 
79
        for x in [0, 1, 100, sys.maxint // 2 - 50, sys.maxint - 1000]:
 
80
            for y in [0, 1, 100, sys.maxint // 2 - 50, sys.maxint - 1000]:
 
81
                f1 = wrapint(self.space, x)
 
82
                f2 = wrapint(self.space, y)
 
83
                result = self.space.unwrap(self.space.mul(f1, f2))
 
84
                assert result == x*y and type(result) == type(x*y)
 
85
 
 
86
    def test_div(self):
 
87
        for i in range(10):
 
88
            res = i//3
 
89
            f1 = wrapint(self.space, i)
 
90
            f2 = wrapint(self.space, 3)
 
91
            result = self.space.div(f1, f2)
 
92
            assert result.intval == res
 
93
 
 
94
    def test_mod(self):
 
95
        x = 1
 
96
        y = 2
 
97
        f1 = wrapint(self.space, x)
 
98
        f2 = wrapint(self.space, y)
 
99
        v = self.space.mod(f1, f2)
 
100
        assert v.intval == x % y
 
101
        # not that mod cannot overflow
 
102
 
 
103
    def test_divmod(self):
 
104
        x = 1
 
105
        y = 2
 
106
        f1 = wrapint(self.space, x)
 
107
        f2 = wrapint(self.space, y)
 
108
        ret = self.space.divmod(f1, f2)
 
109
        v, w = self.space.unwrap(ret)
 
110
        assert (v, w) == divmod(x, y)
 
111
 
 
112
    def test_pow_iii(self):
 
113
        x = 10
 
114
        y = 2
 
115
        z = 13
 
116
        f1 = wrapint(self.space, x)
 
117
        f2 = wrapint(self.space, y)
 
118
        f3 = wrapint(self.space, z)
 
119
        v = self.space.pow(f1, f2, f3)
 
120
        assert v.intval == pow(x, y, z)
 
121
        f1, f2, f3 = [wrapint(self.space, i) for i in (10, -1, 42)]
 
122
        self.space.raises_w(self.space.w_TypeError,
 
123
                            self.space.pow,
 
124
                            f1, f2, f3)
 
125
        f1, f2, f3 = [wrapint(self.space, i) for i in (10, 5, 0)]
 
126
        self.space.raises_w(self.space.w_ValueError,
 
127
                            self.space.pow,
 
128
                            f1, f2, f3)
 
129
 
 
130
    def test_pow_iin(self):
 
131
        x = 10
 
132
        y = 2
 
133
        f1 = wrapint(self.space, x)
 
134
        f2 = wrapint(self.space, y)
 
135
        v = self.space.pow(f1, f2, self.space.w_None)
 
136
        assert v.intval == x ** y
 
137
 
 
138
    def test_neg(self):
 
139
        x = 42
 
140
        f1 = wrapint(self.space, x)
 
141
        v = self.space.neg(f1)
 
142
        assert v.intval == -x
 
143
 
 
144
    def test_pos(self):
 
145
        x = 42
 
146
        f1 = wrapint(self.space, x)
 
147
        v = self.space.pos(f1)
 
148
        assert v.intval == +x
 
149
        x = -42
 
150
        f1 = wrapint(self.space, x)
 
151
        v = self.space.pos(f1)
 
152
        assert v.intval == +x
 
153
 
 
154
    def test_abs(self):
 
155
        x = 42
 
156
        f1 = wrapint(self.space, x)
 
157
        v = self.space.abs(f1)
 
158
        assert v.intval == abs(x)
 
159
        x = -42
 
160
        f1 = wrapint(self.space, x)
 
161
        v = self.space.abs(f1)
 
162
        assert v.intval == abs(x)
 
163
 
 
164
    def test_invert(self):
 
165
        x = 42
 
166
        f1 = wrapint(self.space, x)
 
167
        v = self.space.invert(f1)
 
168
        assert v.intval == ~x
 
169
 
 
170
    def test_lshift(self):
 
171
        x = 12345678
 
172
        y = 2
 
173
        f1 = wrapint(self.space, x)
 
174
        f2 = wrapint(self.space, y)
 
175
        v = self.space.lshift(f1, f2)
 
176
        assert v.intval == x << y
 
177
 
 
178
    def test_rshift(self):
 
179
        x = 12345678
 
180
        y = 2
 
181
        f1 = wrapint(self.space, x)
 
182
        f2 = wrapint(self.space, y)
 
183
        v = self.space.rshift(f1, f2)
 
184
        assert v.intval == x >> y
 
185
 
 
186
    def test_and(self):
 
187
        x = 12345678
 
188
        y = 2
 
189
        f1 = wrapint(self.space, x)
 
190
        f2 = wrapint(self.space, y)
 
191
        v = self.space.and_(f1, f2)
 
192
        assert v.intval == x & y
 
193
 
 
194
    def test_xor(self):
 
195
        x = 12345678
 
196
        y = 2
 
197
        f1 = wrapint(self.space, x)
 
198
        f2 = wrapint(self.space, y)
 
199
        v = self.space.xor(f1, f2)
 
200
        assert v.intval == x ^ y
 
201
 
 
202
    def test_or(self):
 
203
        x = 12345678
 
204
        y = 2
 
205
        f1 = wrapint(self.space, x)
 
206
        f2 = wrapint(self.space, y)
 
207
        v = self.space.or_(f1, f2)
 
208
        assert v.intval == x | y
 
209
 
 
210
    def test_int(self):
 
211
        f1 = wrapint(self.space, 1)
 
212
        result = self.space.int(f1)
 
213
        assert result == f1
 
214
 
 
215
    def test_oct(self):
 
216
        x = 012345
 
217
        f1 = wrapint(self.space, x)
 
218
        result = self.space.oct(f1)
 
219
        assert self.space.unwrap(result) == oct(x)
 
220
 
 
221
    def test_hex(self):
 
222
        x = 0x12345
 
223
        f1 = wrapint(self.space, x)
 
224
        result = self.space.hex(f1)
 
225
        assert self.space.unwrap(result) == hex(x)