def test_if(self): st = If( LessThan(Variable("x"), Number(5)), Assign("x", Number(2)), Assign("x", Multiply(Variable("x"), Variable("x"))), ) self.assertEqual(eval(st.to_python)({"x": 1}), {"x": 2})
def test_if(): st = If( LessThan(Variable("x"), Number(5)), Assign("x", Number(2)), Assign("x", Multiply(Variable("x"), Variable("x"))), ) assert eval(st.to_python)({"x": 1}) == {"x": 2}
def test_assign(self): st = Assign("x", Add(Variable("x"), Number(1))) self.assertEqual(str(st), "x = (x + 1)") en = {"x": Number(2)} while st.reducible: st, en = st.reduce(en) self.assertEqual(en["x"], Number(3))
def test_if_true(): st = If(Variable("x"), Assign("y", Number(1)), Assign("y", Number(2))) assert str(st) == "if (x) { y = 1 } else { y = 2 }" en = {"x": Boolean(True)} while st.reducible: st, en = st.reduce(en) assert en["y"] == Number(1) assert en["x"] == Boolean(True)
def test_expr(): a1 = Add(Variable("x"), Number(1)) m1 = Multiply(Variable("x"), Number(9)) l1 = LessThan(Variable("x"), Variable("y")) assert eval(a1.to_python)({"x": 7}) == 8 assert eval(m1.to_python)({"x": 9}) == 81 assert eval(l1.to_python)({"x": 7, "y": 8})
def test_expr(self): a1 = Add(Variable("x"), Number(1)) m1 = Multiply(Variable("x"), Number(9)) l1 = LessThan(Variable("x"), Variable("y")) self.assertEqual(eval(a1.to_python)({"x": 7}), 8) self.assertEqual(eval(m1.to_python)({"x": 9}), 81) self.assertEqual(eval(l1.to_python)({"x": 7, "y": 8}), True)
def test_sequence(self): st = Sequence( Assign("x", Add(Number(1), Number(1))), Assign("y", Add(Variable("x"), Number(3))), ) en = st.evaluate({}) self.assertEqual(en["x"], Number(2)) self.assertEqual(en["y"], Number(5))
def test_if_true(self): st = If(Variable("x"), Assign("y", Number(1)), Assign("y", Number(2))) self.assertEqual(str(st), "if (x) { y = 1 } else { y = 2 }") en = {"x": Boolean(True)} while st.reducible: st, en = st.reduce(en) self.assertEqual(en["y"], Number(1)) self.assertEqual(en["x"], Boolean(True))
def test_assign(): st = Assign("x", Add(Variable("x"), Number(1))) assert str(st) == "x = x + 1" en = {"x": Number(2)} while st.reducible: st, en = st.reduce(en) assert en["x"] == Number(3) st = Assign("y", Add(Variable("x"), Number(1))) assert eval(st.to_python)({"x": 1}) == {"x": 1, "y": 2}
def test_while(self): seq = While( LessThan(Variable("x"), Number(5)), Assign("x", Multiply(Variable("x"), Number(2))), ) en = {"x": Number(1)} self.assertEqual(str(seq), "while (x < 5) { x = (x * 2) }") while seq.reducible: seq, en = seq.reduce(en) self.assertEqual(en["x"], Number(8))
def test_while(self): st = While( LessThan(Variable("x"), Number(1000)), Assign("x", Add(Variable("x"), Number(1))), ) en = st.evaluate({"x": Number(1)}) self.assertEqual(en["x"], Number(1000)) with self.assertRaises(RuntimeError): st.evaluate_with_recursion({"x": Number(-1000)})
def test_sequence(self): seq = Sequence( Assign("x", Add(Number(1), Number(2))), Assign("y", Add(Variable("x"), Number(3))), ) self.assertEqual(str(seq), "x = (1 + 2); y = (x + 3)") en = {} while seq.reducible: seq, en = seq.reduce(en) self.assertEqual(seq, DoNothing()) self.assertEqual(en["x"], Number(3))
def test_if_false(): st = If(Variable("x"), Assign("y", Number(1)), DoNothing()) en = {"x": Boolean(False)} while st.reducible: st, en = st.reduce(en) assert st == DoNothing() assert en["x"] == Boolean(False)
def test_if_false(self): st = If(Variable("x"), Assign("y", Number(1)), DoNothing()) en = {"x": Boolean(False)} while st.reducible: st, en = st.reduce(en) self.assertEqual(st, DoNothing()) self.assertEqual(en["x"], Boolean(False))
def test_type(self): n1 = Number(5) b1 = Boolean(False) v1 = Variable("x") self.assertEqual(eval(n1.to_python)({}), 5) self.assertEqual(eval(b1.to_python)({}), False) self.assertEqual(eval(v1.to_python)({"x": 7}), 7)
def test_type(self): self.assertEqual(Number(23).evaluate({}), Number(23)) self.assertEqual(Variable("x").evaluate({"x": Number(23)}), Number(23)) self.assertEqual( LessThan(Add(Variable("x"), Number(2)), Variable("y")).evaluate({ "x": Number(2), "y": Number(5) }), Boolean(True), )
def test_if_true_and_false(): st = If( LessThan(Variable("x"), Number(5)), Assign("x", Number(2)), Assign("x", Multiply(Variable("x"), Variable("x"))), ) en = st.evaluate({"x": Number(2)}) assert en["x"] == Number(2) st = If( LessThan(Variable("x"), Number(5)), Assign("x", Number(2)), Assign("x", Multiply(Variable("x"), Variable("x"))), ) en = st.evaluate({"x": Number(10)}) assert en["x"] == Number(100)
def test_if_true_and_false(self): st = If( LessThan(Variable("x"), Number(5)), Assign("x", Number(2)), Assign("x", Multiply(Variable("x"), Variable("x"))), ) en = st.evaluate({"x": Number(2)}) self.assertEqual(en["x"], Number(2)) st = If( LessThan(Variable("x"), Number(5)), Assign("x", Number(2)), Assign("x", Multiply(Variable("x"), Variable("x"))), ) en = st.evaluate({"x": Number(10)}) self.assertEqual(en["x"], Number(100))
def test_type(): assert Number(23).evaluate({}) == Number(23) assert Variable("x").evaluate({"x": Number(23)}) == Number(23) assert LessThan(Add(Variable("x"), Number(2)), Variable("y")).evaluate({ "x": Number(2), "y": Number(5) }) n1 = Number(5) b1 = Boolean(False) v1 = Variable("x") assert eval(n1.to_python)({}) == 5 assert not eval(b1.to_python)({}) assert eval(v1.to_python)({"x": 7}) == 7
def test_add(self): expr = Add(Variable("x"), Variable("y")) en = {"x": Number(1), "y": Number(2)} while expr.reducible: expr = expr.reduce(en) self.assertEqual(expr, Number(3))
def test_less(): expr = LessThan(Variable("x"), Variable("y")) en = {"x": Number(1), "y": Number(3)} while expr.reducible: expr = expr.reduce(en) assert expr == Boolean(True)
def test_mul(): expr = Multiply(Variable("x"), Variable("y")) en = {"x": Number(3), "y": Number(4)} while expr.reducible: expr = expr.reduce(en) assert expr == Number(12)
def test_add(): expr = Add(Variable("x"), Variable("y")) en = {"x": Number(1), "y": Number(2)} while expr.reducible: expr = expr.reduce(en) assert expr == Number(3)
def test_number(): assert Number(3) == Number(3) assert Number(3) != Number(4) assert Number(2) < Number(4) assert Number(4) > Number(2)
def test_interpreter(self): print() SEP = "=" * 50 seq = Assign("x", Add(Variable("x"), Number(1))) Machine(seq, {"x": Number(5)}).run() print(SEP) seq = Sequence( Assign("x", Add(Number(1), Number(1))), Assign("y", Multiply(Variable("x"), Number(2))), ) Machine(seq, {}).run() print(SEP) seq = Sequence( Assign("x", Add(Number(1), Number(1))), Assign("y", Add(Variable("x"), Number(1))), ) Machine(seq, {}).run() print(SEP) seq = While( LessThan(Variable("x"), Number(50)), Assign("x", Add(Variable("x"), Number(3))), ) Machine(seq, {"x": Number(1)}).run() print(SEP) print(seq.to_python) self.assertEqual(eval(seq.to_python)({"x": 1}), {"x": 52})
def test_less(self): expr = LessThan(Variable("x"), Variable("y")) en = {"x": Number(1), "y": Number(3)} while expr.reducible: expr = expr.reduce(en) self.assertEqual(expr, Boolean(True))
def test_assign(self): st = Assign("y", Add(Variable("x"), Number(1))) self.assertEqual(eval(st.to_python)({"x": 1}), {"x": 1, "y": 2})
def test_sequence(self): st = Sequence( Assign("x", Add(Number(1), Number(1))), Assign("y", Add(Variable("x"), Number(3))), ) self.assertEqual(eval(st.to_python)({"x": 2, "y": 1}), {"x": 2, "y": 5})
def test_while(self): st = While( LessThan(Variable("x"), Number(100)), Assign("x", Add(Variable("x"), Number(1))), ) self.assertEqual(eval(st.to_python)({"x": 1}), {"x": 100})
def test_mul(self): expr = Multiply(Variable("x"), Variable("y")) en = {"x": Number(3), "y": Number(4)} while expr.reducible: expr = expr.reduce(en) self.assertEqual(expr, Number(12))