struct.pack has become picky about h (short) and H (unsigned short).
[python/dscho.git] / Lib / test / test_class.py
blob43c1d3b5181f07acf6205ac54d51a11863b07d53
1 "Test the functionality of Python classes implementing operators."
4 testmeths = [
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",
32 # List/dict operations
33 "contains",
34 "getitem",
35 "getslice",
36 "setitem",
37 "setslice",
38 "delitem",
39 "delslice",
41 # Unary operations
42 "neg",
43 "pos",
44 "abs",
45 "int",
46 "long",
47 "float",
48 "oct",
49 "hex",
51 # generic operations
52 "init",
53 "del",
56 # These need to return something other than None
57 # "coerce",
58 # "hash",
59 # "str",
60 # "repr",
62 # These are separate because they can influence the test of other methods.
63 # "getattr",
64 # "setattr",
65 # "delattr",
67 class AllTests:
68 def __coerce__(self, *args):
69 print "__coerce__:", args
70 return (self,) + args
72 def __hash__(self, *args):
73 print "__hash__:", args
74 return hash(id(self))
76 def __str__(self, *args):
77 print "__str__:", args
78 return "AllTests"
80 def __repr__(self, *args):
81 print "__repr__:", args
82 return "AllTests"
84 def __cmp__(self, *args):
85 print "__cmp__:", args
86 return 0
88 for method in testmeths:
89 exec("""def __%(method)s__(self, *args):
90 print "__%(method)s__:", args
91 """%locals(), AllTests.__dict__);
93 # this also tests __init__ of course.
94 testme = AllTests()
96 # Binary operations
98 testme + 1
99 1 + testme
101 testme - 1
102 1 - testme
104 testme * 1
105 1 * testme
107 testme / 1
108 1 / testme
110 testme % 1
111 1 % testme
113 divmod(testme,1)
114 divmod(1, testme)
116 testme ** 1
117 1 ** testme
119 testme >> 1
120 1 >> testme
122 testme << 1
123 1 << testme
125 testme & 1
126 1 & testme
128 testme | 1
129 1 | testme
131 testme ^ 1
132 1 ^ testme
135 # List/dict operations
137 1 in testme
139 testme[1]
140 testme[1] = 1
141 del testme[1]
143 testme[:42]
144 testme[:42] = "The Answer"
145 del testme[:42]
147 testme[2:1024:10]
148 testme[2:1024:10] = "A lot"
149 del testme[2:1024:10]
151 testme[:42, ..., :24:, 24, 100]
152 testme[:42, ..., :24:, 24, 100] = "Strange"
153 del testme[:42, ..., :24:, 24, 100]
156 # Now remove the slice hooks to see if converting normal slices to slice
157 # object works.
159 del AllTests.__getslice__
160 del AllTests.__setslice__
161 del AllTests.__delslice__
163 testme[:42]
164 testme[:42] = "The Answer"
165 del testme[:42]
168 # Unary operations
170 -testme
171 +testme
172 abs(testme)
173 int(testme)
174 long(testme)
175 float(testme)
176 oct(testme)
177 hex(testme)
180 # And the rest...
182 hash(testme)
183 repr(testme)
184 str(testme)
186 testme == 1
187 testme < 1
188 testme > 1
189 testme <> 1
190 testme != 1
191 1 == testme
192 1 < testme
193 1 > testme
194 1 <> testme
195 1 != testme
197 # This test has to be last (duh.)
199 del testme
202 # Interfering tests
204 class ExtraTests:
205 def __getattr__(self, *args):
206 print "__getattr__:", args
207 return "SomeVal"
209 def __setattr__(self, *args):
210 print "__setattr__:", args
212 def __delattr__(self, *args):
213 print "__delattr__:", args
215 testme = ExtraTests()
216 testme.spam
217 testme.eggs = "spam, spam, spam and ham"
218 del testme.cardinal