227 lines
6.1 KiB
Python
227 lines
6.1 KiB
Python
"""
|
|
Tests for Phase 2: Algebra System
|
|
"""
|
|
|
|
from pysecondo.core.nested_list import atom
|
|
from pysecondo.core.types import BaseType
|
|
from pysecondo.algebras.standard import StandardAlgebra
|
|
from pysecondo.algebras.base import AlgebraManager
|
|
import sys
|
|
sys.path.insert(0, '.')
|
|
|
|
|
|
def test_algebra_registration():
|
|
"""Test algebra registration"""
|
|
print("Testing Algebra Registration...")
|
|
|
|
manager = AlgebraManager()
|
|
std_algebra = StandardAlgebra()
|
|
|
|
manager.register_algebra("StandardAlgebra", std_algebra)
|
|
|
|
# Check algebra is registered
|
|
assert "StandardAlgebra" in manager.list_algebras()
|
|
|
|
# Check operators are indexed
|
|
ops = manager.list_operators()
|
|
assert "+" in ops
|
|
assert "-" in ops
|
|
assert "*" in ops
|
|
assert "/" in ops
|
|
assert "and" in ops
|
|
assert "or" in ops
|
|
assert "not" in ops
|
|
|
|
print(" ✓ Algebra registration tests passed")
|
|
|
|
|
|
def test_arithmetic_operators():
|
|
"""Test arithmetic operators"""
|
|
print("Testing Arithmetic Operators...")
|
|
|
|
manager = AlgebraManager()
|
|
manager.register_algebra("StandardAlgebra", StandardAlgebra())
|
|
|
|
# Test addition
|
|
add_op = manager.get_operator("+")
|
|
|
|
# Type map: int + int = int
|
|
result_type = add_op.type_map([BaseType.INT, BaseType.INT])
|
|
assert result_type == BaseType.INT
|
|
|
|
# Type map: int + real = real
|
|
result_type = add_op.type_map([BaseType.INT, BaseType.REAL])
|
|
assert result_type == BaseType.REAL
|
|
|
|
# Value map: 5 + 3 = 8
|
|
result = add_op.value_map([atom(5), atom(3)])
|
|
assert result.value == 8
|
|
|
|
# Test subtraction
|
|
sub_op = manager.get_operator("-")
|
|
result = sub_op.value_map([atom(10), atom(3)])
|
|
assert result.value == 7
|
|
|
|
# Test multiplication
|
|
mul_op = manager.get_operator("*")
|
|
result = mul_op.value_map([atom(6), atom(7)])
|
|
assert result.value == 42
|
|
|
|
# Test division
|
|
div_op = manager.get_operator("/")
|
|
result = div_op.value_map([atom(10), atom(2)])
|
|
assert result.value == 5.0
|
|
|
|
print(" ✓ Arithmetic operators tests passed")
|
|
|
|
|
|
def test_comparison_operators():
|
|
"""Test comparison operators"""
|
|
print("Testing Comparison Operators...")
|
|
|
|
manager = AlgebraManager()
|
|
manager.register_algebra("StandardAlgebra", StandardAlgebra())
|
|
|
|
# Test less than
|
|
lt_op = manager.get_operator("<")
|
|
|
|
result_type = lt_op.type_map([BaseType.INT, BaseType.INT])
|
|
assert result_type == BaseType.BOOL
|
|
|
|
result = lt_op.value_map([atom(3), atom(5)])
|
|
assert result.value is True
|
|
|
|
result = lt_op.value_map([atom(5), atom(3)])
|
|
assert result.value is False
|
|
|
|
# Test greater than
|
|
gt_op = manager.get_operator(">")
|
|
result = gt_op.value_map([atom(10), atom(5)])
|
|
assert result.value is True
|
|
|
|
# Test equal
|
|
eq_op = manager.get_operator("=")
|
|
result = eq_op.value_map([atom(5), atom(5)])
|
|
assert result.value is True
|
|
|
|
result = eq_op.value_map([atom(5), atom(3)])
|
|
assert result.value is False
|
|
|
|
# Test not equal
|
|
ne_op = manager.get_operator("!=")
|
|
result = ne_op.value_map([atom(5), atom(3)])
|
|
assert result.value is True
|
|
|
|
print(" ✓ Comparison operators tests passed")
|
|
|
|
|
|
def test_logical_operators():
|
|
"""Test logical operators"""
|
|
print("Testing Logical Operators...")
|
|
|
|
manager = AlgebraManager()
|
|
manager.register_algebra("StandardAlgebra", StandardAlgebra())
|
|
|
|
# Test and
|
|
and_op = manager.get_operator("and")
|
|
result = and_op.value_map([atom(True), atom(True)])
|
|
assert result.value is True
|
|
|
|
result = and_op.value_map([atom(True), atom(False)])
|
|
assert result.value is False
|
|
|
|
# Test or
|
|
or_op = manager.get_operator("or")
|
|
result = or_op.value_map([atom(True), atom(False)])
|
|
assert result.value is True
|
|
|
|
result = or_op.value_map([atom(False), atom(False)])
|
|
assert result.value is False
|
|
|
|
# Test not
|
|
not_op = manager.get_operator("not")
|
|
result = not_op.value_map([atom(True)])
|
|
assert result.value is False
|
|
|
|
result = not_op.value_map([atom(False)])
|
|
assert result.value is True
|
|
|
|
print(" ✓ Logical operators tests passed")
|
|
|
|
|
|
def test_type_checking():
|
|
"""Test type checking in operators"""
|
|
print("Testing Type Checking...")
|
|
|
|
manager = AlgebraManager()
|
|
manager.register_algebra("StandardAlgebra", StandardAlgebra())
|
|
|
|
add_op = manager.get_operator("+")
|
|
|
|
# Valid types
|
|
try:
|
|
add_op.type_map([BaseType.INT, BaseType.INT])
|
|
except TypeError:
|
|
assert False, "Should not raise TypeError for valid types"
|
|
|
|
# Invalid types
|
|
try:
|
|
add_op.type_map([BaseType.STRING, BaseType.INT])
|
|
assert False, "Should raise TypeError for invalid types"
|
|
except TypeError as e:
|
|
assert "requires" in str(e).lower()
|
|
|
|
# Wrong number of arguments
|
|
try:
|
|
add_op.type_map([BaseType.INT])
|
|
assert False, "Should raise TypeError for wrong argument count"
|
|
except TypeError as e:
|
|
assert "expects 2" in str(e)
|
|
|
|
print(" ✓ Type checking tests passed")
|
|
|
|
|
|
def test_complex_expressions():
|
|
"""Test complex expressions using multiple operators"""
|
|
print("Testing Complex Expressions...")
|
|
|
|
manager = AlgebraManager()
|
|
manager.register_algebra("StandardAlgebra", StandardAlgebra())
|
|
|
|
# Expression: (5 + 3) * 2 = 16
|
|
add_op = manager.get_operator("+")
|
|
mul_op = manager.get_operator("*")
|
|
|
|
temp = add_op.value_map([atom(5), atom(3)])
|
|
result = mul_op.value_map([temp, atom(2)])
|
|
assert result.value == 16
|
|
|
|
# Expression: (10 > 5) and (3 < 7) = True
|
|
gt_op = manager.get_operator(">")
|
|
lt_op = manager.get_operator("<")
|
|
and_op = manager.get_operator("and")
|
|
|
|
temp1 = gt_op.value_map([atom(10), atom(5)])
|
|
temp2 = lt_op.value_map([atom(3), atom(7)])
|
|
result = and_op.value_map([temp1, temp2])
|
|
assert result.value is True
|
|
|
|
print(" ✓ Complex expressions tests passed")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("=" * 50)
|
|
print("Phase 2: Algebra System Tests")
|
|
print("=" * 50)
|
|
|
|
test_algebra_registration()
|
|
test_arithmetic_operators()
|
|
test_comparison_operators()
|
|
test_logical_operators()
|
|
test_type_checking()
|
|
test_complex_expressions()
|
|
|
|
print("\n" + "=" * 50)
|
|
print("All Phase 2 tests passed! ✓")
|
|
print("=" * 50)
|