blob: 2fa9dd57be34b1faf02f080ebe754a7fe5bce0da [file] [log] [blame]
Barry Warsaw50f0e161999-06-10 22:53:10 +00001from test_support import verbose
2import string, sys
3
4def test(name, input, output, *args):
5 if verbose:
6 print 'string.%s%s =? %s... ' % (name, (input,) + args, output),
Barry Warsaw50f0e161999-06-10 22:53:10 +00007 try:
Barry Warsaw8a9514a1999-06-11 17:48:07 +00008 try:
9 f = getattr(string, name)
10 value = apply(f, (input,) + args)
11 except AttributeError:
12 f = getattr(input, name)
13 value = apply(f, args)
Barry Warsaw50f0e161999-06-10 22:53:10 +000014 except:
15 value = sys.exc_type
16 if value != output:
17 if verbose:
18 print 'no'
19 print f, `input`, `output`, `value`
20 else:
21 if verbose:
22 print 'yes'
23
24test('atoi', " 1 ", 1)
25test('atoi', " 1x", ValueError)
26test('atoi', " x1 ", ValueError)
27test('atol', " 1 ", 1L)
28test('atol', " 1x ", ValueError)
29test('atol', " x1 ", ValueError)
30test('atof', " 1 ", 1.0)
31test('atof', " 1x ", ValueError)
32test('atof', " x1 ", ValueError)
33
34test('capitalize', ' hello ', ' hello ')
35test('capitalize', 'hello ', 'Hello ')
36test('find', 'abcdefghiabc', 0, 'abc')
37test('find', 'abcdefghiabc', 9, 'abc', 1)
38test('find', 'abcdefghiabc', -1, 'def', 4)
39test('rfind', 'abcdefghiabc', 9, 'abc')
40test('lower', 'HeLLo', 'hello')
41test('lower', 'hello', 'hello')
42test('upper', 'HeLLo', 'HELLO')
43test('upper', 'HELLO', 'HELLO')
44
45transtable = '\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'
46
47test('maketrans', 'abc', transtable, 'xyz')
48test('maketrans', 'abc', ValueError, 'xyzq')
49
50test('split', 'this is the split function',
51 ['this', 'is', 'the', 'split', 'function'])
52test('split', 'a|b|c|d', ['a', 'b', 'c', 'd'], '|')
53test('split', 'a|b|c|d', ['a', 'b', 'c|d'], '|', 2)
54test('split', 'a b c d', ['a', 'b c d'], None, 1)
55test('split', 'a b c d', ['a', 'b', 'c d'], None, 2)
56test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 3)
57test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 4)
58test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 0)
59test('split', 'a b c d', ['a', 'b', 'c d'], None, 2)
60
61# join now works with any sequence type
62class Sequence:
63 def __init__(self): self.seq = 'wxyz'
64 def __len__(self): return len(self.seq)
65 def __getitem__(self, i): return self.seq[i]
66
67test('join', ['a', 'b', 'c', 'd'], 'a b c d')
68test('join', ('a', 'b', 'c', 'd'), 'abcd', '')
69test('join', Sequence(), 'w x y z')
70
71# try a few long ones
72print string.join(['x' * 100] * 100, ':')
73print string.join(('x' * 100,) * 100, ':')
74
75test('strip', ' hello ', 'hello')
76test('lstrip', ' hello ', 'hello ')
77test('rstrip', ' hello ', ' hello')
78test('strip', 'hello', 'hello')
79
80test('swapcase', 'HeLLo cOmpUteRs', 'hEllO CoMPuTErS')
81test('translate', 'xyzabcdef', 'xyzxyz', transtable, 'def')
82
83table = string.maketrans('a', 'A')
84test('translate', 'abc', 'Abc', table)
85test('translate', 'xyz', 'xyz', table)
86
87test('replace', 'one!two!three!', 'one@two!three!', '!', '@', 1)
88test('replace', 'one!two!three!', 'one@two@three!', '!', '@', 2)
89test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 3)
90test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 4)
91test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 0)
92test('replace', 'one!two!three!', 'one@two@three@', '!', '@')
93test('replace', 'one!two!three!', 'one!two!three!', 'x', '@')
94test('replace', 'one!two!three!', 'one!two!three!', 'x', '@', 2)
95
96test('startswith', 'hello', 1, 'he')
97test('startswith', 'hello', 1, 'hello')
98test('startswith', 'hello', 0, 'hello world')
99test('startswith', 'hello', 1, '')
100test('startswith', 'hello', 0, 'ello')
101test('startswith', 'hello', 1, 'ello', 1)
102test('startswith', 'hello', 1, 'o', 4)
103test('startswith', 'hello', 0, 'o', 5)
104test('startswith', 'hello', 1, '', 5)
105test('startswith', 'hello', 0, 'lo', 6)
106
107test('endswith', 'hello', 1, 'lo')
108test('endswith', 'hello', 0, 'he')
109test('endswith', 'hello', 1, '')
110test('endswith', 'hello', 0, 'hello world')
Barry Warsaw8a9514a1999-06-11 17:48:07 +0000111test('endswith', 'helloworld', 0, 'worl')
112test('endswith', 'helloworld', 1, 'worl', 3, 9)
113test('endswith', 'helloworld', 1, 'world', 3, 12)
114test('endswith', 'helloworld', 1, 'lowo', 1, 7)
115test('endswith', 'helloworld', 1, 'lowo', 2, 7)
116test('endswith', 'helloworld', 1, 'lowo', 3, 7)
117test('endswith', 'helloworld', 0, 'lowo', 4, 7)
118test('endswith', 'helloworld', 0, 'lowo', 3, 8)
Barry Warsaw50f0e161999-06-10 22:53:10 +0000119
120string.whitespace
121string.lowercase
122string.uppercase