~jtaylor/ubuntu/precise/python-numpy/multiarch-fix-818867

« back to all changes in this revision

Viewing changes to numpy/core/tests/test_defchararray.py

  • Committer: Bazaar Package Importer
  • Author(s): Sandro Tosi
  • Date: 2010-10-07 10:19:13 UTC
  • mfrom: (7.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20101007101913-8b1kmt8ho4upcl9s
Tags: 1:1.4.1-5
* debian/patches/10_use_local_python.org_object.inv_sphinx.diff
  - fixed small typo in description
* debian/patches/changeset_r8364.diff
  - fix memory corruption (double free); thanks to Joseph Barillari for the
    report and to Michael Gilbert for pushing resolution; Closes: #581058

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from numpy.testing import *
2
2
from numpy.core import *
3
3
import numpy as np
 
4
import sys
 
5
from numpy.core.multiarray import _vec_string
4
6
 
5
7
class TestBasic(TestCase):
6
 
    def test_construction(self):
7
 
        A = np.array([['abc', '123'],
8
 
                      ['789', 'xyz']])
9
 
        A1 = A.view(np.chararray)
10
 
        A2 = np.chararray.__new__(np.chararray, A.shape, itemsize=A.itemsize,
11
 
                                  buffer=A)
12
 
        assert all(A1 == A2)
 
8
    def test_from_object_array(self):
 
9
        A = np.array([['abc', 2],
 
10
                      ['long   ', '0123456789']], dtype='O')
 
11
        B = np.char.array(A)
 
12
        assert_equal(B.dtype.itemsize, 10)
 
13
        assert_array_equal(B, [['abc', '2'], ['long', '0123456789']])
 
14
 
 
15
    def test_from_object_array_unicode(self):
 
16
        A = np.array([['abc', u'Sigma \u03a3'],
 
17
                      ['long   ', '0123456789']], dtype='O')
 
18
        self.failUnlessRaises(ValueError, np.char.array, (A,))
 
19
        B = np.char.array(A, unicode=True)
 
20
        assert_equal(B.dtype.itemsize, 10 * np.array('a', 'U').dtype.itemsize)
 
21
        assert_array_equal(B, [['abc', u'Sigma \u03a3'], ['long', '0123456789']])
 
22
 
 
23
    def test_from_string_array(self):
 
24
        A = np.array([['abc', 'foo'],
 
25
                      ['long   ', '0123456789']])
 
26
        assert_equal(A.dtype.type, np.string_)
 
27
        B = np.char.array(A)
 
28
        assert_array_equal(B, A)
 
29
        assert_equal(B.dtype, A.dtype)
 
30
        assert_equal(B.shape, A.shape)
 
31
        B[0,0] = 'changed'
 
32
        assert B[0,0] != A[0,0]
 
33
        C = np.char.asarray(A)
 
34
        assert_array_equal(C, A)
 
35
        assert_equal(C.dtype, A.dtype)
 
36
        C[0,0] = 'changed again'
 
37
        assert C[0,0] != B[0,0]
 
38
        assert C[0,0] == A[0,0]
 
39
 
 
40
    def test_from_unicode_array(self):
 
41
        A = np.array([['abc', u'Sigma \u03a3'],
 
42
                      ['long   ', '0123456789']])
 
43
        assert_equal(A.dtype.type, np.unicode_)
 
44
        B = np.char.array(A)
 
45
        assert_array_equal(B, A)
 
46
        assert_equal(B.dtype, A.dtype)
 
47
        assert_equal(B.shape, A.shape)
 
48
        B = np.char.array(A, unicode=True)
 
49
        assert_array_equal(B, A)
 
50
        assert_equal(B.dtype, A.dtype)
 
51
        assert_equal(B.shape, A.shape)
 
52
        def fail():
 
53
            B = np.char.array(A, unicode=False)
 
54
        self.failUnlessRaises(UnicodeEncodeError, fail)
 
55
 
 
56
    def test_unicode_upconvert(self):
 
57
        A = np.char.array(['abc'])
 
58
        B = np.char.array([u'\u03a3'])
 
59
        assert issubclass((A + B).dtype.type, np.unicode_)
 
60
 
 
61
    def test_from_string(self):
 
62
        A = np.char.array('abc')
 
63
        assert_equal(len(A), 1)
 
64
        assert_equal(len(A[0]), 3)
 
65
        assert issubclass(A.dtype.type, np.string_)
 
66
 
 
67
    def test_from_unicode(self):
 
68
        A = np.char.array(u'\u03a3')
 
69
        assert_equal(len(A), 1)
 
70
        assert_equal(len(A[0]), 1)
 
71
        assert_equal(A.itemsize, 4)
 
72
        assert issubclass(A.dtype.type, np.unicode_)
 
73
 
 
74
class TestVecString(TestCase):
 
75
    def test_non_existent_method(self):
 
76
        def fail():
 
77
            _vec_string('a', np.string_, 'bogus')
 
78
        self.failUnlessRaises(AttributeError, fail)
 
79
 
 
80
    def test_non_string_array(self):
 
81
        def fail():
 
82
            _vec_string(1, np.string_, 'strip')
 
83
        self.failUnlessRaises(TypeError, fail)
 
84
 
 
85
    def test_invalid_args_tuple(self):
 
86
        def fail():
 
87
            _vec_string(['a'], np.string_, 'strip', 1)
 
88
        self.failUnlessRaises(TypeError, fail)
 
89
 
 
90
    def test_invalid_type_descr(self):
 
91
        def fail():
 
92
            _vec_string(['a'], 'BOGUS', 'strip')
 
93
        self.failUnlessRaises(TypeError, fail)
 
94
 
 
95
    def test_invalid_function_args(self):
 
96
        def fail():
 
97
            _vec_string(['a'], np.string_, 'strip', (1,))
 
98
        self.failUnlessRaises(TypeError, fail)
 
99
 
 
100
    def test_invalid_result_type(self):
 
101
        def fail():
 
102
            _vec_string(['a'], np.integer, 'strip')
 
103
        self.failUnlessRaises(TypeError, fail)
 
104
 
 
105
    def test_broadcast_error(self):
 
106
        def fail():
 
107
            _vec_string([['abc', 'def']], np.integer, 'find', (['a', 'd', 'j'],))
 
108
        self.failUnlessRaises(ValueError, fail)
13
109
 
14
110
 
15
111
class TestWhitespace(TestCase):
21
117
 
22
118
    def test1(self):
23
119
        assert all(self.A == self.B)
24
 
 
 
120
        assert all(self.A >= self.B)
 
121
        assert all(self.A <= self.B)
 
122
        assert all(negative(self.A > self.B))
 
123
        assert all(negative(self.A < self.B))
 
124
        assert all(negative(self.A != self.B))
25
125
 
26
126
class TestChar(TestCase):
27
127
    def setUp(self):
28
128
        self.A = np.array('abc1', dtype='c').view(np.chararray)
29
129
 
30
130
    def test_it(self):
31
 
        assert self.A.shape == (4,)
32
 
        assert self.A.upper()[:2].tostring() == 'AB'    
 
131
        assert_equal(self.A.shape, (4,))
 
132
        assert_equal(self.A.upper()[:2].tostring(), 'AB')
 
133
 
 
134
class TestComparisons(TestCase):
 
135
    def setUp(self):
 
136
        self.A = np.array([['abc', '123'],
 
137
                           ['789', 'xyz']]).view(np.chararray)
 
138
        self.B = np.array([['efg', '123  '],
 
139
                           ['051', 'tuv']]).view(np.chararray)
 
140
 
 
141
    def test_not_equal(self):
 
142
        assert_array_equal((self.A != self.B), [[True, False], [True, True]])
 
143
 
 
144
    def test_equal(self):
 
145
        assert_array_equal((self.A == self.B), [[False, True], [False, False]])
 
146
 
 
147
    def test_greater_equal(self):
 
148
        assert_array_equal((self.A >= self.B), [[False, True], [True, True]])
 
149
 
 
150
    def test_less_equal(self):
 
151
        assert_array_equal((self.A <= self.B), [[True, True], [False, False]])
 
152
 
 
153
    def test_greater(self):
 
154
        assert_array_equal((self.A > self.B), [[False, False], [True, True]])
 
155
 
 
156
    def test_less(self):
 
157
        assert_array_equal((self.A < self.B), [[True, False], [False, False]])
 
158
 
 
159
class TestComparisonsMixed1(TestComparisons):
 
160
    """Ticket #1276"""
 
161
 
 
162
    def setUp(self):
 
163
        TestComparisons.setUp(self)
 
164
        self.B = np.array([['efg', '123  '],
 
165
                           ['051', 'tuv']], np.unicode_).view(np.chararray)
 
166
 
 
167
class TestComparisonsMixed2(TestComparisons):
 
168
    """Ticket #1276"""
 
169
 
 
170
    def setUp(self):
 
171
        TestComparisons.setUp(self)
 
172
        self.A = np.array([['abc', '123'],
 
173
                           ['789', 'xyz']], np.unicode_).view(np.chararray)
 
174
 
 
175
class TestInformation(TestCase):
 
176
    def setUp(self):
 
177
        self.A = np.array([[' abc ', ''],
 
178
                           ['12345', 'MixedCase'],
 
179
                           ['123 \t 345 \0 ', 'UPPER']]).view(np.chararray)
 
180
        self.B = np.array([[u' \u03a3 ', u''],
 
181
                           [u'12345', u'MixedCase'],
 
182
                           [u'123 \t 345 \0 ', u'UPPER']]).view(np.chararray)
 
183
 
 
184
    def test_len(self):
 
185
        assert issubclass(np.char.str_len(self.A).dtype.type, np.integer)
 
186
        assert_array_equal(np.char.str_len(self.A), [[5, 0], [5, 9], [12, 5]])
 
187
        assert_array_equal(np.char.str_len(self.B), [[3, 0], [5, 9], [12, 5]])
 
188
 
 
189
    def test_count(self):
 
190
        assert issubclass(self.A.count('').dtype.type, np.integer)
 
191
        assert_array_equal(self.A.count('a'), [[1, 0], [0, 1], [0, 0]])
 
192
        assert_array_equal(self.A.count('123'), [[0, 0], [1, 0], [1, 0]])
 
193
        # Python doesn't seem to like counting NULL characters
 
194
        # assert_array_equal(self.A.count('\0'), [[0, 0], [0, 0], [1, 0]])
 
195
        assert_array_equal(self.A.count('a', 0, 2), [[1, 0], [0, 0], [0, 0]])
 
196
        assert_array_equal(self.B.count('a'), [[0, 0], [0, 1], [0, 0]])
 
197
        assert_array_equal(self.B.count('123'), [[0, 0], [1, 0], [1, 0]])
 
198
        # assert_array_equal(self.B.count('\0'), [[0, 0], [0, 0], [1, 0]])
 
199
 
 
200
    def test_endswith(self):
 
201
        assert issubclass(self.A.endswith('').dtype.type, np.bool_)
 
202
        assert_array_equal(self.A.endswith(' '), [[1, 0], [0, 0], [1, 0]])
 
203
        assert_array_equal(self.A.endswith('3', 0, 3), [[0, 0], [1, 0], [1, 0]])
 
204
        def fail():
 
205
            self.A.endswith('3', 'fdjk')
 
206
        self.failUnlessRaises(TypeError, fail)
 
207
 
 
208
    def test_find(self):
 
209
        assert issubclass(self.A.find('a').dtype.type, np.integer)
 
210
        assert_array_equal(self.A.find('a'), [[1, -1], [-1, 6], [-1, -1]])
 
211
        assert_array_equal(self.A.find('3'), [[-1, -1], [2, -1], [2, -1]])
 
212
        assert_array_equal(self.A.find('a', 0, 2), [[1, -1], [-1, -1], [-1, -1]])
 
213
        assert_array_equal(self.A.find(['1', 'P']), [[-1, -1], [0, -1], [0, 1]])
 
214
 
 
215
    def test_index(self):
 
216
        def fail():
 
217
            self.A.index('a')
 
218
        self.failUnlessRaises(ValueError, fail)
 
219
        assert np.char.index('abcba', 'b') == 1
 
220
        assert issubclass(np.char.index('abcba', 'b').dtype.type, np.integer)
 
221
 
 
222
    def test_isalnum(self):
 
223
        assert issubclass(self.A.isalnum().dtype.type, np.bool_)
 
224
        assert_array_equal(self.A.isalnum(), [[False, False], [True, True], [False, True]])
 
225
 
 
226
    def test_isalpha(self):
 
227
        assert issubclass(self.A.isalpha().dtype.type, np.bool_)
 
228
        assert_array_equal(self.A.isalpha(), [[False, False], [False, True], [False, True]])
 
229
 
 
230
    def test_isdigit(self):
 
231
        assert issubclass(self.A.isdigit().dtype.type, np.bool_)
 
232
        assert_array_equal(self.A.isdigit(), [[False, False], [True, False], [False, False]])
 
233
 
 
234
    def test_islower(self):
 
235
        assert issubclass(self.A.islower().dtype.type, np.bool_)
 
236
        assert_array_equal(self.A.islower(), [[True, False], [False, False], [False, False]])
 
237
 
 
238
    def test_isspace(self):
 
239
        assert issubclass(self.A.isspace().dtype.type, np.bool_)
 
240
        assert_array_equal(self.A.isspace(), [[False, False], [False, False], [False, False]])
 
241
 
 
242
    def test_istitle(self):
 
243
        assert issubclass(self.A.istitle().dtype.type, np.bool_)
 
244
        assert_array_equal(self.A.istitle(), [[False, False], [False, False], [False, False]])
 
245
 
 
246
    def test_isupper(self):
 
247
        assert issubclass(self.A.isupper().dtype.type, np.bool_)
 
248
        assert_array_equal(self.A.isupper(), [[False, False], [False, False], [False, True]])
 
249
 
 
250
    def test_rfind(self):
 
251
        assert issubclass(self.A.rfind('a').dtype.type, np.integer)
 
252
        assert_array_equal(self.A.rfind('a'), [[1, -1], [-1, 6], [-1, -1]])
 
253
        assert_array_equal(self.A.rfind('3'), [[-1, -1], [2, -1], [6, -1]])
 
254
        assert_array_equal(self.A.rfind('a', 0, 2), [[1, -1], [-1, -1], [-1, -1]])
 
255
        assert_array_equal(self.A.rfind(['1', 'P']), [[-1, -1], [0, -1], [0, 2]])
 
256
 
 
257
    def test_rindex(self):
 
258
        def fail():
 
259
            self.A.rindex('a')
 
260
        self.failUnlessRaises(ValueError, fail)
 
261
        assert np.char.rindex('abcba', 'b') == 3
 
262
        assert issubclass(np.char.rindex('abcba', 'b').dtype.type, np.integer)
 
263
 
 
264
    def test_startswith(self):
 
265
        assert issubclass(self.A.startswith('').dtype.type, np.bool_)
 
266
        assert_array_equal(self.A.startswith(' '), [[1, 0], [0, 0], [0, 0]])
 
267
        assert_array_equal(self.A.startswith('1', 0, 3), [[0, 0], [1, 0], [1, 0]])
 
268
        def fail():
 
269
            self.A.startswith('3', 'fdjk')
 
270
        self.failUnlessRaises(TypeError, fail)
 
271
 
 
272
 
 
273
class TestMethods(TestCase):
 
274
    def setUp(self):
 
275
        self.A = np.array([[' abc ', ''],
 
276
                           ['12345', 'MixedCase'],
 
277
                           ['123 \t 345 \0 ', 'UPPER']]).view(np.chararray)
 
278
        self.B = np.array([[u' \u03a3 ', u''],
 
279
                           [u'12345', u'MixedCase'],
 
280
                           [u'123 \t 345 \0 ', u'UPPER']]).view(np.chararray)
 
281
 
 
282
    def test_capitalize(self):
 
283
        assert issubclass(self.A.capitalize().dtype.type, np.string_)
 
284
        assert_array_equal(self.A.capitalize(), [
 
285
                [' abc ', ''],
 
286
                ['12345', 'Mixedcase'],
 
287
                ['123 \t 345 \0 ', 'Upper']])
 
288
        assert issubclass(self.B.capitalize().dtype.type, np.unicode_)
 
289
        assert_array_equal(self.B.capitalize(), [
 
290
                [u' \u03c3 ', ''],
 
291
                ['12345', 'Mixedcase'],
 
292
                ['123 \t 345 \0 ', 'Upper']])
 
293
 
 
294
    def test_center(self):
 
295
        assert issubclass(self.A.center(10).dtype.type, np.string_)
 
296
        widths = np.array([[10, 20]])
 
297
        C = self.A.center([10, 20])
 
298
        assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])
 
299
        C = self.A.center(20, '#')
 
300
        assert np.all(C.startswith('#'))
 
301
        assert np.all(C.endswith('#'))
 
302
        C = np.char.center('FOO', [[10, 20], [15, 8]])
 
303
        assert issubclass(C.dtype.type, np.string_)
 
304
        assert_array_equal(C, [
 
305
                ['   FOO    ', '        FOO         '],
 
306
                ['      FOO      ', '  FOO   ']])
 
307
 
 
308
    def test_decode(self):
 
309
        A = np.char.array(['736563726574206d657373616765'])
 
310
        assert A.decode('hex_codec')[0] == 'secret message'
 
311
 
 
312
    def test_encode(self):
 
313
        B = self.B.encode('unicode_escape')
 
314
        assert B[0][0] == r' \u03a3 '
 
315
 
 
316
    def test_expandtabs(self):
 
317
        T = self.A.expandtabs()
 
318
        assert T[2][0] == '123      345'
 
319
 
 
320
    def test_join(self):
 
321
        A = np.char.join([',', '#'], self.A)
 
322
        assert issubclass(A.dtype.type, np.string_)
 
323
        assert_array_equal(np.char.join([',', '#'], self.A), [
 
324
                [' ,a,b,c, ', ''],
 
325
                ['1,2,3,4,5', 'M#i#x#e#d#C#a#s#e'],
 
326
                ['1,2,3, ,\t, ,3,4,5, ,\x00, ', 'U#P#P#E#R']])
 
327
 
 
328
    def test_ljust(self):
 
329
        assert issubclass(self.A.ljust(10).dtype.type, np.string_)
 
330
        widths = np.array([[10, 20]])
 
331
        C = self.A.ljust([10, 20])
 
332
        assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])
 
333
        C = self.A.ljust(20, '#')
 
334
        assert_array_equal(C.startswith('#'), [
 
335
                [False, True], [False, False], [False, False]])
 
336
        assert np.all(C.endswith('#'))
 
337
        C = np.char.ljust('FOO', [[10, 20], [15, 8]])
 
338
        assert issubclass(C.dtype.type, np.string_)
 
339
        assert_array_equal(C, [
 
340
                ['FOO       ', 'FOO                 '],
 
341
                ['FOO            ', 'FOO     ']])
 
342
 
 
343
    def test_lower(self):
 
344
        assert issubclass(self.A.lower().dtype.type, np.string_)
 
345
        assert_array_equal(self.A.lower(), [
 
346
                [' abc ', ''],
 
347
                ['12345', 'mixedcase'],
 
348
                ['123 \t 345 \0 ', 'upper']])
 
349
        assert issubclass(self.B.lower().dtype.type, np.unicode_)
 
350
        assert_array_equal(self.B.lower(), [
 
351
                [u' \u03c3 ', u''],
 
352
                [u'12345', u'mixedcase'],
 
353
                [u'123 \t 345 \0 ', u'upper']])
 
354
 
 
355
    def test_lstrip(self):
 
356
        assert issubclass(self.A.lstrip().dtype.type, np.string_)
 
357
        assert_array_equal(self.A.lstrip(), [
 
358
                ['abc ', ''],
 
359
                ['12345', 'MixedCase'],
 
360
                ['123 \t 345 \0 ', 'UPPER']])
 
361
        assert_array_equal(self.A.lstrip(['1', 'M']), [
 
362
                [' abc', ''],
 
363
                ['2345', 'ixedCase'],
 
364
                ['23 \t 345 \x00', 'UPPER']])
 
365
        assert issubclass(self.B.lstrip().dtype.type, np.unicode_)
 
366
        assert_array_equal(self.B.lstrip(), [
 
367
                [u'\u03a3 ', ''],
 
368
                ['12345', 'MixedCase'],
 
369
                ['123 \t 345 \0 ', 'UPPER']])
 
370
 
 
371
    def test_partition(self):
 
372
        if sys.version_info >= (2, 5):
 
373
            P = self.A.partition(['3', 'M'])
 
374
            assert issubclass(P.dtype.type, np.string_)
 
375
            assert_array_equal(P, [
 
376
                    [(' abc ', '', ''), ('', '', '')],
 
377
                    [('12', '3', '45'), ('', 'M', 'ixedCase')],
 
378
                    [('12', '3', ' \t 345 \0 '), ('UPPER', '', '')]])
 
379
 
 
380
    def test_replace(self):
 
381
        R = self.A.replace(['3', 'a'], ['##########', '@'])
 
382
        assert issubclass(R.dtype.type, np.string_)
 
383
        assert_array_equal(R, [
 
384
                [' abc ', ''],
 
385
                ['12##########45', 'MixedC@se'],
 
386
                ['12########## \t ##########45 \x00', 'UPPER']])
 
387
        R = self.A.replace('a', u'\u03a3')
 
388
        assert issubclass(R.dtype.type, np.unicode_)
 
389
        assert_array_equal(R, [
 
390
                [u' \u03a3bc ', ''],
 
391
                ['12345', u'MixedC\u03a3se'],
 
392
                ['123 \t 345 \x00', 'UPPER']])
 
393
 
 
394
    def test_rjust(self):
 
395
        assert issubclass(self.A.rjust(10).dtype.type, np.string_)
 
396
        widths = np.array([[10, 20]])
 
397
        C = self.A.rjust([10, 20])
 
398
        assert_array_equal(np.char.str_len(C), [[10, 20], [10, 20], [12, 20]])
 
399
        C = self.A.rjust(20, '#')
 
400
        assert np.all(C.startswith('#'))
 
401
        assert_array_equal(C.endswith('#'), [[False, True], [False, False], [False, False]])
 
402
        C = np.char.rjust('FOO', [[10, 20], [15, 8]])
 
403
        assert issubclass(C.dtype.type, np.string_)
 
404
        assert_array_equal(C, [
 
405
                ['       FOO', '                 FOO'],
 
406
                ['            FOO', '     FOO']])
 
407
 
 
408
    def test_rpartition(self):
 
409
        if sys.version_info >= (2, 5):
 
410
            P = self.A.rpartition(['3', 'M'])
 
411
            assert issubclass(P.dtype.type, np.string_)
 
412
            assert_array_equal(P, [
 
413
                    [('', '', ' abc '), ('', '', '')],
 
414
                    [('12', '3', '45'), ('', 'M', 'ixedCase')],
 
415
                    [('123 \t ', '3', '45 \0 '), ('', '', 'UPPER')]])
 
416
 
 
417
    def test_rsplit(self):
 
418
        A = self.A.rsplit('3')
 
419
        assert issubclass(A.dtype.type, np.object_)
 
420
        assert_equal(A.tolist(), [
 
421
                [[' abc '], ['']],
 
422
                [['12', '45'], ['MixedCase']],
 
423
                [['12', ' \t ', '45 \x00 '], ['UPPER']]])
 
424
 
 
425
    def test_rstrip(self):
 
426
        assert issubclass(self.A.rstrip().dtype.type, np.string_)
 
427
        assert_array_equal(self.A.rstrip(), [
 
428
                [' abc', ''],
 
429
                ['12345', 'MixedCase'],
 
430
                ['123 \t 345', 'UPPER']])
 
431
        assert_array_equal(self.A.rstrip(['5', 'ER']), [
 
432
                [' abc ', ''],
 
433
                ['1234', 'MixedCase'],
 
434
                ['123 \t 345 \x00', 'UPP']])
 
435
        assert issubclass(self.B.rstrip().dtype.type, np.unicode_)
 
436
        assert_array_equal(self.B.rstrip(), [
 
437
                [u' \u03a3', ''],
 
438
                ['12345', 'MixedCase'],
 
439
                ['123 \t 345', 'UPPER']])
 
440
 
 
441
    def test_strip(self):
 
442
        assert issubclass(self.A.strip().dtype.type, np.string_)
 
443
        assert_array_equal(self.A.strip(), [
 
444
                ['abc', ''],
 
445
                ['12345', 'MixedCase'],
 
446
                ['123 \t 345', 'UPPER']])
 
447
        assert_array_equal(self.A.strip(['15', 'EReM']), [
 
448
                [' abc ', ''],
 
449
                ['234', 'ixedCas'],
 
450
                ['23 \t 345 \x00', 'UPP']])
 
451
        assert issubclass(self.B.strip().dtype.type, np.unicode_)
 
452
        assert_array_equal(self.B.strip(), [
 
453
                [u'\u03a3', ''],
 
454
                ['12345', 'MixedCase'],
 
455
                ['123 \t 345', 'UPPER']])
 
456
 
 
457
    def test_split(self):
 
458
        A = self.A.split('3')
 
459
        assert issubclass(A.dtype.type, np.object_)
 
460
        assert_equal(A.tolist(), [
 
461
                [[' abc '], ['']],
 
462
                [['12', '45'], ['MixedCase']],
 
463
                [['12', ' \t ', '45 \x00 '], ['UPPER']]])
 
464
 
 
465
    def test_splitlines(self):
 
466
        A = np.char.array(['abc\nfds\nwer']).splitlines()
 
467
        assert issubclass(A.dtype.type, np.object_)
 
468
        assert A.shape == (1,)
 
469
        assert len(A[0]) == 3
 
470
 
 
471
    def test_swapcase(self):
 
472
        assert issubclass(self.A.swapcase().dtype.type, np.string_)
 
473
        assert_array_equal(self.A.swapcase(), [
 
474
                [' ABC ', ''],
 
475
                ['12345', 'mIXEDcASE'],
 
476
                ['123 \t 345 \0 ', 'upper']])
 
477
        assert issubclass(self.B.swapcase().dtype.type, np.unicode_)
 
478
        assert_array_equal(self.B.swapcase(), [
 
479
                [u' \u03c3 ', u''],
 
480
                [u'12345', u'mIXEDcASE'],
 
481
                [u'123 \t 345 \0 ', u'upper']])
 
482
 
 
483
    def test_title(self):
 
484
        assert issubclass(self.A.title().dtype.type, np.string_)
 
485
        assert_array_equal(self.A.title(), [
 
486
                [' Abc ', ''],
 
487
                ['12345', 'Mixedcase'],
 
488
                ['123 \t 345 \0 ', 'Upper']])
 
489
        assert issubclass(self.B.title().dtype.type, np.unicode_)
 
490
        assert_array_equal(self.B.title(), [
 
491
                [u' \u03a3 ', u''],
 
492
                [u'12345', u'Mixedcase'],
 
493
                [u'123 \t 345 \0 ', u'Upper']])
 
494
 
 
495
    def test_upper(self):
 
496
        assert issubclass(self.A.upper().dtype.type, np.string_)
 
497
        assert_array_equal(self.A.upper(), [
 
498
                [' ABC ', ''],
 
499
                ['12345', 'MIXEDCASE'],
 
500
                ['123 \t 345 \0 ', 'UPPER']])
 
501
        assert issubclass(self.B.upper().dtype.type, np.unicode_)
 
502
        assert_array_equal(self.B.upper(), [
 
503
                [u' \u03a3 ', u''],
 
504
                [u'12345', u'MIXEDCASE'],
 
505
                [u'123 \t 345 \0 ', u'UPPER']])
 
506
 
 
507
    def test_isnumeric(self):
 
508
        def fail():
 
509
            self.A.isnumeric()
 
510
        self.failUnlessRaises(TypeError, fail)
 
511
        assert issubclass(self.B.isnumeric().dtype.type, np.bool_)
 
512
        assert_array_equal(self.B.isnumeric(), [
 
513
                [False, False], [True, False], [False, False]])
 
514
 
 
515
    def test_isdecimal(self):
 
516
        def fail():
 
517
            self.A.isdecimal()
 
518
        self.failUnlessRaises(TypeError, fail)
 
519
        assert issubclass(self.B.isdecimal().dtype.type, np.bool_)
 
520
        assert_array_equal(self.B.isdecimal(), [
 
521
                [False, False], [True, False], [False, False]])
33
522
 
34
523
 
35
524
class TestOperations(TestCase):
42
531
    def test_add(self):
43
532
        AB = np.array([['abcefg', '123456'],
44
533
                       ['789051', 'xyztuv']]).view(np.chararray)
45
 
        assert all(AB == (self.A + self.B))
 
534
        assert_array_equal(AB, (self.A + self.B))
 
535
        assert len((self.A + self.B)[0][0]) == 6
46
536
 
47
537
    def test_radd(self):
48
538
        QA = np.array([['qabc', 'q123'],
49
539
                       ['q789', 'qxyz']]).view(np.chararray)
50
 
        assert all(QA == ('q' + self.A))
 
540
        assert_array_equal(QA, ('q' + self.A))
51
541
 
52
542
    def test_mul(self):
53
543
        A = self.A
54
 
#        for r in (2,3,5,7,197):
55
 
#            Ar = np.array([[A[0,0]*r, A[0,1]*r],
56
 
#                           [A[1,0]*r, A[1,1]*r]]).view(np.chararray)
57
 
#
58
 
#            assert all(Ar == (self.A * r))
 
544
        for r in (2,3,5,7,197):
 
545
            Ar = np.array([[A[0,0]*r, A[0,1]*r],
 
546
                           [A[1,0]*r, A[1,1]*r]]).view(np.chararray)
 
547
 
 
548
            assert_array_equal(Ar, (self.A * r))
59
549
 
60
550
        for ob in [object(), 'qrs']:
61
551
            try:
67
557
 
68
558
    def test_rmul(self):
69
559
        A = self.A
70
 
#        for r in (2,3,5,7,197):
71
 
#            Ar = np.array([[A[0,0]*r, A[0,1]*r],
72
 
#                           [A[1,0]*r, A[1,1]*r]]).view(np.chararray)
73
 
#
74
 
#            assert all(Ar == (r * self.A))
 
560
        for r in (2,3,5,7,197):
 
561
            Ar = np.array([[A[0,0]*r, A[0,1]*r],
 
562
                           [A[1,0]*r, A[1,1]*r]]).view(np.chararray)
 
563
            assert_array_equal(Ar, (r * self.A))
75
564
 
76
565
        for ob in [object(), 'qrs']:
77
566
            try:
82
571
                self.fail("chararray can only be multiplied by integers")
83
572
 
84
573
    def test_mod(self):
85
 
        pass
 
574
        """Ticket #856"""
 
575
        F = np.array([['%d', '%f'],['%s','%r']]).view(np.chararray)
 
576
        C = np.array([[3,7],[19,1]])
 
577
        FC = np.array([['3', '7.000000'],
 
578
                       ['19', '1']]).view(np.chararray)
 
579
        assert_array_equal(FC, F % C)
 
580
 
 
581
        A = np.array([['%.3f','%d'],['%s','%r']]).view(np.chararray)
 
582
        A1 = np.array([['1.000','1'],['1','1']]).view(np.chararray)
 
583
        assert_array_equal(A1, (A % 1))
 
584
 
 
585
        A2 = np.array([['1.000','2'],['3','4']]).view(np.chararray)
 
586
        assert_array_equal(A2, (A % [[1,2],[3,4]]))
86
587
 
87
588
    def test_rmod(self):
88
589
        assert ("%s" % self.A) == str(self.A)
98
599
                          "non-string objects")
99
600
 
100
601
 
 
602
 
101
603
if __name__ == "__main__":
102
604
    run_module_suite()