blob: 57700460da3b1d6bbe702c8bf307e9433b960f14 [file] [log] [blame]
Fredrik Lundhf7850422001-01-17 21:51:36 +00001from test_support import verbose
Guido van Rossumedaf1c91996-10-08 14:07:56 +00002import strop, sys
3
Barry Warsawad522fa1996-12-09 21:49:10 +00004def test(name, input, output, *args):
Barry Warsawda0e5201997-01-06 22:46:07 +00005 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +00006 print 'string.%s%s =? %s... ' % (name, (input,) + args, output),
Guido van Rossumedaf1c91996-10-08 14:07:56 +00007 f = getattr(strop, name)
8 try:
Guido van Rossum41360a41998-03-26 19:42:58 +00009 value = apply(f, (input,) + args)
Guido van Rossumedaf1c91996-10-08 14:07:56 +000010 except:
Fred Drake004d5e62000-10-23 17:22:08 +000011 value = sys.exc_type
Guido van Rossumedaf1c91996-10-08 14:07:56 +000012 if value != output:
Guido van Rossum41360a41998-03-26 19:42:58 +000013 if verbose:
14 print 'no'
15 print f, `input`, `output`, `value`
Barry Warsawda0e5201997-01-06 22:46:07 +000016 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000017 if verbose:
18 print 'yes'
Guido van Rossumedaf1c91996-10-08 14:07:56 +000019
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)
Barry Warsawad522fa1996-12-09 21:49:10 +000029
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('upper', 'HeLLo', 'HELLO')
38
39transtable = '\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'
40
41test('maketrans', 'abc', transtable, 'xyz')
42test('maketrans', 'abc', ValueError, 'xyzq')
43
44test('split', 'this is the split function',
45 ['this', 'is', 'the', 'split', 'function'])
46test('split', 'a|b|c|d', ['a', 'b', 'c', 'd'], '|')
47test('split', 'a|b|c|d', ['a', 'b', 'c|d'], '|', 2)
Barry Warsawc99a2391997-12-02 00:30:04 +000048test('split', 'a b c d', ['a', 'b c d'], None, 1)
49test('split', 'a b c d', ['a', 'b', 'c d'], None, 2)
50test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 3)
51test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 4)
52test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 0)
53test('split', 'a b c d', ['a', 'b', 'c d'], None, 2)
Barry Warsawad522fa1996-12-09 21:49:10 +000054
Barry Warsawda0e5201997-01-06 22:46:07 +000055# join now works with any sequence type
56class Sequence:
57 def __init__(self): self.seq = 'wxyz'
58 def __len__(self): return len(self.seq)
59 def __getitem__(self, i): return self.seq[i]
60
Barry Warsawad522fa1996-12-09 21:49:10 +000061test('join', ['a', 'b', 'c', 'd'], 'a b c d')
Barry Warsawda0e5201997-01-06 22:46:07 +000062test('join', ('a', 'b', 'c', 'd'), 'abcd', '')
63test('join', Sequence(), 'w x y z')
64
65# try a few long ones
66print strop.join(['x' * 100] * 100, ':')
67print strop.join(('x' * 100,) * 100, ':')
Barry Warsawad522fa1996-12-09 21:49:10 +000068
69test('strip', ' hello ', 'hello')
70test('lstrip', ' hello ', 'hello ')
71test('rstrip', ' hello ', ' hello')
72
73test('swapcase', 'HeLLo cOmpUteRs', 'hEllO CoMPuTErS')
74test('translate', 'xyzabcdef', 'xyzxyz', transtable, 'def')
75
Barry Warsaw91811901997-11-29 00:25:30 +000076test('replace', 'one!two!three!', 'one@two!three!', '!', '@', 1)
77test('replace', 'one!two!three!', 'one@two@three!', '!', '@', 2)
78test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 3)
79test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 4)
80test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 0)
81test('replace', 'one!two!three!', 'one@two@three@', '!', '@')
82test('replace', 'one!two!three!', 'one!two!three!', 'x', '@')
83test('replace', 'one!two!three!', 'one!two!three!', 'x', '@', 2)
84
Barry Warsawad522fa1996-12-09 21:49:10 +000085strop.whitespace
86strop.lowercase
87strop.uppercase