summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_operator.py
blob: 439d340ce3c86ffbb4e826901c0927202fa7a086 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import operator
import unittest

import test_support


class OperatorTestCase(unittest.TestCase):
    def test_lt(self):
        self.failIf(operator.lt(1, 0))
        self.failIf(operator.lt(1, 0.0))
        self.failIf(operator.lt(1, 1))
        self.failIf(operator.lt(1, 1.0))
        self.failUnless(operator.lt(1, 2))
        self.failUnless(operator.lt(1, 2.0))

    def test_le(self):
        self.failIf(operator.le(1, 0))
        self.failIf(operator.le(1, 0.0))
        self.failUnless(operator.le(1, 1))
        self.failUnless(operator.le(1, 1.0))
        self.failUnless(operator.le(1, 2))
        self.failUnless(operator.le(1, 2.0))

    def test_eq(self):
        self.failIf(operator.eq(1, 0))
        self.failIf(operator.eq(1, 0.0))
        self.failUnless(operator.eq(1, 1))
        self.failUnless(operator.eq(1, 1.0))
        self.failIf(operator.eq(1, 2))
        self.failIf(operator.eq(1, 2.0))

    def test_ne(self):
        self.failUnless(operator.ne(1, 0))
        self.failUnless(operator.ne(1, 0.0))
        self.failIf(operator.ne(1, 1))
        self.failIf(operator.ne(1, 1.0))
        self.failUnless(operator.ne(1, 2))
        self.failUnless(operator.ne(1, 2.0))

    def test_ge(self):
        self.failUnless(operator.ge(1, 0))
        self.failUnless(operator.ge(1, 0.0))
        self.failUnless(operator.ge(1, 1))
        self.failUnless(operator.ge(1, 1.0))
        self.failIf(operator.ge(1, 2))
        self.failIf(operator.ge(1, 2.0))

    def test_gt(self):
        self.failUnless(operator.gt(1, 0))
        self.failUnless(operator.gt(1, 0.0))
        self.failIf(operator.gt(1, 1))
        self.failIf(operator.gt(1, 1.0))
        self.failIf(operator.gt(1, 2))
        self.failIf(operator.gt(1, 2.0))

    def test_abs(self):
        self.failUnless(operator.abs(-1) == 1)
        self.failUnless(operator.abs(1) == 1)

    def test_add(self):
        self.failUnless(operator.add(3, 4) == 7)

    def test_bitwise_and(self):
        self.failUnless(operator.and_(0xf, 0xa) == 0xa)

    def test_concat(self):
        self.failUnless(operator.concat('py', 'thon') == 'python')
        self.failUnless(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])

    def test_countOf(self):
        self.failUnless(operator.countOf([1, 2, 1, 3, 1, 4], 3) == 1)
        self.failUnless(operator.countOf([1, 2, 1, 3, 1, 4], 5) == 0)

    def test_delitem(self):
        a = [4, 3, 2, 1]
        self.failUnless(operator.delitem(a, 1) is None)
        self.assert_(a == [4, 2, 1])

    def test_delslice(self):
        a = range(10)
        self.failUnless(operator.delslice(a, 2, 8) is None)
        self.assert_(a == [0, 1, 8, 9])

    def test_div(self):
        self.failUnless(operator.floordiv(5, 2) == 2)

    def test_floordiv(self):
        self.failUnless(operator.floordiv(5, 2) == 2)

    def test_truediv(self):
        self.failUnless(operator.truediv(5, 2) == 2.5)

    def test_getitem(self):
        a = range(10)
        self.failUnless(operator.getitem(a, 2) == 2)

    def test_getslice(self):
        a = range(10)
        self.failUnless(operator.getslice(a, 4, 6) == [4, 5])

    def test_indexOf(self):
        self.failUnless(operator.indexOf([4, 3, 2, 1], 3) == 1)
        self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0)

    def test_invert(self):
        self.failUnless(operator.inv(4) == -5)

    def test_isCallable(self):
        class C:
            pass
        def check(self, o, v):
            self.assert_(operator.isCallable(o) == callable(o) == v)
        check(self, 4, 0)
        check(self, operator.isCallable, 1)
        check(self, C, 1)
        check(self, C(), 0)

    def test_isMappingType(self):
        self.failIf(operator.isMappingType(1))
        self.failIf(operator.isMappingType(operator.isMappingType))
        self.failUnless(operator.isMappingType(operator.__dict__))
        self.failUnless(operator.isMappingType({}))

    def test_isNumberType(self):
        self.failUnless(operator.isNumberType(8))
        self.failUnless(operator.isNumberType(8j))
        self.failUnless(operator.isNumberType(8L))
        self.failUnless(operator.isNumberType(8.3))
        self.failIf(operator.isNumberType(dir()))

    def test_isSequenceType(self):
        self.failUnless(operator.isSequenceType(dir()))
        self.failUnless(operator.isSequenceType(()))
        self.failUnless(operator.isSequenceType(xrange(10)))
        self.failUnless(operator.isSequenceType('yeahbuddy'))
        self.failIf(operator.isSequenceType(3))

    def test_lshift(self):
        self.failUnless(operator.lshift(5, 1) == 10)
        self.failUnless(operator.lshift(5, 0) == 5)
        self.assertRaises(ValueError, operator.lshift, 2, -1)

    def test_mod(self):
        self.failUnless(operator.mod(5, 2) == 1)

    def test_mul(self):
        self.failUnless(operator.mul(5, 2) == 10)

    def test_neg(self):
        self.failUnless(operator.neg(5) == -5)
        self.failUnless(operator.neg(-5) == 5)
        self.failUnless(operator.neg(0) == 0)
        self.failUnless(operator.neg(-0) == 0)

    def test_bitwise_or(self):
        self.failUnless(operator.or_(0xa, 0x5) == 0xf)

    def test_pos(self):
        self.failUnless(operator.pos(5) == 5)
        self.failUnless(operator.pos(-5) == -5)
        self.failUnless(operator.pos(0) == 0)
        self.failUnless(operator.pos(-0) == 0)

    def test_repeat(self):
        a = range(3)
        self.failUnless(operator.repeat(a, 2) == a+a)
        self.failUnless(operator.repeat(a, 1) == a)
        self.failUnless(operator.repeat(a, 0) == [])
        a = (1, 2, 3)
        self.failUnless(operator.repeat(a, 2) == a+a)
        self.failUnless(operator.repeat(a, 1) == a)
        self.failUnless(operator.repeat(a, 0) == ())
        a = '123'
        self.failUnless(operator.repeat(a, 2) == a+a)
        self.failUnless(operator.repeat(a, 1) == a)
        self.failUnless(operator.repeat(a, 0) == '')

    def test_rshift(self):
        self.failUnless(operator.rshift(5, 1) == 2)
        self.failUnless(operator.rshift(5, 0) == 5)
        self.assertRaises(ValueError, operator.rshift, 2, -1)

    def test_contains(self):
        self.failUnless(operator.contains(range(4), 2))
        self.failIf(operator.contains(range(4), 5))
        self.failUnless(operator.sequenceIncludes(range(4), 2))
        self.failIf(operator.sequenceIncludes(range(4), 5))

    def test_setitem(self):
        a = range(3)
        self.failUnless(operator.setitem(a, 0, 2) is None)
        self.assert_(a == [2, 1, 2])
        self.assertRaises(IndexError, operator.setitem, a, 4, 2)

    def test_setslice(self):
        a = range(4)
        self.failUnless(operator.setslice(a, 1, 3, [2, 1]) is None)
        self.assert_(a == [0, 2, 1, 3])

    def test_sub(self):
        self.failUnless(operator.sub(5, 2) == 3)

    def test_truth(self):
        self.failUnless(operator.truth(5))
        self.failUnless(operator.truth([0]))
        self.failIf(operator.truth(0))
        self.failIf(operator.truth([]))

    def test_bitwise_xor(self):
        self.failUnless(operator.xor(0xb, 0xc) == 0x7)


def test_main():
    test_support.run_unittest(OperatorTestCase)


if __name__ == "__main__":
    test_main()