Exemplo n.º 1
0
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)
Exemplo n.º 2
0
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))
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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
Exemplo n.º 5
0
        pass

    assert A(1, 2) == A(1, 2)
    assert B(1, 2) == B(1, 2)
    assert A(1, 2) != B(1, 2)


def test_parent():
    nested = Basic(Basic(Basic(1), 2), 3)

    assert nested.parent is None
    assert nested.args[0].parent is nested
    assert nested.args[0].args[0].parent is nested.args[0]


@mark.parametrize(
    "expr, types, expected_atoms",
    [
        (One, (), {One}),
        (Add(One, Two, One), (), {One, Two}),
        (Add(One, Two, One), (Symbol,), set()),
        (Add(One, X), (Integer,), {One}),
        (Add(One, X, Y), (Symbol,), {X, Y}),
        (Mul(Add(One, X, Y), Two), (Add,), {Add(One, X, Y)}),
        (Mul(Add(One, X, Y), Two), (Add, Symbol), {X, Y, Add(One, X, Y)}),
        (Eq(X, Add(One, Two)), (Symbol,), {X}),
    ],
)
def test_atoms(expr, types, expected_atoms):
    assert expr.atoms(*types) == expected_atoms
Exemplo n.º 6
0
def test_force_root():
    with raises(
            ValueError,
            match=r"Expresion of type Eq can be only in the root of the formula"
    ):
        Add(Eq(1, 2), 3)
Exemplo n.º 7
0
from calculator.core import Add, Basic, Div, Eq, Mul, Neg, Nroot, Pow, PrintFormat
from calculator.numbers import Float, Integer, Symbol
from calculator.printer.latexprinter import LatexPrinter
from tests._shortcuts import Four, One, Three, Two


@fixture
def latex_printer():
    return LatexPrinter()


@mark.parametrize(
    "expression, latex",
    [
        (Add(One, Two), r"1+2"),
        (Add(One, Two, Three), r"1+2+3"),
        (Add(Two, Neg(One)), r"2-1"),
        (Add(Three, Neg(Two), Neg(One)), r"3-2-1"),
        (Add(Three, Neg(Add(Two, Neg(One)))), r"3-\left(2-1\right)"),
        (Mul(Two, Three), r"2 \cdot 3"),
        (Mul(Two, Three, Four), r"2 \cdot 3 \cdot 4"),
        (Neg(One), r"-1"),
        (Neg(Two), r"-2"),
        (Add(Neg(Two), Neg(Four)), r"-2-4"),
        (Add(Two, Neg(Neg(Four))), r"2-\left(-4\right)"),
        (Mul(Two, Add(Two, Three)), r"2 \cdot \left(2+3\right)"),
        (Div(Float(1.2), Float(0.1), print_format=PrintFormat.DIV_RATIONAL), r"\frac{1.2}{0.1}"),
        (Div(Float(1.2), Float(0.1), print_format=PrintFormat.DIV_COLON), r"1.2 : 0.1"),
        (Div(Float(1.2), Add(One, Two), print_format=PrintFormat.DIV_COLON), r"1.2 : \left(1+2\right)"),
        (Div(Float(1.2), Mul(One, Two), print_format=PrintFormat.DIV_COLON), r"1.2 : \left(1 \cdot 2\right)"),
Exemplo n.º 8
0
def test_init():
    exp1 = Add(Mul(Integer(1), Integer(2)), Div(Integer(3), Integer(4)))
    tree = Tree(exp1)

    assert tree.size == 7
Exemplo n.º 9
0
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")})