182 lines
5.3 KiB
Python
182 lines
5.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Script de debug y testing para verificar correcciones del error as_coeff_Mul
|
|
"""
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Agregar directorio actual al path
|
|
sys.path.insert(0, str(Path(__file__).parent))
|
|
|
|
from bracket_parser import BracketParser
|
|
from hybrid_evaluation_engine import HybridEvaluationEngine
|
|
from hybrid_base_types import Hex, Bin, IP4, HybridCalcType
|
|
import sympy
|
|
|
|
|
|
def test_sympy_integration():
|
|
"""Test específico de integración con SymPy"""
|
|
print("=== Test Integración SymPy ===")
|
|
|
|
try:
|
|
# Test creación básica
|
|
h = Hex("FF")
|
|
print(f"✅ Hex[FF] creado: {h}")
|
|
print(f" Valor interno: {h._value}")
|
|
print(f" Es SymPy Basic: {isinstance(h, sympy.Basic)}")
|
|
print(f" Args: {h.args}")
|
|
print(f" Func: {h.func}")
|
|
|
|
# Test métodos requeridos por SymPy
|
|
print(f"✅ as_coeff_Mul(): {h.as_coeff_Mul()}")
|
|
print(f"✅ as_coeff_Add(): {h.as_coeff_Add()}")
|
|
print(f"✅ as_base_exp(): {h.as_base_exp()}")
|
|
|
|
# Test propiedades
|
|
print(f"✅ is_number: {h.is_number}")
|
|
print(f"✅ is_real: {h.is_real}")
|
|
print(f"✅ is_integer: {h.is_integer}")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error en test básico: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
|
|
def test_arithmetic_operations():
|
|
"""Test específico de operaciones aritméticas"""
|
|
print("\n=== Test Operaciones Aritméticas ===")
|
|
|
|
try:
|
|
h = Hex("FF")
|
|
print(f"Hex[FF] creado: {h} (valor: {h._value})")
|
|
|
|
# Test suma simple
|
|
print("\n1. Probando h + 1...")
|
|
result1 = h + 1
|
|
print(f" Resultado: {result1} (tipo: {type(result1)})")
|
|
|
|
# Test con SymPy sympify
|
|
print("\n2. Probando sympify(h + 1)...")
|
|
try:
|
|
expr = sympy.sympify("h + 1", locals={'h': h})
|
|
print(f" SymPy expr: {expr} (tipo: {type(expr)})")
|
|
except Exception as e:
|
|
print(f" Error en sympify: {e}")
|
|
|
|
# Test multiplicación
|
|
print("\n3. Probando h * 2...")
|
|
result3 = h * 2
|
|
print(f" Resultado: {result3} (tipo: {type(result3)})")
|
|
|
|
# Test con Bin
|
|
print("\n4. Probando Bin[1010] * 2...")
|
|
b = Bin("1010")
|
|
result4 = b * 2
|
|
print(f" Bin[1010]: {b} (valor: {b._value})")
|
|
print(f" Resultado: {result4} (tipo: {type(result4)})")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error en operaciones aritméticas: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
|
|
def test_evaluation_engine():
|
|
"""Test del motor de evaluación con las correcciones"""
|
|
print("\n=== Test Motor de Evaluación ===")
|
|
|
|
engine = HybridEvaluationEngine()
|
|
|
|
test_expressions = [
|
|
# Casos que fallaban antes
|
|
"Hex[FF] + 1",
|
|
"Bin[1010] * 2",
|
|
"IP4[192.168.1.100/24].NetworkAddress[]",
|
|
|
|
# Casos básicos
|
|
"Hex[FF]",
|
|
"Bin[1010]",
|
|
|
|
# Asignaciones
|
|
"z = 5",
|
|
"w = z + 3",
|
|
|
|
# SymPy básico
|
|
"x + 2*y",
|
|
"diff(x**2, x)",
|
|
]
|
|
|
|
for expr in test_expressions:
|
|
try:
|
|
print(f"\nEvaluando: '{expr}'")
|
|
result = engine.evaluate_line(expr)
|
|
|
|
if result.is_error:
|
|
print(f" ❌ Error: {result.error}")
|
|
else:
|
|
print(f" ✅ Resultado: {result.result} (tipo: {type(result.result)})")
|
|
print(f" Info: {result.result_type}")
|
|
if result.numeric_result:
|
|
print(f" Numérico: {result.numeric_result}")
|
|
|
|
except Exception as e:
|
|
print(f" ❌ Excepción: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
|
|
def test_problematic_cases():
|
|
"""Test de casos específicos que causaban problemas"""
|
|
print("\n=== Test Casos Problemáticos ===")
|
|
|
|
# Test directo de la operación que fallaba
|
|
print("1. Test directo Hex + int...")
|
|
try:
|
|
h = Hex("FF")
|
|
result = h.__add__(1)
|
|
print(f" h.__add__(1): {result} (tipo: {type(result)})")
|
|
|
|
# Test usando operador
|
|
result2 = h + 1
|
|
print(f" h + 1: {result2} (tipo: {type(result2)})")
|
|
|
|
except Exception as e:
|
|
print(f" ❌ Error: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
# Test con SymPy sympify directo
|
|
print("\n2. Test SymPy sympify directo...")
|
|
try:
|
|
h = Hex("FF")
|
|
sympified = sympy.sympify(h)
|
|
print(f" sympify(Hex[FF]): {sympified} (tipo: {type(sympified)})")
|
|
|
|
# Test operaciones con sympified
|
|
result = sympified + 1
|
|
print(f" sympified + 1: {result} (tipo: {type(result)})")
|
|
|
|
except Exception as e:
|
|
print(f" ❌ Error: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
|
|
def main():
|
|
"""Función principal de testing"""
|
|
print("Debug y Testing - Corrección error as_coeff_Mul")
|
|
print("=" * 60)
|
|
|
|
test_sympy_integration()
|
|
test_arithmetic_operations()
|
|
test_evaluation_engine()
|
|
test_problematic_cases()
|
|
|
|
print("\n" + "=" * 60)
|
|
print("Testing completado.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|