import unittest from oop import Tu class TextProcessor(Tu): def __init__(self): super().__init__() class TestQueryBridgeWords(unittest.TestCase): def setUp(self): # Initialize a graph for testing self.graph = { 'a': {'b': 1, 'c': 2}, 'b': {'c': 1, 'd': 2}, 'c': {'d': 1}, 'x': {}, 'z': {} } self.processor = TextProcessor() self.processor.generate_directed_dict(self.processor.read_text_file("1.txt")) self.processor.generate_directed_graph() def test_normal_path1(self): # 正常情况:存在路径 word1 = 'to' word2 = 'out' expected_path = ['to', 'seek', 'out'] expected_length = 2 path,length = self.processor.calcShortestPath(word1, word2) self.assertEqual(path, [expected_path]) self.assertEqual(length, expected_length) """def test_normal_path2(self): # 正常情况:存在路径 word1 = 'to' word2 = 'new' expected_path = ['to', 'seek', 'out',"new"],['to', 'seek', "strange",'new'],['to', 'explore', "strange",'new'] expected_length = 3 path, length = self.processor.calcShortestPath(word1, word2) path_set=set([]) for i in path : path_set.add(i) self.assertSetEqual(path_set, set(expected_path)) self.assertEqual(length, expected_length)""" def test_no_path(self): # 无路径情况 word1 = 'civilizations' word2 = 'to' expected_path = [] expected_length = 0 path, length = self.processor.calcShortestPath(word1, word2) self.assertEqual(path, expected_path) self.assertEqual(length, expected_length) def test_node_not_exists(self): # 节点不存在情况 word1 = 'now' word2 = 'lod' expected_path = [] expected_length = 0 path, length = self.processor.calcShortestPath(word1, word2) self.assertEqual(path, expected_path) self.assertEqual(length, expected_length) def test_same_node(self): # 同一节点情况 word1 = 'to' word2 = 'to' expected_path = [] expected_length = 0 path, length = self.processor.calcShortestPath(word1, word2) self.assertEqual(path, expected_path) self.assertEqual(length, expected_length) def test_valid_bridge_words_exist(self): # Valid equivalence class: bridge words exist result = self.processor.queryBridgeWords('the', 'floor') self.assertIn('forest', result) def test_valid_bridge_words_not_exist(self): # Valid equivalence class: no bridge words result = self.processor.queryBridgeWords('on', 'the') self.assertEqual(result, []) def test_invalid_word1_not_in_graph(self): # Invalid equivalence class: word1 not in graph result = self.processor.queryBridgeWords('an', 'a') self.assertEqual(result, []) def test_invalid_word2_not_in_graph(self): # Invalid equivalence class: word2 not in graph result = self.processor.queryBridgeWords('a', 'an') self.assertEqual(result, []) def test_invalid_both_words_not_in_graph(self): # Invalid equivalence class: both words not in graph result = self.processor.queryBridgeWords('now', 'lod') self.assertEqual(result, []) def test_edge_case_min_word(self): # Edge case: minimum word result = self.processor.queryBridgeWords('a', 'b') self.assertEqual(result, []) def test_edge_case_max_word(self): # Edge case: maximum word result = self.processor.queryBridgeWords('x', 'z') self.assertEqual(result, []) def test_edge_case_no_bridge_words(self): # Edge case: no bridge words result = self.processor.queryBridgeWords('new', 'worlds') self.assertEqual(result, []) def test_edge_case_single_bridge_word(self): # Edge case: single bridge word result = self.processor.queryBridgeWords('the', 'floor') self.assertIn('forest', result) if __name__ == '__main__': unittest.main()