~mwhudson/pypy/imported-builtinrefactor

« back to all changes in this revision

Viewing changes to pypy/appspace/test/test_complexobject.py

  • Committer: hpk
  • Date: 2003-09-17 20:58:52 UTC
  • Revision ID: svn-v4:fd0d7bf2-dfb6-0310-8d31-b7ecfe96aada:pypy/branch/builtinrefactor:1357
the final merge of the builtinrefactor branch into the trunk.  See 

http://codespeak.net/pipermail/pypy-dev/2003q3/001012.html

for in-depth discussion and description of what is new. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
#taken from CPython 2.3 (?)
4
 
 
5
 
"""
6
 
Test module for class complex in complexobject.py
7
 
 
8
 
As it seems there are some numerical differences in 
9
 
the __div__ and __divmod__ methods which have to be 
10
 
sorted out.
11
 
"""
12
 
 
13
 
import autopath
14
 
 
15
 
import math
16
 
import cmath
17
 
import sys
18
 
import types
19
 
import unittest
20
 
 
21
 
from pypy.tool import test
22
 
from pypy.appspace.complexobject import complex as pycomplex
23
 
    
24
 
 
25
 
try:
26
 
    unicode
27
 
    have_unicode = 1
28
 
except NameError:
29
 
    have_unicode = 0
30
 
 
31
 
 
32
 
def equal(a, b):
33
 
    "Compare two complex or normal numbers. 0 if different, 1 if roughly equal."
34
 
    
35
 
    numTypes = [types.IntType, types.LongType, types.FloatType]
36
 
    da, db = dir(a), dir(b)
37
 
    
38
 
    if 'real' in da and 'real' in db and 'imag' in da and 'imag' in db:
39
 
        if math.fabs(a.real-b.real) > 1e-10:
40
 
            return 0
41
 
        if math.fabs(a.imag-b.imag) > 1e-10:
42
 
            return 0
43
 
        else:
44
 
            return 1
45
 
    elif type(a) in numTypes and type(b) in numTypes:
46
 
        if math.fabs(a-b) > 1e-10:
47
 
            return 0
48
 
        else:
49
 
            return 1
50
 
    
51
 
 
52
 
 
53
 
 
54
 
def enumerate():
55
 
    valueRange = xrange(-3, 3)
56
 
    res = []
57
 
    for x0 in valueRange:
58
 
        for y0 in valueRange:
59
 
            for x1 in valueRange:
60
 
                for y1 in valueRange:
61
 
                    z0c = complex(x0,y0)
62
 
                    z1c = complex(x1,y1)
63
 
                    z0p = pycomplex(x0,y0)
64
 
                    z1p = pycomplex(x1,y1)
65
 
                    res.append((z0c, z1c, z0p, z1p))
66
 
 
67
 
    return res
68
 
 
69
 
 
70
 
 
71
 
 
72
 
class TestComplex(unittest.TestCase):
73
 
 
74
 
    def assertAEqual(self, a, b):
75
 
        if not equal(a, b):
76
 
            raise self.failureException, '%s ~== %s'%(a, b)
77
 
 
78
 
    def test_wrongInit1(self):
79
 
        "Compare wrong init. with CPython."
80
 
        
81
 
        try:
82
 
            complex("1", "1")
83
 
        except TypeError:
84
 
            pass
85
 
        else:
86
 
            self.fail('complex("1", "1")')
87
 
 
88
 
        try:
89
 
            pycomplex("1", "1")
90
 
        except TypeError:
91
 
            pass
92
 
        else:
93
 
            self.fail('complex("1", "1")')
94
 
 
95
 
 
96
 
    def test_wrongInit2(self):
97
 
        "Compare wrong init. with CPython."
98
 
        
99
 
        try:
100
 
            complex(1, "1")
101
 
        except TypeError:
102
 
            pass
103
 
        else:
104
 
            self.fail('complex(1, "1")')
105
 
 
106
 
        try:
107
 
            pycomplex(1, "1")
108
 
        except TypeError:
109
 
            pass
110
 
        else:
111
 
            self.fail('complex(1, "1")')
112
 
 
113
 
 
114
 
    def test_wrongInitFromString(self):
115
 
        "Compare string init. with CPython."
116
 
 
117
 
        if complex("  3.14+J  ") != 3.14+1j:
118
 
            self.fail('complex("  3.14+J  )"')
119
 
        if not equal(pycomplex("  3.14+J  "), pycomplex(3.14,1)):
120
 
            self.fail('complex("  3.14+J  )"')
121
 
 
122
 
 
123
 
    def test_wrongInitFromUnicodeString(self):
124
 
        "Compare unicode string init. with CPython."
125
 
 
126
 
        if have_unicode:
127
 
            if complex(unicode("  3.14+J  ")) != 3.14+1j:
128
 
                self.fail('complex(u"  3.14+J  )"')
129
 
            if not equal(pycomplex(unicode("  3.14+J  ")), pycomplex(3.14, 1)):
130
 
                self.fail('complex(u"  3.14+J  )"')
131
 
 
132
 
 
133
 
    def test_class(self):
134
 
        "Compare class with CPython."
135
 
        
136
 
        class Z:
137
 
            def __complex__(self):
138
 
                return 3.14j
139
 
        z = Z()
140
 
        if complex(z) != 3.14j:
141
 
            self.fail('complex(classinstance)')
142
 
 
143
 
        if not equal(complex(z), pycomplex(0, 3.14)): 
144
 
            self.fail('complex(classinstance)')
145
 
 
146
 
 
147
 
    def test_add_sub_mul_div(self):
148
 
        "Compare add/sub/mul/div with CPython."
149
 
        
150
 
        for (z0c, z1c, z0p, z1p) in enumerate():
151
 
            mc = z0c*z1c
152
 
            mp = z0p*z1p
153
 
            self.assertAEqual(mc, mp)
154
 
 
155
 
            sc = z0c+z1c
156
 
            sp = z0p+z1p
157
 
            self.assertAEqual(sc, sp)
158
 
 
159
 
            dc = z0c-z1c
160
 
            dp = z0p-z1p
161
 
            self.assertAEqual(dc, dp)
162
 
 
163
 
            if not equal(z1c, complex(0,0)): 
164
 
                qc = z0c/z1c
165
 
                qp = z0p/z1p
166
 
                self.assertAEqual(qc, qp)
167
 
 
168
 
                
169
 
    def test_special(self):
170
 
        "Compare special methods with CPython."
171
 
        
172
 
        for (x, y) in [(0,0), (0,1), (1,3.)]:
173
 
            zc = complex(x, y)
174
 
            zp = pycomplex(x, y)
175
 
 
176
 
            self.assertAEqual(zc, zp)
177
 
            self.assertAEqual(-zc, -zp)
178
 
            self.assertAEqual(+zc, +zp)
179
 
            self.assertAEqual(abs(zc), abs(zp))
180
 
            self.assertAEqual(zc, zp)
181
 
            self.assertEqual(zc.conjugate(), zp.conjugate())
182
 
            self.assertEqual(str(zc), str(zp))
183
 
            self.assertEqual(hash(zc), hash(zp))
184
 
 
185
 
 
186
 
    # this fails on python2.3 and is depreacted anyway
187
 
    def _test_divmod(self):
188
 
        "Compare divmod with CPython."
189
 
        
190
 
        for (z0c, z1c, z0p, z1p) in enumerate():
191
 
            mc = z0c*z1c
192
 
            mp = z0p*z1p
193
 
            self.assertAEqual(mc, mp)
194
 
 
195
 
            if not equal(z1c, complex(0,0)): 
196
 
                ddc, mmc = divmod(z0c, z1c)
197
 
                self.assertAEqual(ddc*z1c + mmc, z0c)
198
 
                ddp, mmp = divmod(z0p, z1p)
199
 
                self.assertAEqual(ddp*z1p + mmp, z0p)
200
 
                self.assertAEqual(ddc, ddp)
201
 
                self.assertAEqual(mmc, mmp)
202
 
 
203
 
 
204
 
    # these fail on python2.3
205
 
    def _test_mod(self):
206
 
        "Compare mod with CPython."
207
 
        
208
 
        for (z0c, z1c, z0p, z1p) in enumerate():
209
 
            mc = z0c*z1c
210
 
            mp = z0p*z1p
211
 
            self.assertAEqual(mc, mp)
212
 
 
213
 
            if not equal(z1c, complex(0,0)): 
214
 
                rc = z0c%z1c
215
 
                rp = z0p%z1p
216
 
                self.assertAEqual(rc, rp)
217
 
                    
218
 
    def test_div(self):
219
 
        "Compare mod with CPython."
220
 
        
221
 
        for (z0c, z1c, z0p, z1p) in enumerate():
222
 
            mc = z0c*z1c
223
 
            mp = z0p*z1p
224
 
            self.assertAEqual(mc, mp)
225
 
 
226
 
            if not equal(z1c, complex(0,0)): 
227
 
                rc = z0c/z1c
228
 
                rp = z0p/z1p
229
 
                self.assertAEqual(rc, rp)
230
 
                    
231
 
 
232
 
    def test_pow(self):
233
 
        "Compare pow with CPython."
234
 
        
235
 
        for (z0c, z1c, z0p, z1p) in enumerate():
236
 
            if not equal(z0c, 0j) and (z1c.imag != 0.0):
237
 
                pc = z0c**z1c
238
 
                pp = z0p**z1p
239
 
                self.assertAEqual(pc, pp)
240
 
                pc = z0c**z0c.real
241
 
                pp = z0p**z0p.real
242
 
                self.assertAEqual(pc, pp)
243
 
 
244
 
if __name__ == "__main__":
245
 
    unittest.main()