annotate Lib/test/test_richcmp.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 77c71f258df7
children 614e32900ef7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Tests for rich comparisons
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
3 import unittest
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45302
diff changeset
4 from test import support
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
5
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
6 import operator
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 class Number:
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 def __init__(self, x):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 self.x = x
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 def __lt__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 return self.x < other
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 def __le__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 return self.x <= other
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 def __eq__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 return self.x == other
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 def __ne__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 return self.x != other
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 def __gt__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 return self.x > other
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 def __ge__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 return self.x >= other
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 def __cmp__(self, other):
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45302
diff changeset
32 raise support.TestFailed("Number.__cmp__() should not be called")
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 def __repr__(self):
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
35 return "Number(%r)" % (self.x, )
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 class Vector:
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 def __init__(self, data):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 self.data = data
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 def __len__(self):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 return len(self.data)
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 def __getitem__(self, i):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 return self.data[i]
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 def __setitem__(self, i, v):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 self.data[i] = v
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
48475
f4e7ef4329a9 Manual forward port of 64962 - use PyObject_HashNotImplemented as a tp_hash level indicator that the default hash implementation has not been inherited
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
51 __hash__ = None # Vectors cannot be hashed
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
40401
88133417dfd0 - patch #1600346 submitted by Tomer Filiba
Jack Diederich <jackdied@gmail.com>
parents: 37180
diff changeset
53 def __bool__(self):
43014
9834301a2fd3 Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents: 42188
diff changeset
54 raise TypeError("Vectors cannot be used in Boolean contexts")
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 def __cmp__(self, other):
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45302
diff changeset
57 raise support.TestFailed("Vector.__cmp__() should not be called")
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 def __repr__(self):
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
60 return "Vector(%r)" % (self.data, )
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 def __lt__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 return Vector([a < b for a, b in zip(self.data, self.__cast(other))])
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 def __le__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 return Vector([a <= b for a, b in zip(self.data, self.__cast(other))])
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 def __eq__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 return Vector([a == b for a, b in zip(self.data, self.__cast(other))])
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 def __ne__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 return Vector([a != b for a, b in zip(self.data, self.__cast(other))])
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 def __gt__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 return Vector([a > b for a, b in zip(self.data, self.__cast(other))])
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 def __ge__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 return Vector([a >= b for a, b in zip(self.data, self.__cast(other))])
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 def __cast(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 if isinstance(other, Vector):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 other = other.data
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 if len(self.data) != len(other):
43014
9834301a2fd3 Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents: 42188
diff changeset
84 raise ValueError("Cannot compare vectors of different length")
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 return other
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
87 opmap = {
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
88 "lt": (lambda a,b: a< b, operator.lt, operator.__lt__),
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
89 "le": (lambda a,b: a<=b, operator.le, operator.__le__),
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
90 "eq": (lambda a,b: a==b, operator.eq, operator.__eq__),
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
91 "ne": (lambda a,b: a!=b, operator.ne, operator.__ne__),
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
92 "gt": (lambda a,b: a> b, operator.gt, operator.__gt__),
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
93 "ge": (lambda a,b: a>=b, operator.ge, operator.__ge__)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
94 }
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
95
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
96 class VectorTest(unittest.TestCase):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
97
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
98 def checkfail(self, error, opname, *args):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
99 for op in opmap[opname]:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
100 self.assertRaises(error, op, *args)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
101
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
102 def checkequal(self, opname, a, b, expres):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
103 for op in opmap[opname]:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
104 realres = op(a, b)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
105 # can't use assertEqual(realres, expres) here
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
106 self.assertEqual(len(realres), len(expres))
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40849
diff changeset
107 for i in range(len(realres)):
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
108 # results are bool, so we can use "is" here
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49518
diff changeset
109 self.assertTrue(realres[i] is expres[i])
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
111 def test_mixed(self):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
112 # check that comparisons involving Vector objects
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
113 # which return rich results (i.e. Vectors with itemwise
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
114 # comparison results) work
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
115 a = Vector(range(2))
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
116 b = Vector(range(3))
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
117 # all comparisons should fail for different length
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
118 for opname in opmap:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
119 self.checkfail(ValueError, opname, a, b)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
120
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40849
diff changeset
121 a = list(range(5))
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
122 b = 5 * [2]
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
123 # try mixed arguments (but not (a, b) as that won't return a bool vector)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
124 args = [(a, Vector(b)), (Vector(a), b), (Vector(a), Vector(b))]
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
125 for (a, b) in args:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
126 self.checkequal("lt", a, b, [True, True, False, False, False])
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
127 self.checkequal("le", a, b, [True, True, True, False, False])
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
128 self.checkequal("eq", a, b, [False, False, True, False, False])
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
129 self.checkequal("ne", a, b, [True, True, False, True, True ])
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
130 self.checkequal("gt", a, b, [False, False, False, True, True ])
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
131 self.checkequal("ge", a, b, [False, False, True, True, True ])
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
132
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40401
diff changeset
133 for ops in opmap.values():
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
134 for op in ops:
40401
88133417dfd0 - patch #1600346 submitted by Tomer Filiba
Jack Diederich <jackdied@gmail.com>
parents: 37180
diff changeset
135 # calls __bool__, which should fail
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
136 self.assertRaises(TypeError, bool, op(a, b))
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
137
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
138 class NumberTest(unittest.TestCase):
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
139
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
140 def test_basic(self):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
141 # Check that comparisons involving Number objects
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
142 # give the same results give as comparing the
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
143 # corresponding ints
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40849
diff changeset
144 for a in range(3):
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40849
diff changeset
145 for b in range(3):
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
146 for typea in (int, Number):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
147 for typeb in (int, Number):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
148 if typea==typeb==int:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
149 continue # the combination int, int is useless
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
150 ta = typea(a)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
151 tb = typeb(b)
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40401
diff changeset
152 for ops in opmap.values():
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
153 for op in ops:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
154 realoutcome = op(a, b)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
155 testoutcome = op(ta, tb)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
156 self.assertEqual(realoutcome, testoutcome)
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
158 def checkvalue(self, opname, a, b, expres):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
159 for typea in (int, Number):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
160 for typeb in (int, Number):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
161 ta = typea(a)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
162 tb = typeb(b)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
163 for op in opmap[opname]:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
164 realres = op(ta, tb)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
165 realres = getattr(realres, "x", realres)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49518
diff changeset
166 self.assertTrue(realres is expres)
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
167
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
168 def test_values(self):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
169 # check all operators and all comparison results
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
170 self.checkvalue("lt", 0, 0, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
171 self.checkvalue("le", 0, 0, True )
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
172 self.checkvalue("eq", 0, 0, True )
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
173 self.checkvalue("ne", 0, 0, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
174 self.checkvalue("gt", 0, 0, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
175 self.checkvalue("ge", 0, 0, True )
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
176
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
177 self.checkvalue("lt", 0, 1, True )
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
178 self.checkvalue("le", 0, 1, True )
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
179 self.checkvalue("eq", 0, 1, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
180 self.checkvalue("ne", 0, 1, True )
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
181 self.checkvalue("gt", 0, 1, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
182 self.checkvalue("ge", 0, 1, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
183
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
184 self.checkvalue("lt", 1, 0, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
185 self.checkvalue("le", 1, 0, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
186 self.checkvalue("eq", 1, 0, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
187 self.checkvalue("ne", 1, 0, True )
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
188 self.checkvalue("gt", 1, 0, True )
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
189 self.checkvalue("ge", 1, 0, True )
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
190
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
191 class MiscTest(unittest.TestCase):
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
192
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
193 def test_misbehavin(self):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
194 class Misb:
59613
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58966
diff changeset
195 def __lt__(self_, other): return 0
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58966
diff changeset
196 def __gt__(self_, other): return 0
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58966
diff changeset
197 def __eq__(self_, other): return 0
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58966
diff changeset
198 def __le__(self_, other): self.fail("This shouldn't happen")
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58966
diff changeset
199 def __ge__(self_, other): self.fail("This shouldn't happen")
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58966
diff changeset
200 def __ne__(self_, other): self.fail("This shouldn't happen")
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
201 a = Misb()
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
202 b = Misb()
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
203 self.assertEqual(a<b, 0)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
204 self.assertEqual(a==b, 0)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
205 self.assertEqual(a>b, 0)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
206
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
207 def test_not(self):
40401
88133417dfd0 - patch #1600346 submitted by Tomer Filiba
Jack Diederich <jackdied@gmail.com>
parents: 37180
diff changeset
208 # Check that exceptions in __bool__ are properly
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
209 # propagated by the not operator
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
210 import operator
37180
64ae32b07829 Exceptions should inherit from Exception now.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35420
diff changeset
211 class Exc(Exception):
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
212 pass
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
213 class Bad:
40401
88133417dfd0 - patch #1600346 submitted by Tomer Filiba
Jack Diederich <jackdied@gmail.com>
parents: 37180
diff changeset
214 def __bool__(self):
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
215 raise Exc
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
216
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
217 def do(bad):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
218 not bad
16026
865266011dfa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16000
diff changeset
219
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
220 for func in (do, operator.not_):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
221 self.assertRaises(Exc, func, Bad())
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
222
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
223 def test_recursion(self):
30801
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
224 # Check that comparison for recursive objects fails gracefully
45302
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 44373
diff changeset
225 from collections import UserList
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
226 a = UserList()
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
227 b = UserList()
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
228 a.append(b)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
229 b.append(a)
30801
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
230 self.assertRaises(RuntimeError, operator.eq, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
231 self.assertRaises(RuntimeError, operator.ne, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
232 self.assertRaises(RuntimeError, operator.lt, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
233 self.assertRaises(RuntimeError, operator.le, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
234 self.assertRaises(RuntimeError, operator.gt, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
235 self.assertRaises(RuntimeError, operator.ge, a, b)
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
236
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
237 b.append(17)
30801
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
238 # Even recursive lists of different lengths are different,
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
239 # but they cannot be ordered
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49518
diff changeset
240 self.assertTrue(not (a == b))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49518
diff changeset
241 self.assertTrue(a != b)
30801
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
242 self.assertRaises(RuntimeError, operator.lt, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
243 self.assertRaises(RuntimeError, operator.le, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
244 self.assertRaises(RuntimeError, operator.gt, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
245 self.assertRaises(RuntimeError, operator.ge, a, b)
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
246 a.append(17)
30801
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
247 self.assertRaises(RuntimeError, operator.eq, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
248 self.assertRaises(RuntimeError, operator.ne, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
249 a.insert(0, 11)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
250 b.insert(0, 12)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49518
diff changeset
251 self.assertTrue(not (a == b))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49518
diff changeset
252 self.assertTrue(a != b)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49518
diff changeset
253 self.assertTrue(a < b)
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
254
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
255 class DictTest(unittest.TestCase):
15989
1f1eff081a2a Add test for misbehaving rich comparisons (always returning 0) --
Guido van Rossum <guido@python.org>
parents: 15988
diff changeset
256
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
257 def test_dicts(self):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
258 # Verify that __eq__ and __ne__ work for dicts even if the keys and
35420
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 30801
diff changeset
259 # values don't support anything other than __eq__ and __ne__ (and
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 30801
diff changeset
260 # __hash__). Complex numbers are a fine example of that.
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
261 import random
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
262 imag1a = {}
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
263 for i in range(50):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
264 imag1a[random.randrange(100)*1j] = random.randrange(100)*1j
40849
1d53ea0fc23f Some more test now pass.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
265 items = list(imag1a.items())
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
266 random.shuffle(items)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
267 imag1b = {}
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
268 for k, v in items:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
269 imag1b[k] = v
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
270 imag2 = imag1b.copy()
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
271 imag2[k] = v + 1.0
42188
81930eaae715 Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
272 self.assertEqual(imag1a, imag1a)
81930eaae715 Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
273 self.assertEqual(imag1a, imag1b)
81930eaae715 Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
274 self.assertEqual(imag2, imag2)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49518
diff changeset
275 self.assertTrue(imag1a != imag2)
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
276 for opname in ("lt", "le", "gt", "ge"):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
277 for op in opmap[opname]:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
278 self.assertRaises(TypeError, op, imag1a, imag2)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
279
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
280 class ListTest(unittest.TestCase):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
281
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
282 def test_coverage(self):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
283 # exercise all comparisons for lists
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
284 x = [42]
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
285 self.assertIs(x<x, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
286 self.assertIs(x<=x, True)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
287 self.assertIs(x==x, True)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
288 self.assertIs(x!=x, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
289 self.assertIs(x>x, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
290 self.assertIs(x>=x, True)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
291 y = [42, 42]
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
292 self.assertIs(x<y, True)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
293 self.assertIs(x<=y, True)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
294 self.assertIs(x==y, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
295 self.assertIs(x!=y, True)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
296 self.assertIs(x>y, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
297 self.assertIs(x>=y, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
298
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
299 def test_badentry(self):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
300 # make sure that exceptions for item comparison are properly
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
301 # propagated in list comparisons
37180
64ae32b07829 Exceptions should inherit from Exception now.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35420
diff changeset
302 class Exc(Exception):
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 16026
diff changeset
303 pass
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
304 class Bad:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
305 def __eq__(self, other):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
306 raise Exc
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
307
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
308 x = [Bad()]
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
309 y = [Bad()]
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
310
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
311 for op in opmap["eq"]:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
312 self.assertRaises(Exc, op, x, y)
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 16026
diff changeset
313
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
314 def test_goodentry(self):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
315 # This test exercises the final call to PyObject_RichCompare()
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
316 # in Objects/listobject.c::list_richcompare()
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
317 class Good:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
318 def __lt__(self, other):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
319 return True
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
320
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
321 x = [Good()]
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
322 y = [Good()]
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
323
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
324 for op in opmap["lt"]:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
325 self.assertIs(op(x, y), True)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
326
44373
2750d531fdc0 Merged revisions 59565-59594 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43014
diff changeset
327
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
328 def test_main():
48475
f4e7ef4329a9 Manual forward port of 64962 - use PyObject_HashNotImplemented as a tp_hash level indicator that the default hash implementation has not been inherited
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
329 support.run_unittest(VectorTest, NumberTest, MiscTest, DictTest, ListTest)
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
330
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
331 if __name__ == "__main__":
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
332 test_main()