maze_python/tests/test_history_iteration.py

196 lines
5.8 KiB
Python

#!/usr/bin/env python3
"""
测试历史迭代展示功能
验证生成过程的历史记录和展示功能
"""
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from maze_generator import MazeGenerator
from maze import Maze
import time
def test_history_generation():
"""测试迷宫生成历史记录功能"""
print("=== 测试迷宫生成历史记录 ===")
generator = MazeGenerator(size=21, filename="test.csv", name="测试迷宫")
generator.generate(seed=12345)
history = generator.get_history_mazes()
print(f"历史步数: {len(history)}")
if len(history) == 0:
print("❌ 历史记录为空")
return False
# 验证历史记录的每一步都是有效的迷宫
for i, maze_state in enumerate(history):
if len(maze_state) != 21 or len(maze_state[0]) != 21:
print(f"❌ 历史步骤 {i} 尺寸错误: {len(maze_state)}x{len(maze_state[0])}")
return False
print(f"✅ 历史记录验证通过,共 {len(history)}")
return True
def test_maze_history_methods():
"""测试Maze类的历史展示方法"""
print("\n=== 测试Maze历史展示方法 ===")
maze = Maze(wall_size=30, maze_size=630, file_name="test.csv")
maze.generate()
# 验证历史数据
if len(maze.history_mazes) == 0:
print("❌ 生成后历史数据为空")
return False
print(f"历史步数: {len(maze.history_mazes)}")
print(f"初始历史步骤: {maze.history_step}")
print(f"初始展示模式: {'历史' if maze.show_history else '路径'}")
# 测试历史步骤控制
original_step = maze.history_step
# 测试下一步
result = maze.next_history_step()
if result and maze.history_step == original_step + 1:
print("✅ next_history_step 正常工作")
else:
print(f"❌ next_history_step 失败,步骤: {original_step} -> {maze.history_step}")
return False
# 测试上一步
result = maze.prev_history_step()
if result and maze.history_step == original_step:
print("✅ prev_history_step 正常工作")
else:
print(f"❌ prev_history_step 失败,步骤: {maze.history_step}")
return False
# 测试模式切换
original_mode = maze.show_history
maze.toggle_history_mode()
if maze.show_history != original_mode:
print("✅ toggle_history_mode 正常工作")
else:
print("❌ toggle_history_mode 失败")
return False
# 切换回原模式
maze.toggle_history_mode()
return True
def test_load_without_history():
"""测试加载存档时不展示历史"""
print("\n=== 测试加载存档时历史处理 ===")
# 先生成一个迷宫并保存
maze1 = Maze(wall_size=30, maze_size=600, file_name="test.csv")
maze1.generate()
if len(maze1.history_mazes) == 0:
print("❌ 生成的迷宫没有历史数据")
return False
print(f"生成迷宫的历史步数: {len(maze1.history_mazes)}")
# 保存迷宫
save_result = maze1.save_game(format_type="json")
if not save_result:
print("❌ 保存迷宫失败")
return False
# 创建新的迷宫实例并加载
maze2 = Maze(wall_size=30, maze_size=600, file_name="test.csv")
# 假设有一个保存的文件
import glob
json_files = glob.glob("saves/*.json")
if not json_files:
print("❌ 没有找到保存的JSON文件")
return False
load_result = maze2.load_game(json_files[0])
if not load_result:
print("❌ 加载迷宫失败")
return False
# 验证加载后历史数据被清空
if len(maze2.history_mazes) == 0 and not maze2.show_history:
print("✅ 加载存档后历史数据正确清空")
return True
else:
print(f"❌ 加载存档后历史数据未正确清空: {len(maze2.history_mazes)} 步, 展示模式: {maze2.show_history}")
return False
def test_history_boundary_conditions():
"""测试历史展示的边界条件"""
print("\n=== 测试历史展示边界条件 ===")
maze = Maze(wall_size=30, maze_size=600, file_name="test.csv")
maze.generate()
if len(maze.history_mazes) == 0:
print("❌ 没有历史数据进行测试")
return False
# 测试超出上限
maze.history_step = len(maze.history_mazes) - 1
result = maze.next_history_step()
if not result:
print("✅ 历史步骤上限控制正常")
else:
print("❌ 历史步骤上限控制失败")
return False
# 测试超出下限
maze.history_step = 0
result = maze.prev_history_step()
if not result:
print("✅ 历史步骤下限控制正常")
else:
print("❌ 历史步骤下限控制失败")
return False
return True
def main():
print("开始测试历史迭代展示功能...")
tests = [
test_history_generation,
test_maze_history_methods,
test_load_without_history,
test_history_boundary_conditions
]
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)