1 # Tests for rich comparisons
3 from test
.test_support
import TestFailed
, verify
, verbose
10 def __lt__(self
, other
):
13 def __le__(self
, other
):
14 return self
.x
<= other
16 def __eq__(self
, other
):
17 return self
.x
== other
19 def __ne__(self
, other
):
20 return self
.x
!= other
22 def __gt__(self
, other
):
25 def __ge__(self
, other
):
26 return self
.x
>= other
28 def __cmp__(self
, other
):
29 raise TestFailed
, "Number.__cmp__() should not be called"
32 return "Number(%s)" % repr(self
.x
)
36 def __init__(self
, data
):
42 def __getitem__(self
, i
):
45 def __setitem__(self
, i
, v
):
49 raise TypeError, "Vectors cannot be hashed"
51 def __nonzero__(self
):
52 raise TypeError, "Vectors cannot be used in Boolean contexts"
54 def __cmp__(self
, other
):
55 raise TestFailed
, "Vector.__cmp__() should not be called"
58 return "Vector(%s)" % repr(self
.data
)
60 def __lt__(self
, other
):
61 return Vector([a
< b
for a
, b
in zip(self
.data
, self
.__cast
(other
))])
63 def __le__(self
, other
):
64 return Vector([a
<= b
for a
, b
in zip(self
.data
, self
.__cast
(other
))])
66 def __eq__(self
, other
):
67 return Vector([a
== b
for a
, b
in zip(self
.data
, self
.__cast
(other
))])
69 def __ne__(self
, other
):
70 return Vector([a
!= b
for a
, b
in zip(self
.data
, self
.__cast
(other
))])
72 def __gt__(self
, other
):
73 return Vector([a
> b
for a
, b
in zip(self
.data
, self
.__cast
(other
))])
75 def __ge__(self
, other
):
76 return Vector([a
>= b
for a
, b
in zip(self
.data
, self
.__cast
(other
))])
78 def __cast(self
, other
):
79 if isinstance(other
, Vector
):
81 if len(self
.data
) != len(other
):
82 raise ValueError, "Cannot compare vectors of different length"
85 operators
= "<", "<=", "==", "!=", ">", ">="
88 opmap
[op
] = eval("lambda a, b: a %s b" % op
)
99 raise TestFailed
, "a %s b for different length should fail" % op
103 print "%23s %-2s %-23s -> %s" % (a
, op
, b
, opmap
[op
](a
, b
))
104 print "%23s %-2s %-23s -> %s" % (a
, op
, b
.data
, opmap
[op
](a
, b
.data
))
105 print "%23s %-2s %-23s -> %s" % (a
.data
, op
, b
, opmap
[op
](a
.data
, b
))
108 raise TestFailed
, "a %s b shouldn't be true" % op
110 raise TestFailed
, "a %s b shouldn't be false" % op
114 def testop(a
, b
, op
):
117 except AttributeError:
121 except AttributeError:
124 realoutcome
= opfunc(ax
, bx
)
125 testoutcome
= opfunc(a
, b
)
126 if realoutcome
!= testoutcome
:
127 print "Error for", a
, op
, b
, ": expected", realoutcome
,
128 print "but got", testoutcome
130 ## print a, op, b, "-->", testoutcome # and "true" or "false"
143 testit(Number(a
), Number(b
))
147 def tabulate(c1
=Number
, c2
=Number
):
151 print "operator:", op
158 print '----------+-' * 4
164 print "| %9s" % opfunc(a
, b
),
166 print '----------+-' * 4
172 def __lt__(self
, other
): return 0
173 def __gt__(self
, other
): return 0
174 def __eq__(self
, other
): return 0
175 def __le__(self
, other
): raise TestFailed
, "This shouldn't happen"
176 def __ge__(self
, other
): raise TestFailed
, "This shouldn't happen"
177 def __ne__(self
, other
): raise TestFailed
, "This shouldn't happen"
178 def __cmp__(self
, other
): raise RuntimeError, "expected"
189 raise TestFailed
, "cmp(Misb(), Misb()) didn't raise RuntimeError"
192 from UserList
import UserList
193 a
= UserList(); a
.append(a
)
194 b
= UserList(); b
.append(b
)
195 def check(s
, a
=a
, b
=b
):
200 raise TestFailed
, s
+ " was false but expected to be true"
201 except RuntimeError, msg
:
202 raise TestFailed
, str(msg
)
204 print "recursion tests: a=%s, b=%s" % (a
, b
)
209 print "recursion tests: a=%s, b=%s" % (a
, b
)
214 print "recursion tests: a=%s, b=%s" % (a
, b
)
219 print "recursion tests: a=%s, b=%s" % (a
, b
)
222 if verbose
: print "recursion tests ok"
225 # Verify that __eq__ and __ne__ work for dicts even if the keys and
226 # values don't support anything other than __eq__ and __ne__. Complex
227 # numbers are a fine example of that.
231 imag1a
[random
.randrange(100)*1j
] = random
.randrange(100)*1j
232 items
= imag1a
.items()
233 random
.shuffle(items
)
237 imag2
= imag1b
.copy()
239 verify(imag1a
== imag1a
, "imag1a == imag1a should have worked")
240 verify(imag1a
== imag1b
, "imag1a == imag1b should have worked")
241 verify(imag2
== imag2
, "imag2 == imag2 should have worked")
242 verify(imag1a
!= imag2
, "imag1a != imag2 should have worked")
243 for op
in "<", "<=", ">", ">=":
245 eval("imag1a %s imag2" % op
)
249 raise TestFailed("expected TypeError from imag1a %s imag2" % op
)