示例#1
0
 def test_iter3(self):
   t2 = SetTrie()
   it = iter(t2)
   self.assertRaises(StopIteration, it.__next__)
示例#2
0
 def setUp(self):
   self.t = SetTrie([{1, 3}, {1, 3, 5}, {1, 4}, {1, 2, 4}, {2, 4}, {2, 3, 5}])
示例#3
0
文件: test.py 项目: tzing/pysettrie
 def setUp(self):
     self.t = SetTrie([(1, 3), (1, 3, 5), (1, 4), (1, 2, 4), (2, 4),
                       (2, 3, 5)])
示例#4
0
class TestSetTrie(unittest.TestCase):
  """
  UnitTest for SetTrie class
  """

  def setUp(self):
    self.t = SetTrie([{1, 3}, {1, 3, 5}, {1, 4}, {1, 2, 4}, {2, 4}, {2, 3, 5}])

  def test_print(self):
    expected = """None
  1
    2
      4#
    3#
      5#
    4#
  2
    3
      5#
    4#
"""
    from io import StringIO
    outp = StringIO()
    self.t.printtree(stream=outp)
    self.assertEqual(outp.getvalue(), expected)

  def test_iter(self):
    a = []
    for s in self.t:
      a.append(s)
    self.assertEqual(a, [{1, 2, 4}, {1, 3}, {1, 3, 5}, {1, 4}, {2, 3, 5}, {2, 4}])

  def test_iter2(self):
    it = iter(self.t)
    for s in it:
      pass
    self.assertRaises(StopIteration, it.__next__)

  def test_iter3(self):
    t2 = SetTrie()
    it = iter(t2)
    self.assertRaises(StopIteration, it.__next__)

  def test_aslist(self):
    self.assertEqual(self.t.aslist(), [{1, 2, 4}, {1, 3}, {1, 3, 5}, {1, 4}, {2, 3, 5}, {2, 4}])
    
  def test_str(self):
    self.assertEqual(str(self.t), "[{1, 2, 4}, {1, 3}, {1, 3, 5}, {1, 4}, {2, 3, 5}, {2, 4}]")

  def test_contains(self):
    self.assertTrue(self.t.contains( {1, 3} ))
    self.assertFalse(self.t.contains( {1} ))
    self.assertTrue(self.t.contains( {1, 3, 5} ))
    self.assertFalse(self.t.contains( {1, 3, 5, 7} ))

  def test_in(self):
    self.assertTrue({1, 3} in self.t)
    self.assertFalse({1} in self.t)
    self.assertTrue({1, 3, 5} in self.t)
    self.assertFalse({1, 3, 5, 7} in self.t)
    
  def test_hassuperset(self):
    self.assertTrue(self.t.hassuperset({3, 5}))
    self.assertFalse(self.t.hassuperset({6}))
    self.assertTrue(self.t.hassuperset({1, 2, 4}))
    self.assertFalse(self.t.hassuperset({2, 4, 5} ))
    
  def test_supersets(self):
    self.assertEqual(self.t.supersets({3, 5}), [{1, 3, 5}, {2, 3, 5}])
    self.assertEqual(self.t.supersets({1, 4}), [{1, 2, 4}, {1, 4}])
    self.assertEqual(self.t.supersets({1, 3, 5}),  [{1, 3, 5}])
    self.assertEqual(self.t.supersets({2}),  [{1, 2, 4}, {2, 3, 5}, {2, 4}])
    self.assertEqual(self.t.supersets({1}),  [{1, 2, 4}, {1, 3}, {1, 3, 5}, {1, 4}])
    self.assertEqual(self.t.supersets({1, 2, 5}),  [])
    self.assertEqual(self.t.supersets({1, 2, 4, 5}),  [])
    self.assertEqual(self.t.supersets({6}),  [])

  def test_hassubset(self):
    self.assertTrue(self.t.hassubset({1, 2, 3}))
    self.assertTrue(self.t.hassubset({2, 3, 4, 5}))
    self.assertTrue(self.t.hassubset({1, 4}))
    self.assertTrue(self.t.hassubset({2, 3, 5}))
    self.assertFalse(self.t.hassubset({3, 4, 5}))
    self.assertFalse(self.t.hassubset({6, 7, 8, 9, 1000}))

  def test_subsets(self):
    self.assertEqual(self.t.subsets({1, 2, 4, 11}), [{1, 2, 4}, {1, 4}, {2, 4}])
    self.assertEqual(self.t.subsets({1, 2, 4}), [{1, 2, 4}, {1, 4}, {2, 4}])
    self.assertEqual(self.t.subsets({1, 2}), [])
    self.assertEqual(self.t.subsets({1, 2, 3, 4, 5}), [{1, 2, 4}, {1, 3}, {1, 3, 5}, {1, 4}, {2, 3, 5}, {2, 4}])
    self.assertEqual(self.t.subsets({0, 1, 3, 5}), [{1, 3}, {1, 3, 5}])
    self.assertEqual(self.t.subsets({1, 2, 5}), [])
    self.assertEqual(self.t.subsets({1, 4}), [{1, 4}]) # :)   
    self.assertEqual(self.t.subsets({1, 3, 5}), [{1, 3}, {1, 3, 5}])
    self.assertEqual(self.t.subsets({1, 3, 5, 111}), [{1, 3}, {1, 3, 5}])
    self.assertEqual(self.t.subsets({1, 4, 8}), [{1, 4}])
    self.assertEqual(self.t.subsets({2, 3, 4, 5}), [{2, 3, 5}, {2, 4}])
    self.assertEqual(self.t.subsets({2, 3, 5, 6}), [{2, 3, 5}])
示例#5
0
文件: test.py 项目: tzing/pysettrie
class TestSetTrie(unittest.TestCase):
    """
    UnitTest for SetTrie class
    """
    def setUp(self):
        self.t = SetTrie([(1, 3), (1, 3, 5), (1, 4), (1, 2, 4), (2, 4),
                          (2, 3, 5)])

    def test_iter(self):
        self.assertCountEqual(
            list(self.t),
            [
                (1, 2, 4),
                (1, 3),
                (1, 3, 5),
                (1, 4),
                (2, 3, 5),
                (2, 4),
            ],
        )

    def test_iter2(self):
        it = iter(self.t)
        for s in it:
            pass
        self.assertRaises(StopIteration, it.__next__)

    def test_iter3(self):
        t2 = SetTrie()
        it = iter(t2)
        self.assertRaises(StopIteration, it.__next__)

    def test_repr(self):
        self.assertEqual(repr(self.t), "<SetTrie with 6 sets>")

    def test_contains(self):
        self.assertIn((1, 3), self.t)
        self.assertNotIn((1, ), self.t)
        self.assertIn((1, 3, 5), self.t)
        self.assertNotIn((1, 3, 5, 7), self.t)

    def test_has_superset(self):
        self.assertTrue(self.t.has_superset((3, 5)))
        self.assertFalse(self.t.has_superset((6, )))
        self.assertTrue(self.t.has_superset((1, 2, 4)))
        self.assertFalse(self.t.has_superset((2, 4, 5)))

    def test_supersets(self):
        self.assertCountEqual(self.t.iter_supersets((3, 5)), [(1, 3, 5),
                                                              (2, 3, 5)])
        self.assertCountEqual(self.t.iter_supersets((1, 4)), [(1, 2, 4),
                                                              (1, 4)])
        self.assertCountEqual(self.t.iter_supersets((1, 3, 5)), [(1, 3, 5)])
        self.assertCountEqual(self.t.iter_supersets((2, )), [(1, 2, 4),
                                                             (2, 3, 5),
                                                             (2, 4)])
        self.assertCountEqual(self.t.iter_supersets((1, )), [(1, 2, 4), (1, 3),
                                                             (1, 3, 5),
                                                             (1, 4)])
        self.assertCountEqual(self.t.iter_supersets((1, 2, 5)), [])
        self.assertCountEqual(self.t.iter_supersets((1, 2, 4, 5)), [])
        self.assertCountEqual(self.t.iter_supersets((6, )), [])

    def test_has_subset(self):
        self.assertTrue(self.t.has_subset((1, 2, 3)))
        self.assertTrue(self.t.has_subset((2, 3, 4, 5)))
        self.assertTrue(self.t.has_subset((1, 4)))
        self.assertTrue(self.t.has_subset((2, 3, 5)))
        self.assertFalse(self.t.has_subset((3, 4, 5)))
        self.assertFalse(self.t.has_subset((6, 7, 8, 9, 1000)))

    def test_subsets(self):
        self.assertCountEqual(
            self.t.iter_subsets((1, 2, 4, 11)),
            [(1, 2, 4), (1, 4), (2, 4)],
        )
        self.assertCountEqual(self.t.iter_subsets((1, 2, 4)), [(1, 2, 4),
                                                               (1, 4), (2, 4)])
        self.assertCountEqual(self.t.iter_subsets((1, 2)), [])
        self.assertCountEqual(
            self.t.iter_subsets((1, 2, 3, 4, 5)),
            [(1, 2, 4), (1, 3), (1, 3, 5), (1, 4), (2, 3, 5), (2, 4)],
        )
        self.assertCountEqual(self.t.iter_subsets((0, 1, 3, 5)), [(1, 3),
                                                                  (1, 3, 5)])
        self.assertCountEqual(self.t.iter_subsets((1, 2, 5)), [])
        self.assertCountEqual(self.t.iter_subsets((1, 4)), [(1, 4)])  # :)
        self.assertCountEqual(self.t.iter_subsets((1, 3, 5)), [(1, 3),
                                                               (1, 3, 5)])
        self.assertCountEqual(
            self.t.iter_subsets((1, 3, 5, 111)),
            [(1, 3), (1, 3, 5)],
        )
        self.assertCountEqual(self.t.iter_subsets((1, 4, 8)), [(1, 4)])
        self.assertCountEqual(self.t.iter_subsets((2, 3, 4, 5)), [(2, 3, 5),
                                                                  (2, 4)])
        self.assertCountEqual(self.t.iter_subsets((2, 3, 5, 6)), [(2, 3, 5)])

    def test_discard(self):
        self.assertIn([1, 3], self.t)
        self.t.discard((1, 3))
        self.assertIn([1, 3, 5], self.t)
        self.assertNotIn([1, 3], self.t)