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

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

        self.task_root = self.mvs.execute("CN", [])[0]
        self.task_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, "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_while_true(self):
        """
        Test the code:
            while True:
                a
            b
        """
        self.inst = self.mvs.execute("CNV", [{"value":"while"}])[0]

        self.const_a = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_b = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_cond = 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_cond = self.mvs.execute("CNV", [True])[0]

        self.mvs.execute("CD", [self.task_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "cond", self.const_cond])
        self.mvs.execute("CD", [self.inst, "body", self.const_a])
        self.mvs.execute("CD", [self.inst, "next", self.const_b])
        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_cond, "node", self.value_cond])

        ### Execute rules
        self.assertEquals(get_inst(self.root, self.mvs), "while")
        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), "while")
        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)

        # Loop 2
        self.assertEquals(get_inst(self.root, self.mvs), "while")
        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), "while")
        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)

        # Loop 3
        self.assertEquals(get_inst(self.root, self.mvs), "while")
        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), "while")
        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")

        # This will keep on going

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

        self.const_a = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_b = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_cond = 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_cond = self.mvs.execute("CNV", [False])[0]

        self.mvs.execute("CD", [self.task_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "cond", self.const_cond])
        self.mvs.execute("CD", [self.inst, "body", self.const_a])
        self.mvs.execute("CD", [self.inst, "next", self.const_b])
        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_cond, "node", self.value_cond])

        ### Execute rules
        self.assertEquals(get_inst(self.root, self.mvs), "while")
        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), "while")
        self.assertEquals(get_phase(self.root, self.mvs), "cond")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "while")
        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), "b")

    def test_while_variable(self):
        """
        Test the code:
            while True:
                a
            b
        But where the True changes to a False after 2 iterations
        """
        self.inst = self.mvs.execute("CNV", [{"value":"while"}])[0]

        self.const_a = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_b = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_cond = 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_cond = self.mvs.execute("CNV", [True])[0]

        self.mvs.execute("CD", [self.task_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "cond", self.const_cond])
        self.mvs.execute("CD", [self.inst, "body", self.const_a])
        self.mvs.execute("CD", [self.inst, "next", self.const_b])
        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_cond, "node", self.value_cond])

        ### Execute rules
        self.assertEquals(get_inst(self.root, self.mvs), "while")
        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), "while")
        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)

        # Loop 2
        self.assertEquals(get_inst(self.root, self.mvs), "while")
        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), "while")
        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)

        #####
        # Modify the constant to force the loop to stop
        #####

        new_const_cond = self.mvs.execute("CNV", [False])[0]
        self.mvs.execute("DN", [self.value_cond])
        self.mvs.execute("CD", [self.const_cond, "node", new_const_cond])

        ##### Modifications done

        # Loop 3
        self.assertEquals(get_inst(self.root, self.mvs), "while")
        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), "while")
        self.assertEquals(get_phase(self.root, self.mvs), "cond")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "while")
        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), "b")
