annotate Lib/test/test_operator.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents b8a97ad5f7ce
children aedd2d0dcb31
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4777
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
1 import operator
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
2 import sys
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
3
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
4 def test(name, input, output, *args):
4825
b8a97ad5f7ce test_rotor.py: New test of the rotor module.
Barry Warsaw <barry@python.org>
parents: 4777
diff changeset
5 print 'testing:', name
4777
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
6 f = getattr(operator, name)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
7 params = (input,) + args
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
8 try:
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
9 val = apply(f, params)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
10 except:
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
11 val = sys.exc_type
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
12 if val <> output:
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
13 print '%s%s = %s: %s expected' % (f.__name__, params, `val`, `output`)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
14
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
15 test('abs', -1, 1)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
16 test('add', 3, 7, 4)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
17 test('and_', 0xf, 0xa, 0xa)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
18 test('concat', 'py', 'python', 'thon')
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
19
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
20 test('countOf', [1, 2, 1, 3, 1, 4], 1, 3)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
21
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
22 a = [4, 3, 2, 1]
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
23 test('delitem', a, None, 1)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
24 if a <> [4, 2, 1]:
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
25 print 'delitem() failed'
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
26
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
27 a = range(10)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
28 test('delslice', a, None, 2, 8)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
29 if a <> [0, 1, 8, 9]:
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
30 print 'delslice() failed'
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
31
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
32 a = range(10)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
33 test('div', 5, 2, 2)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
34 test('getitem', a, 2, 2)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
35 test('getslice', a, [4, 5], 4, 6)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
36 test('indexOf', [4, 3, 2, 1], 1, 3)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
37 test('inv', 4, -5)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
38 test('isCallable', 4, 0)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
39 test('isCallable', operator.isCallable, 1)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
40 test('isMappingType', operator.isMappingType, 0)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
41 test('isMappingType', operator.__dict__, 1)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
42 test('isNumberType', 8.3, 1)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
43 test('isNumberType', dir(), 0)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
44 test('isSequenceType', dir(), 1)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
45 test('isSequenceType', 'yeahbuddy', 1)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
46 test('isSequenceType', 3, 0)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
47 test('lshift', 5, 10, 1)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
48 test('mod', 5, 1, 2)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
49 test('mul', 5, 10, 2)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
50 test('neg', 5, -5)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
51 test('or_', 0xa, 0xf, 0x5)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
52 test('pos', -5, -5)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
53
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
54 a = range(3)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
55 test('repeat', a, a+a, 2)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
56 test('rshift', 5, 2, 1)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
57
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
58 test('sequenceIncludes', range(4), 1, 2)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
59 test('sequenceIncludes', range(4), 0, 5)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
60
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
61 test('setitem', a, None, 0, 2)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
62 if a <> [2, 1, 2]:
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
63 print 'setitem() failed'
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
64
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
65 a = range(4)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
66 test('setslice', a, None, 1, 3, [2, 1])
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
67 if a <> [0, 2, 1, 3]:
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
68 print 'setslice() failed:', a
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
69
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
70 test('sub', 5, 2, 3)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
71 test('truth', 5, 1)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
72 test('truth', [], 0)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
73 test('xor', 0xb, 0x7, 0xc)
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
74
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
75
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
76 # some negative tests
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
77 test('indexOf', [4, 3, 2, 1], ValueError, 9)