def test_7_2_operation_power(self):
        """
        1
        /2^2
        =
        """
        model = SRPN_Model()
        alist = []

        nr1 = 1
        r1 = R(RT.IN, nr1)
        alist.append(nr1)
        self.assertEqual(model.take_in(str(nr1)), [r1])

        nr1 = "/2^2"
        alist[-1] = alist[-1] // pow(2, 2)
        r1 = R(RT.OP, alist[-1])
        self.assertEqual(model.take_in(nr1), [r1])

        print(alist)
        op = "="
        self.assertEqual(model.take_in(op), [R(RT.DT, alist[-1])])

        op = "d"
        self.assertEqual(model.take_in(op), [R(RT.DS, alist)])
    def test_7_1_simple_power(self):
        """
        1
        2
        ^
        =
        """
        model = SRPN_Model()
        alist = []

        nr1 = 1
        r1 = R(RT.IN, nr1)
        alist.append(nr1)
        self.assertEqual(model.take_in(str(nr1)), [r1])

        nr1 = 2
        r1 = R(RT.IN, nr1)
        alist.append(nr1)
        self.assertEqual(model.take_in(str(nr1)), [r1])

        op = "^"
        alist[-2] = pow(alist[-2], alist[-1])
        alist.pop(-1)
        self.assertEqual(model.take_in(op), [R(RT.OP, alist[-1])])

        op = "="
        self.assertEqual(model.take_in(op), [R(RT.DT, alist[-1])])

        op = "d"
        self.assertEqual(model.take_in(op), [R(RT.DS, alist)])
 def test_saturates_numbers(self):
     """
     Test if it is saturating 
     """
     model = SRPN_Model()
     self.assertEqual(model.take_in("1111111111111111111111"),
                      [R(RT.IN, 2147483647)])
     self.assertEqual(model.take_in("-111111111111111111111"),
                      [R(RT.IN, -2147483648)])
 def test_1_3(self):
     model = SRPN_Model()
     nr = 9
     self.assertEqual(model.take_in(str(nr)), [R(RT.IN, nr)])
     nr = 4
     self.assertEqual(model.take_in(str(nr)), [R(RT.IN, nr)])
     op = "*"
     res = 9 * 4
     self.assertEqual(model.take_in(op), [R(RT.OP, res)])
     self.assertEqual(model.take_in("="), [R(RT.DT, res)])
 def test_1_5(self):
     model = SRPN_Model()
     nr1 = 11
     self.assertEqual(model.take_in(str(nr1)), [R(RT.IN, nr1)])
     nr2 = 3
     self.assertEqual(model.take_in(str(nr2)), [R(RT.IN, nr2)])
     op = "%"
     res = nr1 % nr2
     self.assertEqual(model.take_in(op), [R(RT.OP, res)])
     self.assertEqual(model.take_in("="), [R(RT.DT, res)])
 def test_read_correctly_numbers(self):
     """
     Basic reading numbers 
     single
     multiline
     negtive
     """
     model = SRPN_Model()
     self.assertEqual(model.take_in(1), [R(RT.IN, 1)])
     self.assertEqual(model.take_in("2 3 "), [R(RT.IN, 2), R(RT.IN, 3)])
     self.assertEqual(model.take_in(" -3 -5"), [R(RT.IN, -3), R(RT.IN, -5)])
    def test_7_3_Removing_encapsulated_comments(self):
        model = SRPN_Model()
        alist = list()
        nr1 = "1 #comment 1 #"
        r1 = R(RT.IN, 1)
        alist.append(1)
        self.assertEqual(model.take_in(str(nr1)), [r1])

        nr1 = "1 #comment 1#"
        r1 = R(RT.IN, 1)
        alist.append(1)
        self.assertEqual(model.take_in(str(nr1)), [r1])
    def test_4_1(self):
        """
        1
        +
        """
        model = SRPN_Model()
        alist = []

        nr1 = 1
        r1 = R(RT.IN, nr1)
        alist.append(nr1)
        self.assertEqual(model.take_in(str(nr1)), [r1])

        op = "+"
        self.assertEqual(model.take_in(op), [R(RT.ER, Error(ERROR.ST_UNDRF))])
    def test_7_5_Removing_line_comms_and_doing_op(self):
        model = SRPN_Model()
        alist = list()
        nr1 = "11 #"
        r1 = R(RT.IN, 11)
        alist.append(11)
        self.assertEqual(model.take_in(str(nr1)), [r1])

        nr1 = "112"
        r1 = R(RT.DO_NOTHING)
        self.assertEqual(model.take_in(str(nr1)), [r1])

        nr1 = "# /2^2"
        alist[-1] = alist[-1] // pow(2, 2)
        r1 = R(RT.OP, alist[-1])
        self.assertEqual(model.take_in(nr1), [r1])
    def test_7_4_Removing_line_comms(self):
        model = SRPN_Model()
        alist = list()
        nr1 = "11 #"
        r1 = R(RT.IN, 11)
        alist.append(11)
        self.assertEqual(model.take_in(str(nr1)), [r1])

        nr1 = "112"
        r1 = R(RT.DO_NOTHING)
        self.assertEqual(model.take_in(str(nr1)), [r1])

        nr1 = "#112"
        r1 = R(RT.IN, 112)
        alist.append(112)
        self.assertEqual(model.take_in(str(nr1)), [r1])
    def test_3_2(self):
        model = SRPN_Model()
        alist = []

        nr1 = -2147483647
        r1 = R(RT.IN, nr1)
        alist.append(nr1)
        self.assertEqual(model.take_in(str(nr1)), [r1])

        nr2 = 1
        r2 = R(RT.IN, nr2)
        alist.append(nr2)
        self.assertEqual(model.take_in(str(nr2)), [r2])

        op = "-"
        alist[-2] -= alist[-1]
        alist.pop(-1)
        self.assertEqual(model.take_in(op), [R(RT.OP, alist[-1])])

        op = "="
        self.assertEqual(model.take_in(op), [R(RT.DT, alist[-1])])

        nr2 = 20
        r2 = R(RT.IN, nr2)
        alist.append(nr2)
        self.assertEqual(model.take_in(str(nr2)), [r2])

        op = "-"
        alist.pop(-1)
        self.assertEqual(model.take_in(op), [R(RT.OP, alist[-1])])

        op = "="
        self.assertEqual(model.take_in(op), [R(RT.DT, alist[-1])])
class SRPN_Controller:
    """
    SRPMN Controller is an itermediary between the user, the model and the 
    """

    view = SRPN_View()
    model = SRPN_Model()

    def __init__(self):
        self.model = SRPN_Model()
        print(self.view.disp_wlcm())
        while True:
            self.read_input()

    def read_input(self):
        """
        reads the user input and sends it to the model
        """
        rw_input = ""
        rw_input = input()
        result_list = self.model.take_in(rw_input)
        self.serve(result_list)

    def serve(self, result_list):
        """
        takes the list of results and based on the code sends to the view
        the data
        """
        """Debug only code [TO DO] delete 
        for i in range(len(result_list)):
            print ("step: {}, code: {}, data: {}".format(\
                    i,result_list[i].code,result_list[i].data)) #[DEBUG]
        """

        for result in result_list:

            if result.code == RT.DT:  # single number
                print(self.view.disp_top(result))

            elif result.code == RT.DS:  # list
                print(self.view.disp_stack(result))

            elif result.code == RT.IN:  # insertion
                dummy = " do nothing"

            elif result.code == RT.ER:  # error
                print(self.view.disp_error(result))

            elif result.code == RT.OP:  # opperation
                dummy = " do nothing"

            elif result.code == RT.DO_NOTHING:  # opperation
                dummy = " do nothing"
    def test_3_3(self):
        model = SRPN_Model()
        alist = []

        nr1 = 100000
        r1 = R(RT.IN, nr1)
        alist.append(nr1)
        self.assertEqual(model.take_in(str(nr1)), [r1])

        nr2 = 0
        r2 = R(RT.IN, nr2)
        alist.append(nr2)
        self.assertEqual(model.take_in(str(nr2)), [r2])

        op = "-"
        alist[-2] -= alist[-1]
        alist.pop(-1)
        self.assertEqual(model.take_in(op), [R(RT.OP, alist[-1])])

        op = "d"
        self.assertEqual(model.take_in(op), [R(RT.DS, alist)])

        op = "-"
        self.assertEqual(model.take_in(op), [R(RT.ER, Error(ERROR.ST_UNDRF))])

        op = "="
        self.assertEqual(model.take_in(op), [R(RT.DT, alist[-1])])
    def test_2_2(self):
        model3 = SRPN_Model()
        alist = []
        nr1 = 1234
        r1 = R(RT.IN, nr1)
        alist.append(nr1)
        self.assertEqual(model3.take_in(str(nr1)), [r1])

        nr2 = 2345
        r2 = R(RT.IN, nr2)
        alist.append(nr2)
        self.assertEqual(model3.take_in(str(nr2)), [r2])

        nr3 = 3456
        r3 = R(RT.IN, nr3)
        alist.append(nr3)
        self.assertEqual(model3.take_in(str(nr3)), [r3])

        op = "d"
        self.assertEqual(model3.take_in(op), [R(RT.DS, alist)])

        op = "+"
        alist[-2] += alist[-1]
        alist.pop(-1)
        self.assertEqual(model3.take_in(op), [R(RT.OP, alist[-1])])

        op = "d"
        self.assertEqual(model3.take_in(op), [R(RT.DS, alist)])

        op = "+"
        alist[-2] += alist[-1]
        alist.pop(-1)
        self.assertEqual(model3.take_in(op), [R(RT.OP, alist[-1])])

        op = "d"
        self.assertEqual(model3.take_in(op), [R(RT.DS, alist)])

        op = "="
        self.assertEqual(model3.take_in(op), [R(RT.DT, alist[-1])])
    def test_2_1(self):
        model2 = SRPN_Model()
        nr1 = 3
        self.assertEqual(model2.take_in(str(nr1)), [R(RT.IN, nr1)])
        nr2 = 3
        self.assertEqual(model2.take_in(str(nr2)), [R(RT.IN, nr2)])
        op = "*"
        res1 = nr1 * nr2
        self.assertEqual(model2.take_in(op), [R(RT.OP, res1)])

        nr3 = 4
        self.assertEqual(model2.take_in(str(nr3)), [R(RT.IN, nr3)])
        nr4 = 4
        self.assertEqual(model2.take_in(str(nr4)), [R(RT.IN, nr4)])
        op = "*"
        res = nr3 * nr4
        self.assertEqual(model2.take_in(op), [R(RT.OP, res)])
        op = "+"
        res = res1 + res
        self.assertEqual(model2.take_in(op), [R(RT.OP, res)])
        self.assertEqual(model2.take_in("="), [R(RT.DT, res)])
    def test_4_2(self):
        """
        10
        5
        -5
        +
        /
        """
        model = SRPN_Model()
        alist = []

        nr1 = 10
        r1 = R(RT.IN, nr1)
        alist.append(nr1)
        self.assertEqual(model.take_in(str(nr1)), [r1])

        nr1 = 5
        r1 = R(RT.IN, nr1)
        alist.append(nr1)
        self.assertEqual(model.take_in(str(nr1)), [r1])

        nr1 = -5
        r1 = R(RT.IN, nr1)
        alist.append(nr1)
        self.assertEqual(model.take_in(str(nr1)), [r1])

        op = "+"
        alist[-2] += alist[-1]
        alist.pop(-1)
        self.assertEqual(model.take_in(op), [R(RT.OP, alist[-1])])

        op = "/"
        self.assertEqual(model.take_in(op), [R(RT.ER, Error(ERROR.DIV0))])

        op = "d"
        self.assertEqual(model.take_in(op), [R(RT.DS, alist)])
 def test_1_2(self):
     model = SRPN_Model()
     self.assertEqual(model.take_in("11"), [R(RT.IN, 11)])
     self.assertEqual(model.take_in("3"), [R(RT.IN, 3)])
     self.assertEqual(model.take_in("-"), [R(RT.OP, 8)])
     self.assertEqual(model.take_in("="), [R(RT.DT, 8)])
 def test_1_1(self):
     model = SRPN_Model()
     self.assertEqual(model.take_in("10"), [R(RT.IN, 10)])
     self.assertEqual(model.take_in("2"), [R(RT.IN, 2)])
     self.assertEqual(model.take_in("+"), [R(RT.OP, 12)])
     self.assertEqual(model.take_in("="), [R(RT.DT, 12)])