~ubuntu-branches/ubuntu/feisty/python-numpy/feisty

« back to all changes in this revision

Viewing changes to numpy/lib/tests/test_type_check.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-07-12 10:00:24 UTC
  • Revision ID: james.westby@ubuntu.com-20060712100024-5lw9q2yczlisqcrt
Tags: upstream-0.9.8
ImportĀ upstreamĀ versionĀ 0.9.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
import sys
 
3
 
 
4
from numpy.testing import *
 
5
set_package_path()
 
6
import numpy.lib;reload(numpy.lib);reload(numpy.lib.type_check)
 
7
from numpy.lib import *
 
8
from numpy.core import *
 
9
restore_path()
 
10
 
 
11
def assert_all(x):
 
12
    assert(all(x)), x
 
13
 
 
14
class test_mintypecode(ScipyTestCase):
 
15
 
 
16
    def check_default_1(self):
 
17
        for itype in '1bcsuwil':
 
18
            assert_equal(mintypecode(itype),'d')
 
19
        assert_equal(mintypecode('f'),'f')
 
20
        assert_equal(mintypecode('d'),'d')
 
21
        assert_equal(mintypecode('F'),'F')
 
22
        assert_equal(mintypecode('D'),'D')
 
23
 
 
24
    def check_default_2(self):
 
25
        for itype in '1bcsuwil':
 
26
            assert_equal(mintypecode(itype+'f'),'f')
 
27
            assert_equal(mintypecode(itype+'d'),'d')
 
28
            assert_equal(mintypecode(itype+'F'),'F')
 
29
            assert_equal(mintypecode(itype+'D'),'D')
 
30
        assert_equal(mintypecode('ff'),'f')
 
31
        assert_equal(mintypecode('fd'),'d')
 
32
        assert_equal(mintypecode('fF'),'F')
 
33
        assert_equal(mintypecode('fD'),'D')
 
34
        assert_equal(mintypecode('df'),'d')
 
35
        assert_equal(mintypecode('dd'),'d')
 
36
        #assert_equal(mintypecode('dF',savespace=1),'F')
 
37
        assert_equal(mintypecode('dF'),'D')
 
38
        assert_equal(mintypecode('dD'),'D')
 
39
        assert_equal(mintypecode('Ff'),'F')
 
40
        #assert_equal(mintypecode('Fd',savespace=1),'F')
 
41
        assert_equal(mintypecode('Fd'),'D')
 
42
        assert_equal(mintypecode('FF'),'F')
 
43
        assert_equal(mintypecode('FD'),'D')
 
44
        assert_equal(mintypecode('Df'),'D')
 
45
        assert_equal(mintypecode('Dd'),'D')
 
46
        assert_equal(mintypecode('DF'),'D')
 
47
        assert_equal(mintypecode('DD'),'D')
 
48
 
 
49
    def check_default_3(self):
 
50
        assert_equal(mintypecode('fdF'),'D')
 
51
        #assert_equal(mintypecode('fdF',savespace=1),'F')
 
52
        assert_equal(mintypecode('fdD'),'D')
 
53
        assert_equal(mintypecode('fFD'),'D')
 
54
        assert_equal(mintypecode('dFD'),'D')
 
55
 
 
56
        assert_equal(mintypecode('ifd'),'d')
 
57
        assert_equal(mintypecode('ifF'),'F')
 
58
        assert_equal(mintypecode('ifD'),'D')
 
59
        assert_equal(mintypecode('idF'),'D')
 
60
        #assert_equal(mintypecode('idF',savespace=1),'F')
 
61
        assert_equal(mintypecode('idD'),'D')
 
62
 
 
63
class test_isscalar(ScipyTestCase):
 
64
    def check_basic(self):
 
65
        assert(isscalar(3))
 
66
        assert(not isscalar([3]))
 
67
        assert(not isscalar((3,)))
 
68
        assert(isscalar(3j))
 
69
        assert(isscalar(10L))
 
70
        assert(isscalar(4.0))
 
71
 
 
72
class test_real(ScipyTestCase):
 
73
    def check_real(self):
 
74
        y = rand(10,)
 
75
        assert_array_equal(y,real(y))
 
76
 
 
77
    def check_cmplx(self):
 
78
        y = rand(10,)+1j*rand(10,)
 
79
        assert_array_equal(y.real,real(y))
 
80
 
 
81
class test_imag(ScipyTestCase):
 
82
    def check_real(self):
 
83
        y = rand(10,)
 
84
        assert_array_equal(0,imag(y))
 
85
 
 
86
    def check_cmplx(self):
 
87
        y = rand(10,)+1j*rand(10,)
 
88
        assert_array_equal(y.imag,imag(y))
 
89
 
 
90
class test_iscomplex(ScipyTestCase):
 
91
    def check_fail(self):
 
92
        z = array([-1,0,1])
 
93
        res = iscomplex(z)
 
94
        assert(not sometrue(res))
 
95
    def check_pass(self):
 
96
        z = array([-1j,1,0])
 
97
        res = iscomplex(z)
 
98
        assert_array_equal(res,[1,0,0])
 
99
 
 
100
class test_isreal(ScipyTestCase):
 
101
    def check_pass(self):
 
102
        z = array([-1,0,1j])
 
103
        res = isreal(z)
 
104
        assert_array_equal(res,[1,1,0])
 
105
    def check_fail(self):
 
106
        z = array([-1j,1,0])
 
107
        res = isreal(z)
 
108
        assert_array_equal(res,[0,1,1])
 
109
 
 
110
class test_iscomplexobj(ScipyTestCase):
 
111
    def check_basic(self):
 
112
        z = array([-1,0,1])
 
113
        assert(not iscomplexobj(z))
 
114
        z = array([-1j,0,-1])
 
115
        assert(iscomplexobj(z))
 
116
 
 
117
class test_isrealobj(ScipyTestCase):
 
118
    def check_basic(self):
 
119
        z = array([-1,0,1])
 
120
        assert(isrealobj(z))
 
121
        z = array([-1j,0,-1])
 
122
        assert(not isrealobj(z))
 
123
 
 
124
class test_isnan(ScipyTestCase):
 
125
    def check_goodvalues(self):
 
126
        z = array((-1.,0.,1.))
 
127
        res = isnan(z) == 0
 
128
        assert_all(alltrue(res))
 
129
    def check_posinf(self):
 
130
        assert_all(isnan(array((1.,))/0.) == 0)
 
131
    def check_neginf(self):
 
132
        assert_all(isnan(array((-1.,))/0.) == 0)
 
133
    def check_ind(self):
 
134
        assert_all(isnan(array((0.,))/0.) == 1)
 
135
    #def check_qnan(self):             log(-1) return pi*j now
 
136
    #    assert_all(isnan(log(-1.)) == 1)
 
137
    def check_integer(self):
 
138
        assert_all(isnan(1) == 0)
 
139
    def check_complex(self):
 
140
        assert_all(isnan(1+1j) == 0)
 
141
    def check_complex1(self):
 
142
        assert_all(isnan(array(0+0j)/0.) == 1)
 
143
 
 
144
class test_isfinite(ScipyTestCase):
 
145
    def check_goodvalues(self):
 
146
        z = array((-1.,0.,1.))
 
147
        res = isfinite(z) == 1
 
148
        assert_all(alltrue(res))
 
149
    def check_posinf(self):
 
150
        assert_all(isfinite(array((1.,))/0.) == 0)
 
151
    def check_neginf(self):
 
152
        assert_all(isfinite(array((-1.,))/0.) == 0)
 
153
    def check_ind(self):
 
154
        assert_all(isfinite(array((0.,))/0.) == 0)
 
155
    #def check_qnan(self):
 
156
    #    assert_all(isfinite(log(-1.)) == 0)
 
157
    def check_integer(self):
 
158
        assert_all(isfinite(1) == 1)
 
159
    def check_complex(self):
 
160
        assert_all(isfinite(1+1j) == 1)
 
161
    def check_complex1(self):
 
162
        assert_all(isfinite(array(1+1j)/0.) == 0)
 
163
 
 
164
class test_isinf(ScipyTestCase):
 
165
    def check_goodvalues(self):
 
166
        z = array((-1.,0.,1.))
 
167
        res = isinf(z) == 0
 
168
        assert_all(alltrue(res))
 
169
    def check_posinf(self):
 
170
        assert_all(isinf(array((1.,))/0.) == 1)
 
171
    def check_posinf_scalar(self):
 
172
        assert_all(isinf(array(1.,)/0.) == 1)
 
173
    def check_neginf(self):
 
174
        assert_all(isinf(array((-1.,))/0.) == 1)
 
175
    def check_neginf_scalar(self):
 
176
        assert_all(isinf(array(-1.)/0.) == 1)
 
177
    def check_ind(self):
 
178
        assert_all(isinf(array((0.,))/0.) == 0)
 
179
    #def check_qnan(self):
 
180
    #    assert_all(isinf(log(-1.)) == 0)
 
181
    #    assert_all(isnan(log(-1.)) == 1)
 
182
 
 
183
class test_isposinf(ScipyTestCase):
 
184
    def check_generic(self):
 
185
        vals = isposinf(array((-1.,0,1))/0.)
 
186
        assert(vals[0] == 0)
 
187
        assert(vals[1] == 0)
 
188
        assert(vals[2] == 1)
 
189
 
 
190
class test_isneginf(ScipyTestCase):
 
191
    def check_generic(self):
 
192
        vals = isneginf(array((-1.,0,1))/0.)
 
193
        assert(vals[0] == 1)
 
194
        assert(vals[1] == 0)
 
195
        assert(vals[2] == 0)
 
196
 
 
197
class test_nan_to_num(ScipyTestCase):
 
198
    def check_generic(self):
 
199
        vals = nan_to_num(array((-1.,0,1))/0.)
 
200
        assert_all(vals[0] < -1e10) and assert_all(isfinite(vals[0]))
 
201
        assert(vals[1] == 0)
 
202
        assert_all(vals[2] > 1e10) and assert_all(isfinite(vals[2]))
 
203
    def check_integer(self):
 
204
        vals = nan_to_num(1)
 
205
        assert_all(vals == 1)
 
206
    def check_complex_good(self):
 
207
        vals = nan_to_num(1+1j)
 
208
        assert_all(vals == 1+1j)
 
209
    def check_complex_bad(self):
 
210
        v = 1+1j
 
211
        v += array(0+1.j)/0.
 
212
        vals = nan_to_num(v)
 
213
        # !! This is actually (unexpectedly) zero
 
214
        assert_all(isfinite(vals))
 
215
    def check_complex_bad2(self):
 
216
        v = 1+1j
 
217
        v += array(-1+1.j)/0.
 
218
        vals = nan_to_num(v)
 
219
        assert_all(isfinite(vals))
 
220
        #assert_all(vals.imag > 1e10)  and assert_all(isfinite(vals))
 
221
        # !! This is actually (unexpectedly) positive
 
222
        # !! inf.  Comment out for now, and see if it
 
223
        # !! changes
 
224
        #assert_all(vals.real < -1e10) and assert_all(isfinite(vals))
 
225
 
 
226
 
 
227
class test_real_if_close(ScipyTestCase):
 
228
    def check_basic(self):
 
229
        a = rand(10)
 
230
        b = real_if_close(a+1e-15j)
 
231
        assert_all(isrealobj(b))
 
232
        assert_array_equal(a,b)
 
233
        b = real_if_close(a+1e-7j)
 
234
        assert_all(iscomplexobj(b))
 
235
        b = real_if_close(a+1e-7j,tol=1e-6)
 
236
        assert_all(isrealobj(b))
 
237
 
 
238
if __name__ == "__main__":
 
239
    ScipyTest().run()