blob: b75c5ba78f72f55fc217fa77042885b3e9e38055 [file] [log] [blame]
Barry Warsawa09ec191996-12-18 19:56:22 +00001import operator
2import sys
3
4def test(name, input, output, *args):
Barry Warsaw5e056bb1996-12-23 23:39:42 +00005 print 'testing:', name
Barry Warsawa09ec191996-12-18 19:56:22 +00006 f = getattr(operator, name)
7 params = (input,) + args
8 try:
Guido van Rossum41360a41998-03-26 19:42:58 +00009 val = apply(f, params)
Barry Warsawa09ec191996-12-18 19:56:22 +000010 except:
Guido van Rossum41360a41998-03-26 19:42:58 +000011 val = sys.exc_type
Fred Drake132dce22000-12-12 23:11:42 +000012 if val != output:
Guido van Rossum41360a41998-03-26 19:42:58 +000013 print '%s%s = %s: %s expected' % (f.__name__, params, `val`, `output`)
Barry Warsawa09ec191996-12-18 19:56:22 +000014
15test('abs', -1, 1)
16test('add', 3, 7, 4)
17test('and_', 0xf, 0xa, 0xa)
18test('concat', 'py', 'python', 'thon')
19
20test('countOf', [1, 2, 1, 3, 1, 4], 1, 3)
21
22a = [4, 3, 2, 1]
23test('delitem', a, None, 1)
Fred Drake132dce22000-12-12 23:11:42 +000024if a != [4, 2, 1]:
Barry Warsawa09ec191996-12-18 19:56:22 +000025 print 'delitem() failed'
26
27a = range(10)
28test('delslice', a, None, 2, 8)
Fred Drake132dce22000-12-12 23:11:42 +000029if a != [0, 1, 8, 9]:
Barry Warsawa09ec191996-12-18 19:56:22 +000030 print 'delslice() failed'
31
32a = range(10)
33test('div', 5, 2, 2)
34test('getitem', a, 2, 2)
35test('getslice', a, [4, 5], 4, 6)
36test('indexOf', [4, 3, 2, 1], 1, 3)
37test('inv', 4, -5)
38test('isCallable', 4, 0)
39test('isCallable', operator.isCallable, 1)
40test('isMappingType', operator.isMappingType, 0)
41test('isMappingType', operator.__dict__, 1)
42test('isNumberType', 8.3, 1)
43test('isNumberType', dir(), 0)
44test('isSequenceType', dir(), 1)
45test('isSequenceType', 'yeahbuddy', 1)
46test('isSequenceType', 3, 0)
47test('lshift', 5, 10, 1)
48test('mod', 5, 1, 2)
49test('mul', 5, 10, 2)
50test('neg', 5, -5)
51test('or_', 0xa, 0xf, 0x5)
52test('pos', -5, -5)
53
54a = range(3)
55test('repeat', a, a+a, 2)
56test('rshift', 5, 2, 1)
57
58test('sequenceIncludes', range(4), 1, 2)
59test('sequenceIncludes', range(4), 0, 5)
60
61test('setitem', a, None, 0, 2)
Fred Drake132dce22000-12-12 23:11:42 +000062if a != [2, 1, 2]:
Barry Warsawa09ec191996-12-18 19:56:22 +000063 print 'setitem() failed'
64
65a = range(4)
66test('setslice', a, None, 1, 3, [2, 1])
Fred Drake132dce22000-12-12 23:11:42 +000067if a != [0, 2, 1, 3]:
Barry Warsawa09ec191996-12-18 19:56:22 +000068 print 'setslice() failed:', a
69
70test('sub', 5, 2, 3)
71test('truth', 5, 1)
72test('truth', [], 0)
73test('xor', 0xb, 0x7, 0xc)
74
75
76# some negative tests
77test('indexOf', [4, 3, 2, 1], ValueError, 9)