blob: 1fc9971731625ccdb6126df5e31477d39fae5956 [file] [log] [blame]
Thomas Wouters1d75a792000-08-17 22:37:32 +00001"Test the functionality of Python classes implementing operators."
2
3
4testmeths = [
5
6# Binary operations
7 "add",
8 "radd",
9 "sub",
10 "rsub",
11 "mul",
12 "rmul",
13 "div",
14 "rdiv",
15 "mod",
16 "rmod",
17 "divmod",
18 "rdivmod",
19 "pow",
20 "rpow",
21 "rshift",
22 "rrshift",
23 "lshift",
24 "rlshift",
25 "and",
26 "rand",
27 "or",
28 "ror",
29 "xor",
30 "rxor",
31
32# List/dict operations
33 "contains",
34 "getitem",
35 "getslice",
36 "setitem",
37 "setslice",
38 "delitem",
39 "delslice",
40
41# Unary operations
42 "neg",
43 "pos",
44 "abs",
45 "int",
46 "long",
47 "float",
48 "oct",
49 "hex",
50
51# generic operations
52 "init",
53 "del",
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
67class AllTests:
68 def __coerce__(self, *args):
69 print "__coerce__:", args
70 return (self,) + args
71
72 def __hash__(self, *args):
73 print "__hash__:", args
74 return id(self)
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
88for method in testmeths:
89 exec("""def __%(method)s__(self, *args):
90 print "__%(method)s__:", args
91"""%locals(), AllTests.__dict__);
92
93# this also tests __init__ of course.
94testme = AllTests()
95
96# Binary operations
97
98testme + 1
991 + testme
100
101testme - 1
1021 - testme
103
104testme * 1
1051 * testme
106
107testme / 1
1081 / testme
109
110testme % 1
1111 % testme
112
113divmod(testme,1)
114divmod(1, testme)
115
116testme ** 1
1171 ** 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
134
135# List/dict operations
136
1371 in testme
138
139testme[1]
140testme[1] = 1
141del testme[1]
142
143testme[:42]
144testme[:42] = "The Answer"
145del testme[:42]
146
147testme[2:1024:10]
148testme[2:1024:10] = "A lot"
149del testme[2:1024:10]
150
151testme[:42, ..., :24:, 24, 100]
152testme[:42, ..., :24:, 24, 100] = "Strange"
153del testme[:42, ..., :24:, 24, 100]
154
155
156# Now remove the slice hooks to see if converting normal slices to slice
157# object works.
158
159del AllTests.__getslice__
160del AllTests.__setslice__
161del AllTests.__delslice__
162
163testme[:42]
164testme[:42] = "The Answer"
165del testme[:42]
166
167
168# Unary operations
169
170-testme
171+testme
172abs(testme)
173int(testme)
174long(testme)
175float(testme)
176oct(testme)
177hex(testme)
178
179
180# And the rest...
181
182hash(testme)
183repr(testme)
184str(testme)
185
186testme == 1
187testme < 1
188testme > 1
189testme <> 1
190testme != 1
1911 == testme
1921 < testme
1931 > testme
1941 <> testme
1951 != testme
196
197# This test has to be last (duh.)
198
199del testme
200
201
202# Interfering tests
203
204class ExtraTests:
205 def __getattr__(self, *args):
206 print "__getattr__:", args
207 return "SomeVal"
208
209 def __setattr__(self, *args):
210 print "__setattr__:", args
211
212 def __delattr__(self, *args):
213 print "__delattr__:", args
214
215testme = ExtraTests()
216testme.spam
217testme.eggs = "spam, spam, spam and ham"
218del testme.cardinal
219