204 lines
6.6 KiB
Python
204 lines
6.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
测试多次生成迷宫时历史数据重置功能
|
|
验证连续生成迷宫时不会有历史数据混乱
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
from maze import Maze
|
|
import time
|
|
|
|
def test_multiple_generation():
|
|
"""测试多次生成迷宫时历史数据重置"""
|
|
print("=== 测试多次生成迷宫历史重置 ===")
|
|
|
|
maze = Maze(wall_size=30, maze_size=600, file_name="test.csv")
|
|
|
|
# 第一次生成迷宫
|
|
print("\n--- 第一次生成迷宫 ---")
|
|
maze.generate()
|
|
|
|
first_history_count = len(maze.history_mazes)
|
|
first_path_count = len(maze.full_path)
|
|
|
|
print(f"第一次生成 - 历史步数: {first_history_count}, 路径长度: {first_path_count}")
|
|
|
|
if first_history_count == 0:
|
|
print("❌ 第一次生成后没有历史数据")
|
|
return False
|
|
|
|
# 验证初始状态
|
|
if maze.history_step != 0 or maze.show_history != False or maze.path_step != 0:
|
|
print(f"❌ 第一次生成后状态不正确: history_step={maze.history_step}, show_history={maze.show_history}, path_step={maze.path_step}")
|
|
return False
|
|
|
|
# 切换到历史模式并前进几步
|
|
maze.toggle_history_mode()
|
|
maze.next_history_step()
|
|
maze.next_history_step()
|
|
|
|
print(f"历史模式前进后 - history_step: {maze.history_step}, show_history: {maze.show_history}")
|
|
|
|
# 第二次生成迷宫
|
|
print("\n--- 第二次生成迷宫 ---")
|
|
maze.generate()
|
|
|
|
second_history_count = len(maze.history_mazes)
|
|
second_path_count = len(maze.full_path)
|
|
|
|
print(f"第二次生成 - 历史步数: {second_history_count}, 路径长度: {second_path_count}")
|
|
|
|
# 验证状态重置
|
|
if maze.history_step != 0:
|
|
print(f"❌ 第二次生成后history_step未重置: {maze.history_step}")
|
|
return False
|
|
|
|
if maze.show_history != False:
|
|
print(f"❌ 第二次生成后show_history未重置: {maze.show_history}")
|
|
return False
|
|
|
|
if maze.path_step != 0:
|
|
print(f"❌ 第二次生成后path_step未重置: {maze.path_step}")
|
|
return False
|
|
|
|
if second_history_count == 0:
|
|
print("❌ 第二次生成后没有新的历史数据")
|
|
return False
|
|
|
|
# 验证历史数据是全新的(长度可能不同)
|
|
if second_history_count == first_history_count:
|
|
# 如果长度相同,检查内容是否不同
|
|
if maze.history_mazes[0] == maze.history_mazes[0]: # 比较第一步
|
|
print("⚠️ 两次生成的历史数据第一步相同(可能是随机种子问题,但功能正常)")
|
|
|
|
print(f"✅ 第二次生成后所有状态正确重置")
|
|
|
|
# 第三次生成以确保一致性
|
|
print("\n--- 第三次生成迷宫 ---")
|
|
|
|
# 先改变一些状态
|
|
maze.toggle_history_mode()
|
|
maze.next_history_step()
|
|
maze.next_path_step()
|
|
|
|
maze.generate()
|
|
|
|
third_history_count = len(maze.history_mazes)
|
|
third_path_count = len(maze.full_path)
|
|
|
|
print(f"第三次生成 - 历史步数: {third_history_count}, 路径长度: {third_path_count}")
|
|
|
|
# 再次验证状态重置
|
|
if maze.history_step != 0 or maze.show_history != False or maze.path_step != 0:
|
|
print(f"❌ 第三次生成后状态未正确重置")
|
|
return False
|
|
|
|
print("✅ 第三次生成后状态也正确重置")
|
|
|
|
return True
|
|
|
|
def test_maze_generator_reset():
|
|
"""测试MazeGenerator的历史重置"""
|
|
print("\n=== 测试MazeGenerator历史重置 ===")
|
|
|
|
from maze_generator import MazeGenerator
|
|
|
|
generator = MazeGenerator(size=21, filename="test.csv", name="测试")
|
|
|
|
# 第一次生成
|
|
generator.generate(seed=12345)
|
|
first_history = len(generator.get_history_mazes())
|
|
first_elements = len(generator.special_elements)
|
|
|
|
print(f"第一次生成 - 历史: {first_history}, 特殊元素: {first_elements}")
|
|
|
|
# 第二次生成
|
|
generator.generate(seed=54321)
|
|
second_history = len(generator.get_history_mazes())
|
|
second_elements = len(generator.special_elements)
|
|
|
|
print(f"第二次生成 - 历史: {second_history}, 特殊元素: {second_elements}")
|
|
|
|
if first_history == 0 or second_history == 0:
|
|
print("❌ 生成器历史数据为空")
|
|
return False
|
|
|
|
# 验证特殊元素也被重置
|
|
if second_elements == 0:
|
|
print("❌ 第二次生成后特殊元素为空")
|
|
return False
|
|
|
|
print("✅ MazeGenerator历史和特殊元素正确重置")
|
|
return True
|
|
|
|
def test_state_isolation():
|
|
"""测试不同迷宫实例之间的状态隔离"""
|
|
print("\n=== 测试迷宫实例状态隔离 ===")
|
|
|
|
# 创建两个迷宫实例
|
|
maze1 = Maze(wall_size=30, maze_size=600, file_name="test1.csv")
|
|
maze2 = Maze(wall_size=30, maze_size=600, file_name="test2.csv")
|
|
|
|
# 生成第一个迷宫
|
|
maze1.generate()
|
|
history1 = len(maze1.history_mazes)
|
|
|
|
# 修改第一个迷宫的状态
|
|
maze1.toggle_history_mode()
|
|
maze1.next_history_step()
|
|
|
|
# 生成第二个迷宫
|
|
maze2.generate()
|
|
history2 = len(maze2.history_mazes)
|
|
|
|
# 验证第二个迷宫的状态是独立的
|
|
if maze2.show_history != False or maze2.history_step != 0:
|
|
print(f"❌ 第二个迷宫状态被第一个影响: show_history={maze2.show_history}, history_step={maze2.history_step}")
|
|
return False
|
|
|
|
# 验证第一个迷宫状态没变
|
|
if maze1.show_history != True or maze1.history_step == 0:
|
|
print(f"❌ 第一个迷宫状态意外改变: show_history={maze1.show_history}, history_step={maze1.history_step}")
|
|
return False
|
|
|
|
print(f"✅ 两个迷宫实例状态正确隔离 (历史: {history1}, {history2})")
|
|
return True
|
|
|
|
def main():
|
|
print("开始测试多次生成迷宫时历史数据重置...")
|
|
|
|
tests = [
|
|
test_multiple_generation,
|
|
test_maze_generator_reset,
|
|
test_state_isolation
|
|
]
|
|
|
|
passed = 0
|
|
total = len(tests)
|
|
|
|
for test in tests:
|
|
try:
|
|
if test():
|
|
passed += 1
|
|
else:
|
|
print(f"❌ 测试 {test.__name__} 失败")
|
|
except Exception as e:
|
|
print(f"❌ 测试 {test.__name__} 出现异常: {str(e)}")
|
|
|
|
print(f"\n=== 测试结果 ===")
|
|
print(f"通过: {passed}/{total}")
|
|
|
|
if passed == total:
|
|
print("🎉 所有历史重置功能测试通过!")
|
|
return True
|
|
else:
|
|
print("❌ 部分测试失败,请检查相关功能")
|
|
return False
|
|
|
|
if __name__ == "__main__":
|
|
success = main()
|
|
sys.exit(0 if success else 1)
|