def test_subtree(): exp1 = Add(Mul(Integer(1), Integer(2)), Div(Integer(3), Integer(4))) tree1 = Tree(exp1) tree2 = tree1.subtree(tree1.root_nid) tree3 = tree1.subtree(tree1.root_nid, deep=True) assert tree1.get_node(tree1.root_nid) == tree2.get_node( tree2.root_nid) == tree3.get_node(tree3.root_nid) assert set(tree1._nodes) == set(tree2._nodes) assert not set(tree2._nodes).intersection(set(tree3._nodes))
def test_print_format_mixe_fraction_default_whole_part(): assert Div(One, Two, print_format=PrintFormat.DIV_MIXED_FRACTION).whole_part == 0 assert Div(Three, Two, print_format=PrintFormat.DIV_MIXED_FRACTION).whole_part == 1 assert Div(Integer(11), Three, print_format=PrintFormat.DIV_MIXED_FRACTION).whole_part == 3 assert Div(Integer(11), Three, print_format=PrintFormat.DIV_MIXED_FRACTION, whole_part=2).whole_part == 2
def test_deepcopy(): exp1 = Add(Div(Basic(Integer(1)), Integer(2)), Integer(3)) exp2 = deepcopy(exp1) tree1 = Tree(exp1) tree2 = Tree(exp2) tree3 = deepcopy(tree1) assert exp1 == exp2 assert not set(tree1._nodes).intersection(set(tree2._nodes)) assert tree3.root == exp1 assert set(tree3._nodes) == set(Tree(tree3.root)._nodes)
def test_hash(): class A(Basic): pass assert hash(Basic(1, 2)) == hash(Basic(1, 2)) assert hash(Basic(1, Basic(2, 3))) == hash(Basic(1, Basic(2, 3))) assert hash(A(1, 2)) != hash(Basic(1, 2)) expr1 = Add( Mul(Integer(2), X), Neg(Mul(Integer(3), Pow(X, Two))), Neg(Integer(7)), Mul(Integer(7), X), Neg(Pow(X, Two)), ) expr2 = Add( Mul(Integer(2), X), Neg(Mul(Integer(3), Pow(X, Two))), Neg(Integer(7)), Mul(Integer(7), X), Neg(Pow(X, Two)), ) assert hash(expr1) == hash(expr2)
def test_tree_string(): exp1 = Add(Mul(Integer(1), Integer(2)), Div(Integer(3), Symbol("x"))) tree = Tree(exp1) tree_string_exp = ("" "Add\n" "|-- Mul\n" "| |-- Integer(1)\n" "| +-- Integer(2)\n" "+-- Div\n" " |-- Integer(3)\n" " +-- Symbol(x)\n") assert tree.tree_string() == tree_string_exp
def to_basic(self): def factoritem_to_basic(e: Basic, power: int) -> Basic: if power == 1: return e else: return Pow(e, Integer(power)) exprs = [ factoritem_to_basic(e, power) for e, power in self.factors.items() ] if len(exprs) == 0: arg: Basic = Integer(1) elif len(exprs) == 1: arg = exprs[0] else: arg = Mul(*exprs) if self.coef == 1: return arg else: return Neg(arg)
def test_neg(): assert Neg(Integer(1)) == Neg(Integer(1))
from calculator.core import Neg from calculator.numbers import Integer, Symbol Zero = Integer(0) One = Integer(1) Two = Integer(2) Three = Integer(3) Four = Integer(4) Five = Integer(5) Six = Integer(6) Seven = Integer(7) Eight = Integer(8) Nine = Integer(9) X = Symbol("x") Y = Symbol("y") Z = Symbol("z") MinusOne = Neg(Integer(1)) X = Symbol("x") Y = Symbol("y")
( Div(Div(Three, Two, print_format=PrintFormat.DIV_COLON), One, print_format=PrintFormat.DIV_COLON), r"3 : 2 : 1", ), (Nroot(Two, Two), r"\sqrt{2}"), (Nroot(Two, Three), r"\sqrt[3] {2}"), (Div(One, Two, print_format=PrintFormat.DIV_RATIONAL), r"\frac{1}{2}"), (Div(Two, Four, print_format=PrintFormat.DIV_RATIONAL), r"\frac{2}{4}"), (Pow(Two, Four), r"2^{4}"), (Pow(Pow(Two, Three), Four), r"\left(2^{3}\right)^{4}"), (Div(Add(One, Two), Four, print_format=PrintFormat.DIV_RATIONAL), r"\frac{1+2}{4}"), ( Add( Two, Div(One, Nroot(Three, Two), print_format=PrintFormat.DIV_RATIONAL), Neg(Mul(Pow(Two, Four), Integer(7))), ), r"2+\frac{1}{\sqrt{3}}-2^{4} \cdot 7", ), (Add(Two, Mul(Three, Four)), r"2+3 \cdot 4"), ( Mul(Add(Two, Three), Four), r"\left(2+3\right) \cdot 4", ), (Mul(Add(Two, Three), Mul(Four, Symbol("x"))), r"\left(2+3\right) \cdot 4x"), (Mul(Symbol("x"), Symbol("x")), r"x \cdot x"), (Mul(Symbol("x"), Symbol("y")), r"xy"), (Div(Float(1.2), Add(One, Neg(Two)), print_format=PrintFormat.DIV_COLON), r"1.2 : \left(1-2\right)"), (Mul(Four, Symbol("x"), Symbol("y")), r"4xy"), (Mul(Four, Mul(Symbol("x"), Symbol("y"))), r"4 \cdot xy"), (Add(One, Add(Two, Three)), r"1+\left(2+3\right)"),
def test_init(): exp1 = Add(Mul(Integer(1), Integer(2)), Div(Integer(3), Integer(4))) tree = Tree(exp1) assert tree.size == 7
def test_leaves(): exp1 = Add(Mul(Integer(1), Integer(2)), Div(Integer(3), Symbol("x"))) tree = Tree(exp1) assert (set(tree.leaves()) == set(tree.leaves(nid=tree.root_nid)) == {Integer(1), Integer(2), Integer(3), Symbol("x")})
def test_integer_value_error(): with raises(TypeError): Integer(None)
from pytest import mark, raises from calculator.core import Neg from calculator.numbers import Float, Integer, Number @mark.parametrize( "base, casted", [ (1, Integer(1)), (-1, Neg(Integer(1))), (Integer(1), Integer(1)), (Integer(3.0), Integer(3)), ("-1.2", Neg(Float("1.2"))), ("1.2", Float("1.2")), (Number(3.3), Float("3.3")), (Number(15 / 5), Integer(3)), ], ) def test_number(base, casted): assert Number(base) == casted assert isinstance(Number(base), type(casted)) def test_value_error(): with raises(ValueError): Number("foo") def test_type_error(): with raises(TypeError):
def factoritem_to_basic(e: Basic, power: int) -> Basic: if power == 1: return e else: return Pow(e, Integer(power))