1
1
from numpy.testing import *
2
2
from numpy.core import *
5
from numpy.core.multiarray import _vec_string
5
7
class TestBasic(TestCase):
6
def test_construction(self):
7
A = np.array([['abc', '123'],
9
A1 = A.view(np.chararray)
10
A2 = np.chararray.__new__(np.chararray, A.shape, itemsize=A.itemsize,
8
def test_from_object_array(self):
9
A = np.array([['abc', 2],
10
['long ', '0123456789']], dtype='O')
12
assert_equal(B.dtype.itemsize, 10)
13
assert_array_equal(B, [['abc', '2'], ['long', '0123456789']])
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']])
23
def test_from_string_array(self):
24
A = np.array([['abc', 'foo'],
25
['long ', '0123456789']])
26
assert_equal(A.dtype.type, np.string_)
28
assert_array_equal(B, A)
29
assert_equal(B.dtype, A.dtype)
30
assert_equal(B.shape, A.shape)
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]
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_)
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)
53
B = np.char.array(A, unicode=False)
54
self.failUnlessRaises(UnicodeEncodeError, fail)
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_)
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_)
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_)
74
class TestVecString(TestCase):
75
def test_non_existent_method(self):
77
_vec_string('a', np.string_, 'bogus')
78
self.failUnlessRaises(AttributeError, fail)
80
def test_non_string_array(self):
82
_vec_string(1, np.string_, 'strip')
83
self.failUnlessRaises(TypeError, fail)
85
def test_invalid_args_tuple(self):
87
_vec_string(['a'], np.string_, 'strip', 1)
88
self.failUnlessRaises(TypeError, fail)
90
def test_invalid_type_descr(self):
92
_vec_string(['a'], 'BOGUS', 'strip')
93
self.failUnlessRaises(TypeError, fail)
95
def test_invalid_function_args(self):
97
_vec_string(['a'], np.string_, 'strip', (1,))
98
self.failUnlessRaises(TypeError, fail)
100
def test_invalid_result_type(self):
102
_vec_string(['a'], np.integer, 'strip')
103
self.failUnlessRaises(TypeError, fail)
105
def test_broadcast_error(self):
107
_vec_string([['abc', 'def']], np.integer, 'find', (['a', 'd', 'j'],))
108
self.failUnlessRaises(ValueError, fail)
15
111
class TestWhitespace(TestCase):
23
119
assert all(self.A == self.B)
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))
26
126
class TestChar(TestCase):
28
128
self.A = np.array('abc1', dtype='c').view(np.chararray)
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')
134
class TestComparisons(TestCase):
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)
141
def test_not_equal(self):
142
assert_array_equal((self.A != self.B), [[True, False], [True, True]])
144
def test_equal(self):
145
assert_array_equal((self.A == self.B), [[False, True], [False, False]])
147
def test_greater_equal(self):
148
assert_array_equal((self.A >= self.B), [[False, True], [True, True]])
150
def test_less_equal(self):
151
assert_array_equal((self.A <= self.B), [[True, True], [False, False]])
153
def test_greater(self):
154
assert_array_equal((self.A > self.B), [[False, False], [True, True]])
157
assert_array_equal((self.A < self.B), [[True, False], [False, False]])
159
class TestComparisonsMixed1(TestComparisons):
163
TestComparisons.setUp(self)
164
self.B = np.array([['efg', '123 '],
165
['051', 'tuv']], np.unicode_).view(np.chararray)
167
class TestComparisonsMixed2(TestComparisons):
171
TestComparisons.setUp(self)
172
self.A = np.array([['abc', '123'],
173
['789', 'xyz']], np.unicode_).view(np.chararray)
175
class TestInformation(TestCase):
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)
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]])
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]])
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]])
205
self.A.endswith('3', 'fdjk')
206
self.failUnlessRaises(TypeError, fail)
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]])
215
def test_index(self):
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)
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]])
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]])
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]])
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]])
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]])
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]])
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]])
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]])
257
def test_rindex(self):
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)
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]])
269
self.A.startswith('3', 'fdjk')
270
self.failUnlessRaises(TypeError, fail)
273
class TestMethods(TestCase):
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)
282
def test_capitalize(self):
283
assert issubclass(self.A.capitalize().dtype.type, np.string_)
284
assert_array_equal(self.A.capitalize(), [
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(), [
291
['12345', 'Mixedcase'],
292
['123 \t 345 \0 ', 'Upper']])
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, [
308
def test_decode(self):
309
A = np.char.array(['736563726574206d657373616765'])
310
assert A.decode('hex_codec')[0] == 'secret message'
312
def test_encode(self):
313
B = self.B.encode('unicode_escape')
314
assert B[0][0] == r' \u03a3 '
316
def test_expandtabs(self):
317
T = self.A.expandtabs()
318
assert T[2][0] == '123 345'
321
A = np.char.join([',', '#'], self.A)
322
assert issubclass(A.dtype.type, np.string_)
323
assert_array_equal(np.char.join([',', '#'], self.A), [
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']])
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, [
343
def test_lower(self):
344
assert issubclass(self.A.lower().dtype.type, np.string_)
345
assert_array_equal(self.A.lower(), [
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(), [
352
[u'12345', u'mixedcase'],
353
[u'123 \t 345 \0 ', u'upper']])
355
def test_lstrip(self):
356
assert issubclass(self.A.lstrip().dtype.type, np.string_)
357
assert_array_equal(self.A.lstrip(), [
359
['12345', 'MixedCase'],
360
['123 \t 345 \0 ', 'UPPER']])
361
assert_array_equal(self.A.lstrip(['1', 'M']), [
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(), [
368
['12345', 'MixedCase'],
369
['123 \t 345 \0 ', 'UPPER']])
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', '', '')]])
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, [
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, [
391
['12345', u'MixedC\u03a3se'],
392
['123 \t 345 \x00', 'UPPER']])
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, [
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')]])
417
def test_rsplit(self):
418
A = self.A.rsplit('3')
419
assert issubclass(A.dtype.type, np.object_)
420
assert_equal(A.tolist(), [
422
[['12', '45'], ['MixedCase']],
423
[['12', ' \t ', '45 \x00 '], ['UPPER']]])
425
def test_rstrip(self):
426
assert issubclass(self.A.rstrip().dtype.type, np.string_)
427
assert_array_equal(self.A.rstrip(), [
429
['12345', 'MixedCase'],
430
['123 \t 345', 'UPPER']])
431
assert_array_equal(self.A.rstrip(['5', 'ER']), [
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(), [
438
['12345', 'MixedCase'],
439
['123 \t 345', 'UPPER']])
441
def test_strip(self):
442
assert issubclass(self.A.strip().dtype.type, np.string_)
443
assert_array_equal(self.A.strip(), [
445
['12345', 'MixedCase'],
446
['123 \t 345', 'UPPER']])
447
assert_array_equal(self.A.strip(['15', 'EReM']), [
450
['23 \t 345 \x00', 'UPP']])
451
assert issubclass(self.B.strip().dtype.type, np.unicode_)
452
assert_array_equal(self.B.strip(), [
454
['12345', 'MixedCase'],
455
['123 \t 345', 'UPPER']])
457
def test_split(self):
458
A = self.A.split('3')
459
assert issubclass(A.dtype.type, np.object_)
460
assert_equal(A.tolist(), [
462
[['12', '45'], ['MixedCase']],
463
[['12', ' \t ', '45 \x00 '], ['UPPER']]])
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
471
def test_swapcase(self):
472
assert issubclass(self.A.swapcase().dtype.type, np.string_)
473
assert_array_equal(self.A.swapcase(), [
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(), [
480
[u'12345', u'mIXEDcASE'],
481
[u'123 \t 345 \0 ', u'upper']])
483
def test_title(self):
484
assert issubclass(self.A.title().dtype.type, np.string_)
485
assert_array_equal(self.A.title(), [
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(), [
492
[u'12345', u'Mixedcase'],
493
[u'123 \t 345 \0 ', u'Upper']])
495
def test_upper(self):
496
assert issubclass(self.A.upper().dtype.type, np.string_)
497
assert_array_equal(self.A.upper(), [
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(), [
504
[u'12345', u'MIXEDCASE'],
505
[u'123 \t 345 \0 ', u'UPPER']])
507
def test_isnumeric(self):
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]])
515
def test_isdecimal(self):
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]])
35
524
class TestOperations(TestCase):