import unittest
from modelverse_kernel.main import ModelverseKernel
from utils import execute_until_finished, MvSWrapper, get_inst, get_phase, get_returnvalue

class Testif(unittest.TestCase):
    def setUp(self):
        self.mvs = MvSWrapper()
        self.root = self.mvs.execute("RR", [])[0]
        self.mvk = ModelverseKernel(self.root)

        self.user_root = self.mvs.execute("CN", [])[0]
        self.user_frame = self.mvs.execute("CN", [])[0]
        self.symbols = self.mvs.execute("CN", [])[0]
        self.evalstack = self.mvs.execute("CN", [])[0]
        self.returnvalue = self.mvs.execute("CN", [])[0]
        self.phase = self.mvs.execute("CNV", ["init"])[0]

        self.mvs.execute("CD", [self.root, "user_1", self.user_root])
        self.mvs.execute("CD", [self.user_root, "frame", self.user_frame])
        self.mvs.execute("CD", [self.user_frame, "symbols", self.symbols])
        self.mvs.execute("CD", [self.user_frame, "evalstack", self.evalstack])
        self.mvs.execute("CD", [self.user_frame, "phase", self.phase])
        self.mvs.execute("CD", [self.user_frame, "returnvalue", self.returnvalue])

    def test_if_true_with_else(self):
        """
        Test the code:
            if True:
                a
            else:
                b
            c
        """
        self.inst = self.mvs.execute("CNV", [{"value":"if"}])[0]

        self.const_a = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_b = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_c = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_true = self.mvs.execute("CNV", [{"value":"constant"}])[0]

        self.value_a = self.mvs.execute("CNV", ["a"])[0]
        self.value_b = self.mvs.execute("CNV", ["b"])[0]
        self.value_c = self.mvs.execute("CNV", ["c"])[0]
        self.value_true = self.mvs.execute("CNV", [True])[0]

        self.mvs.execute("CD", [self.user_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "cond", self.const_true])
        self.mvs.execute("CD", [self.inst, "then", self.const_a])
        self.mvs.execute("CD", [self.inst, "else", self.const_b])
        self.mvs.execute("CD", [self.inst, "next", self.const_c])
        self.mvs.execute("CD", [self.const_a, "node", self.value_a])
        self.mvs.execute("CD", [self.const_b, "node", self.value_b])
        self.mvs.execute("CD", [self.const_c, "node", self.value_c])
        self.mvs.execute("CD", [self.const_true, "node", self.value_true])

        ### Execute rules
        self.assertEquals(get_inst(self.root, self.mvs), "if")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue(self.root, self.mvs), True)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "if")
        self.assertEquals(get_phase(self.root, self.mvs), "cond")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue(self.root, self.mvs), "a")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "if")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue(self.root, self.mvs), "c")

    def test_if_true_without_else(self):
        """
        Test the code:
            if True:
                a
            c
        """
        self.inst = self.mvs.execute("CNV", [{"value":"if"}])[0]

        self.const_a = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_c = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_true = self.mvs.execute("CNV", [{"value":"constant"}])[0]

        self.value_a = self.mvs.execute("CNV", ["a"])[0]
        self.value_c = self.mvs.execute("CNV", ["c"])[0]
        self.value_true = self.mvs.execute("CNV", [True])[0]

        self.mvs.execute("CD", [self.user_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "cond", self.const_true])
        self.mvs.execute("CD", [self.inst, "then", self.const_a])
        self.mvs.execute("CD", [self.inst, "next", self.const_c])
        self.mvs.execute("CD", [self.const_a, "node", self.value_a])
        self.mvs.execute("CD", [self.const_c, "node", self.value_c])
        self.mvs.execute("CD", [self.const_true, "node", self.value_true])

        ### Execute rules
        self.assertEquals(get_inst(self.root, self.mvs), "if")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue(self.root, self.mvs), True)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "if")
        self.assertEquals(get_phase(self.root, self.mvs), "cond")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue(self.root, self.mvs), "a")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "if")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue(self.root, self.mvs), "c")

    def test_if_false_with_else(self):
        """
        Test the code:
            if False:
                a
            else:
                b
            c
        """
        self.inst = self.mvs.execute("CNV", [{"value":"if"}])[0]

        self.const_a = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_b = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_c = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_false = self.mvs.execute("CNV", [{"value":"constant"}])[0]

        self.value_a = self.mvs.execute("CNV", ["a"])[0]
        self.value_b = self.mvs.execute("CNV", ["b"])[0]
        self.value_c = self.mvs.execute("CNV", ["c"])[0]
        self.value_false = self.mvs.execute("CNV", [False])[0]

        self.mvs.execute("CD", [self.user_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "cond", self.const_false])
        self.mvs.execute("CD", [self.inst, "then", self.const_a])
        self.mvs.execute("CD", [self.inst, "else", self.const_b])
        self.mvs.execute("CD", [self.inst, "next", self.const_c])
        self.mvs.execute("CD", [self.const_a, "node", self.value_a])
        self.mvs.execute("CD", [self.const_b, "node", self.value_b])
        self.mvs.execute("CD", [self.const_c, "node", self.value_c])
        self.mvs.execute("CD", [self.const_false, "node", self.value_false])

        ### Execute rules
        self.assertEquals(get_inst(self.root, self.mvs), "if")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue(self.root, self.mvs), False)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "if")
        self.assertEquals(get_phase(self.root, self.mvs), "cond")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue(self.root, self.mvs), "b")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "if")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue(self.root, self.mvs), "c")

    def test_if_false_without_else(self):
        """
        Test the code:
            if False:
                a
            c
        """
        self.inst = self.mvs.execute("CNV", [{"value":"if"}])[0]

        self.const_a = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_c = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_false = self.mvs.execute("CNV", [{"value":"constant"}])[0]

        self.value_a = self.mvs.execute("CNV", ["a"])[0]
        self.value_c = self.mvs.execute("CNV", ["c"])[0]
        self.value_false = self.mvs.execute("CNV", [False])[0]

        self.mvs.execute("CD", [self.user_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "cond", self.const_false])
        self.mvs.execute("CD", [self.inst, "then", self.const_a])
        self.mvs.execute("CD", [self.inst, "next", self.const_c])
        self.mvs.execute("CD", [self.const_a, "node", self.value_a])
        self.mvs.execute("CD", [self.const_c, "node", self.value_c])
        self.mvs.execute("CD", [self.const_false, "node", self.value_false])

        ### Execute rules
        self.assertEquals(get_inst(self.root, self.mvs), "if")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue(self.root, self.mvs), False)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "if")
        self.assertEquals(get_phase(self.root, self.mvs), "cond")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "if")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue(self.root, self.mvs), "c")
