#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""测试安装的包是否正常工作"""

import sys
import io

# 设置输出编码为 UTF-8
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

from slice_wrapper import (
    slice_stress,
    RollerParams,
    RacewayParams,
    CrownType,
    RacewayType,
    is_fortran_available,
)
import slice_wrapper

print("=" * 60)
print("测试 bstart-trb 包安装")
print("=" * 60)

# 检查版本
print(f"\n[OK] 包版本: {slice_wrapper.__version__}")

# 检查 Fortran 模块
fortran_available = is_fortran_available()
print(f"[OK] Fortran 模块可用: {fortran_available}")

if not fortran_available:
    print("\n[ERROR] Fortran 模块未编译或加载失败")
    exit(1)

# 创建测试参数
print("\n" + "=" * 60)
print("运行计算测试")
print("=" * 60)

roller = RollerParams(
    d1=10.0,
    d2=10.0,
    length=9.6,
    alfa=0.0,
    tilt=0.0,
    crown_type=CrownType.LOGARITHMIC,
    curve_q=14100.0,
)

raceway = RacewayParams(
    diameter=-150.0,
    raceway_type=RacewayType.OUTER,
    fai=0.0,
)

print(f"\n输入参数:")
print(f"  滚子直径: {roller.d1} mm")
print(f"  滚子长度: {roller.length} mm")
print(f"  滚道直径: {raceway.diameter} mm")
print(f"  载荷: 1340.86 N")
print(f"  切片数: 30")

# 执行计算
result = slice_stress(roller, raceway, load=1340.86, n_slice=30)

print(f"\n计算结果:")
print(f"  [OK] 收敛: {result.converged}")
print(f"  [OK] 错误码: {result.error_code}")
print(f"  [OK] 滚子变形: {result.deflection:.6f} mm")
print(f"  [OK] 平衡载荷: {result.equilibrium_load:.2f} N")
print(f"  [OK] 最大应力: {result.max_stress:.0f} MPa")
print(f"  [OK] 最小应力: {result.min_stress:.0f} MPa")
print(f"  [OK] 平均应力: {result.mean_stress:.0f} MPa")
print(f"  [OK] 应力均匀度: {result.stress_uniformity:.2%}")
print(f"  [OK] 接触切片数: {result.contact_slices}/{result.n_slice}")

print("\n" + "=" * 60)
print("[SUCCESS] 所有测试通过!")
print("=" * 60)
