Example #1
0
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}
Example #2
0
    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})
Example #3
0
    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))
Example #4
0
 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})
Example #5
0
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}
Example #6
0
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)
Example #7
0
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})
Example #8
0
    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)
Example #9
0
    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)})
Example #10
0
 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))
Example #11
0
 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),
     )
Example #12
0
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
Example #13
0
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)
Example #14
0
 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})
Example #15
0
 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))
Example #16
0
 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))