def test_add_populated_queue(self): """Test ID: Q3.""" new_q = Queue(['one', 'two', 3]) old_len = len(new_q) new_q.add('four') new_len = len(new_q) self.assertEqual(new_len, old_len + 1)
def bfs_search(grph, start, target): """ Perform a breadth-first search on a graph with a source and destination. @param grph: the Graph object to perform a BFS on. @type grph: Graph @param start: the source node to start the BFS at. @type start: str @param target: the destination node to reach and end the BFS. @type target: str @return: the path from start to target reached by the BFS, otherwise an empty path if the target could not be reached or if start and/or target are invalid nodes in the graph. @rtype: list @raises TypeError: if the grph is not a Graph object. """ if not isinstance(grph, Graph): raise TypeError('This is not a Graph object.') queue = Queue() queue.add(start) while len(queue) > 0: v = queue.remove() node = v[-1] if node == target: return list(v) adjacent_nodes = grph.get_adj_list(node) if adjacent_nodes == None: return [] for adj_node in adjacent_nodes: visited = list(v) visited.append(adj_node) queue.add(visited) return []
class TestQueue(unittest.TestCase): def setUp(self): self.Queue = Queue() def test_add_check_len(self): """Test ID: Q1.""" old_len = len(self.Queue) self.Queue.add('hi') new_len = len(self.Queue) self.assertEqual(old_len + 1, new_len) def test_add_check_item(self): """Test ID: Q2.""" self.Queue.add('bye') q_len = len(self.Queue) self.assertEqual(self.Queue.q_list[q_len - 1], 'bye') def test_add_populated_queue(self): """Test ID: Q3.""" new_q = Queue(['one', 'two', 3]) old_len = len(new_q) new_q.add('four') new_len = len(new_q) self.assertEqual(new_len, old_len + 1) def test_remove(self): """Test ID: Q4.""" self.Queue = Queue(['my', 'name', 'is', 'Stephen']) old_len = len(self.Queue) removed_item = self.Queue.remove() self.assertEqual(removed_item, 'my') self.assertEqual(old_len - 1, len(self.Queue)) def test_remove_empty_q(self): """Test ID: Q5.""" self.assertIsNone(self.Queue.remove()) def test_front(self): """Test ID: Q6.""" self.Queue = Queue(['good', 'morning', 'to', 'you', 'sir']) old_len = len(self.Queue) front_item = self.Queue.front() msg = 'items not the same' self.assertEqual(front_item, 'good', msg) self.assertEqual(len(self.Queue), old_len) def test_front_empty_q(self): """Test ID: Q7.""" front_item = self.Queue.front() self.assertIsNone(front_item) def test_len(self): """Test ID: Q8.""" self.Queue = Queue(['one', 'two', 'three']) expected_len = 3 self.assertEqual(len(self.Queue), expected_len) def test_len_empty_queue(self): """Test ID: Q9.""" new_q = Queue() expected_len = 0 self.assertEqual(len(new_q), expected_len) def test_str(self): """Test ID: Q10.""" q = Queue(['one', 'two', 'three']) ex = """['one', 'two', 'three']""" self.assertEqual(str(q), ex) def test_str_empty_queue(self): """Test ID: Q11.""" q = Queue() ex = '[]' self.assertEqual(str(q), ex) def test_str_multiple_types(self): """Test ID: Q12.""" q = Queue(['one', 1, 3.234, 'hello']) ex = """['one', 1, 3.234, 'hello']""" self.assertEqual(str(q), ex)