def test_while(): seq = While( LessThan(Variable("x"), Number(5)), Assign("x", Multiply(Variable("x"), Number(2))), ) en = {"x": Number(1)} assert str(seq) == "while (x < 5) { x = x * 2 }" while seq.reducible: seq, en = seq.reduce(en) assert en["x"] == Number(8) st = While( LessThan(Variable("x"), Number(1000)), Assign("x", Add(Variable("x"), Number(1))), ) en = st.evaluate({"x": Number(1)}) assert en["x"] == Number(1000) en = st.evaluate_with_recursion({"x": Number(500)}) assert en["x"] == Number(1000) with pytest.raises(RuntimeError): st.evaluate_with_recursion({"x": Number(-1000)}) st = While( LessThan(Variable("x"), Number(100)), Assign("x", Add(Variable("x"), Number(1))), ) assert eval(st.to_python)({"x": 1}) == {"x": 100}
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_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_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_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_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_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_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_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_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_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_sequence(): seq = Sequence( Assign("x", Add(Number(1), Number(2))), Assign("y", Add(Variable("x"), Number(3))), ) assert str(seq) == "x = 1 + 2; y = x + 3" en = {} while seq.reducible: seq, en = seq.reduce(en) assert seq == DoNothing() assert en["x"] == Number(3) st = Sequence( Assign("x", Add(Number(1), Number(1))), Assign("y", Add(Variable("x"), Number(3))), ) en = st.evaluate({}) assert en["x"] == Number(2) assert en["y"] == Number(5) st = Sequence( Assign("x", Add(Number(1), Number(1))), Assign("y", Add(Variable("x"), Number(3))), ) assert eval(st.to_python)({"x": 2, "y": 1}) == {"x": 2, "y": 5}
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_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_assign(self): st = Assign("y", Add(Variable("x"), Number(1))) self.assertEqual(eval(st.to_python)({"x": 1}), {"x": 1, "y": 2})
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_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})