test_parser.py 7.68 KB
Newer Older
1
# vim: set fileencoding=utf-8 :
2 3 4 5 6 7 8 9 10 11 12 13 14 15
# This file is part of TRS (http://math.kompiler.org)
#
# TRS is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# TRS is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with TRS.  If not, see <http://www.gnu.org/licenses/>.
16
from src.parser import Parser, find_possibilities
17
from src.node import ExpressionNode as Node, ExpressionLeaf as Leaf, \
18 19 20 21
        SPECIAL_TOKENS, sin, cos, der, log, ln, integral, indef, absolute, \
        Scope
from src.possibilities import Possibility as P
from src.rules.numerics import add_numerics
22
from tests.parser import ParserWrapper, run_expressions, line, graph
23
from tests.rulestestcase import RulesTestCase, tree
24 25


26
class TestParser(RulesTestCase):
27 28 29 30 31 32 33 34
    def test_constructor(self):
        node = Node('+', Leaf(1), Leaf(4))
        self.assertEqual(ParserWrapper(Parser).run(['1 + 4']), node)

    def test_identifiers(self):
        run_expressions(Parser, [('a', Leaf('a'))])

    def test_graph(self):
35
        self.assertEqual(graph(Parser, '4a'), ("""
36 37 38
         *
        ╭┴╮
        4 a
39
        """).replace('\n        ', '\n')[1:-1])
40 41

    def test_line(self):
42
        self.assertEqual(line(Parser, '4-a'), '4 - a')
43 44 45 46

    def test_reset_after_failure(self):
        parser = ParserWrapper(Parser)
        parser.run(['-(3a+6b)'])
47
        parser.parser.find_possibilities()
48 49
        possibilities1 = parser.parser.possibilities
        self.assertNotEqual(possibilities1, [])
50 51

        parser.run(['5+2*6'])
52
        parser.parser.find_possibilities()
53 54 55 56
        possibilities2 = parser.parser.possibilities
        self.assertNotEqual(possibilities2, [])

        self.assertNotEqual(possibilities1, possibilities2)
57

Taddeus Kroes's avatar
Taddeus Kroes committed
58 59 60 61 62 63 64 65
    def test_binary(self):
        a, b, c = tree('a, b, c')

        self.assertEqual(tree('a ^^ b'), a & b)
        self.assertEqual(tree('a vv b'), a | b)
        self.assertEqual(tree('a vv b vv c'), (a | b) | c)
        self.assertEqual(tree('a vv b ^^ c'), a | (b & c))

66 67 68
    def test_preprocessor(self):
        self.assertEqual(tree('ab'), tree('a * b'))
        self.assertEqual(tree('abc'), tree('a * b * c'))
69
        self.assertEqual(tree('a2'), tree('a * 2'))
70 71 72 73 74
        self.assertEqual(tree('a 2'), tree('a * 2'))
        self.assertEqual(tree('2a'), tree('2 * a'))
        self.assertEqual(tree('2(a + b)'), tree('2 * (a + b)'))
        self.assertEqual(tree('(a + b)2'), tree('(a + b) * 2'))

75 76 77 78 79 80 81
        self.assertEqual(tree('(a)(b)'), tree('(a) * (b)'))
        self.assertEqual(tree('(a)[b]\''), tree('(a) * [b]\''))

        # FIXME: self.assertEqual(tree('(a)|b|'), tree('(a) * |b|'))
        # FIXME: self.assertEqual(tree('|a|(b)'), tree('|a| * (b)'))
        # FIXME: self.assertEqual(tree('a|b|'), tree('a * |b|'))
        # FIXME: self.assertEqual(tree('|a|b'), tree('|a| * b'))
82
        self.assertEqual(tree('|a||b|'), tree('|a| * |b|'))
83

84
    def test_functions(self):
85
        x = tree('x')
86

87
        self.assertEqual(tree('sin x'), sin(x))
88
        self.assertEqual(tree('sin 2 x'), sin(2) * x)  # FIXME: correct?
89
        self.assertEqual(tree('sin x ^ 2'), sin(x ** 2))
90 91
        self.assertEqual(tree('sin(x) ^ 2'), sin(x) ** 2)
        self.assertEqual(tree('sin(x ^ 2)'), sin(x ** 2))
92

93
        self.assertEqual(tree('sin cos x'), sin(cos(x)))
94 95
        self.assertEqual(tree('sin cos x ^ 2'), sin(cos(x ** 2)))
        self.assertEqual(tree('sin cos(x) ^ 2'), sin(cos(x) ** 2))
96

97
    def test_bracket_derivative(self):
98 99 100
        x = tree('x')

        self.assertEqual(tree('[x]\''), der(x))
101
        self.assertEqual(tree('[x]\'\''), der(der(x)))
102

103
    def test_delta_derivative(self):
104
        exp, x, d = tree('x ^ 2, x, d')
105 106

        self.assertEqual(tree('d/dx x ^ 2'), der(exp, x))
107
        self.assertEqual(tree('d / dx x ^ 2'), der(exp, x))
108 109
        self.assertEqual(tree('d/dx x ^ 2 + x'), der(exp, x) + x)
        self.assertEqual(tree('d/dx (x ^ 2 + x)'), der(exp + x, x))
110 111
        self.assertEqual(tree('d/d'), d / d)
        # FIXME: self.assertEqual(tree('d(x ^ 2)/dx'), der(exp, x))
112 113 114 115 116 117 118 119

    def test_logarithm(self):
        x, g = tree('x, g')

        self.assertEqual(tree('log(x, e)'), ln(x))
        self.assertEqual(tree('log(x, 10)'), log(x))
        self.assertEqual(tree('log(x, 2)'), log(x, 2))
        self.assertEqual(tree('log(x, g)'), log(x, g))
120 121 122 123 124

        self.assertEqual(tree('log_2(x)'), log(x, 2))
        self.assertEqual(tree('log_10(x)'), log(x))
        self.assertEqual(tree('log_g(x)'), log(x, g))
        self.assertEqual(tree('log_g x'), log(x, g))
125 126 127 128 129 130 131

    def test_special_tokens(self):
        for token in SPECIAL_TOKENS:
            self.assertEqual(tree(token), Leaf(token))
            a, t = Leaf('a'), Leaf(token)
            self.assertEqual(tree('a' + token), a * t)
            # FIXME: self.assertEqual(tree('a' + token + 'a'), a * t * a)
132 133

    def test_integral(self):
134
        x, y, dx, a, b, l2 = tree('x, y, dx, a, b, 2')
135 136

        self.assertEqual(tree('int x'), integral(x, x))
137 138 139
        self.assertEqual(tree('int x ^ 2'), integral(x ** 2, x))
        self.assertEqual(tree('int x ^ 2 dx'), integral(x ** 2, x))
        self.assertEqual(tree('int x ^ 2 dy'), integral(x ** 2, y))
140

141 142 143
        self.assertEqual(tree('int_a^b x ^ 2'), integral(x ** 2, x, a, b))
        self.assertEqual(tree('int_a^b x ^ 2 dy'), integral(x ** 2, y, a, b))
        self.assertEqual(tree('int_(a-b)^(a+b) x ^ 2'),
144
                         integral(x ** 2, x, a - b, a + b))
145

146
        self.assertEqual(tree('int_a^b 2x'), integral(l2 * x, x, a, b))
147 148 149 150 151 152 153 154 155 156 157
        self.assertEqual(tree('int_a^b^2 x'), integral(x, x, a, b ** 2))
        self.assertEqual(tree('int_a^(b2) x'), integral(x, x, a, b * 2))

        self.assertEqual(tree('int x ^ 2 + 1'), integral(x ** 2, x) + 1)
        self.assertEqual(tree('int x ^ 2 + 1 dx'), integral(x ** 2 + 1, x))

        self.assertEqual(tree('int_a^b x ^ 2 dx'), integral(x ** 2, x, a, b))
        self.assertEqual(tree('int_a^(b2) x ^ 2 + 1 dx'),
                         integral(x ** 2 + 1, x, a, b * 2))
        self.assertEqual(tree('int_(a^2)^b x ^ 2 + 1 dx'),
                         integral(x ** 2 + 1, x, a ** 2, b))
158

159 160 161
        self.assertEqual(tree('int_(-a)^b x dx'), integral(x, x, -a, b))
        # FIXME: self.assertEqual(tree('int_-a^b x dx'), integral(x, x, -a, b))

162 163 164 165
    def test_indefinite_integral(self):
        x2, a, b = tree('x ^ 2, a, b')

        self.assertEqual(tree('[x ^ 2]_a^b'), indef(x2, a, b))
166 167 168 169 170

    def test_absolute_value(self):
        x = tree('x')

        self.assertEqual(tree('|x|'), absolute(x))
171
        self.assertEqual(tree('|x2|'), absolute(x * 2))
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

    def test_find_possibilities_basic(self):
        l1, l2 = root = tree('1 + 2')
        self.assertEqual(find_possibilities(root),
                [P(root, add_numerics, (Scope(root), l1, l2))])

    def test_find_possibilities_duplicates(self):
        (l1, l2), l3 = root = tree('1 + 2 + 3')
        self.assertEqual(find_possibilities(root),
                [P(root, add_numerics, (Scope(root), l1, l2)),
                 P(root, add_numerics, (Scope(root), l1, l3)),
                 P(root, add_numerics, (Scope(root), l2, l3))])

    def test_no_expression_error(self):
        self.assertRaises(RuntimeError, ParserWrapper(Parser).run, ['', '?'])
187 188 189

    def test_precedence(self):
        self.assertEqual(tree('ab / cd'), tree('a * (b / c) * d'))
190 191 192

    def test_pi_multiplication_sign(self):
        self.assertEqual(tree('apia'), tree('a * pi * a'))