Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1 | "Test the functionality of Python classes implementing operators." |
| 2 | |
Guido van Rossum | 2312024 | 2001-01-18 23:47:15 +0000 | [diff] [blame] | 3 | from test_support import TestFailed |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 4 | |
| 5 | testmeths = [ |
| 6 | |
| 7 | # Binary operations |
| 8 | "add", |
| 9 | "radd", |
| 10 | "sub", |
| 11 | "rsub", |
| 12 | "mul", |
| 13 | "rmul", |
| 14 | "div", |
| 15 | "rdiv", |
| 16 | "mod", |
| 17 | "rmod", |
| 18 | "divmod", |
| 19 | "rdivmod", |
| 20 | "pow", |
| 21 | "rpow", |
| 22 | "rshift", |
| 23 | "rrshift", |
| 24 | "lshift", |
| 25 | "rlshift", |
| 26 | "and", |
| 27 | "rand", |
| 28 | "or", |
| 29 | "ror", |
| 30 | "xor", |
| 31 | "rxor", |
| 32 | |
| 33 | # List/dict operations |
| 34 | "contains", |
| 35 | "getitem", |
| 36 | "getslice", |
| 37 | "setitem", |
| 38 | "setslice", |
| 39 | "delitem", |
| 40 | "delslice", |
| 41 | |
| 42 | # Unary operations |
| 43 | "neg", |
| 44 | "pos", |
| 45 | "abs", |
| 46 | "int", |
| 47 | "long", |
| 48 | "float", |
| 49 | "oct", |
| 50 | "hex", |
| 51 | |
| 52 | # generic operations |
| 53 | "init", |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 54 | ] |
| 55 | |
| 56 | # These need to return something other than None |
| 57 | # "coerce", |
| 58 | # "hash", |
| 59 | # "str", |
| 60 | # "repr", |
| 61 | |
| 62 | # These are separate because they can influence the test of other methods. |
| 63 | # "getattr", |
| 64 | # "setattr", |
| 65 | # "delattr", |
| 66 | |
| 67 | class AllTests: |
| 68 | def __coerce__(self, *args): |
| 69 | print "__coerce__:", args |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 70 | return (self,) + args |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 71 | |
| 72 | def __hash__(self, *args): |
| 73 | print "__hash__:", args |
Trent Mick | d68d0a6 | 2000-10-04 17:50:59 +0000 | [diff] [blame] | 74 | return hash(id(self)) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 75 | |
| 76 | def __str__(self, *args): |
| 77 | print "__str__:", args |
| 78 | return "AllTests" |
| 79 | |
| 80 | def __repr__(self, *args): |
| 81 | print "__repr__:", args |
| 82 | return "AllTests" |
| 83 | |
| 84 | def __cmp__(self, *args): |
| 85 | print "__cmp__:", args |
| 86 | return 0 |
| 87 | |
Barry Warsaw | 07d8d64 | 2001-08-20 20:29:07 +0000 | [diff] [blame] | 88 | def __del__(self, *args): |
| 89 | print "__del__:", args |
| 90 | |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 91 | for method in testmeths: |
Guido van Rossum | f317a18 | 2001-01-22 14:51:41 +0000 | [diff] [blame] | 92 | exec """def __%(method)s__(self, *args): |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 93 | print "__%(method)s__:", args |
Guido van Rossum | f317a18 | 2001-01-22 14:51:41 +0000 | [diff] [blame] | 94 | """%locals() in AllTests.__dict__ |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 95 | |
| 96 | # this also tests __init__ of course. |
| 97 | testme = AllTests() |
| 98 | |
| 99 | # Binary operations |
| 100 | |
| 101 | testme + 1 |
| 102 | 1 + testme |
| 103 | |
| 104 | testme - 1 |
| 105 | 1 - testme |
| 106 | |
| 107 | testme * 1 |
| 108 | 1 * testme |
| 109 | |
| 110 | testme / 1 |
| 111 | 1 / testme |
| 112 | |
| 113 | testme % 1 |
| 114 | 1 % testme |
| 115 | |
| 116 | divmod(testme,1) |
| 117 | divmod(1, testme) |
| 118 | |
| 119 | testme ** 1 |
| 120 | 1 ** testme |
| 121 | |
| 122 | testme >> 1 |
| 123 | 1 >> testme |
| 124 | |
| 125 | testme << 1 |
| 126 | 1 << testme |
| 127 | |
| 128 | testme & 1 |
| 129 | 1 & testme |
| 130 | |
| 131 | testme | 1 |
| 132 | 1 | testme |
| 133 | |
| 134 | testme ^ 1 |
| 135 | 1 ^ testme |
| 136 | |
| 137 | |
| 138 | # List/dict operations |
| 139 | |
| 140 | 1 in testme |
| 141 | |
| 142 | testme[1] |
| 143 | testme[1] = 1 |
| 144 | del testme[1] |
| 145 | |
| 146 | testme[:42] |
| 147 | testme[:42] = "The Answer" |
| 148 | del testme[:42] |
| 149 | |
| 150 | testme[2:1024:10] |
| 151 | testme[2:1024:10] = "A lot" |
| 152 | del testme[2:1024:10] |
| 153 | |
| 154 | testme[:42, ..., :24:, 24, 100] |
| 155 | testme[:42, ..., :24:, 24, 100] = "Strange" |
| 156 | del testme[:42, ..., :24:, 24, 100] |
| 157 | |
| 158 | |
| 159 | # Now remove the slice hooks to see if converting normal slices to slice |
| 160 | # object works. |
| 161 | |
| 162 | del AllTests.__getslice__ |
| 163 | del AllTests.__setslice__ |
| 164 | del AllTests.__delslice__ |
| 165 | |
Barry Warsaw | 07d8d64 | 2001-08-20 20:29:07 +0000 | [diff] [blame] | 166 | import sys |
| 167 | if sys.platform[:4] != 'java': |
| 168 | testme[:42] |
| 169 | testme[:42] = "The Answer" |
| 170 | del testme[:42] |
| 171 | else: |
| 172 | # This works under Jython, but the actual slice values are |
| 173 | # different. |
| 174 | print "__getitem__: (slice(0, 42, None),)" |
| 175 | print "__setitem__: (slice(0, 42, None), 'The Answer')" |
| 176 | print "__delitem__: (slice(0, 42, None),)" |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 177 | |
| 178 | # Unary operations |
| 179 | |
| 180 | -testme |
| 181 | +testme |
| 182 | abs(testme) |
Barry Warsaw | 07d8d64 | 2001-08-20 20:29:07 +0000 | [diff] [blame] | 183 | if sys.platform[:4] != 'java': |
| 184 | int(testme) |
| 185 | long(testme) |
| 186 | float(testme) |
| 187 | oct(testme) |
| 188 | hex(testme) |
| 189 | else: |
| 190 | # Jython enforced that the these methods return |
| 191 | # a value of the expected type. |
| 192 | print "__int__: ()" |
| 193 | print "__long__: ()" |
| 194 | print "__float__: ()" |
| 195 | print "__oct__: ()" |
| 196 | print "__hex__: ()" |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 197 | |
| 198 | |
| 199 | # And the rest... |
| 200 | |
| 201 | hash(testme) |
| 202 | repr(testme) |
| 203 | str(testme) |
| 204 | |
| 205 | testme == 1 |
| 206 | testme < 1 |
| 207 | testme > 1 |
| 208 | testme <> 1 |
| 209 | testme != 1 |
| 210 | 1 == testme |
| 211 | 1 < testme |
| 212 | 1 > testme |
| 213 | 1 <> testme |
| 214 | 1 != testme |
| 215 | |
| 216 | # This test has to be last (duh.) |
| 217 | |
| 218 | del testme |
Barry Warsaw | 07d8d64 | 2001-08-20 20:29:07 +0000 | [diff] [blame] | 219 | if sys.platform[:4] == 'java': |
| 220 | import java |
| 221 | java.lang.System.gc() |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 222 | |
| 223 | # Interfering tests |
| 224 | |
| 225 | class ExtraTests: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 226 | def __getattr__(self, *args): |
| 227 | print "__getattr__:", args |
| 228 | return "SomeVal" |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 229 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 230 | def __setattr__(self, *args): |
| 231 | print "__setattr__:", args |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 232 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 233 | def __delattr__(self, *args): |
| 234 | print "__delattr__:", args |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 235 | |
| 236 | testme = ExtraTests() |
| 237 | testme.spam |
| 238 | testme.eggs = "spam, spam, spam and ham" |
| 239 | del testme.cardinal |
Guido van Rossum | 2312024 | 2001-01-18 23:47:15 +0000 | [diff] [blame] | 240 | |
| 241 | |
| 242 | # Test correct errors from hash() on objects with comparisons but no __hash__ |
| 243 | |
| 244 | class C0: |
| 245 | pass |
| 246 | |
| 247 | hash(C0()) # This should work; the next two should raise TypeError |
| 248 | |
| 249 | class C1: |
| 250 | def __cmp__(self, other): return 0 |
| 251 | |
| 252 | try: hash(C1()) |
| 253 | except TypeError: pass |
| 254 | else: raise TestFailed, "hash(C1()) should raise an exception" |
| 255 | |
| 256 | class C2: |
| 257 | def __eq__(self, other): return 1 |
| 258 | |
| 259 | try: hash(C2()) |
| 260 | except TypeError: pass |
| 261 | else: raise TestFailed, "hash(C2()) should raise an exception" |