Barry Warsaw | a09ec19 | 1996-12-18 19:56:22 +0000 | [diff] [blame] | 1 | import operator |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 2 | import unittest |
Barry Warsaw | a09ec19 | 1996-12-18 19:56:22 +0000 | [diff] [blame] | 3 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Barry Warsaw | a09ec19 | 1996-12-18 19:56:22 +0000 | [diff] [blame] | 5 | |
Armin Rigo | fd163f9 | 2005-12-29 15:59:19 +0000 | [diff] [blame] | 6 | class Seq1: |
| 7 | def __init__(self, lst): |
| 8 | self.lst = lst |
| 9 | def __len__(self): |
| 10 | return len(self.lst) |
| 11 | def __getitem__(self, i): |
| 12 | return self.lst[i] |
| 13 | def __add__(self, other): |
| 14 | return self.lst + other.lst |
| 15 | def __mul__(self, other): |
| 16 | return self.lst * other |
| 17 | def __rmul__(self, other): |
| 18 | return other * self.lst |
| 19 | |
| 20 | class Seq2(object): |
| 21 | def __init__(self, lst): |
| 22 | self.lst = lst |
| 23 | def __len__(self): |
| 24 | return len(self.lst) |
| 25 | def __getitem__(self, i): |
| 26 | return self.lst[i] |
| 27 | def __add__(self, other): |
| 28 | return self.lst + other.lst |
| 29 | def __mul__(self, other): |
| 30 | return self.lst * other |
| 31 | def __rmul__(self, other): |
| 32 | return other * self.lst |
| 33 | |
Barry Warsaw | a09ec19 | 1996-12-18 19:56:22 +0000 | [diff] [blame] | 34 | |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 35 | class OperatorTestCase(unittest.TestCase): |
| 36 | def test_lt(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 37 | self.failUnlessRaises(TypeError, operator.lt) |
| 38 | self.failUnlessRaises(TypeError, operator.lt, 1j, 2j) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 39 | self.failIf(operator.lt(1, 0)) |
| 40 | self.failIf(operator.lt(1, 0.0)) |
| 41 | self.failIf(operator.lt(1, 1)) |
| 42 | self.failIf(operator.lt(1, 1.0)) |
| 43 | self.failUnless(operator.lt(1, 2)) |
| 44 | self.failUnless(operator.lt(1, 2.0)) |
| 45 | |
| 46 | def test_le(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 47 | self.failUnlessRaises(TypeError, operator.le) |
| 48 | self.failUnlessRaises(TypeError, operator.le, 1j, 2j) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 49 | self.failIf(operator.le(1, 0)) |
| 50 | self.failIf(operator.le(1, 0.0)) |
| 51 | self.failUnless(operator.le(1, 1)) |
| 52 | self.failUnless(operator.le(1, 1.0)) |
| 53 | self.failUnless(operator.le(1, 2)) |
| 54 | self.failUnless(operator.le(1, 2.0)) |
| 55 | |
| 56 | def test_eq(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 57 | class C(object): |
| 58 | def __eq__(self, other): |
| 59 | raise SyntaxError |
| 60 | self.failUnlessRaises(TypeError, operator.eq) |
| 61 | self.failUnlessRaises(SyntaxError, operator.eq, C(), C()) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 62 | self.failIf(operator.eq(1, 0)) |
| 63 | self.failIf(operator.eq(1, 0.0)) |
| 64 | self.failUnless(operator.eq(1, 1)) |
| 65 | self.failUnless(operator.eq(1, 1.0)) |
| 66 | self.failIf(operator.eq(1, 2)) |
| 67 | self.failIf(operator.eq(1, 2.0)) |
| 68 | |
| 69 | def test_ne(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 70 | class C(object): |
| 71 | def __ne__(self, other): |
| 72 | raise SyntaxError |
| 73 | self.failUnlessRaises(TypeError, operator.ne) |
| 74 | self.failUnlessRaises(SyntaxError, operator.ne, C(), C()) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 75 | self.failUnless(operator.ne(1, 0)) |
| 76 | self.failUnless(operator.ne(1, 0.0)) |
| 77 | self.failIf(operator.ne(1, 1)) |
| 78 | self.failIf(operator.ne(1, 1.0)) |
| 79 | self.failUnless(operator.ne(1, 2)) |
| 80 | self.failUnless(operator.ne(1, 2.0)) |
| 81 | |
| 82 | def test_ge(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 83 | self.failUnlessRaises(TypeError, operator.ge) |
| 84 | self.failUnlessRaises(TypeError, operator.ge, 1j, 2j) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 85 | self.failUnless(operator.ge(1, 0)) |
| 86 | self.failUnless(operator.ge(1, 0.0)) |
| 87 | self.failUnless(operator.ge(1, 1)) |
| 88 | self.failUnless(operator.ge(1, 1.0)) |
| 89 | self.failIf(operator.ge(1, 2)) |
| 90 | self.failIf(operator.ge(1, 2.0)) |
| 91 | |
| 92 | def test_gt(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 93 | self.failUnlessRaises(TypeError, operator.gt) |
| 94 | self.failUnlessRaises(TypeError, operator.gt, 1j, 2j) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 95 | self.failUnless(operator.gt(1, 0)) |
| 96 | self.failUnless(operator.gt(1, 0.0)) |
| 97 | self.failIf(operator.gt(1, 1)) |
| 98 | self.failIf(operator.gt(1, 1.0)) |
| 99 | self.failIf(operator.gt(1, 2)) |
| 100 | self.failIf(operator.gt(1, 2.0)) |
| 101 | |
| 102 | def test_abs(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 103 | self.failUnlessRaises(TypeError, operator.abs) |
| 104 | self.failUnlessRaises(TypeError, operator.abs, None) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 105 | self.assertEqual(operator.abs(-1), 1) |
| 106 | self.assertEqual(operator.abs(1), 1) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 107 | |
| 108 | def test_add(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 109 | self.failUnlessRaises(TypeError, operator.add) |
| 110 | self.failUnlessRaises(TypeError, operator.add, None, None) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 111 | self.failUnless(operator.add(3, 4) == 7) |
| 112 | |
| 113 | def test_bitwise_and(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 114 | self.failUnlessRaises(TypeError, operator.and_) |
| 115 | self.failUnlessRaises(TypeError, operator.and_, None, None) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 116 | self.failUnless(operator.and_(0xf, 0xa) == 0xa) |
| 117 | |
| 118 | def test_concat(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 119 | self.failUnlessRaises(TypeError, operator.concat) |
| 120 | self.failUnlessRaises(TypeError, operator.concat, None, None) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 121 | self.failUnless(operator.concat('py', 'thon') == 'python') |
| 122 | self.failUnless(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4]) |
Armin Rigo | fd163f9 | 2005-12-29 15:59:19 +0000 | [diff] [blame] | 123 | self.failUnless(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7]) |
| 124 | self.failUnless(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7]) |
| 125 | self.failUnlessRaises(TypeError, operator.concat, 13, 29) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 126 | |
| 127 | def test_countOf(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 128 | self.failUnlessRaises(TypeError, operator.countOf) |
| 129 | self.failUnlessRaises(TypeError, operator.countOf, None, None) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 130 | self.failUnless(operator.countOf([1, 2, 1, 3, 1, 4], 3) == 1) |
| 131 | self.failUnless(operator.countOf([1, 2, 1, 3, 1, 4], 5) == 0) |
| 132 | |
| 133 | def test_delitem(self): |
| 134 | a = [4, 3, 2, 1] |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 135 | self.failUnlessRaises(TypeError, operator.delitem, a) |
| 136 | self.failUnlessRaises(TypeError, operator.delitem, a, None) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 137 | self.failUnless(operator.delitem(a, 1) is None) |
| 138 | self.assert_(a == [4, 2, 1]) |
| 139 | |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 140 | def test_floordiv(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 141 | self.failUnlessRaises(TypeError, operator.floordiv, 5) |
| 142 | self.failUnlessRaises(TypeError, operator.floordiv, None, None) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 143 | self.failUnless(operator.floordiv(5, 2) == 2) |
| 144 | |
| 145 | def test_truediv(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 146 | self.failUnlessRaises(TypeError, operator.truediv, 5) |
| 147 | self.failUnlessRaises(TypeError, operator.truediv, None, None) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 148 | self.failUnless(operator.truediv(5, 2) == 2.5) |
| 149 | |
| 150 | def test_getitem(self): |
| 151 | a = range(10) |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 152 | self.failUnlessRaises(TypeError, operator.getitem) |
| 153 | self.failUnlessRaises(TypeError, operator.getitem, a, None) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 154 | self.failUnless(operator.getitem(a, 2) == 2) |
| 155 | |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 156 | def test_indexOf(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 157 | self.failUnlessRaises(TypeError, operator.indexOf) |
| 158 | self.failUnlessRaises(TypeError, operator.indexOf, None, None) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 159 | self.failUnless(operator.indexOf([4, 3, 2, 1], 3) == 1) |
| 160 | self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0) |
| 161 | |
| 162 | def test_invert(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 163 | self.failUnlessRaises(TypeError, operator.invert) |
| 164 | self.failUnlessRaises(TypeError, operator.invert, None) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 165 | self.assertEqual(operator.inv(4), -5) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 166 | |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 167 | def test_isMappingType(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 168 | self.failUnlessRaises(TypeError, operator.isMappingType) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 169 | self.failIf(operator.isMappingType(1)) |
| 170 | self.failIf(operator.isMappingType(operator.isMappingType)) |
| 171 | self.failUnless(operator.isMappingType(operator.__dict__)) |
| 172 | self.failUnless(operator.isMappingType({})) |
| 173 | |
| 174 | def test_isNumberType(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 175 | self.failUnlessRaises(TypeError, operator.isNumberType) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 176 | self.failUnless(operator.isNumberType(8)) |
| 177 | self.failUnless(operator.isNumberType(8j)) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 178 | self.failUnless(operator.isNumberType(8)) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 179 | self.failUnless(operator.isNumberType(8.3)) |
| 180 | self.failIf(operator.isNumberType(dir())) |
| 181 | |
| 182 | def test_isSequenceType(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 183 | self.failUnlessRaises(TypeError, operator.isSequenceType) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 184 | self.failUnless(operator.isSequenceType(dir())) |
| 185 | self.failUnless(operator.isSequenceType(())) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 186 | self.failUnless(operator.isSequenceType(range(10))) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 187 | self.failUnless(operator.isSequenceType('yeahbuddy')) |
| 188 | self.failIf(operator.isSequenceType(3)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 189 | class Dict(dict): pass |
| 190 | self.failIf(operator.isSequenceType(Dict())) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 191 | |
| 192 | def test_lshift(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 193 | self.failUnlessRaises(TypeError, operator.lshift) |
| 194 | self.failUnlessRaises(TypeError, operator.lshift, None, 42) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 195 | self.failUnless(operator.lshift(5, 1) == 10) |
| 196 | self.failUnless(operator.lshift(5, 0) == 5) |
| 197 | self.assertRaises(ValueError, operator.lshift, 2, -1) |
| 198 | |
| 199 | def test_mod(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 200 | self.failUnlessRaises(TypeError, operator.mod) |
| 201 | self.failUnlessRaises(TypeError, operator.mod, None, 42) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 202 | self.failUnless(operator.mod(5, 2) == 1) |
| 203 | |
| 204 | def test_mul(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 205 | self.failUnlessRaises(TypeError, operator.mul) |
| 206 | self.failUnlessRaises(TypeError, operator.mul, None, None) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 207 | self.failUnless(operator.mul(5, 2) == 10) |
| 208 | |
| 209 | def test_neg(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 210 | self.failUnlessRaises(TypeError, operator.neg) |
| 211 | self.failUnlessRaises(TypeError, operator.neg, None) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 212 | self.assertEqual(operator.neg(5), -5) |
| 213 | self.assertEqual(operator.neg(-5), 5) |
| 214 | self.assertEqual(operator.neg(0), 0) |
| 215 | self.assertEqual(operator.neg(-0), 0) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 216 | |
| 217 | def test_bitwise_or(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 218 | self.failUnlessRaises(TypeError, operator.or_) |
| 219 | self.failUnlessRaises(TypeError, operator.or_, None, None) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 220 | self.failUnless(operator.or_(0xa, 0x5) == 0xf) |
| 221 | |
| 222 | def test_pos(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 223 | self.failUnlessRaises(TypeError, operator.pos) |
| 224 | self.failUnlessRaises(TypeError, operator.pos, None) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 225 | self.assertEqual(operator.pos(5), 5) |
| 226 | self.assertEqual(operator.pos(-5), -5) |
| 227 | self.assertEqual(operator.pos(0), 0) |
| 228 | self.assertEqual(operator.pos(-0), 0) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 229 | |
Raymond Hettinger | 5959c55 | 2002-08-19 03:19:09 +0000 | [diff] [blame] | 230 | def test_pow(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 231 | self.failUnlessRaises(TypeError, operator.pow) |
| 232 | self.failUnlessRaises(TypeError, operator.pow, None, None) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 233 | self.assertEqual(operator.pow(3,5), 3**5) |
| 234 | self.assertEqual(operator.__pow__(3,5), 3**5) |
Raymond Hettinger | 5959c55 | 2002-08-19 03:19:09 +0000 | [diff] [blame] | 235 | self.assertRaises(TypeError, operator.pow, 1) |
| 236 | self.assertRaises(TypeError, operator.pow, 1, 2, 3) |
| 237 | |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 238 | def test_repeat(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 239 | a = list(range(3)) |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 240 | self.failUnlessRaises(TypeError, operator.repeat) |
| 241 | self.failUnlessRaises(TypeError, operator.repeat, a, None) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 242 | self.failUnless(operator.repeat(a, 2) == a+a) |
| 243 | self.failUnless(operator.repeat(a, 1) == a) |
| 244 | self.failUnless(operator.repeat(a, 0) == []) |
| 245 | a = (1, 2, 3) |
| 246 | self.failUnless(operator.repeat(a, 2) == a+a) |
| 247 | self.failUnless(operator.repeat(a, 1) == a) |
| 248 | self.failUnless(operator.repeat(a, 0) == ()) |
| 249 | a = '123' |
| 250 | self.failUnless(operator.repeat(a, 2) == a+a) |
| 251 | self.failUnless(operator.repeat(a, 1) == a) |
| 252 | self.failUnless(operator.repeat(a, 0) == '') |
Armin Rigo | fd163f9 | 2005-12-29 15:59:19 +0000 | [diff] [blame] | 253 | a = Seq1([4, 5, 6]) |
| 254 | self.failUnless(operator.repeat(a, 2) == [4, 5, 6, 4, 5, 6]) |
| 255 | self.failUnless(operator.repeat(a, 1) == [4, 5, 6]) |
| 256 | self.failUnless(operator.repeat(a, 0) == []) |
| 257 | a = Seq2([4, 5, 6]) |
| 258 | self.failUnless(operator.repeat(a, 2) == [4, 5, 6, 4, 5, 6]) |
| 259 | self.failUnless(operator.repeat(a, 1) == [4, 5, 6]) |
| 260 | self.failUnless(operator.repeat(a, 0) == []) |
| 261 | self.failUnlessRaises(TypeError, operator.repeat, 6, 7) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 262 | |
| 263 | def test_rshift(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 264 | self.failUnlessRaises(TypeError, operator.rshift) |
| 265 | self.failUnlessRaises(TypeError, operator.rshift, None, 42) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 266 | self.failUnless(operator.rshift(5, 1) == 2) |
| 267 | self.failUnless(operator.rshift(5, 0) == 5) |
| 268 | self.assertRaises(ValueError, operator.rshift, 2, -1) |
| 269 | |
| 270 | def test_contains(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 271 | self.failUnlessRaises(TypeError, operator.contains) |
| 272 | self.failUnlessRaises(TypeError, operator.contains, None, None) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 273 | self.failUnless(operator.contains(range(4), 2)) |
| 274 | self.failIf(operator.contains(range(4), 5)) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 275 | |
| 276 | def test_setitem(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 277 | a = list(range(3)) |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 278 | self.failUnlessRaises(TypeError, operator.setitem, a) |
| 279 | self.failUnlessRaises(TypeError, operator.setitem, a, None, None) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 280 | self.failUnless(operator.setitem(a, 0, 2) is None) |
| 281 | self.assert_(a == [2, 1, 2]) |
| 282 | self.assertRaises(IndexError, operator.setitem, a, 4, 2) |
| 283 | |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 284 | def test_sub(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 285 | self.failUnlessRaises(TypeError, operator.sub) |
| 286 | self.failUnlessRaises(TypeError, operator.sub, None, None) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 287 | self.failUnless(operator.sub(5, 2) == 3) |
| 288 | |
| 289 | def test_truth(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 290 | class C(object): |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 291 | def __bool__(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 292 | raise SyntaxError |
| 293 | self.failUnlessRaises(TypeError, operator.truth) |
| 294 | self.failUnlessRaises(SyntaxError, operator.truth, C()) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 295 | self.failUnless(operator.truth(5)) |
| 296 | self.failUnless(operator.truth([0])) |
| 297 | self.failIf(operator.truth(0)) |
| 298 | self.failIf(operator.truth([])) |
| 299 | |
| 300 | def test_bitwise_xor(self): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 301 | self.failUnlessRaises(TypeError, operator.xor) |
| 302 | self.failUnlessRaises(TypeError, operator.xor, None, None) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 303 | self.failUnless(operator.xor(0xb, 0xc) == 0x7) |
| 304 | |
Raymond Hettinger | 9543b34 | 2003-01-18 23:22:20 +0000 | [diff] [blame] | 305 | def test_is(self): |
| 306 | a = b = 'xyzpdq' |
| 307 | c = a[:3] + b[3:] |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 308 | self.failUnlessRaises(TypeError, operator.is_) |
Raymond Hettinger | 9543b34 | 2003-01-18 23:22:20 +0000 | [diff] [blame] | 309 | self.failUnless(operator.is_(a, b)) |
| 310 | self.failIf(operator.is_(a,c)) |
| 311 | |
| 312 | def test_is_not(self): |
| 313 | a = b = 'xyzpdq' |
| 314 | c = a[:3] + b[3:] |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 315 | self.failUnlessRaises(TypeError, operator.is_not) |
Raymond Hettinger | 9543b34 | 2003-01-18 23:22:20 +0000 | [diff] [blame] | 316 | self.failIf(operator.is_not(a, b)) |
| 317 | self.failUnless(operator.is_not(a,c)) |
Fred Drake | cd112f5 | 2001-08-11 03:21:35 +0000 | [diff] [blame] | 318 | |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 319 | def test_attrgetter(self): |
| 320 | class A: |
| 321 | pass |
| 322 | a = A() |
| 323 | a.name = 'arthur' |
| 324 | f = operator.attrgetter('name') |
| 325 | self.assertEqual(f(a), 'arthur') |
| 326 | f = operator.attrgetter('rank') |
| 327 | self.assertRaises(AttributeError, f, a) |
| 328 | f = operator.attrgetter(2) |
| 329 | self.assertRaises(TypeError, f, a) |
| 330 | self.assertRaises(TypeError, operator.attrgetter) |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 331 | |
| 332 | # multiple gets |
| 333 | record = A() |
| 334 | record.x = 'X' |
| 335 | record.y = 'Y' |
| 336 | record.z = 'Z' |
| 337 | self.assertEqual(operator.attrgetter('x','z','y')(record), ('X', 'Z', 'Y')) |
| 338 | self.assertRaises(TypeError, operator.attrgetter('x', (), 'y'), record) |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 339 | |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 340 | class C(object): |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 341 | def __getattr__(self, name): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 342 | raise SyntaxError |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 343 | self.failUnlessRaises(SyntaxError, operator.attrgetter('foo'), C()) |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 344 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 345 | # recursive gets |
| 346 | a = A() |
| 347 | a.name = 'arthur' |
| 348 | a.child = A() |
| 349 | a.child.name = 'thomas' |
| 350 | f = operator.attrgetter('child.name') |
| 351 | self.assertEqual(f(a), 'thomas') |
| 352 | self.assertRaises(AttributeError, f, a.child) |
| 353 | f = operator.attrgetter('name', 'child.name') |
| 354 | self.assertEqual(f(a), ('arthur', 'thomas')) |
| 355 | f = operator.attrgetter('name', 'child.name', 'child.child.name') |
| 356 | self.assertRaises(AttributeError, f, a) |
| 357 | |
| 358 | a.child.child = A() |
| 359 | a.child.child.name = 'johnson' |
| 360 | f = operator.attrgetter('child.child.name') |
| 361 | self.assertEqual(f(a), 'johnson') |
| 362 | f = operator.attrgetter('name', 'child.name', 'child.child.name') |
| 363 | self.assertEqual(f(a), ('arthur', 'thomas', 'johnson')) |
| 364 | |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 365 | def test_itemgetter(self): |
| 366 | a = 'ABCDE' |
| 367 | f = operator.itemgetter(2) |
| 368 | self.assertEqual(f(a), 'C') |
| 369 | f = operator.itemgetter(10) |
| 370 | self.assertRaises(IndexError, f, a) |
| 371 | |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 372 | class C(object): |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 373 | def __getitem__(self, name): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 374 | raise SyntaxError |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 375 | self.failUnlessRaises(SyntaxError, operator.itemgetter(42), C()) |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 376 | |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 377 | f = operator.itemgetter('name') |
| 378 | self.assertRaises(TypeError, f, a) |
| 379 | self.assertRaises(TypeError, operator.itemgetter) |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 380 | |
| 381 | d = dict(key='val') |
| 382 | f = operator.itemgetter('key') |
| 383 | self.assertEqual(f(d), 'val') |
| 384 | f = operator.itemgetter('nonkey') |
| 385 | self.assertRaises(KeyError, f, d) |
| 386 | |
| 387 | # example used in the docs |
| 388 | inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)] |
| 389 | getcount = operator.itemgetter(1) |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 390 | self.assertEqual(list(map(getcount, inventory)), [3, 2, 5, 1]) |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 391 | self.assertEqual(sorted(inventory, key=getcount), |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 392 | [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]) |
| 393 | |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 394 | # multiple gets |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 395 | data = list(map(str, range(20))) |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 396 | self.assertEqual(operator.itemgetter(2,10,5)(data), ('2', '10', '5')) |
| 397 | self.assertRaises(TypeError, operator.itemgetter(2, 'x', 5), data) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 398 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 399 | def test_methodcaller(self): |
| 400 | self.assertRaises(TypeError, operator.methodcaller) |
| 401 | class A: |
| 402 | def foo(self, *args, **kwds): |
| 403 | return args[0] + args[1] |
| 404 | def bar(self, f=42): |
| 405 | return f |
| 406 | a = A() |
| 407 | f = operator.methodcaller('foo') |
| 408 | self.assertRaises(IndexError, f, a) |
| 409 | f = operator.methodcaller('foo', 1, 2) |
| 410 | self.assertEquals(f(a), 3) |
| 411 | f = operator.methodcaller('bar') |
| 412 | self.assertEquals(f(a), 42) |
| 413 | self.assertRaises(TypeError, f, a, a) |
| 414 | f = operator.methodcaller('bar', f=5) |
| 415 | self.assertEquals(f(a), 5) |
| 416 | |
Armin Rigo | f5bd3b4 | 2005-12-29 16:50:42 +0000 | [diff] [blame] | 417 | def test_inplace(self): |
| 418 | class C(object): |
| 419 | def __iadd__ (self, other): return "iadd" |
| 420 | def __iand__ (self, other): return "iand" |
Armin Rigo | f5bd3b4 | 2005-12-29 16:50:42 +0000 | [diff] [blame] | 421 | def __ifloordiv__(self, other): return "ifloordiv" |
| 422 | def __ilshift__ (self, other): return "ilshift" |
| 423 | def __imod__ (self, other): return "imod" |
| 424 | def __imul__ (self, other): return "imul" |
| 425 | def __ior__ (self, other): return "ior" |
| 426 | def __ipow__ (self, other): return "ipow" |
| 427 | def __irshift__ (self, other): return "irshift" |
| 428 | def __isub__ (self, other): return "isub" |
| 429 | def __itruediv__ (self, other): return "itruediv" |
| 430 | def __ixor__ (self, other): return "ixor" |
| 431 | def __getitem__(self, other): return 5 # so that C is a sequence |
| 432 | c = C() |
| 433 | self.assertEqual(operator.iadd (c, 5), "iadd") |
| 434 | self.assertEqual(operator.iand (c, 5), "iand") |
Armin Rigo | f5bd3b4 | 2005-12-29 16:50:42 +0000 | [diff] [blame] | 435 | self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv") |
| 436 | self.assertEqual(operator.ilshift (c, 5), "ilshift") |
| 437 | self.assertEqual(operator.imod (c, 5), "imod") |
| 438 | self.assertEqual(operator.imul (c, 5), "imul") |
| 439 | self.assertEqual(operator.ior (c, 5), "ior") |
| 440 | self.assertEqual(operator.ipow (c, 5), "ipow") |
| 441 | self.assertEqual(operator.irshift (c, 5), "irshift") |
| 442 | self.assertEqual(operator.isub (c, 5), "isub") |
| 443 | self.assertEqual(operator.itruediv (c, 5), "itruediv") |
| 444 | self.assertEqual(operator.ixor (c, 5), "ixor") |
| 445 | self.assertEqual(operator.iconcat (c, c), "iadd") |
| 446 | self.assertEqual(operator.irepeat (c, 5), "imul") |
| 447 | self.assertEqual(operator.__iadd__ (c, 5), "iadd") |
| 448 | self.assertEqual(operator.__iand__ (c, 5), "iand") |
Armin Rigo | f5bd3b4 | 2005-12-29 16:50:42 +0000 | [diff] [blame] | 449 | self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv") |
| 450 | self.assertEqual(operator.__ilshift__ (c, 5), "ilshift") |
| 451 | self.assertEqual(operator.__imod__ (c, 5), "imod") |
| 452 | self.assertEqual(operator.__imul__ (c, 5), "imul") |
| 453 | self.assertEqual(operator.__ior__ (c, 5), "ior") |
| 454 | self.assertEqual(operator.__ipow__ (c, 5), "ipow") |
| 455 | self.assertEqual(operator.__irshift__ (c, 5), "irshift") |
| 456 | self.assertEqual(operator.__isub__ (c, 5), "isub") |
| 457 | self.assertEqual(operator.__itruediv__ (c, 5), "itruediv") |
| 458 | self.assertEqual(operator.__ixor__ (c, 5), "ixor") |
| 459 | self.assertEqual(operator.__iconcat__ (c, c), "iadd") |
| 460 | self.assertEqual(operator.__irepeat__ (c, 5), "imul") |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 461 | |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 462 | def test_main(verbose=None): |
| 463 | import sys |
| 464 | test_classes = ( |
| 465 | OperatorTestCase, |
| 466 | ) |
| 467 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 468 | support.run_unittest(*test_classes) |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 469 | |
| 470 | # verify reference counting |
| 471 | if verbose and hasattr(sys, "gettotalrefcount"): |
| 472 | import gc |
| 473 | counts = [None] * 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 474 | for i in range(len(counts)): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 475 | support.run_unittest(*test_classes) |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 476 | gc.collect() |
| 477 | counts[i] = sys.gettotalrefcount() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 478 | print(counts) |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 479 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 480 | if __name__ == "__main__": |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 481 | test_main(verbose=True) |