Commit 95b0fc11 authored by Taddeus Kroes's avatar Taddeus Kroes

Switched precedence of power and functions, and added some unit tests for it...

Switched precedence of power and functions, and added some unit tests for it (corresponding to WolframAlpha's output).
parent 2aae04c7
......@@ -63,8 +63,8 @@ class Parser(BisonParser):
('left', ('TIMES', 'DIVIDE')),
('left', ('EQ', )),
('left', ('NEG', )),
('right', ('SIN', 'COS', 'TAN', 'SOLVE', 'INT', 'SQRT')),
('right', ('POW', )),
('right', ('SIN', 'COS', 'TAN', 'SOLVE', 'INT', 'SQRT')),
)
interactive = 0
......
......@@ -4,6 +4,8 @@ import unittest
from src.parser import Parser
from src.node import ExpressionNode as Node, ExpressionLeaf as Leaf
from tests.parser import ParserWrapper, run_expressions, line, graph
from tests.rulestestcase import tree
from src.rules.goniometry import sin, cos
class TestParser(unittest.TestCase):
......@@ -15,11 +17,11 @@ class TestParser(unittest.TestCase):
run_expressions(Parser, [('a', Leaf('a'))])
def test_graph(self):
assert graph(Parser, '4a') == ("""
self.assertEqual(graph(Parser, '4a'), ("""
*
╭┴╮
4 a
""").replace('\n ', '\n')[1:-1]
""").replace('\n ', '\n')[1:-1])
def test_line(self):
self.assertEqual(line(Parser, '4-a'), '4 - a')
......@@ -35,3 +37,14 @@ class TestParser(unittest.TestCase):
self.assertNotEqual(possibilities2, [])
self.assertNotEqual(possibilities1, possibilities2)
def test_functions(self):
root, x = tree('sin x, x')
self.assertEqual(root, sin(x))
self.assertEqual(tree('sin x ^ 2'), sin(x) ** 2)
self.assertEqual(tree('sin(x) ^ 2'), sin(x) ** 2)
self.assertEqual(tree('sin (x) ^ 2'), sin(x) ** 2)
self.assertEqual(tree('sin(x ^ 2)'), sin(x ** 2))
self.assertEqual(tree('sin cos x'), sin(cos(x)))
self.assertEqual(tree('sin cos x ^ 2'), sin(cos(x)) ** 2)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment