Skip to content

Commit

Permalink
adding tests. fix typo
Browse files Browse the repository at this point in the history
improving tests

more fixes

more tests
  • Loading branch information
mmatera committed May 26, 2023
1 parent 5110739 commit 8e7e774
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
18 changes: 15 additions & 3 deletions mathics/eval/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,9 @@ def test_nonnegative_arithmetic_expr(expr: BaseElement) -> bool:
Check if the expression is an arithmetic expression
representing a nonnegative number
"""
if not test_arithmetic_expr(expr):
return False

if test_zero_arithmetic_expr(expr) or test_positive_arithmetic_expr(expr):
return True

Expand All @@ -455,6 +458,9 @@ def test_nonpositive_arithetic_expr(expr: BaseElement) -> bool:
Check if the expression is an arithmetic expression
representing a nonnegative number
"""
if not test_arithmetic_expr(expr):
return False

if test_zero_arithmetic_expr(expr) or test_negative_arithmetic_expr(expr):
return True
return False
Expand All @@ -473,6 +479,7 @@ def test_positive_arithmetic_expr(expr: BaseElement) -> bool:
return False

head, elements = expr.get_head(), expr.elements
print("check specifics", expr)
if head is SymbolPlus:
positive_nonpositive_terms = {True: [], False: []}
for term in elements:
Expand Down Expand Up @@ -517,7 +524,9 @@ def test_positive_arithmetic_expr(expr: BaseElement) -> bool:
return test_arithmetic_expr(base)
return test_arithmetic_expr(exponent) and test_positive_arithmetic_expr(base)
if expr.has_form("Exp", 1):
return test_arithmetic_expr(exponent, only_real=True)
return test_arithmetic_expr(expr.elements[0], only_real=True)
if expr.has_form("Sqrt", 1):
return test_positive_arithmetic_expr(expr.elements[0])
if head is SymbolLog:
if len(elements) > 2:
return False
Expand All @@ -526,8 +535,11 @@ def test_positive_arithmetic_expr(expr: BaseElement) -> bool:
return False
arg = elements[-1]
return test_positive_arithmetic_expr(eval_add_numbers(arg, IntegerM1))
if head.has_form("Abs", 1):
return True
if expr.has_form("Abs", 1):
arg = elements[0]
return test_arithmetic_expr(
arg, only_real=False
) and not test_zero_arithmetic_expr(arg)
if head.has_form("DirectedInfinity", 1):
return test_positive_arithmetic_expr(elements[0])

Expand Down
6 changes: 6 additions & 0 deletions mathics/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,9 @@ def format_result(self, str_expression=None, timeout=None, form=None):
if form is None:
form = self.form
return res.do_format(self.evaluation, form)

def parse(self, str_expression):
"""
Just parse the expression
"""
return parse(self.definitions, MathicsSingleLineFeeder(str_expression))
86 changes: 86 additions & 0 deletions test/builtin/arithmetic/test_lowlevel_properties.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# -*- coding: utf-8 -*-
"""
Unit tests for mathics.eval.arithmetic low level positivity tests
"""
from test.helper import session

import pytest

from mathics.eval.arithmetic import (
test_arithmetic_expr as check_arithmetic,
test_positive_arithmetic_expr as check_positive,
test_zero_arithmetic_expr as check_zero,
)


@pytest.mark.parametrize(
("str_expr", "expected", "msg"),
[
("I", False, None),
("0", False, None),
("1", True, None),
("Pi", True, None),
("a", False, None),
("-Pi", False, None),
("(-1)^2", True, None),
("(-1)^3", False, None),
("Sqrt[2]", True, None),
("Sqrt[-2]", False, None),
("(-2)^(1/2)", False, None),
("(2)^(1/2)", True, None),
("Exp[a]", False, None),
("Exp[2.3]", True, None),
("Log[1/2]", False, None),
("Exp[I]", False, None),
("Log[3]", True, None),
("Log[I]", False, None),
("Abs[a]", False, None),
("Abs[0]", False, None),
("Abs[1+3 I]", True, None),
("Sin[Pi]", False, None),
],
)
def test_positivity(str_expr, expected, msg):
expr = session.parse(str_expr)
if msg:
assert check_positive(expr) == expected, msg
else:
assert check_positive(expr) == expected


@pytest.mark.parametrize(
("str_expr", "expected", "msg"),
[
("I", False, None),
("0", True, None),
("1", False, None),
("Pi", False, None),
("a", False, None),
("a-a", True, None),
("3-3.", True, None),
("2-Sqrt[4]", True, None),
("-Pi", False, None),
("(-1)^2", False, None),
("(-1)^3", False, None),
("Sqrt[2]", False, None),
("Sqrt[-2]", False, None),
("(-2)^(1/2)", False, None),
("(2)^(1/2)", False, None),
("Exp[a]", False, None),
("Exp[2.3]", False, None),
("Log[1/2]", False, None),
("Exp[I]", False, None),
("Log[3]", False, None),
("Log[I]", False, None),
("Abs[a]", False, None),
("Abs[0]", False, None),
("Abs[1+3 I]", False, None),
# ("Sin[Pi]", False, None),
],
)
def test_zero(str_expr, expected, msg):
expr = session.parse(str_expr)
if msg:
assert check_zero(expr) == expected, msg
else:
assert check_zero(expr) == expected

0 comments on commit 8e7e774

Please sign in to comment.