home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 June / PCWorld_2005-06_cd.bin / software / vyzkuste / firewally / firewally.exe / framework-2.3.exe / test_sets.py < prev    next >
Text File  |  2003-12-30  |  26KB  |  815 lines

  1. #!/usr/bin/env python
  2.  
  3. import unittest, operator, copy, pickle
  4. from sets import Set, ImmutableSet
  5. from test import test_support
  6.  
  7. empty_set = Set()
  8.  
  9. #==============================================================================
  10.  
  11. class TestBasicOps(unittest.TestCase):
  12.  
  13.     def test_repr(self):
  14.         if self.repr is not None:
  15.             self.assertEqual(`self.set`, self.repr)
  16.  
  17.     def test_length(self):
  18.         self.assertEqual(len(self.set), self.length)
  19.  
  20.     def test_self_equality(self):
  21.         self.assertEqual(self.set, self.set)
  22.  
  23.     def test_equivalent_equality(self):
  24.         self.assertEqual(self.set, self.dup)
  25.  
  26.     def test_copy(self):
  27.         self.assertEqual(self.set.copy(), self.dup)
  28.  
  29.     def test_self_union(self):
  30.         result = self.set | self.set
  31.         self.assertEqual(result, self.dup)
  32.  
  33.     def test_empty_union(self):
  34.         result = self.set | empty_set
  35.         self.assertEqual(result, self.dup)
  36.  
  37.     def test_union_empty(self):
  38.         result = empty_set | self.set
  39.         self.assertEqual(result, self.dup)
  40.  
  41.     def test_self_intersection(self):
  42.         result = self.set & self.set
  43.         self.assertEqual(result, self.dup)
  44.  
  45.     def test_empty_intersection(self):
  46.         result = self.set & empty_set
  47.         self.assertEqual(result, empty_set)
  48.  
  49.     def test_intersection_empty(self):
  50.         result = empty_set & self.set
  51.         self.assertEqual(result, empty_set)
  52.  
  53.     def test_self_symmetric_difference(self):
  54.         result = self.set ^ self.set
  55.         self.assertEqual(result, empty_set)
  56.  
  57.     def checkempty_symmetric_difference(self):
  58.         result = self.set ^ empty_set
  59.         self.assertEqual(result, self.set)
  60.  
  61.     def test_self_difference(self):
  62.         result = self.set - self.set
  63.         self.assertEqual(result, empty_set)
  64.  
  65.     def test_empty_difference(self):
  66.         result = self.set - empty_set
  67.         self.assertEqual(result, self.dup)
  68.  
  69.     def test_empty_difference_rev(self):
  70.         result = empty_set - self.set
  71.         self.assertEqual(result, empty_set)
  72.  
  73.     def test_iteration(self):
  74.         for v in self.set:
  75.             self.assert_(v in self.values)
  76.  
  77.     def test_pickling(self):
  78.         p = pickle.dumps(self.set)
  79.         copy = pickle.loads(p)
  80.         self.assertEqual(self.set, copy,
  81.                          "%s != %s" % (self.set, copy))
  82.  
  83. #------------------------------------------------------------------------------
  84.  
  85. class TestBasicOpsEmpty(TestBasicOps):
  86.     def setUp(self):
  87.         self.case   = "empty set"
  88.         self.values = []
  89.         self.set    = Set(self.values)
  90.         self.dup    = Set(self.values)
  91.         self.length = 0
  92.         self.repr   = "Set([])"
  93.  
  94. #------------------------------------------------------------------------------
  95.  
  96. class TestBasicOpsSingleton(TestBasicOps):
  97.     def setUp(self):
  98.         self.case   = "unit set (number)"
  99.         self.values = [3]
  100.         self.set    = Set(self.values)
  101.         self.dup    = Set(self.values)
  102.         self.length = 1
  103.         self.repr   = "Set([3])"
  104.  
  105.     def test_in(self):
  106.         self.failUnless(3 in self.set)
  107.  
  108.     def test_not_in(self):
  109.         self.failUnless(2 not in self.set)
  110.  
  111. #------------------------------------------------------------------------------
  112.  
  113. class TestBasicOpsTuple(TestBasicOps):
  114.     def setUp(self):
  115.         self.case   = "unit set (tuple)"
  116.         self.values = [(0, "zero")]
  117.         self.set    = Set(self.values)
  118.         self.dup    = Set(self.values)
  119.         self.length = 1
  120.         self.repr   = "Set([(0, 'zero')])"
  121.  
  122.     def test_in(self):
  123.         self.failUnless((0, "zero") in self.set)
  124.  
  125.     def test_not_in(self):
  126.         self.failUnless(9 not in self.set)
  127.  
  128. #------------------------------------------------------------------------------
  129.  
  130. class TestBasicOpsTriple(TestBasicOps):
  131.     def setUp(self):
  132.         self.case   = "triple set"
  133.         self.values = [0, "zero", operator.add]
  134.         self.set    = Set(self.values)
  135.         self.dup    = Set(self.values)
  136.         self.length = 3
  137.         self.repr   = None
  138.  
  139. #==============================================================================
  140.  
  141. def baditer():
  142.     raise TypeError
  143.     yield True
  144.  
  145. def gooditer():
  146.     yield True
  147.  
  148. class TestExceptionPropagation(unittest.TestCase):
  149.     """SF 628246:  Set constructor should not trap iterator TypeErrors"""
  150.  
  151.     def test_instanceWithException(self):
  152.         self.assertRaises(TypeError, Set, baditer())
  153.  
  154.     def test_instancesWithoutException(self):
  155.         # All of these iterables should load without exception.
  156.         Set([1,2,3])
  157.         Set((1,2,3))
  158.         Set({'one':1, 'two':2, 'three':3})
  159.         Set(xrange(3))
  160.         Set('abc')
  161.         Set(gooditer())
  162.  
  163. #==============================================================================
  164.  
  165. class TestSetOfSets(unittest.TestCase):
  166.     def test_constructor(self):
  167.         inner = Set([1])
  168.         outer = Set([inner])
  169.         element = outer.pop()
  170.         self.assertEqual(type(element), ImmutableSet)
  171.         outer.add(inner)        # Rebuild set of sets with .add method
  172.         outer.remove(inner)
  173.         self.assertEqual(outer, Set())   # Verify that remove worked
  174.         outer.discard(inner)    # Absence of KeyError indicates working fine
  175.  
  176. #==============================================================================
  177.  
  178. class TestBinaryOps(unittest.TestCase):
  179.     def setUp(self):
  180.         self.set = Set((2, 4, 6))
  181.  
  182.     def test_eq(self):              # SF bug 643115
  183.         self.assertEqual(self.set, Set({2:1,4:3,6:5}))
  184.  
  185.     def test_union_subset(self):
  186.         result = self.set | Set([2])
  187.         self.assertEqual(result, Set((2, 4, 6)))
  188.  
  189.     def test_union_superset(self):
  190.         result = self.set | Set([2, 4, 6, 8])
  191.         self.assertEqual(result, Set([2, 4, 6, 8]))
  192.  
  193.     def test_union_overlap(self):
  194.         result = self.set | Set([3, 4, 5])
  195.         self.assertEqual(result, Set([2, 3, 4, 5, 6]))
  196.  
  197.     def test_union_non_overlap(self):
  198.         result = self.set | Set([8])
  199.         self.assertEqual(result, Set([2, 4, 6, 8]))
  200.  
  201.     def test_intersection_subset(self):
  202.         result = self.set & Set((2, 4))
  203.         self.assertEqual(result, Set((2, 4)))
  204.  
  205.     def test_intersection_superset(self):
  206.         result = self.set & Set([2, 4, 6, 8])
  207.         self.assertEqual(result, Set([2, 4, 6]))
  208.  
  209.     def test_intersection_overlap(self):
  210.         result = self.set & Set([3, 4, 5])
  211.         self.assertEqual(result, Set([4]))
  212.  
  213.     def test_intersection_non_overlap(self):
  214.         result = self.set & Set([8])
  215.         self.assertEqual(result, empty_set)
  216.  
  217.     def test_sym_difference_subset(self):
  218.         result = self.set ^ Set((2, 4))
  219.         self.assertEqual(result, Set([6]))
  220.  
  221.     def test_sym_difference_superset(self):
  222.         result = self.set ^ Set((2, 4, 6, 8))
  223.         self.assertEqual(result, Set([8]))
  224.  
  225.     def test_sym_difference_overlap(self):
  226.         result = self.set ^ Set((3, 4, 5))
  227.         self.assertEqual(result, Set([2, 3, 5, 6]))
  228.  
  229.     def test_sym_difference_non_overlap(self):
  230.         result = self.set ^ Set([8])
  231.         self.assertEqual(result, Set([2, 4, 6, 8]))
  232.  
  233.     def test_cmp(self):
  234.         a, b = Set('a'), Set('b')
  235.         self.assertRaises(TypeError, cmp, a, b)
  236.  
  237.         # You can view this as a buglet:  cmp(a, a) does not raise TypeError,
  238.         # because __eq__ is tried before __cmp__, and a.__eq__(a) returns True,
  239.         # which Python thinks is good enough to synthesize a cmp() result
  240.         # without calling __cmp__.
  241.         self.assertEqual(cmp(a, a), 0)
  242.  
  243.         self.assertRaises(TypeError, cmp, a, 12)
  244.         self.assertRaises(TypeError, cmp, "abc", a)
  245.  
  246. #==============================================================================
  247.  
  248. class TestUpdateOps(unittest.TestCase):
  249.     def setUp(self):
  250.         self.set = Set((2, 4, 6))
  251.  
  252.     def test_union_subset(self):
  253.         self.set |= Set([2])
  254.         self.assertEqual(self.set, Set((2, 4, 6)))
  255.  
  256.     def test_union_superset(self):
  257.         self.set |= Set([2, 4, 6, 8])
  258.         self.assertEqual(self.set, Set([2, 4, 6, 8]))
  259.  
  260.     def test_union_overlap(self):
  261.         self.set |= Set([3, 4, 5])
  262.         self.assertEqual(self.set, Set([2, 3, 4, 5, 6]))
  263.  
  264.     def test_union_non_overlap(self):
  265.         self.set |= Set([8])
  266.         self.assertEqual(self.set, Set([2, 4, 6, 8]))
  267.  
  268.     def test_union_method_call(self):
  269.         self.set.union_update(Set([3, 4, 5]))
  270.         self.assertEqual(self.set, Set([2, 3, 4, 5, 6]))
  271.  
  272.     def test_intersection_subset(self):
  273.         self.set &= Set((2, 4))
  274.         self.assertEqual(self.set, Set((2, 4)))
  275.  
  276.     def test_intersection_superset(self):
  277.         self.set &= Set([2, 4, 6, 8])
  278.         self.assertEqual(self.set, Set([2, 4, 6]))
  279.  
  280.     def test_intersection_overlap(self):
  281.         self.set &= Set([3, 4, 5])
  282.         self.assertEqual(self.set, Set([4]))
  283.  
  284.     def test_intersection_non_overlap(self):
  285.         self.set &= Set([8])
  286.         self.assertEqual(self.set, empty_set)
  287.  
  288.     def test_intersection_method_call(self):
  289.         self.set.intersection_update(Set([3, 4, 5]))
  290.         self.assertEqual(self.set, Set([4]))
  291.  
  292.     def test_sym_difference_subset(self):
  293.         self.set ^= Set((2, 4))
  294.         self.assertEqual(self.set, Set([6]))
  295.  
  296.     def test_sym_difference_superset(self):
  297.         self.set ^= Set((2, 4, 6, 8))
  298.         self.assertEqual(self.set, Set([8]))
  299.  
  300.     def test_sym_difference_overlap(self):
  301.         self.set ^= Set((3, 4, 5))
  302.         self.assertEqual(self.set, Set([2, 3, 5, 6]))
  303.  
  304.     def test_sym_difference_non_overlap(self):
  305.         self.set ^= Set([8])
  306.         self.assertEqual(self.set, Set([2, 4, 6, 8]))
  307.  
  308.     def test_sym_difference_method_call(self):
  309.         self.set.symmetric_difference_update(Set([3, 4, 5]))
  310.         self.assertEqual(self.set, Set([2, 3, 5, 6]))
  311.  
  312.     def test_difference_subset(self):
  313.         self.set -= Set((2, 4))
  314.         self.assertEqual(self.set, Set([6]))
  315.  
  316.     def test_difference_superset(self):
  317.         self.set -= Set((2, 4, 6, 8))
  318.         self.assertEqual(self.set, Set([]))
  319.  
  320.     def test_difference_overlap(self):
  321.         self.set -= Set((3, 4, 5))
  322.         self.assertEqual(self.set, Set([2, 6]))
  323.  
  324.     def test_difference_non_overlap(self):
  325.         self.set -= Set([8])
  326.         self.assertEqual(self.set, Set([2, 4, 6]))
  327.  
  328.     def test_difference_method_call(self):
  329.         self.set.difference_update(Set([3, 4, 5]))
  330.         self.assertEqual(self.set, Set([2, 6]))
  331.  
  332. #==============================================================================
  333.  
  334. class TestMutate(unittest.TestCase):
  335.     def setUp(self):
  336.         self.values = ["a", "b", "c"]
  337.         self.set = Set(self.values)
  338.  
  339.     def test_add_present(self):
  340.         self.set.add("c")
  341.         self.assertEqual(self.set, Set("abc"))
  342.  
  343.     def test_add_absent(self):
  344.         self.set.add("d")
  345.         self.assertEqual(self.set, Set("abcd"))
  346.  
  347.     def test_add_until_full(self):
  348.         tmp = Set()
  349.         expected_len = 0
  350.         for v in self.values:
  351.             tmp.add(v)
  352.             expected_len += 1
  353.             self.assertEqual(len(tmp), expected_len)
  354.         self.assertEqual(tmp, self.set)
  355.  
  356.     def test_remove_present(self):
  357.         self.set.remove("b")
  358.         self.assertEqual(self.set, Set("ac"))
  359.  
  360.     def test_remove_absent(self):
  361.         try:
  362.             self.set.remove("d")
  363.             self.fail("Removing missing element should have raised LookupError")
  364.         except LookupError:
  365.             pass
  366.  
  367.     def test_remove_until_empty(self):
  368.         expected_len = len(self.set)
  369.         for v in self.values:
  370.             self.set.remove(v)
  371.             expected_len -= 1
  372.             self.assertEqual(len(self.set), expected_len)
  373.  
  374.     def test_discard_present(self):
  375.         self.set.discard("c")
  376.         self.assertEqual(self.set, Set("ab"))
  377.  
  378.     def test_discard_absent(self):
  379.         self.set.discard("d")
  380.         self.assertEqual(self.set, Set("abc"))
  381.  
  382.     def test_clear(self):
  383.         self.set.clear()
  384.         self.assertEqual(len(self.set), 0)
  385.  
  386.     def test_pop(self):
  387.         popped = {}
  388.         while self.set:
  389.             popped[self.set.pop()] = None
  390.         self.assertEqual(len(popped), len(self.values))
  391.         for v in self.values:
  392.             self.failUnless(v in popped)
  393.  
  394.     def test_update_empty_tuple(self):
  395.         self.set.union_update(())
  396.         self.assertEqual(self.set, Set(self.values))
  397.  
  398.     def test_update_unit_tuple_overlap(self):
  399.         self.set.union_update(("a",))
  400.         self.assertEqual(self.set, Set(self.values))
  401.  
  402.     def test_update_unit_tuple_non_overlap(self):
  403.         self.set.union_update(("a", "z"))
  404.         self.assertEqual(self.set, Set(self.values + ["z"]))
  405.  
  406. #==============================================================================
  407.  
  408. class TestSubsets(unittest.TestCase):
  409.  
  410.     case2method = {"<=": "issubset",
  411.                    ">=": "issuperset",
  412.                   }
  413.  
  414.     reverse = {"==": "==",
  415.                "!=": "!=",
  416.                "<":  ">",
  417.                ">":  "<",
  418.                "<=": ">=",
  419.                ">=": "<=",
  420.               }
  421.  
  422.     def test_issubset(self):
  423.         x = self.left
  424.         y = self.right
  425.         for case in "!=", "==", "<", "<=", ">", ">=":
  426.             expected = case in self.cases
  427.             # Test the binary infix spelling.
  428.             result = eval("x" + case + "y", locals())
  429.             self.assertEqual(result, expected)
  430.             # Test the "friendly" method-name spelling, if one exists.
  431.             if case in TestSubsets.case2method:
  432.                 method = getattr(x, TestSubsets.case2method[case])
  433.                 result = method(y)
  434.                 self.assertEqual(result, expected)
  435.  
  436.             # Now do the same for the operands reversed.
  437.             rcase = TestSubsets.reverse[case]
  438.             result = eval("y" + rcase + "x", locals())
  439.             self.assertEqual(result, expected)
  440.             if rcase in TestSubsets.case2method:
  441.                 method = getattr(y, TestSubsets.case2method[rcase])
  442.                 result = method(x)
  443.                 self.assertEqual(result, expected)
  444. #------------------------------------------------------------------------------
  445.  
  446. class TestSubsetEqualEmpty(TestSubsets):
  447.     left  = Set()
  448.     right = Set()
  449.     name  = "both empty"
  450.     cases = "==", "<=", ">="
  451.  
  452. #------------------------------------------------------------------------------
  453.  
  454. class TestSubsetEqualNonEmpty(TestSubsets):
  455.     left  = Set([1, 2])
  456.     right = Set([1, 2])
  457.     name  = "equal pair"
  458.     cases = "==", "<=", ">="
  459.  
  460. #------------------------------------------------------------------------------
  461.  
  462. class TestSubsetEmptyNonEmpty(TestSubsets):
  463.     left  = Set()
  464.     right = Set([1, 2])
  465.     name  = "one empty, one non-empty"
  466.     cases = "!=", "<", "<="
  467.  
  468. #------------------------------------------------------------------------------
  469.  
  470. class TestSubsetPartial(TestSubsets):
  471.     left  = Set([1])
  472.     right = Set([1, 2])
  473.     name  = "one a non-empty proper subset of other"
  474.     cases = "!=", "<", "<="
  475.  
  476. #------------------------------------------------------------------------------
  477.  
  478. class TestSubsetNonOverlap(TestSubsets):
  479.     left  = Set([1])
  480.     right = Set([2])
  481.     name  = "neither empty, neither contains"
  482.     cases = "!="
  483.  
  484. #==============================================================================
  485.  
  486. class TestOnlySetsInBinaryOps(unittest.TestCase):
  487.  
  488.     def test_eq_ne(self):
  489.         # Unlike the others, this is testing that == and != *are* allowed.
  490.         self.assertEqual(self.other == self.set, False)
  491.         self.assertEqual(self.set == self.other, False)
  492.         self.assertEqual(self.other != self.set, True)
  493.         self.assertEqual(self.set != self.other, True)
  494.  
  495.     def test_ge_gt_le_lt(self):
  496.         self.assertRaises(TypeError, lambda: self.set < self.other)
  497.         self.assertRaises(TypeError, lambda: self.set <= self.other)
  498.         self.assertRaises(TypeError, lambda: self.set > self.other)
  499.         self.assertRaises(TypeError, lambda: self.set >= self.other)
  500.  
  501.         self.assertRaises(TypeError, lambda: self.other < self.set)
  502.         self.assertRaises(TypeError, lambda: self.other <= self.set)
  503.         self.assertRaises(TypeError, lambda: self.other > self.set)
  504.         self.assertRaises(TypeError, lambda: self.other >= self.set)
  505.  
  506.     def test_union_update_operator(self):
  507.         try:
  508.             self.set |= self.other
  509.         except TypeError:
  510.             pass
  511.         else:
  512.             self.fail("expected TypeError")
  513.  
  514.     def test_union_update(self):
  515.         if self.otherIsIterable:
  516.             self.set.union_update(self.other)
  517.         else:
  518.             self.assertRaises(TypeError, self.set.union_update, self.other)
  519.  
  520.     def test_union(self):
  521.         self.assertRaises(TypeError, lambda: self.set | self.other)
  522.         self.assertRaises(TypeError, lambda: self.other | self.set)
  523.         if self.otherIsIterable:
  524.             self.set.union(self.other)
  525.         else:
  526.             self.assertRaises(TypeError, self.set.union, self.other)
  527.  
  528.     def test_intersection_update_operator(self):
  529.         try:
  530.             self.set &= self.other
  531.         except TypeError:
  532.             pass
  533.         else:
  534.             self.fail("expected TypeError")
  535.  
  536.     def test_intersection_update(self):
  537.         if self.otherIsIterable:
  538.             self.set.intersection_update(self.other)
  539.         else:
  540.             self.assertRaises(TypeError,
  541.                               self.set.intersection_update,
  542.                               self.other)
  543.  
  544.     def test_intersection(self):
  545.         self.assertRaises(TypeError, lambda: self.set & self.other)
  546.         self.assertRaises(TypeError, lambda: self.other & self.set)
  547.         if self.otherIsIterable:
  548.             self.set.intersection(self.other)
  549.         else:
  550.             self.assertRaises(TypeError, self.set.intersection, self.other)
  551.  
  552.     def test_sym_difference_update_operator(self):
  553.         try:
  554.             self.set ^= self.other
  555.         except TypeError:
  556.             pass
  557.         else:
  558.             self.fail("expected TypeError")
  559.  
  560.     def test_sym_difference_update(self):
  561.         if self.otherIsIterable:
  562.             self.set.symmetric_difference_update(self.other)
  563.         else:
  564.             self.assertRaises(TypeError,
  565.                               self.set.symmetric_difference_update,
  566.                               self.other)
  567.  
  568.     def test_sym_difference(self):
  569.         self.assertRaises(TypeError, lambda: self.set ^ self.other)
  570.         self.assertRaises(TypeError, lambda: self.other ^ self.set)
  571.         if self.otherIsIterable:
  572.             self.set.symmetric_difference(self.other)
  573.         else:
  574.             self.assertRaises(TypeError, self.set.symmetric_difference, self.other)
  575.  
  576.     def test_difference_update_operator(self):
  577.         try:
  578.             self.set -= self.other
  579.         except TypeError:
  580.             pass
  581.         else:
  582.             self.fail("expected TypeError")
  583.  
  584.     def test_difference_update(self):
  585.         if self.otherIsIterable:
  586.             self.set.difference_update(self.other)
  587.         else:
  588.             self.assertRaises(TypeError,
  589.                               self.set.difference_update,
  590.                               self.other)
  591.  
  592.     def test_difference(self):
  593.         self.assertRaises(TypeError, lambda: self.set - self.other)
  594.         self.assertRaises(TypeError, lambda: self.other - self.set)
  595.         if self.otherIsIterable:
  596.             self.set.difference(self.other)
  597.         else:
  598.             self.assertRaises(TypeError, self.set.difference, self.other)
  599. #------------------------------------------------------------------------------
  600.  
  601. class TestOnlySetsNumeric(TestOnlySetsInBinaryOps):
  602.     def setUp(self):
  603.         self.set   = Set((1, 2, 3))
  604.         self.other = 19
  605.         self.otherIsIterable = False
  606.  
  607. #------------------------------------------------------------------------------
  608.  
  609. class TestOnlySetsDict(TestOnlySetsInBinaryOps):
  610.     def setUp(self):
  611.         self.set   = Set((1, 2, 3))
  612.         self.other = {1:2, 3:4}
  613.         self.otherIsIterable = True
  614.  
  615. #------------------------------------------------------------------------------
  616.  
  617. class TestOnlySetsOperator(TestOnlySetsInBinaryOps):
  618.     def setUp(self):
  619.         self.set   = Set((1, 2, 3))
  620.         self.other = operator.add
  621.         self.otherIsIterable = False
  622.  
  623. #------------------------------------------------------------------------------
  624.  
  625. class TestOnlySetsTuple(TestOnlySetsInBinaryOps):
  626.     def setUp(self):
  627.         self.set   = Set((1, 2, 3))
  628.         self.other = (2, 4, 6)
  629.         self.otherIsIterable = True
  630.  
  631. #------------------------------------------------------------------------------
  632.  
  633. class TestOnlySetsString(TestOnlySetsInBinaryOps):
  634.     def setUp(self):
  635.         self.set   = Set((1, 2, 3))
  636.         self.other = 'abc'
  637.         self.otherIsIterable = True
  638.  
  639. #------------------------------------------------------------------------------
  640.  
  641. class TestOnlySetsGenerator(TestOnlySetsInBinaryOps):
  642.     def setUp(self):
  643.         def gen():
  644.             for i in xrange(0, 10, 2):
  645.                 yield i
  646.         self.set   = Set((1, 2, 3))
  647.         self.other = gen()
  648.         self.otherIsIterable = True
  649.  
  650. #==============================================================================
  651.  
  652. class TestCopying(unittest.TestCase):
  653.  
  654.     def test_copy(self):
  655.         dup = self.set.copy()
  656.         dup_list = list(dup); dup_list.sort()
  657.         set_list = list(self.set); set_list.sort()
  658.         self.assertEqual(len(dup_list), len(set_list))
  659.         for i in range(len(dup_list)):
  660.             self.failUnless(dup_list[i] is set_list[i])
  661.  
  662.     def test_deep_copy(self):
  663.         dup = copy.deepcopy(self.set)
  664.         ##print type(dup), `dup`
  665.         dup_list = list(dup); dup_list.sort()
  666.         set_list = list(self.set); set_list.sort()
  667.         self.assertEqual(len(dup_list), len(set_list))
  668.         for i in range(len(dup_list)):
  669.             self.assertEqual(dup_list[i], set_list[i])
  670.  
  671. #------------------------------------------------------------------------------
  672.  
  673. class TestCopyingEmpty(TestCopying):
  674.     def setUp(self):
  675.         self.set = Set()
  676.  
  677. #------------------------------------------------------------------------------
  678.  
  679. class TestCopyingSingleton(TestCopying):
  680.     def setUp(self):
  681.         self.set = Set(["hello"])
  682.  
  683. #------------------------------------------------------------------------------
  684.  
  685. class TestCopyingTriple(TestCopying):
  686.     def setUp(self):
  687.         self.set = Set(["zero", 0, None])
  688.  
  689. #------------------------------------------------------------------------------
  690.  
  691. class TestCopyingTuple(TestCopying):
  692.     def setUp(self):
  693.         self.set = Set([(1, 2)])
  694.  
  695. #------------------------------------------------------------------------------
  696.  
  697. class TestCopyingNested(TestCopying):
  698.     def setUp(self):
  699.         self.set = Set([((1, 2), (3, 4))])
  700.  
  701. #==============================================================================
  702.  
  703. class TestIdentities(unittest.TestCase):
  704.     def setUp(self):
  705.         self.a = Set('abracadabra')
  706.         self.b = Set('alacazam')
  707.  
  708.     def test_binopsVsSubsets(self):
  709.         a, b = self.a, self.b
  710.         self.assert_(a - b < a)
  711.         self.assert_(b - a < b)
  712.         self.assert_(a & b < a)
  713.         self.assert_(a & b < b)
  714.         self.assert_(a | b > a)
  715.         self.assert_(a | b > b)
  716.         self.assert_(a ^ b < a | b)
  717.  
  718.     def test_commutativity(self):
  719.         a, b = self.a, self.b
  720.         self.assertEqual(a&b, b&a)
  721.         self.assertEqual(a|b, b|a)
  722.         self.assertEqual(a^b, b^a)
  723.         if a != b:
  724.             self.assertNotEqual(a-b, b-a)
  725.  
  726.     def test_summations(self):
  727.         # check that sums of parts equal the whole
  728.         a, b = self.a, self.b
  729.         self.assertEqual((a-b)|(a&b)|(b-a), a|b)
  730.         self.assertEqual((a&b)|(a^b), a|b)
  731.         self.assertEqual(a|(b-a), a|b)
  732.         self.assertEqual((a-b)|b, a|b)
  733.         self.assertEqual((a-b)|(a&b), a)
  734.         self.assertEqual((b-a)|(a&b), b)
  735.         self.assertEqual((a-b)|(b-a), a^b)
  736.  
  737.     def test_exclusion(self):
  738.         # check that inverse operations show non-overlap
  739.         a, b, zero = self.a, self.b, Set()
  740.         self.assertEqual((a-b)&b, zero)
  741.         self.assertEqual((b-a)&a, zero)
  742.         self.assertEqual((a&b)&(a^b), zero)
  743.  
  744. #==============================================================================
  745.  
  746. libreftest = """
  747. Example from the Library Reference:  Doc/lib/libsets.tex
  748.  
  749. >>> from sets import Set as Base  # override _repr to get sorted output
  750. >>> class Set(Base):
  751. ...     def _repr(self):
  752. ...         return Base._repr(self, sorted=True)
  753. >>> engineers = Set(['John', 'Jane', 'Jack', 'Janice'])
  754. >>> programmers = Set(['Jack', 'Sam', 'Susan', 'Janice'])
  755. >>> managers = Set(['Jane', 'Jack', 'Susan', 'Zack'])
  756. >>> employees = engineers | programmers | managers           # union
  757. >>> engineering_management = engineers & managers            # intersection
  758. >>> fulltime_management = managers - engineers - programmers # difference
  759. >>> engineers.add('Marvin')
  760. >>> print engineers
  761. Set(['Jack', 'Jane', 'Janice', 'John', 'Marvin'])
  762. >>> employees.issuperset(engineers)           # superset test
  763. False
  764. >>> employees.union_update(engineers)         # update from another set
  765. >>> employees.issuperset(engineers)
  766. True
  767. >>> for group in [engineers, programmers, managers, employees]:
  768. ...     group.discard('Susan')                # unconditionally remove element
  769. ...     print group
  770. ...
  771. Set(['Jack', 'Jane', 'Janice', 'John', 'Marvin'])
  772. Set(['Jack', 'Janice', 'Sam'])
  773. Set(['Jack', 'Jane', 'Zack'])
  774. Set(['Jack', 'Jane', 'Janice', 'John', 'Marvin', 'Sam', 'Zack'])
  775. """
  776.  
  777. #==============================================================================
  778.  
  779. __test__ = {'libreftest' : libreftest}
  780.  
  781. def test_main(verbose=None):
  782.     from test import test_sets
  783.     test_support.run_unittest(
  784.         TestSetOfSets,
  785.         TestExceptionPropagation,
  786.         TestBasicOpsEmpty,
  787.         TestBasicOpsSingleton,
  788.         TestBasicOpsTuple,
  789.         TestBasicOpsTriple,
  790.         TestBinaryOps,
  791.         TestUpdateOps,
  792.         TestMutate,
  793.         TestSubsetEqualEmpty,
  794.         TestSubsetEqualNonEmpty,
  795.         TestSubsetEmptyNonEmpty,
  796.         TestSubsetPartial,
  797.         TestSubsetNonOverlap,
  798.         TestOnlySetsNumeric,
  799.         TestOnlySetsDict,
  800.         TestOnlySetsOperator,
  801.         TestOnlySetsTuple,
  802.         TestOnlySetsString,
  803.         TestOnlySetsGenerator,
  804.         TestCopyingEmpty,
  805.         TestCopyingSingleton,
  806.         TestCopyingTriple,
  807.         TestCopyingTuple,
  808.         TestCopyingNested,
  809.         TestIdentities,
  810.     )
  811.     test_support.run_doctest(test_sets, verbose)
  812.  
  813. if __name__ == "__main__":
  814.     test_main(verbose=True)
  815.