artifacts: add calculators/ — the 30 built calculators (5/variant) + machine-docs + git logs
This commit is contained in:
@ -0,0 +1,159 @@
|
||||
import unittest
|
||||
|
||||
from calc.lexer import tokenize
|
||||
from calc.parser import BinOp, Num, ParseError, Unary, parse
|
||||
|
||||
|
||||
def p(src):
|
||||
return parse(tokenize(src))
|
||||
|
||||
|
||||
class TestPrecedence(unittest.TestCase):
|
||||
"""D1 — * and / bind tighter than + and -"""
|
||||
|
||||
def test_add_mul(self):
|
||||
# 1+2*3 → 1+(2*3)
|
||||
tree = p("1+2*3")
|
||||
self.assertEqual(
|
||||
tree,
|
||||
BinOp("+", Num(1), BinOp("*", Num(2), Num(3))),
|
||||
)
|
||||
|
||||
def test_mul_add(self):
|
||||
# 3*2+1 → (3*2)+1
|
||||
tree = p("3*2+1")
|
||||
self.assertEqual(
|
||||
tree,
|
||||
BinOp("+", BinOp("*", Num(3), Num(2)), Num(1)),
|
||||
)
|
||||
|
||||
def test_sub_div(self):
|
||||
# 10-4/2 → 10-(4/2)
|
||||
tree = p("10-4/2")
|
||||
self.assertEqual(
|
||||
tree,
|
||||
BinOp("-", Num(10), BinOp("/", Num(4), Num(2))),
|
||||
)
|
||||
|
||||
|
||||
class TestLeftAssociativity(unittest.TestCase):
|
||||
"""D2 — same-precedence operators associate left"""
|
||||
|
||||
def test_sub_left(self):
|
||||
# 8-3-2 → (8-3)-2
|
||||
tree = p("8-3-2")
|
||||
self.assertEqual(
|
||||
tree,
|
||||
BinOp("-", BinOp("-", Num(8), Num(3)), Num(2)),
|
||||
)
|
||||
|
||||
def test_div_left(self):
|
||||
# 8/4/2 → (8/4)/2
|
||||
tree = p("8/4/2")
|
||||
self.assertEqual(
|
||||
tree,
|
||||
BinOp("/", BinOp("/", Num(8), Num(4)), Num(2)),
|
||||
)
|
||||
|
||||
def test_add_left(self):
|
||||
# 1+2+3 → (1+2)+3
|
||||
tree = p("1+2+3")
|
||||
self.assertEqual(
|
||||
tree,
|
||||
BinOp("+", BinOp("+", Num(1), Num(2)), Num(3)),
|
||||
)
|
||||
|
||||
def test_mul_left(self):
|
||||
# 2*3*4 → (2*3)*4
|
||||
tree = p("2*3*4")
|
||||
self.assertEqual(
|
||||
tree,
|
||||
BinOp("*", BinOp("*", Num(2), Num(3)), Num(4)),
|
||||
)
|
||||
|
||||
|
||||
class TestParentheses(unittest.TestCase):
|
||||
"""D3 — parentheses override precedence"""
|
||||
|
||||
def test_parens_force_add_first(self):
|
||||
# (1+2)*3 → mul at root, add under left
|
||||
tree = p("(1+2)*3")
|
||||
self.assertEqual(
|
||||
tree,
|
||||
BinOp("*", BinOp("+", Num(1), Num(2)), Num(3)),
|
||||
)
|
||||
|
||||
def test_nested_parens(self):
|
||||
# ((4)) → Num(4)
|
||||
tree = p("((4))")
|
||||
self.assertEqual(tree, Num(4))
|
||||
|
||||
def test_parens_right(self):
|
||||
# 2*(3+4) → mul at root, add under right
|
||||
tree = p("2*(3+4)")
|
||||
self.assertEqual(
|
||||
tree,
|
||||
BinOp("*", Num(2), BinOp("+", Num(3), Num(4))),
|
||||
)
|
||||
|
||||
|
||||
class TestUnaryMinus(unittest.TestCase):
|
||||
"""D4 — leading and nested unary minus"""
|
||||
|
||||
def test_simple(self):
|
||||
tree = p("-5")
|
||||
self.assertEqual(tree, Unary("-", Num(5)))
|
||||
|
||||
def test_unary_paren(self):
|
||||
# -(1+2) → Unary at root
|
||||
tree = p("-(1+2)")
|
||||
self.assertEqual(tree, Unary("-", BinOp("+", Num(1), Num(2))))
|
||||
|
||||
def test_mul_unary(self):
|
||||
# 3 * -2 → BinOp('*', Num(3), Unary('-', Num(2)))
|
||||
tree = p("3 * -2")
|
||||
self.assertEqual(
|
||||
tree,
|
||||
BinOp("*", Num(3), Unary("-", Num(2))),
|
||||
)
|
||||
|
||||
def test_double_unary(self):
|
||||
# --5 → Unary('-', Unary('-', Num(5)))
|
||||
tree = p("--5")
|
||||
self.assertEqual(tree, Unary("-", Unary("-", Num(5))))
|
||||
|
||||
|
||||
class TestErrors(unittest.TestCase):
|
||||
"""D5 — malformed input raises ParseError"""
|
||||
|
||||
def test_trailing_op(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p("1 +")
|
||||
|
||||
def test_unclosed_paren(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p("(1")
|
||||
|
||||
def test_two_numbers(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p("1 2")
|
||||
|
||||
def test_close_open(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p(")(")
|
||||
|
||||
def test_empty(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p("")
|
||||
|
||||
def test_just_op(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p("+")
|
||||
|
||||
def test_mismatched_parens(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p("(1+2")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user