Barry Warsaw | da0e520 | 1997-01-06 22:46:07 +0000 | [diff] [blame] | 1 | from test_support import verbose |
Guido van Rossum | edaf1c9 | 1996-10-08 14:07:56 +0000 | [diff] [blame] | 2 | import strop, sys |
| 3 | |
Barry Warsaw | ad522fa | 1996-12-09 21:49:10 +0000 | [diff] [blame] | 4 | def test(name, input, output, *args): |
Barry Warsaw | da0e520 | 1997-01-06 22:46:07 +0000 | [diff] [blame] | 5 | if verbose: |
| 6 | print 'string.%s%s =? %s... ' % (name, (input,) + args, output), |
Guido van Rossum | edaf1c9 | 1996-10-08 14:07:56 +0000 | [diff] [blame] | 7 | f = getattr(strop, name) |
| 8 | try: |
Barry Warsaw | ad522fa | 1996-12-09 21:49:10 +0000 | [diff] [blame] | 9 | value = apply(f, (input,) + args) |
Guido van Rossum | edaf1c9 | 1996-10-08 14:07:56 +0000 | [diff] [blame] | 10 | except: |
| 11 | value = sys.exc_type |
Guido van Rossum | edaf1c9 | 1996-10-08 14:07:56 +0000 | [diff] [blame] | 12 | if value != output: |
Barry Warsaw | da0e520 | 1997-01-06 22:46:07 +0000 | [diff] [blame] | 13 | if verbose: |
| 14 | print 'no' |
Guido van Rossum | edaf1c9 | 1996-10-08 14:07:56 +0000 | [diff] [blame] | 15 | print f, `input`, `output`, `value` |
Barry Warsaw | da0e520 | 1997-01-06 22:46:07 +0000 | [diff] [blame] | 16 | else: |
| 17 | if verbose: |
| 18 | print 'yes' |
Guido van Rossum | edaf1c9 | 1996-10-08 14:07:56 +0000 | [diff] [blame] | 19 | |
| 20 | test('atoi', " 1 ", 1) |
| 21 | test('atoi', " 1x", ValueError) |
| 22 | test('atoi', " x1 ", ValueError) |
| 23 | test('atol', " 1 ", 1L) |
| 24 | test('atol', " 1x ", ValueError) |
| 25 | test('atol', " x1 ", ValueError) |
| 26 | test('atof', " 1 ", 1.0) |
| 27 | test('atof', " 1x ", ValueError) |
| 28 | test('atof', " x1 ", ValueError) |
Barry Warsaw | ad522fa | 1996-12-09 21:49:10 +0000 | [diff] [blame] | 29 | |
| 30 | test('capitalize', ' hello ', ' hello ') |
| 31 | test('capitalize', 'hello ', 'Hello ') |
| 32 | test('find', 'abcdefghiabc', 0, 'abc') |
| 33 | test('find', 'abcdefghiabc', 9, 'abc', 1) |
| 34 | test('find', 'abcdefghiabc', -1, 'def', 4) |
| 35 | test('rfind', 'abcdefghiabc', 9, 'abc') |
| 36 | test('lower', 'HeLLo', 'hello') |
| 37 | test('upper', 'HeLLo', 'HELLO') |
| 38 | |
| 39 | transtable = '\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 | |
| 41 | test('maketrans', 'abc', transtable, 'xyz') |
| 42 | test('maketrans', 'abc', ValueError, 'xyzq') |
| 43 | |
| 44 | test('split', 'this is the split function', |
| 45 | ['this', 'is', 'the', 'split', 'function']) |
| 46 | test('split', 'a|b|c|d', ['a', 'b', 'c', 'd'], '|') |
| 47 | test('split', 'a|b|c|d', ['a', 'b', 'c|d'], '|', 2) |
| 48 | |
Barry Warsaw | da0e520 | 1997-01-06 22:46:07 +0000 | [diff] [blame] | 49 | # join now works with any sequence type |
| 50 | class Sequence: |
| 51 | def __init__(self): self.seq = 'wxyz' |
| 52 | def __len__(self): return len(self.seq) |
| 53 | def __getitem__(self, i): return self.seq[i] |
| 54 | |
Barry Warsaw | ad522fa | 1996-12-09 21:49:10 +0000 | [diff] [blame] | 55 | test('join', ['a', 'b', 'c', 'd'], 'a b c d') |
Barry Warsaw | da0e520 | 1997-01-06 22:46:07 +0000 | [diff] [blame] | 56 | test('join', ('a', 'b', 'c', 'd'), 'abcd', '') |
| 57 | test('join', Sequence(), 'w x y z') |
| 58 | |
| 59 | # try a few long ones |
| 60 | print strop.join(['x' * 100] * 100, ':') |
| 61 | print strop.join(('x' * 100,) * 100, ':') |
Barry Warsaw | ad522fa | 1996-12-09 21:49:10 +0000 | [diff] [blame] | 62 | |
| 63 | test('strip', ' hello ', 'hello') |
| 64 | test('lstrip', ' hello ', 'hello ') |
| 65 | test('rstrip', ' hello ', ' hello') |
| 66 | |
| 67 | test('swapcase', 'HeLLo cOmpUteRs', 'hEllO CoMPuTErS') |
| 68 | test('translate', 'xyzabcdef', 'xyzxyz', transtable, 'def') |
| 69 | |
| 70 | strop.whitespace |
| 71 | strop.lowercase |
| 72 | strop.uppercase |