239 lines
8.3 KiB
Python
239 lines
8.3 KiB
Python
|
|
"""
|
|||
|
|
STT Python绑定测试脚本
|
|||
|
|
|
|||
|
|
这个脚本测试STT Python绑定的基本功能。
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import sys
|
|||
|
|
import os
|
|||
|
|
import tempfile
|
|||
|
|
import unittest
|
|||
|
|
|
|||
|
|
# 尝试导入STT模块
|
|||
|
|
try:
|
|||
|
|
import stt
|
|||
|
|
STT_AVAILABLE = True
|
|||
|
|
except ImportError as e:
|
|||
|
|
print(f"警告: 无法导入STT模块 - {e}")
|
|||
|
|
print("请确保已经构建并安装了STT Python绑定")
|
|||
|
|
STT_AVAILABLE = False
|
|||
|
|
stt = None
|
|||
|
|
|
|||
|
|
class TestSttBinding(unittest.TestCase):
|
|||
|
|
"""STT绑定测试类"""
|
|||
|
|
|
|||
|
|
@classmethod
|
|||
|
|
def setUpClass(cls):
|
|||
|
|
"""测试类设置"""
|
|||
|
|
if not STT_AVAILABLE:
|
|||
|
|
raise unittest.SkipTest("STT模块不可用")
|
|||
|
|
|
|||
|
|
# 创建临时目录用于测试输出
|
|||
|
|
cls.temp_dir = tempfile.mkdtemp()
|
|||
|
|
print(f"测试临时目录: {cls.temp_dir}")
|
|||
|
|
|
|||
|
|
@classmethod
|
|||
|
|
def tearDownClass(cls):
|
|||
|
|
"""测试类清理"""
|
|||
|
|
if hasattr(cls, 'temp_dir') and os.path.exists(cls.temp_dir):
|
|||
|
|
# 清理临时文件
|
|||
|
|
import shutil
|
|||
|
|
shutil.rmtree(cls.temp_dir)
|
|||
|
|
print(f"清理临时目录: {cls.temp_dir}")
|
|||
|
|
|
|||
|
|
def test_module_import(self):
|
|||
|
|
"""测试模块导入"""
|
|||
|
|
self.assertIsNotNone(stt, "STT模块应该可用")
|
|||
|
|
self.assertTrue(hasattr(stt, 'SttGenerator'), "应该包含SttGenerator类")
|
|||
|
|
self.assertTrue(hasattr(stt, 'create_stt'), "应该包含create_stt函数")
|
|||
|
|
self.assertTrue(hasattr(stt, 'WGS84'), "应该包含WGS84常量")
|
|||
|
|
|
|||
|
|
def test_generator_creation(self):
|
|||
|
|
"""测试生成器创建"""
|
|||
|
|
generator = stt.SttGenerator()
|
|||
|
|
self.assertIsNotNone(generator, "应该能够创建生成器实例")
|
|||
|
|
|
|||
|
|
# 测试基本方法存在
|
|||
|
|
self.assertTrue(hasattr(generator, 'set_tree_depth'), "应该有set_tree_depth方法")
|
|||
|
|
self.assertTrue(hasattr(generator, 'set_reference_system'), "应该有set_reference_system方法")
|
|||
|
|
self.assertTrue(hasattr(generator, 'run'), "应该有run方法")
|
|||
|
|
self.assertTrue(hasattr(generator, 'run_full'), "应该有run_full方法")
|
|||
|
|
|
|||
|
|
def test_basic_generation(self):
|
|||
|
|
"""测试基本生成功能"""
|
|||
|
|
generator = stt.SttGenerator()
|
|||
|
|
|
|||
|
|
# 设置基本参数
|
|||
|
|
generator.set_tree_depth(2, 4) # 使用较小的深度进行快速测试
|
|||
|
|
generator.set_reference_system("WGS84")
|
|||
|
|
|
|||
|
|
# 创建输出文件路径
|
|||
|
|
output_file = os.path.join(self.temp_dir, "test_basic.msh")
|
|||
|
|
|
|||
|
|
# 运行生成
|
|||
|
|
result = generator.run(output_file)
|
|||
|
|
|
|||
|
|
# 检查结果
|
|||
|
|
self.assertEqual(result, 0, "生成应该成功返回0")
|
|||
|
|
|
|||
|
|
# 检查输出文件是否存在
|
|||
|
|
if os.path.exists(output_file):
|
|||
|
|
file_size = os.path.getsize(output_file)
|
|||
|
|
print(f"生成文件大小: {file_size} 字节")
|
|||
|
|
self.assertGreater(file_size, 0, "输出文件应该非空")
|
|||
|
|
else:
|
|||
|
|
print("警告: 输出文件未生成,这可能是因为生成器需要更多参数")
|
|||
|
|
|
|||
|
|
def test_quick_creation(self):
|
|||
|
|
"""测试快速创建功能"""
|
|||
|
|
output_file = os.path.join(self.temp_dir, "test_quick.msh")
|
|||
|
|
|
|||
|
|
# 使用便利函数
|
|||
|
|
info = stt.create_stt(
|
|||
|
|
min_depth=2,
|
|||
|
|
max_depth=4,
|
|||
|
|
reference_system="Earth",
|
|||
|
|
output_file=output_file
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 检查返回信息
|
|||
|
|
self.assertIsInstance(info, dict, "应该返回字典信息")
|
|||
|
|
self.assertEqual(info['min_depth'], 2, "最小深度应该正确")
|
|||
|
|
self.assertEqual(info['max_depth'], 4, "最大深度应该正确")
|
|||
|
|
self.assertEqual(info['reference_system'], "Earth", "参考系统应该正确")
|
|||
|
|
self.assertEqual(info['output_file'], output_file, "输出文件应该正确")
|
|||
|
|
|
|||
|
|
# 检查文件生成
|
|||
|
|
if os.path.exists(output_file):
|
|||
|
|
file_size = os.path.getsize(output_file)
|
|||
|
|
print(f"快速创建文件大小: {file_size} 字节")
|
|||
|
|
self.assertGreater(file_size, 0, "输出文件应该非空")
|
|||
|
|
|
|||
|
|
def test_custom_reference_system(self):
|
|||
|
|
"""测试自定义参考系统"""
|
|||
|
|
generator = stt.SttGenerator()
|
|||
|
|
|
|||
|
|
# 测试预设系统
|
|||
|
|
generator.set_reference_system("WGS84")
|
|||
|
|
generator.set_reference_system("Earth")
|
|||
|
|
generator.set_reference_system("Moon")
|
|||
|
|
|
|||
|
|
# 测试自定义半径
|
|||
|
|
generator.set_pole_equator_radius(6378137.0, 6356752.3) # WGS84参数
|
|||
|
|
|
|||
|
|
# 设置其他参数
|
|||
|
|
generator.set_tree_depth(2, 3)
|
|||
|
|
generator.set_icosahedron_orient(0.0, 90.0)
|
|||
|
|
|
|||
|
|
# 运行测试
|
|||
|
|
output_file = os.path.join(self.temp_dir, "test_custom.msh")
|
|||
|
|
result = generator.run(output_file)
|
|||
|
|
|
|||
|
|
self.assertEqual(result, 0, "自定义参考系统生成应该成功")
|
|||
|
|
|
|||
|
|
def test_module_constants(self):
|
|||
|
|
"""测试模块常量"""
|
|||
|
|
self.assertEqual(stt.WGS84, "WGS84", "WGS84常量应该正确")
|
|||
|
|
self.assertEqual(stt.EARTH, "Earth", "Earth常量应该正确")
|
|||
|
|
self.assertEqual(stt.MOON, "Moon", "Moon常量应该正确")
|
|||
|
|
|
|||
|
|
def test_module_info(self):
|
|||
|
|
"""测试模块信息"""
|
|||
|
|
info = stt.get_info()
|
|||
|
|
self.assertIsInstance(info, dict, "应该返回字典信息")
|
|||
|
|
self.assertIn('version', info, "应该包含版本信息")
|
|||
|
|
self.assertIn('author', info, "应该包含作者信息")
|
|||
|
|
|
|||
|
|
print("模块信息:")
|
|||
|
|
for key, value in info.items():
|
|||
|
|
print(f" {key}: {value}")
|
|||
|
|
|
|||
|
|
def test_advanced_parameters(self):
|
|||
|
|
"""测试高级参数"""
|
|||
|
|
generator = stt.SttGenerator()
|
|||
|
|
|
|||
|
|
# 设置参数
|
|||
|
|
generator.set_tree_depth(2, 4)
|
|||
|
|
generator.set_reference_system("WGS84")
|
|||
|
|
generator.set_icosahedron_orient(0.0, 90.0)
|
|||
|
|
|
|||
|
|
# 使用完整参数运行
|
|||
|
|
params = {
|
|||
|
|
"output_msh": os.path.join(self.temp_dir, "test_advanced.msh"),
|
|||
|
|
"output_vertex": os.path.join(self.temp_dir, "vertices.txt"),
|
|||
|
|
"output_triangle_center": os.path.join(self.temp_dir, "centers.txt"),
|
|||
|
|
"output_neighbor": os.path.join(self.temp_dir, "neighbors.txt")
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
result = generator.run_full(params)
|
|||
|
|
self.assertEqual(result, 0, "高级参数生成应该成功")
|
|||
|
|
|
|||
|
|
# 检查生成的文件
|
|||
|
|
for key, filename in params.items():
|
|||
|
|
if os.path.exists(filename):
|
|||
|
|
file_size = os.path.getsize(filename)
|
|||
|
|
print(f"{key} 文件大小: {file_size} 字节")
|
|||
|
|
|
|||
|
|
def test_error_handling(self):
|
|||
|
|
"""测试错误处理"""
|
|||
|
|
generator = stt.SttGenerator()
|
|||
|
|
|
|||
|
|
# 测试无效参数(应该不会崩溃)
|
|||
|
|
try:
|
|||
|
|
generator.set_tree_depth(-1, 10) # 无效深度
|
|||
|
|
generator.set_reference_system("InvalidSystem")
|
|||
|
|
# 这些调用应该能够处理而不会崩溃
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"错误处理测试: {e}")
|
|||
|
|
|
|||
|
|
def run_basic_tests():
|
|||
|
|
"""运行基本测试"""
|
|||
|
|
print("运行STT Python绑定基本测试...")
|
|||
|
|
|
|||
|
|
if not STT_AVAILABLE:
|
|||
|
|
print("错误: STT模块不可用,无法运行测试")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
# 创建简单的测试
|
|||
|
|
try:
|
|||
|
|
print("1. 测试模块导入...")
|
|||
|
|
generator = stt.SttGenerator()
|
|||
|
|
print(" ✓ 模块导入成功")
|
|||
|
|
|
|||
|
|
print("2. 测试基本功能...")
|
|||
|
|
generator.set_tree_depth(2, 3)
|
|||
|
|
generator.set_reference_system("WGS84")
|
|||
|
|
print(" ✓ 基本设置成功")
|
|||
|
|
|
|||
|
|
print("3. 测试快速创建...")
|
|||
|
|
info = stt.create_stt(2, 3, "Earth", "test.msh")
|
|||
|
|
print(f" ✓ 快速创建成功: {info}")
|
|||
|
|
|
|||
|
|
print("4. 测试模块信息...")
|
|||
|
|
info = stt.get_info()
|
|||
|
|
print(f" ✓ 模块信息: {info['version']}")
|
|||
|
|
|
|||
|
|
print("基本测试通过!")
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"测试失败: {e}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
"""主函数"""
|
|||
|
|
print("STT Python绑定测试")
|
|||
|
|
print("=" * 30)
|
|||
|
|
|
|||
|
|
# 运行基本测试
|
|||
|
|
if run_basic_tests():
|
|||
|
|
print("\n运行完整单元测试...")
|
|||
|
|
unittest.main(argv=[''], exit=False, verbosity=2)
|
|||
|
|
else:
|
|||
|
|
print("\n基本测试失败,跳过完整测试")
|
|||
|
|
sys.exit(1)
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|