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

class TestString(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])

        execute_until_finished(self.mvk, self.mvs, "load_primitives", [])
        self.primitives = read_primitive_interfaces(self.root, self.mvs)

    def test_eq(self):
        self.helper_primitives_2_params("value_eq", "abc", "abc", True)

    def test_eq_startswith(self):
        self.helper_primitives_2_params("value_eq", "abc", "abcdef", False)

    def test_eq_samelength(self):
        self.helper_primitives_2_params("value_eq", "abc", "def", False)

    def test_eq_diff(self):
        self.helper_primitives_2_params("value_eq", "abc", "defghij", False)

    def test_eq_empty(self):
        self.helper_primitives_2_params("value_eq", "", "", True)

    def test_neq(self):
        self.helper_primitives_2_params("value_neq", "abc", "abc", False)

    def test_neq_startswith(self):
        self.helper_primitives_2_params("value_neq", "abc", "abcdef", True)

    def test_neq_samelength(self):
        self.helper_primitives_2_params("value_neq", "abc", "def", True)

    def test_neq_diff(self):
        self.helper_primitives_2_params("value_neq", "abc", "defghij", True)

    def test_neq_empty(self):
        self.helper_primitives_2_params("value_neq", "", "", False)

    def test_join(self):
        self.helper_primitives_2_params("string_join", "abc", "def", "abcdef")

    def test_join_empty_left(self):
        self.helper_primitives_2_params("string_join", "", "def", "def")

    def test_join_empty_right(self):
        self.helper_primitives_2_params("string_join", "abc", "", "abc")

    def test_join_empty_both(self):
        self.helper_primitives_2_params("string_join", "", "", "")

    def test_get_first(self):
        self.helper_primitives_2_params("string_get", "abcdefghijklmnopqrstuvwxyz", 0, "a")

    def test_get_last(self):
        self.helper_primitives_2_params("string_get", "abcdefghijklmnopqrstuvwxyz", 25, "z")

    def test_len(self):
        self.helper_primitives_1_params("string_len", "abcdefghijklmnopqrstuvwxyz", 26)

    def test_len_empty(self):
        self.helper_primitives_1_params("string_len", "", 0)

    def test_split(self):
        self.helper_primitives_2_params("string_split", "abc;defg;hij", ";", ["abc", "defg", "hij"])

    def test_split_single(self):
        self.helper_primitives_2_params("string_split", "abc;defg;hij", "/", ["abc;defg;hij"])

    def test_split_withempty(self):
        self.helper_primitives_2_params("string_split", "abc;defg;;hij", ";", ["abc", "defg", "", "hij"])

    def helper_primitives_2_params(self, operation, a, b, result):
        self.actual_arg_a = self.mvs.execute("CN", [])[0]
        self.actual_arg_b = self.mvs.execute("CN", [])[0]

        self.inst = self.mvs.execute("CNV", [{"value":"call"}])[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_first = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_second = 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_first = self.mvs.execute("CNV", [a])[0]
        self.value_second = self.mvs.execute("CNV", [b])[0]

        self.mvs.execute("CD", [self.user_frame, "IP", self.inst])
        self.const_func = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.mvs.execute("CD", [self.inst, "func", self.const_func])
        self.mvs.execute("CD", [self.const_func, "node", self.primitives[operation]])
        self.mvs.execute("CD", [self.inst, "next", self.const_c])
        self.mvs.execute("CD", [self.inst, "params", self.actual_arg_a])
        self.mvs.execute("CD", [self.actual_arg_a, "next_param", self.actual_arg_b])
        self.mvs.execute("CD", [self.inst, "last_param", self.actual_arg_b])
        self.mvs.execute("CD", [self.actual_arg_a, "value", self.const_first])
        self.mvs.execute("CD", [self.actual_arg_b, "value", self.const_second])
        self.mvs.execute("CD", [self.actual_arg_a, "name", self.value_a])
        self.mvs.execute("CD", [self.actual_arg_b, "name", self.value_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_c, "node", self.value_c])
        self.mvs.execute("CD", [self.const_first, "node", self.value_first])
        self.mvs.execute("CD", [self.const_second, "node", self.value_second])

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

        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase_ref(self.root, self.mvs), self.actual_arg_a)
        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), "call")
        self.assertEquals(get_phase_ref(self.root, self.mvs), self.actual_arg_b)
        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), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "call")
        execute_until_finished(self.mvk, self.mvs)

        # Body points to empty instruction, as it is a primitive
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        # This will have triggered the primitive, so now we are back where we left off
        # Just check whether or not the correct value is in the returnvalue to be used
        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        if isinstance(result, list):
            ref = get_returnvalue_ref(self.root, self.mvs)
            for i, v in enumerate(result):
                self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [ref, i])[0]])[0], v)
        else:
            self.assertEquals(get_returnvalue(self.root, self.mvs), result)
        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 helper_primitives_1_params(self, operation, a, result):
        self.actual_arg_a = self.mvs.execute("CN", [])[0]

        self.inst = self.mvs.execute("CNV", [{"value":"call"}])[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_first = 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_first = self.mvs.execute("CNV", [a])[0]

        self.mvs.execute("CD", [self.user_frame, "IP", self.inst])
        self.const_func = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.mvs.execute("CD", [self.inst, "func", self.const_func])
        self.mvs.execute("CD", [self.const_func, "node", self.primitives[operation]])
        self.mvs.execute("CD", [self.inst, "next", self.const_c])
        self.mvs.execute("CD", [self.inst, "params", self.actual_arg_a])
        self.mvs.execute("CD", [self.inst, "last_param", self.actual_arg_a])
        self.mvs.execute("CD", [self.actual_arg_a, "value", self.const_first])
        self.mvs.execute("CD", [self.actual_arg_a, "name", self.value_a])
        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_first, "node", self.value_first])

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

        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase_ref(self.root, self.mvs), self.actual_arg_a)
        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), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "call")
        execute_until_finished(self.mvk, self.mvs)

        # Body points to empty instruction, as it is a primitive
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        # This will have triggered the primitive, so now we are back where we left off
        # Just check whether or not the correct value is in the returnvalue to be used
        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue(self.root, self.mvs), result)
        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 helper_primitives_3_params(self, operation, a, b, c, result):
        self.actual_arg_a = self.mvs.execute("CN", [])[0]
        self.actual_arg_b = self.mvs.execute("CN", [])[0]
        self.actual_arg_c = self.mvs.execute("CN", [])[0]

        self.inst = self.mvs.execute("CNV", [{"value":"call"}])[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_first = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_second = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_third = 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_first = self.mvs.execute("CNV", [a])[0]
        self.value_second = self.mvs.execute("CNV", [b])[0]
        self.value_third = self.mvs.execute("CNV", [c])[0]

        self.mvs.execute("CD", [self.user_frame, "IP", self.inst])
        self.const_func = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.mvs.execute("CD", [self.inst, "func", self.const_func])
        self.mvs.execute("CD", [self.const_func, "node", self.primitives[operation]])
        self.mvs.execute("CD", [self.inst, "next", self.const_c])
        self.mvs.execute("CD", [self.inst, "params", self.actual_arg_a])
        self.mvs.execute("CD", [self.actual_arg_a, "next_param", self.actual_arg_b])
        self.mvs.execute("CD", [self.actual_arg_b, "next_param", self.actual_arg_c])
        self.mvs.execute("CD", [self.inst, "last_param", self.actual_arg_c])
        self.mvs.execute("CD", [self.actual_arg_a, "value", self.const_first])
        self.mvs.execute("CD", [self.actual_arg_b, "value", self.const_second])
        self.mvs.execute("CD", [self.actual_arg_c, "value", self.const_third])
        self.mvs.execute("CD", [self.actual_arg_a, "name", self.value_a])
        self.mvs.execute("CD", [self.actual_arg_b, "name", self.value_b])
        self.mvs.execute("CD", [self.actual_arg_c, "name", self.value_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_first, "node", self.value_first])
        self.mvs.execute("CD", [self.const_second, "node", self.value_second])
        self.mvs.execute("CD", [self.const_third, "node", self.value_third])

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

        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase_ref(self.root, self.mvs), self.actual_arg_a)
        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), "call")
        self.assertEquals(get_phase_ref(self.root, self.mvs), self.actual_arg_b)
        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), "call")
        self.assertEquals(get_phase_ref(self.root, self.mvs), self.actual_arg_c)
        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)
        execute_until_finished(self.mvk, self.mvs)

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

        # Body points to empty instruction, as it is a primitive
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        # This will have triggered the primitive, so now we are back where we left off
        # Just check whether or not the correct value is in the returnvalue to be used
        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue(self.root, self.mvs), result)
        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")
