blob: fcfc24be34e0c4d2b470c60d1b33b0cd2977c4a7 [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",
54 "del",
55 ]
56
57# These need to return something other than None
58# "coerce",
59# "hash",
60# "str",
61# "repr",
62
63# These are separate because they can influence the test of other methods.
64# "getattr",
65# "setattr",
66# "delattr",
67
68class AllTests:
69 def __coerce__(self, *args):
70 print "__coerce__:", args
Fred Drake004d5e62000-10-23 17:22:08 +000071 return (self,) + args
Thomas Wouters1d75a792000-08-17 22:37:32 +000072
73 def __hash__(self, *args):
74 print "__hash__:", args
Trent Mickd68d0a62000-10-04 17:50:59 +000075 return hash(id(self))
Thomas Wouters1d75a792000-08-17 22:37:32 +000076
77 def __str__(self, *args):
78 print "__str__:", args
79 return "AllTests"
80
81 def __repr__(self, *args):
82 print "__repr__:", args
83 return "AllTests"
84
85 def __cmp__(self, *args):
86 print "__cmp__:", args
87 return 0
88
89for method in testmeths:
Guido van Rossumf317a182001-01-22 14:51:41 +000090 exec """def __%(method)s__(self, *args):
Thomas Wouters1d75a792000-08-17 22:37:32 +000091 print "__%(method)s__:", args
Guido van Rossumf317a182001-01-22 14:51:41 +000092"""%locals() in AllTests.__dict__
Thomas Wouters1d75a792000-08-17 22:37:32 +000093
94# this also tests __init__ of course.
95testme = AllTests()
96
97# Binary operations
98
99testme + 1
1001 + testme
101
102testme - 1
1031 - testme
104
105testme * 1
1061 * testme
107
108testme / 1
1091 / testme
110
111testme % 1
1121 % testme
113
114divmod(testme,1)
115divmod(1, testme)
116
117testme ** 1
1181 ** testme
119
120testme >> 1
1211 >> testme
122
123testme << 1
1241 << testme
125
126testme & 1
1271 & testme
128
129testme | 1
1301 | testme
131
132testme ^ 1
1331 ^ testme
134
135
136# List/dict operations
137
1381 in testme
139
140testme[1]
141testme[1] = 1
142del testme[1]
143
144testme[:42]
145testme[:42] = "The Answer"
146del testme[:42]
147
148testme[2:1024:10]
149testme[2:1024:10] = "A lot"
150del testme[2:1024:10]
151
152testme[:42, ..., :24:, 24, 100]
153testme[:42, ..., :24:, 24, 100] = "Strange"
154del testme[:42, ..., :24:, 24, 100]
155
156
157# Now remove the slice hooks to see if converting normal slices to slice
158# object works.
159
160del AllTests.__getslice__
161del AllTests.__setslice__
162del AllTests.__delslice__
163
164testme[:42]
165testme[:42] = "The Answer"
166del testme[:42]
167
168
169# Unary operations
170
171-testme
172+testme
173abs(testme)
174int(testme)
175long(testme)
176float(testme)
177oct(testme)
178hex(testme)
179
180
181# And the rest...
182
183hash(testme)
184repr(testme)
185str(testme)
186
187testme == 1
188testme < 1
189testme > 1
190testme <> 1
191testme != 1
1921 == testme
1931 < testme
1941 > testme
1951 <> testme
1961 != testme
197
198# This test has to be last (duh.)
199
200del testme
201
202
203# Interfering tests
204
205class ExtraTests:
Fred Drake004d5e62000-10-23 17:22:08 +0000206 def __getattr__(self, *args):
207 print "__getattr__:", args
208 return "SomeVal"
Thomas Wouters1d75a792000-08-17 22:37:32 +0000209
Fred Drake004d5e62000-10-23 17:22:08 +0000210 def __setattr__(self, *args):
211 print "__setattr__:", args
Thomas Wouters1d75a792000-08-17 22:37:32 +0000212
Fred Drake004d5e62000-10-23 17:22:08 +0000213 def __delattr__(self, *args):
214 print "__delattr__:", args
Thomas Wouters1d75a792000-08-17 22:37:32 +0000215
216testme = ExtraTests()
217testme.spam
218testme.eggs = "spam, spam, spam and ham"
219del testme.cardinal
Guido van Rossum23120242001-01-18 23:47:15 +0000220
221
222# Test correct errors from hash() on objects with comparisons but no __hash__
223
224class C0:
225 pass
226
227hash(C0()) # This should work; the next two should raise TypeError
228
229class C1:
230 def __cmp__(self, other): return 0
231
232try: hash(C1())
233except TypeError: pass
234else: raise TestFailed, "hash(C1()) should raise an exception"
235
236class C2:
237 def __eq__(self, other): return 1
238
239try: hash(C2())
240except TypeError: pass
241else: raise TestFailed, "hash(C2()) should raise an exception"