Commit a836452c authored by Sander Mathijs van Veen's avatar Sander Mathijs van Veen

Merge branch 'master' of kompiler.org:trs

parents 6bc98dd5 af1fd43e
...@@ -22,7 +22,6 @@ class Possibility(object): ...@@ -22,7 +22,6 @@ class Possibility(object):
return '<Possibility root="%s" handler=%s args=%s>' \ return '<Possibility root="%s" handler=%s args=%s>' \
% (self.root, self.handler.func_name, self.args) % (self.root, self.handler.func_name, self.args)
# TODO: Add unit tests
def __eq__(self, other): def __eq__(self, other):
return self.handler == other.handler \ return self.handler == other.handler \
and hash(self.root) == hash(other.root) \ and hash(self.root) == hash(other.root) \
......
...@@ -10,9 +10,9 @@ class RulesTestCase(unittest.TestCase): ...@@ -10,9 +10,9 @@ class RulesTestCase(unittest.TestCase):
for p, e in zip(possibilities, expected): for p, e in zip(possibilities, expected):
self.assertEqual(p.root, e.root) self.assertEqual(p.root, e.root)
if p.args == None: if p.args == None: # pragma: nocover
self.assertIsNone(e.args) self.assertIsNone(e.args)
elif e.args == None: elif e.args == None: # pragma: nocover
self.assertIsNone(p.args) self.assertIsNone(p.args)
else: else:
for pair in zip(p.args, e.args): for pair in zip(p.args, e.args):
......
...@@ -8,6 +8,20 @@ class TestNode(unittest.TestCase): ...@@ -8,6 +8,20 @@ class TestNode(unittest.TestCase):
def setUp(self): def setUp(self):
self.l = [L(1), N('*', L(2), L(3)), L(4), L(5)] self.l = [L(1), N('*', L(2), L(3)), L(4), L(5)]
def test___lt__(self):
self.assertTrue(L(1) < L(2))
self.assertFalse(L(1) < L(1))
self.assertFalse(L(2) < L(1))
self.assertTrue(L(2) < N('+', L(1), L(2)))
self.assertFalse(N('+', L(1), L(2)) < L(1))
self.assertTrue(N('^', L('a'), L(2)) < N('^', L('a'), L(3)))
self.assertTrue(N('^', L(2), L('a')) < N('^', L(3), L('a')))
self.assertTrue(N('*', L(2), N('^', L('a'), L('b')))
< N('*', L(3), N('^', L('a'), L('b'))))
self.assertFalse(N('^', L('a'), L(3)) < N('^', L('a'), L(2)))
def test_is_power_true(self): def test_is_power_true(self):
self.assertTrue(N('^', *self.l[:2]).is_power()) self.assertTrue(N('^', *self.l[:2]).is_power())
self.assertFalse(N('+', *self.l[:2]).is_power()) self.assertFalse(N('+', *self.l[:2]).is_power())
......
import unittest
from src.possibilities import MESSAGES, Possibility as P, filter_duplicates
from tests.test_rules_poly import tree
def dummy_handler(root, args): # pragma: nocover
pass
def dummy_handler_msg(root, args): # pragma: nocover
pass
MESSAGES[dummy_handler_msg] = 'foo {1} + {2} bar'
class TestPossibilities(unittest.TestCase):
def setUp(self):
self.l1, self.l2 = self.n = tree('1 + 2')
self.p0 = P(self.n, dummy_handler, (self.l1, self.l2))
self.p1 = P(self.n, dummy_handler_msg, (self.l1, self.l2))
def test___str__(self):
self.assertEqual(str(self.p0),
'<Possibility root="1 + 2" handler=dummy_handler args=(1, 2)>')
self.assertEqual(str(self.p1), 'foo 1 + 2 bar')
def test___repr__(self):
self.assertEqual(repr(self.p0),
'<Possibility root="1 + 2" handler=dummy_handler args=(1, 2)>')
def test___eq__(self):
assert self.p0 == P(self.n, dummy_handler, (self.l1, self.l2))
assert self.p0 != self.p1
def test_filter_duplicates(self):
self.assertEqual(filter_duplicates([]), [])
self.assertEqual(filter_duplicates([1, 2]), [1, 2])
self.assertEqual(filter_duplicates([1, 2, 2]), [1, 2])
self.assertEqual(filter_duplicates([1, 2, 3, 2]), [1, 2, 3])
...@@ -58,10 +58,8 @@ class TestRulesNumerics(RulesTestCase): ...@@ -58,10 +58,8 @@ class TestRulesNumerics(RulesTestCase):
self.assertEqual(divide_numerics(i3 / f2, (3, 2.0)), 1.5) self.assertEqual(divide_numerics(i3 / f2, (3, 2.0)), 1.5)
self.assertEqual(divide_numerics(f3 / f2, (3.0, 2.0)), 1.5) self.assertEqual(divide_numerics(f3 / f2, (3.0, 2.0)), 1.5)
def test_add_numerics(self):
l0, l1 = tree('1,2')
self.assertEqual(add_numerics(l0 + l1, (l0, l1, 1, 2)), 3)
def test_add_numerics(self): def test_add_numerics(self):
l0, a, l1 = tree('1,a,2') l0, a, l1 = tree('1,a,2')
self.assertEqual(add_numerics(l0 + l1, (l0, l1, 1, 2)), 3)
self.assertEqual(add_numerics(l0 + a + l1, (l0, l1, 1, 2)), L(3) + a) self.assertEqual(add_numerics(l0 + a + l1, (l0, l1, 1, 2)), L(3) + a)
from src.rules.poly import match_combine_polynomes, combine_polynomes from src.rules.poly import match_combine_polynomes, combine_polynomes
from src.rules.numerics import add_numerics from src.rules.numerics import add_numerics
from src.possibilities import Possibility as P from src.possibilities import Possibility as P
from src.node import ExpressionLeaf as L
from src.parser import Parser from src.parser import Parser
from tests.parser import ParserWrapper from tests.parser import ParserWrapper
from tests.rulestestcase import RulesTestCase from tests.rulestestcase import RulesTestCase
......
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