blob: c1c663c521f550a51f884f033540562e8d1b2e0e [file] [log] [blame]
Thomas Wouters1d75a792000-08-17 22:37:32 +00001"Test the functionality of Python classes implementing operators."
2
Guido van Rossum23120242001-01-18 23:47:15 +00003from test_support import TestFailed
Thomas Wouters1d75a792000-08-17 22:37:32 +00004
5testmeths = [
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 Wouters1d75a792000-08-17 22:37:32 +000054 ]
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
67class AllTests:
68 def __coerce__(self, *args):
69 print "__coerce__:", args
Fred Drake004d5e62000-10-23 17:22:08 +000070 return (self,) + args
Thomas Wouters1d75a792000-08-17 22:37:32 +000071
72 def __hash__(self, *args):
73 print "__hash__:", args
Trent Mickd68d0a62000-10-04 17:50:59 +000074 return hash(id(self))
Thomas Wouters1d75a792000-08-17 22:37:32 +000075
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 Warsaw07d8d642001-08-20 20:29:07 +000088 def __del__(self, *args):
89 print "__del__:", args
90
Thomas Wouters1d75a792000-08-17 22:37:32 +000091for method in testmeths:
Guido van Rossumf317a182001-01-22 14:51:41 +000092 exec """def __%(method)s__(self, *args):
Thomas Wouters1d75a792000-08-17 22:37:32 +000093 print "__%(method)s__:", args
Guido van Rossumf317a182001-01-22 14:51:41 +000094"""%locals() in AllTests.__dict__
Thomas Wouters1d75a792000-08-17 22:37:32 +000095
96# this also tests __init__ of course.
97testme = AllTests()
98
99# Binary operations
100
101testme + 1
1021 + testme
103
104testme - 1
1051 - testme
106
107testme * 1
1081 * testme
109
110testme / 1
1111 / testme
112
113testme % 1
1141 % testme
115
116divmod(testme,1)
117divmod(1, testme)
118
119testme ** 1
1201 ** testme
121
122testme >> 1
1231 >> testme
124
125testme << 1
1261 << testme
127
128testme & 1
1291 & testme
130
131testme | 1
1321 | testme
133
134testme ^ 1
1351 ^ testme
136
137
138# List/dict operations
139
1401 in testme
141
142testme[1]
143testme[1] = 1
144del testme[1]
145
146testme[:42]
147testme[:42] = "The Answer"
148del testme[:42]
149
150testme[2:1024:10]
151testme[2:1024:10] = "A lot"
152del testme[2:1024:10]
153
154testme[:42, ..., :24:, 24, 100]
155testme[:42, ..., :24:, 24, 100] = "Strange"
156del 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
162del AllTests.__getslice__
163del AllTests.__setslice__
164del AllTests.__delslice__
165
Barry Warsaw07d8d642001-08-20 20:29:07 +0000166import sys
167if sys.platform[:4] != 'java':
168 testme[:42]
169 testme[:42] = "The Answer"
170 del testme[:42]
171else:
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 Wouters1d75a792000-08-17 22:37:32 +0000177
178# Unary operations
179
180-testme
181+testme
182abs(testme)
Barry Warsaw07d8d642001-08-20 20:29:07 +0000183if sys.platform[:4] != 'java':
184 int(testme)
185 long(testme)
186 float(testme)
187 oct(testme)
188 hex(testme)
189else:
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 Wouters1d75a792000-08-17 22:37:32 +0000197
198
199# And the rest...
200
201hash(testme)
202repr(testme)
203str(testme)
204
205testme == 1
206testme < 1
207testme > 1
208testme <> 1
209testme != 1
2101 == testme
2111 < testme
2121 > testme
2131 <> testme
2141 != testme
215
216# This test has to be last (duh.)
217
218del testme
Barry Warsaw07d8d642001-08-20 20:29:07 +0000219if sys.platform[:4] == 'java':
220 import java
221 java.lang.System.gc()
Thomas Wouters1d75a792000-08-17 22:37:32 +0000222
223# Interfering tests
224
225class ExtraTests:
Fred Drake004d5e62000-10-23 17:22:08 +0000226 def __getattr__(self, *args):
227 print "__getattr__:", args
228 return "SomeVal"
Thomas Wouters1d75a792000-08-17 22:37:32 +0000229
Fred Drake004d5e62000-10-23 17:22:08 +0000230 def __setattr__(self, *args):
231 print "__setattr__:", args
Thomas Wouters1d75a792000-08-17 22:37:32 +0000232
Fred Drake004d5e62000-10-23 17:22:08 +0000233 def __delattr__(self, *args):
234 print "__delattr__:", args
Thomas Wouters1d75a792000-08-17 22:37:32 +0000235
236testme = ExtraTests()
237testme.spam
238testme.eggs = "spam, spam, spam and ham"
239del testme.cardinal
Guido van Rossum23120242001-01-18 23:47:15 +0000240
241
242# Test correct errors from hash() on objects with comparisons but no __hash__
243
244class C0:
245 pass
246
247hash(C0()) # This should work; the next two should raise TypeError
248
249class C1:
250 def __cmp__(self, other): return 0
251
252try: hash(C1())
253except TypeError: pass
254else: raise TestFailed, "hash(C1()) should raise an exception"
255
256class C2:
257 def __eq__(self, other): return 1
258
259try: hash(C2())
260except TypeError: pass
261else: raise TestFailed, "hash(C2()) should raise an exception"