Guido van Rossum | 3e06ab1 | 2000-06-29 19:35:29 +0000 | [diff] [blame^] | 1 | import operator |
| 2 | import sys |
| 3 | |
| 4 | def test(name, input, output, *args): |
| 5 | print 'testing:', name |
| 6 | f = getattr(operator, name) |
| 7 | params = (input,) + args |
| 8 | try: |
| 9 | val = apply(f, params) |
| 10 | except: |
| 11 | val = sys.exc_type |
| 12 | if val <> output: |
| 13 | print '%s%s = %s: %s expected' % (f.__name__, params, `val`, `output`) |
| 14 | |
| 15 | test('abs', -1, 1) |
| 16 | test('add', 3, 7, 4) |
| 17 | test('and_', 0xf, 0xa, 0xa) |
| 18 | test('concat', 'py', 'python', 'thon') |
| 19 | |
| 20 | test('countOf', [1, 2, 1, 3, 1, 4], 1, 3) |
| 21 | |
| 22 | a = [4, 3, 2, 1] |
| 23 | test('delitem', a, None, 1) |
| 24 | if a <> [4, 2, 1]: |
| 25 | print 'delitem() failed' |
| 26 | |
| 27 | a = range(10) |
| 28 | test('delslice', a, None, 2, 8) |
| 29 | if a <> [0, 1, 8, 9]: |
| 30 | print 'delslice() failed' |
| 31 | |
| 32 | a = range(10) |
| 33 | test('div', 5, 2, 2) |
| 34 | test('getitem', a, 2, 2) |
| 35 | test('getslice', a, [4, 5], 4, 6) |
| 36 | test('indexOf', [4, 3, 2, 1], 1, 3) |
| 37 | test('inv', 4, -5) |
| 38 | test('isCallable', 4, 0) |
| 39 | test('isCallable', operator.isCallable, 1) |
| 40 | test('isMappingType', operator.isMappingType, 0) |
| 41 | test('isMappingType', operator.__dict__, 1) |
| 42 | test('isNumberType', 8.3, 1) |
| 43 | test('isNumberType', dir(), 0) |
| 44 | test('isSequenceType', dir(), 1) |
| 45 | test('isSequenceType', 'yeahbuddy', 1) |
| 46 | test('isSequenceType', 3, 0) |
| 47 | test('lshift', 5, 10, 1) |
| 48 | test('mod', 5, 1, 2) |
| 49 | test('mul', 5, 10, 2) |
| 50 | test('neg', 5, -5) |
| 51 | test('or_', 0xa, 0xf, 0x5) |
| 52 | test('pos', -5, -5) |
| 53 | |
| 54 | a = range(3) |
| 55 | test('repeat', a, a+a, 2) |
| 56 | test('rshift', 5, 2, 1) |
| 57 | |
| 58 | test('sequenceIncludes', range(4), 1, 2) |
| 59 | test('sequenceIncludes', range(4), 0, 5) |
| 60 | |
| 61 | test('setitem', a, None, 0, 2) |
| 62 | if a <> [2, 1, 2]: |
| 63 | print 'setitem() failed' |
| 64 | |
| 65 | a = range(4) |
| 66 | test('setslice', a, None, 1, 3, [2, 1]) |
| 67 | if a <> [0, 2, 1, 3]: |
| 68 | print 'setslice() failed:', a |
| 69 | |
| 70 | test('sub', 5, 2, 3) |
| 71 | test('truth', 5, 1) |
| 72 | test('truth', [], 0) |
| 73 | test('xor', 0xb, 0x7, 0xc) |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 74 | |
| 75 | |
Guido van Rossum | 3e06ab1 | 2000-06-29 19:35:29 +0000 | [diff] [blame^] | 76 | # some negative tests |
| 77 | test('indexOf', [4, 3, 2, 1], ValueError, 9) |