2024-06-03 09:38:15 +00:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from oop import Tu
|
|
|
|
class TextProcessor(Tu):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
class TestQueryBridgeWords(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2024-06-16 15:24:15 +00:00
|
|
|
# Initialize a graph for testing
|
2024-06-03 09:38:15 +00:00
|
|
|
self.graph = {
|
2024-06-16 15:24:15 +00:00
|
|
|
'a': {'b': 1, 'c': 2},
|
|
|
|
'b': {'c': 1, 'd': 2},
|
|
|
|
'c': {'d': 1},
|
|
|
|
'x': {},
|
|
|
|
'z': {}
|
2024-06-03 09:38:15 +00:00
|
|
|
}
|
|
|
|
self.processor = TextProcessor()
|
|
|
|
self.processor.generate_directed_dict(self.processor.read_text_file("1.txt"))
|
|
|
|
self.processor.generate_directed_graph()
|
2024-06-16 15:24:15 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
self.assertEqual(path, [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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-06-03 09:38:15 +00:00
|
|
|
|
|
|
|
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
|
2024-06-16 15:24:15 +00:00
|
|
|
result = self.processor.queryBridgeWords('now', 'lod')
|
2024-06-03 09:38:15 +00:00
|
|
|
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)
|
|
|
|
|
2024-06-16 15:24:15 +00:00
|
|
|
|
2024-06-03 09:38:15 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|