blob: f291b3f91634876c30f210828afbba6df5b4bee2 [file] [log] [blame]
Barry Warsaw50f0e161999-06-10 22:53:10 +00001from test_support import verbose
2import string, sys
3
Barry Warsaw6e1d78a1999-06-15 16:49:11 +00004# XXX: kludge... short circuit if strings don't have methods
5try:
6 ''.join
7except AttributeError:
8 raise ImportError
9
Barry Warsaw50f0e161999-06-10 22:53:10 +000010def test(name, input, output, *args):
11 if verbose:
12 print 'string.%s%s =? %s... ' % (name, (input,) + args, output),
Barry Warsaw50f0e161999-06-10 22:53:10 +000013 try:
Barry Warsaw8a9514a1999-06-11 17:48:07 +000014 try:
15 f = getattr(string, name)
16 value = apply(f, (input,) + args)
17 except AttributeError:
18 f = getattr(input, name)
19 value = apply(f, args)
Barry Warsaw50f0e161999-06-10 22:53:10 +000020 except:
21 value = sys.exc_type
22 if value != output:
23 if verbose:
24 print 'no'
25 print f, `input`, `output`, `value`
26 else:
27 if verbose:
28 print 'yes'
29
30test('atoi', " 1 ", 1)
31test('atoi', " 1x", ValueError)
32test('atoi', " x1 ", ValueError)
33test('atol', " 1 ", 1L)
34test('atol', " 1x ", ValueError)
35test('atol', " x1 ", ValueError)
36test('atof', " 1 ", 1.0)
37test('atof', " 1x ", ValueError)
38test('atof', " x1 ", ValueError)
39
40test('capitalize', ' hello ', ' hello ')
41test('capitalize', 'hello ', 'Hello ')
42test('find', 'abcdefghiabc', 0, 'abc')
43test('find', 'abcdefghiabc', 9, 'abc', 1)
44test('find', 'abcdefghiabc', -1, 'def', 4)
45test('rfind', 'abcdefghiabc', 9, 'abc')
46test('lower', 'HeLLo', 'hello')
47test('lower', 'hello', 'hello')
48test('upper', 'HeLLo', 'HELLO')
49test('upper', 'HELLO', 'HELLO')
50
51transtable = '\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`xyzdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377'
52
53test('maketrans', 'abc', transtable, 'xyz')
54test('maketrans', 'abc', ValueError, 'xyzq')
55
56test('split', 'this is the split function',
57 ['this', 'is', 'the', 'split', 'function'])
58test('split', 'a|b|c|d', ['a', 'b', 'c', 'd'], '|')
59test('split', 'a|b|c|d', ['a', 'b', 'c|d'], '|', 2)
60test('split', 'a b c d', ['a', 'b c d'], None, 1)
61test('split', 'a b c d', ['a', 'b', 'c d'], None, 2)
62test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 3)
63test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 4)
64test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 0)
65test('split', 'a b c d', ['a', 'b', 'c d'], None, 2)
66
67# join now works with any sequence type
68class Sequence:
69 def __init__(self): self.seq = 'wxyz'
70 def __len__(self): return len(self.seq)
71 def __getitem__(self, i): return self.seq[i]
72
73test('join', ['a', 'b', 'c', 'd'], 'a b c d')
74test('join', ('a', 'b', 'c', 'd'), 'abcd', '')
75test('join', Sequence(), 'w x y z')
Barry Warsawd5258681999-06-14 18:38:42 +000076test('join', 7, TypeError)
77
78class BadStr:
79 def __str__(self): raise RuntimeError
80
81class BadSeq(Sequence):
82 def __init__(self): self.seq = [7, 'hello', BadStr()]
83
84test('join', BadSeq(), RuntimeError)
Barry Warsaw50f0e161999-06-10 22:53:10 +000085
86# try a few long ones
87print string.join(['x' * 100] * 100, ':')
88print string.join(('x' * 100,) * 100, ':')
89
90test('strip', ' hello ', 'hello')
91test('lstrip', ' hello ', 'hello ')
92test('rstrip', ' hello ', ' hello')
93test('strip', 'hello', 'hello')
94
95test('swapcase', 'HeLLo cOmpUteRs', 'hEllO CoMPuTErS')
96test('translate', 'xyzabcdef', 'xyzxyz', transtable, 'def')
97
98table = string.maketrans('a', 'A')
99test('translate', 'abc', 'Abc', table)
100test('translate', 'xyz', 'xyz', table)
101
102test('replace', 'one!two!three!', 'one@two!three!', '!', '@', 1)
103test('replace', 'one!two!three!', 'one@two@three!', '!', '@', 2)
104test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 3)
105test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 4)
106test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 0)
107test('replace', 'one!two!three!', 'one@two@three@', '!', '@')
108test('replace', 'one!two!three!', 'one!two!three!', 'x', '@')
109test('replace', 'one!two!three!', 'one!two!three!', 'x', '@', 2)
110
111test('startswith', 'hello', 1, 'he')
112test('startswith', 'hello', 1, 'hello')
113test('startswith', 'hello', 0, 'hello world')
114test('startswith', 'hello', 1, '')
115test('startswith', 'hello', 0, 'ello')
116test('startswith', 'hello', 1, 'ello', 1)
117test('startswith', 'hello', 1, 'o', 4)
118test('startswith', 'hello', 0, 'o', 5)
119test('startswith', 'hello', 1, '', 5)
120test('startswith', 'hello', 0, 'lo', 6)
Barry Warsaw122473f1999-06-11 17:51:13 +0000121test('startswith', 'helloworld', 1, 'lowo', 3)
122test('startswith', 'helloworld', 1, 'lowo', 3, 7)
123test('startswith', 'helloworld', 0, 'lowo', 3, 6)
Barry Warsaw50f0e161999-06-10 22:53:10 +0000124
125test('endswith', 'hello', 1, 'lo')
126test('endswith', 'hello', 0, 'he')
127test('endswith', 'hello', 1, '')
128test('endswith', 'hello', 0, 'hello world')
Barry Warsaw8a9514a1999-06-11 17:48:07 +0000129test('endswith', 'helloworld', 0, 'worl')
130test('endswith', 'helloworld', 1, 'worl', 3, 9)
131test('endswith', 'helloworld', 1, 'world', 3, 12)
132test('endswith', 'helloworld', 1, 'lowo', 1, 7)
133test('endswith', 'helloworld', 1, 'lowo', 2, 7)
134test('endswith', 'helloworld', 1, 'lowo', 3, 7)
135test('endswith', 'helloworld', 0, 'lowo', 4, 7)
136test('endswith', 'helloworld', 0, 'lowo', 3, 8)
Barry Warsaw6e1d78a1999-06-15 16:49:11 +0000137test('endswith', 'ab', 0, 'ab', 0, 1)
138test('endswith', 'ab', 0, 'ab', 0, 0)
Barry Warsaw50f0e161999-06-10 22:53:10 +0000139
140string.whitespace
141string.lowercase
142string.uppercase