Skip to content

Commit a09ec19

Browse files
committed
Test of the operator module
1 parent 3990042 commit a09ec19

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

Lib/test/output/test_operator

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test_operator

Lib/test/test_operator.py

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

Lib/test/testall.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
'test_fcntl',
3030
'test_gdbm',
3131
'test_grp',
32+
'test_operator',
3233
]
3334

3435
if __name__ == '__main__':

0 commit comments

Comments
 (0)