python-2.5.2/win32/Lib/test/test_compare.py
changeset 0 ae805ac0140d
equal deleted inserted replaced
-1:000000000000 0:ae805ac0140d
       
     1 import sys
       
     2 import unittest
       
     3 from test import test_support
       
     4 
       
     5 class Empty:
       
     6     def __repr__(self):
       
     7         return '<Empty>'
       
     8 
       
     9 class Coerce:
       
    10     def __init__(self, arg):
       
    11         self.arg = arg
       
    12 
       
    13     def __repr__(self):
       
    14         return '<Coerce %s>' % self.arg
       
    15 
       
    16     def __coerce__(self, other):
       
    17         if isinstance(other, Coerce):
       
    18             return self.arg, other.arg
       
    19         else:
       
    20             return self.arg, other
       
    21 
       
    22 class Cmp:
       
    23     def __init__(self,arg):
       
    24         self.arg = arg
       
    25 
       
    26     def __repr__(self):
       
    27         return '<Cmp %s>' % self.arg
       
    28 
       
    29     def __cmp__(self, other):
       
    30         return cmp(self.arg, other)
       
    31 
       
    32 class ComparisonTest(unittest.TestCase):
       
    33     set1 = [2, 2.0, 2L, 2+0j, Coerce(2), Cmp(2.0)]
       
    34     set2 = [[1], (3,), None, Empty()]
       
    35     candidates = set1 + set2
       
    36 
       
    37     def test_comparisons(self):
       
    38         for a in self.candidates:
       
    39             for b in self.candidates:
       
    40                 if ((a in self.set1) and (b in self.set1)) or a is b:
       
    41                     self.assertEqual(a, b)
       
    42                 else:
       
    43                     self.assertNotEqual(a, b)
       
    44 
       
    45     def test_id_comparisons(self):
       
    46         # Ensure default comparison compares id() of args
       
    47         L = []
       
    48         for i in range(10):
       
    49             L.insert(len(L)//2, Empty())
       
    50         for a in L:
       
    51             for b in L:
       
    52                 self.assertEqual(cmp(a, b), cmp(id(a), id(b)),
       
    53                                  'a=%r, b=%r' % (a, b))
       
    54 
       
    55 def test_main():
       
    56     test_support.run_unittest(ComparisonTest)
       
    57 
       
    58 if __name__ == '__main__':
       
    59     test_main()