# Given a 32-bit signed integer, reverse digits of an integer.

# Example 1:
# Input: 123
# Output: 321

# Example 2:
# Input: -123
# Output: -321

# Example 3:
# Input: 120
# Output: 21

# Note:
# Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31,  2^31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

from tests.test_cases import TestCases


class Solution:
    def reverse(self, x: int) -> int:
        """Solution for reverse 32-bit int problem"""
        res = 0
        sign = -1 if x < 0 else 1
        temp = x * sign
        min_int = -2**31
        max_int = 2**31 - 1
        while temp > 0:
            res = (res * 10) + (temp % 10)
            temp = temp // 10
            if res * sign < min_int or res * sign > max_int:
                return 0
        return res
    
    def solve(self):
        """Run the base test cases for the problem"""
        test_not_overflown_res = 987654321
        test_overflown_res = 2147483659
        i1 = 123
        o1 = 321
        i2 = -123
        o2 = -321
        i3 = 120
        o3 = 21
        i4 = -9999999111
        o4 = -1119999999
        i5 = 0
        o5 = 0

        expected_not_overflown_res = 123456789
        expected_overflown_res = 0

        print('Running Test Cases')
        TestCases.test_two_int_results(self.reverse(test_not_overflown_res), expected_not_overflown_res)
        TestCases.test_two_int_results(self.reverse(test_overflown_res), expected_overflown_res)
        TestCases.test_two_int_results(self.reverse(i1), o1)
        TestCases.test_two_int_results(self.reverse(i2), o2)
        TestCases.test_two_int_results(self.reverse(i3), o3)
        TestCases.test_two_int_results(self.reverse(i4), o4)
        TestCases.test_two_int_results(self.reverse(i5), o5)
