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