aboutsummaryrefslogtreecommitdiff
path: root/Examples/test-suite/python/python_richcompare_runme.py
blob: 09af8a4e8b12fdd39f62bbb46cfba3e3ea270b62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import python_richcompare
import sys

def check_unorderable_types(exception):
#    if str(exception).find("unorderable types") == -1:
#        raise RuntimeError("A TypeError 'unorderable types' exception was expected"), None, sys.exc_info()[2]
    pass # Exception message seems to vary from one version of Python to another

base1 = python_richcompare.BaseClass(1)
base2 = python_richcompare.BaseClass(2)
base3 = python_richcompare.BaseClass(3)
a1 = python_richcompare.SubClassA(1)
a2 = python_richcompare.SubClassA(2)
a3 = python_richcompare.SubClassA(3)
b1 = python_richcompare.SubClassB(1)
b2 = python_richcompare.SubClassB(2)
b3 = python_richcompare.SubClassB(3)

# Check == and != within a single type
#-------------------------------------------------------------------------

if not (base1 == base1):
    raise RuntimeError("Object not == to itself")

if not (base1 == python_richcompare.BaseClass(1)):
    raise RuntimeError("Object not == to an equivalent object")

if (base1 == base2):
    raise RuntimeError(
        "Comparing non-equivalent objects of the same type, == returned True")

if (base1 != base1):
    raise RuntimeError("Object is != itself")

if (base1 != python_richcompare.BaseClass(1)):
    raise RuntimeError("Object is != an equivalent object")

if not (base1 != base2):
    raise RuntimeError(
        "Comparing non-equivalent objects of the same type, != returned False")


# Check redefined operator== in SubClassA
#-------------------------------------------------------------------------

if (a2 == base2):
    raise RuntimeError("Redefined operator== in SubClassA failed")

if (a2 == b2):
    raise RuntimeError("Redefined operator== in SubClassA failed")

if not (a1 == a2):
    raise RuntimeError("Redefined operator== in SubClassA failed")

# Check up-casting of subclasses
#-------------------------------------------------------------------------

if (base2 != a2):
    raise RuntimeError(
        "Comparing equivalent base and subclass instances, != returned True")

if (a2 == base2):
    raise RuntimeError(
        "Comparing non-equivalent base and subclass instances, == returned True")

if (a1 == b1):
    raise RuntimeError(
        "Comparing equivalent instances of different subclasses, == returned True")

if (b1 == a1):
    raise RuntimeError(
        "Comparing equivalent instances of different subclasses, == returned True")

# Check comparison to other objects
#-------------------------------------------------------------------------------
if (base1 == 42) :
    raise RuntimeError("Comparing class to incompatible type, == returned True")
if not (base1 != 42) :
    raise RuntimeError("Comparing class to incompatible type, != returned False")

if (a1 == 42) :
    raise RuntimeError("Comparing class (with overloaded operator ==) to incompatible type, == returned True")
if not (a1 != 42) :
    raise RuntimeError("Comparing class (with overloaded operator ==) to incompatible type, != returned False")

# Check inequalities
#-------------------------------------------------------------------------

if (a2 > b2):
    raise RuntimeError("operator> failed")

if (a2 < b2):
    raise RuntimeError("operator< failed")

if not (a2 >= b2):
    raise RuntimeError("operator>= failed")

if not (a2 <= b2):
    raise RuntimeError("operator<= failed")

# Check inequalities to other objects
#-------------------------------------------------------------------------------
if sys.version_info[0:2] < (3, 0):
    if (base1 < 42):
        raise RuntimeError("Comparing class to incompatible type, < returned True")
    if (base1 <= 42):
        raise RuntimeError("Comparing class to incompatible type, <= returned True")
    if not (base1 > 42):
        raise RuntimeError("Comparing class to incompatible type, > returned False")
    if not (base1 >= 42):
        raise RuntimeError("Comparing class to incompatible type, >= returned False")
else:
    # Python 3 throws: TypeError: unorderable types
    try:
        res = base1 < 42
        raise RuntimeError("Failed to throw")
    except TypeError as e:
        check_unorderable_types(e)
    try:
        res = base1 <= 42
        raise RuntimeError("Failed to throw")
    except TypeError as e:
        check_unorderable_types(e)
    try:
        res = base1 > 42
        raise RuntimeError("Failed to throw")
    except TypeError as e:
        check_unorderable_types(e)
    try:
        res = base1 >= 42
        raise RuntimeError("Failed to throw")
    except TypeError as e:
        check_unorderable_types(e)

# Check inequalities used for ordering
#-------------------------------------------------------------------------

x = sorted([a2, a3, a1])

if not (x[0] is a1):
    raise RuntimeError("Ordering failed")

if not (x[1] is a2):
    raise RuntimeError("Ordering failed")

if not (x[2] is a3):
    raise RuntimeError("Ordering failed")

x = sorted([base2, a3, b1])

if not (x[0] is b1):
    raise RuntimeError("Ordering failed")

if not (x[1] is base2):
    raise RuntimeError("Ordering failed")

if not (x[2] is a3):
    raise RuntimeError("Ordering failed")


# Test custom exceptions can still be thrown in operators which use %pythonmaybecall
et0 = python_richcompare.ExceptionThrower(0)
et1 = python_richcompare.ExceptionThrower(1)
et2 = python_richcompare.ExceptionThrower(2)

if not(et1 < et2):
    raise RuntimeError("ExceptionThrower (a) failed")

if et2 < et1:
    raise RuntimeError("ExceptionThrower (b) failed")

try:
    x = et2 < et0
    raise RuntimeError("ExceptionThrower failed to throw ValueError (A)")
except ValueError:
    pass

try:
    x = et0 < et2
    raise RuntimeError("ExceptionThrower failed to throw ValueError (B)")
except ValueError:
    pass

if sys.version_info[0:2] >= (3, 0):
    try:
        x = et2 < 99
        raise RuntimeError("ExceptionThrower (d) failed")
    except TypeError:
        pass

    try:
        x = 99 < et2
        raise RuntimeError("ExceptionThrower (e) failed")
    except TypeError:
        pass

    try:
        x = et0 < 99
        raise RuntimeError("ExceptionThrower (f) failed")
    except TypeError:
        pass

    try:
        x = 99 < et0
        raise RuntimeError("ExceptionThrower (g) failed")
    except TypeError:
        pass


# Overloaded operators and custom exceptions
c0 = python_richcompare.SubClassCThrower(0)
c1 = python_richcompare.SubClassCThrower(1)
c1b = python_richcompare.SubClassCThrower(1)
c2 = python_richcompare.SubClassCThrower(2)

if c1 == c2:
    raise RuntimeError("SubClassCThrower failed (a)")

if not(c1 == c1b):
    raise RuntimeError("SubClassCThrower failed (b)")

if c0 == 99:
    raise RuntimeError("SubClassCThrower failed (c)")

if 99 == c0:
    raise RuntimeError("SubClassCThrower failed (d)")

try:
    x = c0 == c1
    raise RuntimeError("SubClassCThrower failed to throw (A)")
except ValueError:
    pass

try:
    x = c1 == c0
    raise RuntimeError("SubClassCThrower failed to throw (B)")
except ValueError:
    pass