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

« back to all changes in this revision

Viewing changes to numpy/core/tests/test_multiarray.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
from numpy.testing import *
 
3
from numpy.core import *
 
4
from numpy import random
 
5
 
 
6
class test_flags(ScipyTestCase):
 
7
    def setUp(self):
 
8
        self.a = arange(10)
 
9
 
 
10
    def check_writeable(self):
 
11
        mydict = locals()
 
12
        self.a.flags.writeable = False
 
13
        self.assertRaises(RuntimeError, runstring, 'self.a[0] = 3', mydict)
 
14
        self.a.flags.writeable = True
 
15
        self.a[0] = 5
 
16
        self.a[0] = 0
 
17
 
 
18
    def check_otherflags(self):
 
19
        assert_equal(self.a.flags.carray, True)
 
20
        assert_equal(self.a.flags.farray, False)
 
21
        assert_equal(self.a.flags.behaved, True)
 
22
        assert_equal(self.a.flags.fnc, False)
 
23
        assert_equal(self.a.flags.forc, True)
 
24
        assert_equal(self.a.flags.owndata, True)
 
25
        assert_equal(self.a.flags.writeable, True)
 
26
        assert_equal(self.a.flags.aligned, True)
 
27
        assert_equal(self.a.flags.updateifcopy, False)
 
28
 
 
29
 
 
30
class test_attributes(ScipyTestCase):
 
31
    def setUp(self):
 
32
        self.one = arange(10)
 
33
        self.two = arange(20).reshape(4,5)
 
34
        self.three = arange(60,dtype=float64).reshape(2,5,6)
 
35
 
 
36
    def check_attributes(self):
 
37
        assert_equal(self.one.shape, (10,))
 
38
        assert_equal(self.two.shape, (4,5))
 
39
        assert_equal(self.three.shape, (2,5,6))
 
40
        self.three.shape = (10,3,2)
 
41
        assert_equal(self.three.shape, (10,3,2))
 
42
        self.three.shape = (2,5,6)
 
43
        assert_equal(self.one.strides, (self.one.itemsize,))
 
44
        num = self.two.itemsize
 
45
        assert_equal(self.two.strides, (5*num, num))
 
46
        num = self.three.itemsize
 
47
        assert_equal(self.three.strides, (30*num, 6*num, num))
 
48
        assert_equal(self.one.ndim, 1)
 
49
        assert_equal(self.two.ndim, 2)
 
50
        assert_equal(self.three.ndim, 3)
 
51
        num = self.two.itemsize
 
52
        assert_equal(self.two.size, 20)
 
53
        assert_equal(self.two.nbytes, 20*num)
 
54
        assert_equal(self.two.itemsize, self.two.dtype.itemsize)
 
55
        assert_equal(self.two.base, arange(20))
 
56
 
 
57
    def check_dtypeattr(self):
 
58
        assert_equal(self.one.dtype, dtype(int_))
 
59
        assert_equal(self.three.dtype, dtype(float_))
 
60
        assert_equal(self.one.dtype.char, 'l')
 
61
        assert_equal(self.three.dtype.char, 'd')
 
62
        self.failUnless(self.three.dtype.str[0] in '<>')
 
63
        assert_equal(self.one.dtype.str[1], 'i')
 
64
        assert_equal(self.three.dtype.str[1], 'f')
 
65
 
 
66
    def check_stridesattr(self):
 
67
        x = self.one
 
68
        def make_array(size, offset, strides):
 
69
            return ndarray([size], buffer=x,
 
70
                           offset=offset*x.itemsize,
 
71
                           strides=strides*x.itemsize)
 
72
        assert_equal(make_array(4, 4, -1), array([4, 3, 2, 1]))
 
73
        self.failUnlessRaises(ValueError, make_array, 4, 4, -2)
 
74
        self.failUnlessRaises(ValueError, make_array, 4, 2, -1)
 
75
        self.failUnlessRaises(ValueError, make_array, 8, 3, 1)
 
76
        #self.failUnlessRaises(ValueError, make_array, 8, 3, 0)
 
77
        #self.failUnlessRaises(ValueError, lambda: ndarray([1], strides=4))
 
78
 
 
79
 
 
80
    def check_set_stridesattr(self):
 
81
        x = self.one
 
82
        def make_array(size, offset, strides):
 
83
            try:
 
84
                r = ndarray([size], buffer=x, offset=offset*x.itemsize)
 
85
            except:
 
86
                pass
 
87
            r.strides = strides=strides*x.itemsize
 
88
            return r
 
89
        assert_equal(make_array(4, 4, -1), array([4, 3, 2, 1]))
 
90
        self.failUnlessRaises(ValueError, make_array, 4, 4, -2)
 
91
        self.failUnlessRaises(ValueError, make_array, 4, 2, -1)
 
92
        self.failUnlessRaises(ValueError, make_array, 8, 3, 1)
 
93
        #self.failUnlessRaises(ValueError, make_array, 8, 3, 0)
 
94
 
 
95
    def check_fill(self):
 
96
        for t in "?bhilqpBHILQPfdgFDGO":
 
97
            x = empty((3,2,1), t)
 
98
            y = empty((3,2,1), t)
 
99
            x.fill(1)
 
100
            y[...] = 1
 
101
            assert_equal(x,y)
 
102
 
 
103
        x = array([(0,0.0), (1,1.0)], dtype='i4,f8')
 
104
        x.fill(x[0])
 
105
        assert_equal(x['f1'][1], x['f1'][0])
 
106
 
 
107
class test_dtypedescr(ScipyTestCase):
 
108
    def check_construction(self):
 
109
        d1 = dtype('i4')
 
110
        assert_equal(d1, dtype(int32))
 
111
        d2 = dtype('f8')
 
112
        assert_equal(d2, dtype(float64))
 
113
 
 
114
class test_fromstring(ScipyTestCase):
 
115
    def check_binary(self):
 
116
        a = fromstring('\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@',dtype='<f4')
 
117
        assert_array_equal(a, array([1,2,3,4]))
 
118
 
 
119
    def check_ascii(self):
 
120
        a = fromstring('1 , 2 , 3 , 4',sep=',')
 
121
        b = fromstring('1,2,3,4',dtype=float,sep=',')
 
122
        assert_array_equal(a,b)
 
123
 
 
124
class test_zero_rank(ScipyTestCase):
 
125
    def setUp(self):
 
126
        self.d = array(0), array('x', object)
 
127
 
 
128
    def check_ellipsis_subscript(self):
 
129
        a,b = self.d
 
130
        self.failUnlessEqual(a[...], 0)
 
131
        self.failUnlessEqual(b[...], 'x')
 
132
        self.failUnless(a[...] is a)
 
133
        self.failUnless(b[...] is b)
 
134
 
 
135
    def check_empty_subscript(self):
 
136
        a,b = self.d
 
137
        self.failUnlessEqual(a[()], 0)
 
138
        self.failUnlessEqual(b[()], 'x')
 
139
        self.failUnless(type(a[()]) is a.dtype.type)
 
140
        self.failUnless(type(b[()]) is str)
 
141
 
 
142
    def check_invalid_subscript(self):
 
143
        a,b = self.d
 
144
        self.failUnlessRaises(IndexError, lambda x: x[0], a)
 
145
        self.failUnlessRaises(IndexError, lambda x: x[0], b)
 
146
        self.failUnlessRaises(IndexError, lambda x: x[array([], int)], a)
 
147
        self.failUnlessRaises(IndexError, lambda x: x[array([], int)], b)
 
148
 
 
149
    def check_ellipsis_subscript_assignment(self):
 
150
        a,b = self.d
 
151
        a[...] = 42
 
152
        self.failUnlessEqual(a, 42)
 
153
        b[...] = ''
 
154
        self.failUnlessEqual(b.item(), '')
 
155
 
 
156
    def check_empty_subscript_assignment(self):
 
157
        a,b = self.d
 
158
        a[()] = 42
 
159
        self.failUnlessEqual(a, 42)
 
160
        b[()] = ''
 
161
        self.failUnlessEqual(b.item(), '')
 
162
 
 
163
    def check_invalid_subscript_assignment(self):
 
164
        a,b = self.d
 
165
        def assign(x, i, v):
 
166
            x[i] = v
 
167
        self.failUnlessRaises(IndexError, assign, a, 0, 42)
 
168
        self.failUnlessRaises(IndexError, assign, b, 0, '')
 
169
        self.failUnlessRaises(TypeError, assign, a, (), '')
 
170
 
 
171
    def check_newaxis(self):
 
172
        a,b = self.d
 
173
        self.failUnlessEqual(a[newaxis].shape, (1,))
 
174
        self.failUnlessEqual(a[..., newaxis].shape, (1,))
 
175
        self.failUnlessEqual(a[newaxis, ...].shape, (1,))
 
176
        self.failUnlessEqual(a[..., newaxis].shape, (1,))
 
177
        self.failUnlessEqual(a[newaxis, ..., newaxis].shape, (1,1))
 
178
        self.failUnlessEqual(a[..., newaxis, newaxis].shape, (1,1))
 
179
        self.failUnlessEqual(a[newaxis, newaxis, ...].shape, (1,1))
 
180
        self.failUnlessEqual(a[(newaxis,)*10].shape, (1,)*10)
 
181
 
 
182
    def check_invalid_newaxis(self):
 
183
        a,b = self.d
 
184
        def subscript(x, i): x[i]
 
185
        self.failUnlessRaises(IndexError, subscript, a, (newaxis, 0))
 
186
        self.failUnlessRaises(IndexError, subscript, a, (newaxis,)*50)
 
187
 
 
188
    def check_constructor(self):
 
189
        x = ndarray(())
 
190
        x[()] = 5
 
191
        self.failUnlessEqual(x[()], 5)
 
192
        y = ndarray((),buffer=x)
 
193
        y[()] = 6
 
194
        self.failUnlessEqual(x[()], 6)
 
195
 
 
196
    def check_output(self):
 
197
        x = array(2)
 
198
        self.failUnlessRaises(ValueError, add, x, [1], x)
 
199
        
 
200
class test_creation(ScipyTestCase):
 
201
    def check_from_attribute(self):
 
202
        class x(object):
 
203
            def __array__(self, dtype=None):
 
204
                pass
 
205
        self.failUnlessRaises(ValueError, array, x())
 
206
 
 
207
class test_bool(ScipyTestCase):
 
208
    def check_test_interning(self):
 
209
        a0 = bool_(0)
 
210
        b0 = bool_(False)
 
211
        self.failUnless(a0 is b0)
 
212
        a1 = bool_(1)
 
213
        b1 = bool_(True)
 
214
        self.failUnless(a1 is b1)
 
215
        self.failUnless(array([True])[0] is a1)
 
216
        self.failUnless(array(True)[()] is a1)
 
217
 
 
218
 
 
219
class test_methods(ScipyTestCase):
 
220
    def check_test_round(self):
 
221
        assert_equal(array([1.2,1.5]).round(), [1,2])
 
222
        assert_equal(array(1.5).round(), 2)
 
223
        assert_equal(array([12.2,15.5]).round(-1), [10,20])
 
224
        assert_equal(array([12.15,15.51]).round(1), [12.2,15.5])
 
225
 
 
226
    def check_transpose(self):
 
227
        a = array([[1,2],[3,4]])
 
228
        assert_equal(a.transpose(), [[1,3],[2,4]])
 
229
        self.failUnlessRaises(ValueError, lambda: a.transpose(0))
 
230
        self.failUnlessRaises(ValueError, lambda: a.transpose(0,0))
 
231
        self.failUnlessRaises(ValueError, lambda: a.transpose(0,1,2))
 
232
 
 
233
class test_subscripting(ScipyTestCase):
 
234
    def check_test_zero_rank(self):
 
235
        x = array([1,2,3])
 
236
        self.failUnless(isinstance(x[0], int))
 
237
        self.failUnless(type(x[0, ...]) is ndarray)
 
238
 
 
239
class test_pickling(ScipyTestCase):
 
240
    def setUp(self):
 
241
        self.carray = array([[2,9],[7,0],[3,8]])
 
242
        self.tarray = transpose(self.carray)
 
243
 
 
244
    def check_both(self):
 
245
        import pickle
 
246
        assert_equal(self.carray, pickle.loads(self.carray.dumps()))
 
247
        assert_equal(self.tarray, pickle.loads(self.tarray.dumps()))
 
248
 
 
249
class test_fancy_indexing(ScipyTestCase): 
 
250
    def check_list(self): 
 
251
        x = ones((1,1)) 
 
252
        x[:,[0]] = 2.0 
 
253
        assert_array_equal(x, array([[2.0]])) 
 
254
 
 
255
        x = ones((1,1,1)) 
 
256
        x[:,:,[0]] = 2.0 
 
257
        assert_array_equal(x, array([[[2.0]]])) 
 
258
 
 
259
    def check_tuple(self): 
 
260
        x = ones((1,1)) 
 
261
        x[:,(0,)] = 2.0 
 
262
        assert_array_equal(x, array([[2.0]])) 
 
263
        x = ones((1,1,1)) 
 
264
        x[:,:,(0,)] = 2.0 
 
265
        assert_array_equal(x, array([[[2.0]]])) 
 
266
 
 
267
class test_string_compare(ScipyTestCase):
 
268
    def check_string(self):
 
269
        g1 = array(["This","is","example"])
 
270
        g2 = array(["This","was","example"])
 
271
        assert_array_equal(g1 == g2, [g1[i] == g2[i] for i in [0,1,2]])
 
272
        assert_array_equal(g1 != g2, [g1[i] != g2[i] for i in [0,1,2]])
 
273
        assert_array_equal(g1 <= g2, [g1[i] <= g2[i] for i in [0,1,2]])
 
274
        assert_array_equal(g1 >= g2, [g1[i] >= g2[i] for i in [0,1,2]])
 
275
        assert_array_equal(g1 < g2, [g1[i] < g2[i] for i in [0,1,2]]) 
 
276
        assert_array_equal(g1 > g2, [g1[i] > g2[i] for i in [0,1,2]]) 
 
277
 
 
278
    def check_mixed(self):
 
279
        g1 = array(["spam","spa","spammer","and eggs"])
 
280
        g2 = "spam"
 
281
        assert_array_equal(g1 == g2, [x == g2 for x in g1])
 
282
        assert_array_equal(g1 != g2, [x != g2 for x in g1])
 
283
        assert_array_equal(g1 < g2, [x < g2 for x in g1])
 
284
        assert_array_equal(g1 > g2, [x > g2 for x in g1])
 
285
        assert_array_equal(g1 <= g2, [x <= g2 for x in g1])
 
286
        assert_array_equal(g1 >= g2, [x >= g2 for x in g1])
 
287
        
 
288
 
 
289
    def check_unicode(self):
 
290
        g1 = array([u"This",u"is",u"example"])
 
291
        g2 = array([u"This",u"was",u"example"])
 
292
        assert_array_equal(g1 == g2, [g1[i] == g2[i] for i in [0,1,2]])
 
293
        assert_array_equal(g1 != g2, [g1[i] != g2[i] for i in [0,1,2]])
 
294
        assert_array_equal(g1 <= g2, [g1[i] <= g2[i] for i in [0,1,2]])
 
295
        assert_array_equal(g1 >= g2, [g1[i] >= g2[i] for i in [0,1,2]])
 
296
        assert_array_equal(g1 < g2,  [g1[i] < g2[i] for i in [0,1,2]]) 
 
297
        assert_array_equal(g1 > g2,  [g1[i] > g2[i] for i in [0,1,2]]) 
 
298
 
 
299
 
 
300
class test_argmax(ScipyTestCase):
 
301
    def check_all(self):
 
302
        a = random.normal(0,1,(4,5,6,7,8))
 
303
        for i in xrange(a.ndim):
 
304
            amax = a.max(i)
 
305
            aargmax = a.argmax(i)  
 
306
            axes = range(a.ndim)
 
307
            axes.remove(i)
 
308
            assert all(amax == aargmax.choose(*a.transpose(i,*axes)))        
 
309
 
 
310
 
 
311
# Import tests from unicode
 
312
set_local_path()
 
313
from test_unicode import *
 
314
restore_path()
 
315
 
 
316
if __name__ == "__main__":
 
317
    ScipyTest('numpy.core.multiarray').run()