コード例 #1
0
 def _bfs(self, node_name):
     search_queue = Queue()
     search_queue.add(self.root)
     while len(search_queue) > 0:
         n = search_queue.remove()
         if n.name == node_name:
             return n
         for c in n.get_children():
             search_queue.add(c)
     return None
コード例 #2
0
class TestQueue(unittest.TestCase):
    def setUp(self):
        self.data = ["d1", "d2"]
        self.l1 = Queue()
        self.l2 = Queue()
        self.l3 = Queue()
        self.l2.add(self.data[0])
        for datum in self.data:
            self.l3.add(datum)

    def test_Queue__init__(self):
        self.assertEqual(self.l1.head, None)
        self.assertEqual(self.l1.len, 0)

    def test_Queue__iter__(self):
        for n in self.l1:
            self.assertTrue(False)
        for l in [self.l2, self.l3]:
            for n in l:
                self.assertTrue(repr(n) in self.data)

    def test_Queue_add(self):
        len1 = len(self.l1)
        self.l1.add("foo")
        self.assertTrue(len(self.l1) - len1 == 1)
        self.assertTrue(self.l1.remove() == "foo")

    def test_Queue_remove(self):
        self.assertTrue(self.l3.remove() == self.data[0])

    def test_Queue__len__(self):
        self.assertEquals(len(self.l1), 0)
        self.assertEquals(len(self.l2), 1)
        self.assertEquals(len(self.l3), 2)

    def test_Queue__repr__(self):
        self.assertEquals(repr(self.l1), '')
        self.assertEquals(repr(self.l2), 'd1')
        self.assertEquals(repr(self.l3), 'd1 -> d2')