Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_compare.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 1d84bf5f9478 |
| children |
| rev | line source |
|---|---|
|
37548
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
1 import unittest |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
2 from test import test_support |
|
15687
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
3 |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
4 class Empty: |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
5 def __repr__(self): |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
6 return '<Empty>' |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
7 |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
8 class Coerce: |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
9 def __init__(self, arg): |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
10 self.arg = arg |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
11 |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
12 def __repr__(self): |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
13 return '<Coerce %s>' % self.arg |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
14 |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
15 def __coerce__(self, other): |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
16 if isinstance(other, Coerce): |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
17 return self.arg, other.arg |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
18 else: |
|
15700
4b168eeb61b3
Use == rather than cmp(). The return value of cmp() is not well defined when
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15687
diff
changeset
|
19 return self.arg, other |
|
15687
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
20 |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
21 class Cmp: |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
22 def __init__(self,arg): |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
23 self.arg = arg |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
24 |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
25 def __repr__(self): |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
26 return '<Cmp %s>' % self.arg |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
27 |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
28 def __cmp__(self, other): |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
29 return cmp(self.arg, other) |
|
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
30 |
|
37548
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
31 class ComparisonTest(unittest.TestCase): |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
32 set1 = [2, 2.0, 2L, 2+0j, Coerce(2), Cmp(2.0)] |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
33 set2 = [[1], (3,), None, Empty()] |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
34 candidates = set1 + set2 |
|
15687
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
35 |
|
37548
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
36 def test_comparisons(self): |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
37 for a in self.candidates: |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
38 for b in self.candidates: |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
39 if ((a in self.set1) and (b in self.set1)) or a is b: |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
40 self.assertEqual(a, b) |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
41 else: |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
42 self.assertNotEqual(a, b) |
|
15687
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
43 |
|
37548
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
44 def test_id_comparisons(self): |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
45 # Ensure default comparison compares id() of args |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
46 L = [] |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
47 for i in range(10): |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
48 L.insert(len(L)//2, Empty()) |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
49 for a in L: |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
50 for b in L: |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
51 self.assertEqual(cmp(a, b), cmp(id(a), id(b)), |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
52 'a=%r, b=%r' % (a, b)) |
|
15687
e8f5e61a8b94
Add more tests for compare and coercion in preparation for the coercion
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
diff
changeset
|
53 |
|
37548
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
54 def test_main(): |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
55 test_support.run_unittest(ComparisonTest) |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
56 |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
57 if __name__ == '__main__': |
|
f1c4406bf481
Convert test_compare to use unittest. Hopefully we can find out
Neal Norwitz <nnorwitz@gmail.com>
parents:
24442
diff
changeset
|
58 test_main() |
