219 lines
7.7 KiB
Python
219 lines
7.7 KiB
Python
|
|
"""Comprehensive test for OMML conversion with preprocessing."""
|
||
|
|
|
||
|
|
from app.services.converter import Converter
|
||
|
|
|
||
|
|
|
||
|
|
def test_case_1_array_with_spaces():
|
||
|
|
"""Test: Array with spaces in column specifier (the original issue)."""
|
||
|
|
print("\n" + "=" * 80)
|
||
|
|
print("Test 1: Array with spaces in column specifier")
|
||
|
|
print("=" * 80)
|
||
|
|
|
||
|
|
converter = Converter()
|
||
|
|
|
||
|
|
# The problematic LaTeX from the error
|
||
|
|
latex = r"""\begin{array}{l} D = \left| \begin{array}{c c c c} a _ {1 1} & a _ {1 2} & \dots & a _ {1 n} \\ \vdots & \vdots & & \vdots \\ a _ {i 1} + 0 + \dots + 0 & 0 + a _ {i 2} + \dots + 0 & \dots & 0 + \dots + 0 + a _ {i n} \\ \vdots & \vdots & & \vdots \\ a _ {n 1} & a _ {n 2} & \dots & a _ {n n} \end{array} \right| \\ = \left| \begin{array}{c c c c} a _ {1 1} & a _ {1 2} & \dots & a _ {1 n} \\ \vdots & \vdots & & \vdots \\ a _ {i 1} & 0 & \dots & 0 \\ \vdots & \vdots & & \vdots \\ a _ {n 1} & a _ {n 2} & \dots & a _ {n n} \end{array} \right| + \left| \begin{array}{c c c c} a _ {1 1} & a _ {1 2} & \dots & a _ {1 n} \\ \vdots & \vdots & & \vdots \\ 0 & a _ {i 2} & \dots & 0 \\ \vdots & \vdots & & \vdots \\ a _ {n 1} & a _ {n 2} & \dots & a _ {n n} \end{array} \right| \\ + \dots + \left| \begin{array}{c c c c} a _ {1 1} & a _ {1 2} & \dots & a _ {1 n} \\ \vdots & \vdots & & \vdots \\ 0 & 0 & \dots & a _ {i n} \\ \vdots & \vdots & & \vdots \\ a _ {n 1} & a _ {n 2} & \dots & a _ {n n} \end{array} \right|, \\ \end{array}"""
|
||
|
|
|
||
|
|
print(f"LaTeX length: {len(latex)} chars")
|
||
|
|
print(f"Preview: {latex[:100]}...")
|
||
|
|
|
||
|
|
try:
|
||
|
|
omml = converter.convert_to_omml(latex)
|
||
|
|
print(f"\n✓ SUCCESS: Converted to OMML")
|
||
|
|
print(f"OMML length: {len(omml)} chars")
|
||
|
|
|
||
|
|
if "oMath" in omml:
|
||
|
|
print("✓ Valid OMML structure detected")
|
||
|
|
|
||
|
|
# Check preprocessing worked
|
||
|
|
preprocessed = converter._preprocess_formula_for_omml(latex)
|
||
|
|
if "{c c c c}" not in preprocessed and "{cccc}" in preprocessed:
|
||
|
|
print("✓ Array column specifiers fixed: '{c c c c}' → '{cccc}'")
|
||
|
|
|
||
|
|
return True
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"\n✗ FAILED: {e}")
|
||
|
|
return False
|
||
|
|
|
||
|
|
|
||
|
|
def test_case_2_vmatrix():
|
||
|
|
"""Test: vmatrix environment conversion."""
|
||
|
|
print("\n" + "=" * 80)
|
||
|
|
print("Test 2: vmatrix environment")
|
||
|
|
print("=" * 80)
|
||
|
|
|
||
|
|
converter = Converter()
|
||
|
|
|
||
|
|
latex = r"\begin{vmatrix} a & b \\ c & d \end{vmatrix}"
|
||
|
|
print(f"LaTeX: {latex}")
|
||
|
|
|
||
|
|
try:
|
||
|
|
omml = converter.convert_to_omml(latex)
|
||
|
|
print(f"✓ SUCCESS: Converted to OMML ({len(omml)} chars)")
|
||
|
|
|
||
|
|
# Check if vmatrix was converted
|
||
|
|
preprocessed = converter._preprocess_formula_for_omml(latex)
|
||
|
|
if "vmatrix" not in preprocessed and r"\left|" in preprocessed:
|
||
|
|
print("✓ vmatrix converted to \\left| ... \\right|")
|
||
|
|
|
||
|
|
return True
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"✗ FAILED: {e}")
|
||
|
|
return False
|
||
|
|
|
||
|
|
|
||
|
|
def test_case_3_cases_environment():
|
||
|
|
"""Test: cases environment conversion."""
|
||
|
|
print("\n" + "=" * 80)
|
||
|
|
print("Test 3: cases environment")
|
||
|
|
print("=" * 80)
|
||
|
|
|
||
|
|
converter = Converter()
|
||
|
|
|
||
|
|
latex = r"f(x) = \begin{cases} x^2 & x \geq 0 \\ -x & x < 0 \end{cases}"
|
||
|
|
print(f"LaTeX: {latex}")
|
||
|
|
|
||
|
|
try:
|
||
|
|
omml = converter.convert_to_omml(latex)
|
||
|
|
print(f"✓ SUCCESS: Converted to OMML ({len(omml)} chars)")
|
||
|
|
|
||
|
|
# Check if cases was converted to array
|
||
|
|
preprocessed = converter._preprocess_formula_for_omml(latex)
|
||
|
|
if "cases" not in preprocessed and "array" in preprocessed:
|
||
|
|
print("✓ cases converted to array environment")
|
||
|
|
|
||
|
|
return True
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"✗ FAILED: {e}")
|
||
|
|
return False
|
||
|
|
|
||
|
|
|
||
|
|
def test_case_4_aligned_environment():
|
||
|
|
"""Test: aligned environment conversion."""
|
||
|
|
print("\n" + "=" * 80)
|
||
|
|
print("Test 4: aligned environment")
|
||
|
|
print("=" * 80)
|
||
|
|
|
||
|
|
converter = Converter()
|
||
|
|
|
||
|
|
latex = r"\begin{aligned} x + y &= 5 \\ 2x - y &= 1 \end{aligned}"
|
||
|
|
print(f"LaTeX: {latex}")
|
||
|
|
|
||
|
|
try:
|
||
|
|
omml = converter.convert_to_omml(latex)
|
||
|
|
print(f"✓ SUCCESS: Converted to OMML ({len(omml)} chars)")
|
||
|
|
|
||
|
|
# Check if aligned was converted
|
||
|
|
preprocessed = converter._preprocess_formula_for_omml(latex)
|
||
|
|
if "aligned" not in preprocessed and "array" in preprocessed:
|
||
|
|
print("✓ aligned converted to array environment")
|
||
|
|
if "&" not in preprocessed or preprocessed.count("&") < latex.count("&"):
|
||
|
|
print("✓ Alignment markers removed")
|
||
|
|
|
||
|
|
return True
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"✗ FAILED: {e}")
|
||
|
|
return False
|
||
|
|
|
||
|
|
|
||
|
|
def test_case_5_simple_formula():
|
||
|
|
"""Test: Simple formula (should work without preprocessing)."""
|
||
|
|
print("\n" + "=" * 80)
|
||
|
|
print("Test 5: Simple formula")
|
||
|
|
print("=" * 80)
|
||
|
|
|
||
|
|
converter = Converter()
|
||
|
|
|
||
|
|
latex = r"x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}"
|
||
|
|
print(f"LaTeX: {latex}")
|
||
|
|
|
||
|
|
try:
|
||
|
|
omml = converter.convert_to_omml(latex)
|
||
|
|
print(f"✓ SUCCESS: Converted to OMML ({len(omml)} chars)")
|
||
|
|
return True
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"✗ FAILED: {e}")
|
||
|
|
return False
|
||
|
|
|
||
|
|
|
||
|
|
def test_case_6_nested_structures():
|
||
|
|
"""Test: Nested structures with multiple issues."""
|
||
|
|
print("\n" + "=" * 80)
|
||
|
|
print("Test 6: Nested structures")
|
||
|
|
print("=" * 80)
|
||
|
|
|
||
|
|
converter = Converter()
|
||
|
|
|
||
|
|
latex = r"\left\{ \begin{array}{l c} \begin{vmatrix} a & b \\ c & d \end{vmatrix} & = ad - bc \\ f(x) = \begin{cases} 1 & x > 0 \\ 0 & x \leq 0 \end{cases} & \text{step function} \end{array} \right."
|
||
|
|
print(f"LaTeX: {latex}")
|
||
|
|
|
||
|
|
try:
|
||
|
|
omml = converter.convert_to_omml(latex)
|
||
|
|
print(f"✓ SUCCESS: Converted to OMML ({len(omml)} chars)")
|
||
|
|
|
||
|
|
preprocessed = converter._preprocess_formula_for_omml(latex)
|
||
|
|
print("\nPreprocessing applied:")
|
||
|
|
if "vmatrix" not in preprocessed:
|
||
|
|
print(" ✓ vmatrix converted")
|
||
|
|
if "cases" not in preprocessed:
|
||
|
|
print(" ✓ cases converted")
|
||
|
|
if "{l c}" not in preprocessed and "{lc}" in preprocessed:
|
||
|
|
print(" ✓ Array specifiers fixed")
|
||
|
|
|
||
|
|
return True
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"✗ FAILED: {e}")
|
||
|
|
return False
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
print("=" * 80)
|
||
|
|
print("OMML CONVERSION TEST SUITE")
|
||
|
|
print("Testing preprocessing and conversion")
|
||
|
|
print("=" * 80)
|
||
|
|
|
||
|
|
results = []
|
||
|
|
|
||
|
|
try:
|
||
|
|
results.append(("Simple formula", test_case_5_simple_formula()))
|
||
|
|
results.append(("Array with spaces", test_case_1_array_with_spaces()))
|
||
|
|
results.append(("vmatrix", test_case_2_vmatrix()))
|
||
|
|
results.append(("cases", test_case_3_cases_environment()))
|
||
|
|
results.append(("aligned", test_case_4_aligned_environment()))
|
||
|
|
results.append(("Nested structures", test_case_6_nested_structures()))
|
||
|
|
|
||
|
|
# Summary
|
||
|
|
print("\n" + "=" * 80)
|
||
|
|
print("TEST SUMMARY")
|
||
|
|
print("=" * 80)
|
||
|
|
|
||
|
|
passed = sum(1 for _, result in results if result)
|
||
|
|
total = len(results)
|
||
|
|
|
||
|
|
for name, result in results:
|
||
|
|
status = "✓ PASS" if result else "✗ FAIL"
|
||
|
|
print(f"{status}: {name}")
|
||
|
|
|
||
|
|
print("\n" + "-" * 80)
|
||
|
|
print(f"Total: {passed}/{total} tests passed")
|
||
|
|
|
||
|
|
if passed == total:
|
||
|
|
print("\n✓✓✓ ALL TESTS PASSED ✓✓✓")
|
||
|
|
else:
|
||
|
|
print(f"\n✗✗✗ {total - passed} TESTS FAILED ✗✗✗")
|
||
|
|
|
||
|
|
print("=" * 80)
|
||
|
|
|
||
|
|
except KeyboardInterrupt:
|
||
|
|
print("\n\nTests interrupted by user")
|
||
|
|
except Exception as e:
|
||
|
|
print(f"\n\nTest suite error: {e}")
|
||
|
|
import traceback
|
||
|
|
traceback.print_exc()
|