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", [])
        self.mvk = ModelverseKernel(self.root)

        self.task_root = self.mvs.execute("CN", [])
        self.task_frame = self.mvs.execute("CN", [])
        self.symbols = self.mvs.execute("CN", [])
        self.evalstack = self.mvs.execute("CN", [])
        self.returnvalue = self.mvs.execute("CN", [])
        self.phase = self.mvs.execute("CNV", ["init"])

        self.mvs.execute("CD", [self.root, "task_1", self.task_root])
        self.mvs.execute("CD", [self.task_root, "frame", self.task_frame])
        self.mvs.execute("CD", [self.task_frame, "symbols", self.symbols])
        self.mvs.execute("CD", [self.task_frame, "evalstack", self.evalstack])
        self.mvs.execute("CD", [self.task_frame, "phase", self.phase])
        self.mvs.execute("CD", [self.task_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"}])

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

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

        self.mvs.execute("CD", [self.task_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"}])

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

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

        self.mvs.execute("CD", [self.task_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"}])

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

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

        self.mvs.execute("CD", [self.task_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"}])

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

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

        self.mvs.execute("CD", [self.task_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")
