PyXR

c:\python24\lib \ test \ test_operator.py



0001 import operator
0002 import unittest
0003 
0004 from test import test_support
0005 
0006 
0007 class OperatorTestCase(unittest.TestCase):
0008     def test_lt(self):
0009         self.failUnlessRaises(TypeError, operator.lt)
0010         self.failUnlessRaises(TypeError, operator.lt, 1j, 2j)
0011         self.failIf(operator.lt(1, 0))
0012         self.failIf(operator.lt(1, 0.0))
0013         self.failIf(operator.lt(1, 1))
0014         self.failIf(operator.lt(1, 1.0))
0015         self.failUnless(operator.lt(1, 2))
0016         self.failUnless(operator.lt(1, 2.0))
0017 
0018     def test_le(self):
0019         self.failUnlessRaises(TypeError, operator.le)
0020         self.failUnlessRaises(TypeError, operator.le, 1j, 2j)
0021         self.failIf(operator.le(1, 0))
0022         self.failIf(operator.le(1, 0.0))
0023         self.failUnless(operator.le(1, 1))
0024         self.failUnless(operator.le(1, 1.0))
0025         self.failUnless(operator.le(1, 2))
0026         self.failUnless(operator.le(1, 2.0))
0027 
0028     def test_eq(self):
0029         class C(object):
0030             def __eq__(self, other):
0031                 raise SyntaxError
0032         self.failUnlessRaises(TypeError, operator.eq)
0033         self.failUnlessRaises(SyntaxError, operator.eq, C(), C())
0034         self.failIf(operator.eq(1, 0))
0035         self.failIf(operator.eq(1, 0.0))
0036         self.failUnless(operator.eq(1, 1))
0037         self.failUnless(operator.eq(1, 1.0))
0038         self.failIf(operator.eq(1, 2))
0039         self.failIf(operator.eq(1, 2.0))
0040 
0041     def test_ne(self):
0042         class C(object):
0043             def __ne__(self, other):
0044                 raise SyntaxError
0045         self.failUnlessRaises(TypeError, operator.ne)
0046         self.failUnlessRaises(SyntaxError, operator.ne, C(), C())
0047         self.failUnless(operator.ne(1, 0))
0048         self.failUnless(operator.ne(1, 0.0))
0049         self.failIf(operator.ne(1, 1))
0050         self.failIf(operator.ne(1, 1.0))
0051         self.failUnless(operator.ne(1, 2))
0052         self.failUnless(operator.ne(1, 2.0))
0053 
0054     def test_ge(self):
0055         self.failUnlessRaises(TypeError, operator.ge)
0056         self.failUnlessRaises(TypeError, operator.ge, 1j, 2j)
0057         self.failUnless(operator.ge(1, 0))
0058         self.failUnless(operator.ge(1, 0.0))
0059         self.failUnless(operator.ge(1, 1))
0060         self.failUnless(operator.ge(1, 1.0))
0061         self.failIf(operator.ge(1, 2))
0062         self.failIf(operator.ge(1, 2.0))
0063 
0064     def test_gt(self):
0065         self.failUnlessRaises(TypeError, operator.gt)
0066         self.failUnlessRaises(TypeError, operator.gt, 1j, 2j)
0067         self.failUnless(operator.gt(1, 0))
0068         self.failUnless(operator.gt(1, 0.0))
0069         self.failIf(operator.gt(1, 1))
0070         self.failIf(operator.gt(1, 1.0))
0071         self.failIf(operator.gt(1, 2))
0072         self.failIf(operator.gt(1, 2.0))
0073 
0074     def test_abs(self):
0075         self.failUnlessRaises(TypeError, operator.abs)
0076         self.failUnlessRaises(TypeError, operator.abs, None)
0077         self.failUnless(operator.abs(-1) == 1)
0078         self.failUnless(operator.abs(1) == 1)
0079 
0080     def test_add(self):
0081         self.failUnlessRaises(TypeError, operator.add)
0082         self.failUnlessRaises(TypeError, operator.add, None, None)
0083         self.failUnless(operator.add(3, 4) == 7)
0084 
0085     def test_bitwise_and(self):
0086         self.failUnlessRaises(TypeError, operator.and_)
0087         self.failUnlessRaises(TypeError, operator.and_, None, None)
0088         self.failUnless(operator.and_(0xf, 0xa) == 0xa)
0089 
0090     def test_concat(self):
0091         self.failUnlessRaises(TypeError, operator.concat)
0092         self.failUnlessRaises(TypeError, operator.concat, None, None)
0093         self.failUnless(operator.concat('py', 'thon') == 'python')
0094         self.failUnless(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
0095 
0096     def test_countOf(self):
0097         self.failUnlessRaises(TypeError, operator.countOf)
0098         self.failUnlessRaises(TypeError, operator.countOf, None, None)
0099         self.failUnless(operator.countOf([1, 2, 1, 3, 1, 4], 3) == 1)
0100         self.failUnless(operator.countOf([1, 2, 1, 3, 1, 4], 5) == 0)
0101 
0102     def test_delitem(self):
0103         a = [4, 3, 2, 1]
0104         self.failUnlessRaises(TypeError, operator.delitem, a)
0105         self.failUnlessRaises(TypeError, operator.delitem, a, None)
0106         self.failUnless(operator.delitem(a, 1) is None)
0107         self.assert_(a == [4, 2, 1])
0108 
0109     def test_delslice(self):
0110         a = range(10)
0111         self.failUnlessRaises(TypeError, operator.delslice, a)
0112         self.failUnlessRaises(TypeError, operator.delslice, a, None, None)
0113         self.failUnless(operator.delslice(a, 2, 8) is None)
0114         self.assert_(a == [0, 1, 8, 9])
0115 
0116     def test_div(self):
0117         self.failUnlessRaises(TypeError, operator.div, 5)
0118         self.failUnlessRaises(TypeError, operator.div, None, None)
0119         self.failUnless(operator.floordiv(5, 2) == 2)
0120 
0121     def test_floordiv(self):
0122         self.failUnlessRaises(TypeError, operator.floordiv, 5)
0123         self.failUnlessRaises(TypeError, operator.floordiv, None, None)
0124         self.failUnless(operator.floordiv(5, 2) == 2)
0125 
0126     def test_truediv(self):
0127         self.failUnlessRaises(TypeError, operator.truediv, 5)
0128         self.failUnlessRaises(TypeError, operator.truediv, None, None)
0129         self.failUnless(operator.truediv(5, 2) == 2.5)
0130 
0131     def test_getitem(self):
0132         a = range(10)
0133         self.failUnlessRaises(TypeError, operator.getitem)
0134         self.failUnlessRaises(TypeError, operator.getitem, a, None)
0135         self.failUnless(operator.getitem(a, 2) == 2)
0136 
0137     def test_getslice(self):
0138         a = range(10)
0139         self.failUnlessRaises(TypeError, operator.getslice)
0140         self.failUnlessRaises(TypeError, operator.getslice, a, None, None)
0141         self.failUnless(operator.getslice(a, 4, 6) == [4, 5])
0142 
0143     def test_indexOf(self):
0144         self.failUnlessRaises(TypeError, operator.indexOf)
0145         self.failUnlessRaises(TypeError, operator.indexOf, None, None)
0146         self.failUnless(operator.indexOf([4, 3, 2, 1], 3) == 1)
0147         self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0)
0148 
0149     def test_invert(self):
0150         self.failUnlessRaises(TypeError, operator.invert)
0151         self.failUnlessRaises(TypeError, operator.invert, None)
0152         self.failUnless(operator.inv(4) == -5)
0153 
0154     def test_isCallable(self):
0155         self.failUnlessRaises(TypeError, operator.isCallable)
0156         class C:
0157             pass
0158         def check(self, o, v):
0159             self.assert_(operator.isCallable(o) == callable(o) == v)
0160         check(self, 4, 0)
0161         check(self, operator.isCallable, 1)
0162         check(self, C, 1)
0163         check(self, C(), 0)
0164 
0165     def test_isMappingType(self):
0166         self.failUnlessRaises(TypeError, operator.isMappingType)
0167         self.failIf(operator.isMappingType(1))
0168         self.failIf(operator.isMappingType(operator.isMappingType))
0169         self.failUnless(operator.isMappingType(operator.__dict__))
0170         self.failUnless(operator.isMappingType({}))
0171 
0172     def test_isNumberType(self):
0173         self.failUnlessRaises(TypeError, operator.isNumberType)
0174         self.failUnless(operator.isNumberType(8))
0175         self.failUnless(operator.isNumberType(8j))
0176         self.failUnless(operator.isNumberType(8L))
0177         self.failUnless(operator.isNumberType(8.3))
0178         self.failIf(operator.isNumberType(dir()))
0179 
0180     def test_isSequenceType(self):
0181         self.failUnlessRaises(TypeError, operator.isSequenceType)
0182         self.failUnless(operator.isSequenceType(dir()))
0183         self.failUnless(operator.isSequenceType(()))
0184         self.failUnless(operator.isSequenceType(xrange(10)))
0185         self.failUnless(operator.isSequenceType('yeahbuddy'))
0186         self.failIf(operator.isSequenceType(3))
0187 
0188     def test_lshift(self):
0189         self.failUnlessRaises(TypeError, operator.lshift)
0190         self.failUnlessRaises(TypeError, operator.lshift, None, 42)
0191         self.failUnless(operator.lshift(5, 1) == 10)
0192         self.failUnless(operator.lshift(5, 0) == 5)
0193         self.assertRaises(ValueError, operator.lshift, 2, -1)
0194 
0195     def test_mod(self):
0196         self.failUnlessRaises(TypeError, operator.mod)
0197         self.failUnlessRaises(TypeError, operator.mod, None, 42)
0198         self.failUnless(operator.mod(5, 2) == 1)
0199 
0200     def test_mul(self):
0201         self.failUnlessRaises(TypeError, operator.mul)
0202         self.failUnlessRaises(TypeError, operator.mul, None, None)
0203         self.failUnless(operator.mul(5, 2) == 10)
0204 
0205     def test_neg(self):
0206         self.failUnlessRaises(TypeError, operator.neg)
0207         self.failUnlessRaises(TypeError, operator.neg, None)
0208         self.failUnless(operator.neg(5) == -5)
0209         self.failUnless(operator.neg(-5) == 5)
0210         self.failUnless(operator.neg(0) == 0)
0211         self.failUnless(operator.neg(-0) == 0)
0212 
0213     def test_bitwise_or(self):
0214         self.failUnlessRaises(TypeError, operator.or_)
0215         self.failUnlessRaises(TypeError, operator.or_, None, None)
0216         self.failUnless(operator.or_(0xa, 0x5) == 0xf)
0217 
0218     def test_pos(self):
0219         self.failUnlessRaises(TypeError, operator.pos)
0220         self.failUnlessRaises(TypeError, operator.pos, None)
0221         self.failUnless(operator.pos(5) == 5)
0222         self.failUnless(operator.pos(-5) == -5)
0223         self.failUnless(operator.pos(0) == 0)
0224         self.failUnless(operator.pos(-0) == 0)
0225 
0226     def test_pow(self):
0227         self.failUnlessRaises(TypeError, operator.pow)
0228         self.failUnlessRaises(TypeError, operator.pow, None, None)
0229         self.failUnless(operator.pow(3,5) == 3**5)
0230         self.failUnless(operator.__pow__(3,5) == 3**5)
0231         self.assertRaises(TypeError, operator.pow, 1)
0232         self.assertRaises(TypeError, operator.pow, 1, 2, 3)
0233 
0234     def test_repeat(self):
0235         a = range(3)
0236         self.failUnlessRaises(TypeError, operator.repeat)
0237         self.failUnlessRaises(TypeError, operator.repeat, a, None)
0238         self.failUnless(operator.repeat(a, 2) == a+a)
0239         self.failUnless(operator.repeat(a, 1) == a)
0240         self.failUnless(operator.repeat(a, 0) == [])
0241         a = (1, 2, 3)
0242         self.failUnless(operator.repeat(a, 2) == a+a)
0243         self.failUnless(operator.repeat(a, 1) == a)
0244         self.failUnless(operator.repeat(a, 0) == ())
0245         a = '123'
0246         self.failUnless(operator.repeat(a, 2) == a+a)
0247         self.failUnless(operator.repeat(a, 1) == a)
0248         self.failUnless(operator.repeat(a, 0) == '')
0249 
0250     def test_rshift(self):
0251         self.failUnlessRaises(TypeError, operator.rshift)
0252         self.failUnlessRaises(TypeError, operator.rshift, None, 42)
0253         self.failUnless(operator.rshift(5, 1) == 2)
0254         self.failUnless(operator.rshift(5, 0) == 5)
0255         self.assertRaises(ValueError, operator.rshift, 2, -1)
0256 
0257     def test_contains(self):
0258         self.failUnlessRaises(TypeError, operator.contains)
0259         self.failUnlessRaises(TypeError, operator.contains, None, None)
0260         self.failUnless(operator.contains(range(4), 2))
0261         self.failIf(operator.contains(range(4), 5))
0262         self.failUnless(operator.sequenceIncludes(range(4), 2))
0263         self.failIf(operator.sequenceIncludes(range(4), 5))
0264 
0265     def test_setitem(self):
0266         a = range(3)
0267         self.failUnlessRaises(TypeError, operator.setitem, a)
0268         self.failUnlessRaises(TypeError, operator.setitem, a, None, None)
0269         self.failUnless(operator.setitem(a, 0, 2) is None)
0270         self.assert_(a == [2, 1, 2])
0271         self.assertRaises(IndexError, operator.setitem, a, 4, 2)
0272 
0273     def test_setslice(self):
0274         a = range(4)
0275         self.failUnlessRaises(TypeError, operator.setslice, a)
0276         self.failUnlessRaises(TypeError, operator.setslice, a, None, None, None)
0277         self.failUnless(operator.setslice(a, 1, 3, [2, 1]) is None)
0278         self.assert_(a == [0, 2, 1, 3])
0279 
0280     def test_sub(self):
0281         self.failUnlessRaises(TypeError, operator.sub)
0282         self.failUnlessRaises(TypeError, operator.sub, None, None)
0283         self.failUnless(operator.sub(5, 2) == 3)
0284 
0285     def test_truth(self):
0286         class C(object):
0287             def __nonzero__(self):
0288                 raise SyntaxError
0289         self.failUnlessRaises(TypeError, operator.truth)
0290         self.failUnlessRaises(SyntaxError, operator.truth, C())
0291         self.failUnless(operator.truth(5))
0292         self.failUnless(operator.truth([0]))
0293         self.failIf(operator.truth(0))
0294         self.failIf(operator.truth([]))
0295 
0296     def test_bitwise_xor(self):
0297         self.failUnlessRaises(TypeError, operator.xor)
0298         self.failUnlessRaises(TypeError, operator.xor, None, None)
0299         self.failUnless(operator.xor(0xb, 0xc) == 0x7)
0300 
0301     def test_is(self):
0302         a = b = 'xyzpdq'
0303         c = a[:3] + b[3:]
0304         self.failUnlessRaises(TypeError, operator.is_)
0305         self.failUnless(operator.is_(a, b))
0306         self.failIf(operator.is_(a,c))
0307 
0308     def test_is_not(self):
0309         a = b = 'xyzpdq'
0310         c = a[:3] + b[3:]
0311         self.failUnlessRaises(TypeError, operator.is_not)
0312         self.failIf(operator.is_not(a, b))
0313         self.failUnless(operator.is_not(a,c))
0314 
0315     def test_attrgetter(self):
0316         class A:
0317             pass
0318         a = A()
0319         a.name = 'arthur'
0320         f = operator.attrgetter('name')
0321         self.assertEqual(f(a), 'arthur')
0322         f = operator.attrgetter('rank')
0323         self.assertRaises(AttributeError, f, a)
0324         f = operator.attrgetter(2)
0325         self.assertRaises(TypeError, f, a)
0326         self.assertRaises(TypeError, operator.attrgetter)
0327         self.assertRaises(TypeError, operator.attrgetter, 1, 2)
0328 
0329         class C(object):
0330             def __getattr(self, name):
0331                 raise SyntaxError
0332         self.failUnlessRaises(AttributeError, operator.attrgetter('foo'), C())
0333 
0334     def test_itemgetter(self):
0335         a = 'ABCDE'
0336         f = operator.itemgetter(2)
0337         self.assertEqual(f(a), 'C')
0338         f = operator.itemgetter(10)
0339         self.assertRaises(IndexError, f, a)
0340 
0341         class C(object):
0342             def __getitem(self, name):
0343                 raise SyntaxError
0344         self.failUnlessRaises(TypeError, operator.itemgetter(42), C())
0345 
0346         f = operator.itemgetter('name')
0347         self.assertRaises(TypeError, f, a)
0348         self.assertRaises(TypeError, operator.itemgetter)
0349         self.assertRaises(TypeError, operator.itemgetter, 1, 2)
0350 
0351         d = dict(key='val')
0352         f = operator.itemgetter('key')
0353         self.assertEqual(f(d), 'val')
0354         f = operator.itemgetter('nonkey')
0355         self.assertRaises(KeyError, f, d)
0356 
0357         # example used in the docs
0358         inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
0359         getcount = operator.itemgetter(1)
0360         self.assertEqual(map(getcount, inventory), [3, 2, 5, 1])
0361         self.assertEqual(sorted(inventory, key=getcount),
0362             [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)])
0363 
0364 def test_main():
0365     test_support.run_unittest(OperatorTestCase)
0366 
0367 
0368 if __name__ == "__main__":
0369     test_main()
0370 

Generated by PyXR 0.9.4
SourceForge.net Logo