blob: 424cf521dc904c040b841d0c812dc175542e8320 [file] [log] [blame]
Guido van Rossum28d4ba21996-09-11 19:07:45 +00001import strop, sys
2
Guido van Rossum5de1f8d1996-12-10 16:02:14 +00003def test(name, input, output, *args):
Guido van Rossum28d4ba21996-09-11 19:07:45 +00004 f = getattr(strop, name)
5 try:
Guido van Rossum5de1f8d1996-12-10 16:02:14 +00006 value = apply(f, (input,) + args)
Guido van Rossum28d4ba21996-09-11 19:07:45 +00007 except:
8 value = sys.exc_type
Guido van Rossum28d4ba21996-09-11 19:07:45 +00009 if value != output:
10 print f, `input`, `output`, `value`
11
12test('atoi', " 1 ", 1)
13test('atoi', " 1x", ValueError)
14test('atoi', " x1 ", ValueError)
15test('atol', " 1 ", 1L)
16test('atol', " 1x ", ValueError)
17test('atol', " x1 ", ValueError)
18test('atof', " 1 ", 1.0)
19test('atof', " 1x ", ValueError)
20test('atof', " x1 ", ValueError)
Guido van Rossum5de1f8d1996-12-10 16:02:14 +000021
22test('capitalize', ' hello ', ' hello ')
23test('capitalize', 'hello ', 'Hello ')
24test('find', 'abcdefghiabc', 0, 'abc')
25test('find', 'abcdefghiabc', 9, 'abc', 1)
26test('find', 'abcdefghiabc', -1, 'def', 4)
27test('rfind', 'abcdefghiabc', 9, 'abc')
28test('lower', 'HeLLo', 'hello')
29test('upper', 'HeLLo', 'HELLO')
30
31transtable = '\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'
32
33test('maketrans', 'abc', transtable, 'xyz')
34test('maketrans', 'abc', ValueError, 'xyzq')
35
36test('split', 'this is the split function',
37 ['this', 'is', 'the', 'split', 'function'])
38test('split', 'a|b|c|d', ['a', 'b', 'c', 'd'], '|')
39test('split', 'a|b|c|d', ['a', 'b', 'c|d'], '|', 2)
40
41test('join', ['a', 'b', 'c', 'd'], 'a b c d')
42test('join', ['a', 'b', 'c', 'd'], 'abcd', '')
43
44test('strip', ' hello ', 'hello')
45test('lstrip', ' hello ', 'hello ')
46test('rstrip', ' hello ', ' hello')
47
48test('swapcase', 'HeLLo cOmpUteRs', 'hEllO CoMPuTErS')
49test('translate', 'xyzabcdef', 'xyzxyz', transtable, 'def')
50
51strop.whitespace
52strop.lowercase
53strop.uppercase