from tt.tests.utils import FunctionalTestCase


class TestKmaps(FunctionalTestCase):

    # === Error tests =========================================================

    def test_one_input_variable(self):
        # python -m tt --kmap F = A
        self.functional_test_helper(
            cl_args=['--kmap', 'F = A'],
            expected_stdout='',
            expected_stderr=(
                'ERROR: Karnaugh Map generation requires an equation of at '
                'least 2 variables.\n'))

    def test_one_input_constant(self):
        # python -m tt --kmap F = 1
        self.functional_test_helper(
            cl_args=['--kmap', 'F = 1'],
            expected_stdout='',
            expected_stderr=(
                'ERROR: Karnaugh Map generation requires an equation of at '
                'least 2 variables.\n'))

    def test_two_inputs_constant(self):
        # python -m tt --kmap F = 1 or 0
        self.functional_test_helper(
            cl_args=['--kmap', 'F = 1 or 0'],
            expected_stdout='',
            expected_stderr=(
                'ERROR: Karnaugh Map generation requires an equation of at '
                'least 2 variables.\n'))

    # === Non-error tests =====================================================

    def test_two_vars_or(self):
        # python -m tt --kmap F = A or B
        self.functional_test_helper(
            cl_args=['--kmap', 'F = A or B'],
            expected_stdout=(
                'A \\ B\n'
                '\n'
                '     0       1\n'
                '   +-------+-------+\n'
                ' 0 | 0     | 1     |\n'
                '   |   0   |   1   |\n'
                '   |       |       |\n'
                '   +-------+-------+\n'
                ' 1 | 2     | 3     |\n'
                '   |   1   |   1   |\n'
                '   |       |       |\n'
                '   +-------+-------+\n'
                '\n'),
            expected_stderr='')

    def test_two_vars_all_results_low(self):
        # python -m tt --kmap F = (A and B) and 0
        self.functional_test_helper(
            cl_args=['--kmap', 'F = (A and B) and 0'],
            expected_stdout=(
                'A \\ B\n'
                '\n'
                '     0       1\n'
                '   +-------+-------+\n'
                ' 0 | 0     | 1     |\n'
                '   |   0   |   0   |\n'
                '   |       |       |\n'
                '   +-------+-------+\n'
                ' 1 | 2     | 3     |\n'
                '   |   0   |   0   |\n'
                '   |       |       |\n'
                '   +-------+-------+\n'
                '\n'),
            expected_stderr='')

    def test_two_vars_all_results_high(self):
        # python -m tt --kmap F = (A and B) or 1
        self.functional_test_helper(
            cl_args=['--kmap', 'F = (A and B) or 1'],
            expected_stdout=(
                'A \\ B\n'
                '\n'
                '     0       1\n'
                '   +-------+-------+\n'
                ' 0 | 0     | 1     |\n'
                '   |   1   |   1   |\n'
                '   |       |       |\n'
                '   +-------+-------+\n'
                ' 1 | 2     | 3     |\n'
                '   |   1   |   1   |\n'
                '   |       |       |\n'
                '   +-------+-------+\n'
                '\n'),
            expected_stderr='')

    def test_three_vars(self):
        # python -m tt --kmap F = (op1 xor op2) and op3
        self.functional_test_helper(
            cl_args=['--kmap', 'F = (op1 xor op2) and op3'],
            expected_stdout=(
                'op1 \ op2 op3\n'
                '\n'
                '     00      01      11      10\n'
                '   +-------+-------+-------+-------+\n'
                ' 0 | 0     | 1     | 3     | 2     |\n'
                '   |   0   |   0   |   1   |   0   |\n'
                '   |       |       |       |       |\n'
                '   +-------+-------+-------+-------+\n'
                ' 1 | 4     | 5     | 7     | 6     |\n'
                '   |   0   |   1   |   0   |   0   |\n'
                '   |       |       |       |       |\n'
                '   +-------+-------+-------+-------+\n'
                '\n'
                ),
            expected_stderr='')

    def test_four_vars(self):
        # python -m tt --kmap F = (op1 nand op2) or (op3 xor op4)
        self.functional_test_helper(
            cl_args=['--kmap', 'F = (op1 nand op2) or (op3 xor op4)'],
            expected_stdout=(
                'op1 op2 \ op3 op4\n'
                '\n'
                '      00      01      11      10\n'
                '    +-------+-------+-------+-------+\n'
                ' 00 | 0     | 1     | 3     | 2     |\n'
                '    |   1   |   1   |   1   |   1   |\n'
                '    |       |       |       |       |\n'
                '    +-------+-------+-------+-------+\n'
                ' 01 | 4     | 5     | 7     | 6     |\n'
                '    |   1   |   1   |   1   |   1   |\n'
                '    |       |       |       |       |\n'
                '    +-------+-------+-------+-------+\n'
                ' 11 | 12    | 13    | 15    | 14    |\n'
                '    |   0   |   1   |   0   |   1   |\n'
                '    |       |       |       |       |\n'
                '    +-------+-------+-------+-------+\n'
                ' 10 | 8     | 9     | 11    | 10    |\n'
                '    |   1   |   1   |   1   |   1   |\n'
                '    |       |       |       |       |\n'
                '    +-------+-------+-------+-------+\n'
                '\n'
                ),
            expected_stderr='')

    def test_five_vars(self):
        # python -m tt --kmap "F = (A xor ~B) and ((C or D) xnor E)"
        self.functional_test_helper(
            cl_args=['--kmap', 'F = (A xor ~B) and ((C or D) xnor E)'],
            expected_stdout=(
                'A B \ C D E\n'
                '\n'
                '      000     001     011     010     110     111     101     100\n'  # noqa
                '    +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 00 | 0     | 1     | 3     | 2     | 6     | 7     | 5     | 4     |\n'  # noqa
                '    |   1   |   0   |   1   |   0   |   0   |   1   |   1   |   0   |\n'  # noqa
                '    |       |       |       |       |       |       |       |       |\n'  # noqa
                '    +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 01 | 8     | 9     | 11    | 10    | 14    | 15    | 13    | 12    |\n'  # noqa
                '    |   0   |   0   |   0   |   0   |   0   |   0   |   0   |   0   |\n'  # noqa
                '    |       |       |       |       |       |       |       |       |\n'  # noqa
                '    +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 11 | 24    | 25    | 27    | 26    | 30    | 31    | 29    | 28    |\n'  # noqa
                '    |   1   |   0   |   1   |   0   |   0   |   1   |   1   |   0   |\n'  # noqa
                '    |       |       |       |       |       |       |       |       |\n'  # noqa
                '    +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 10 | 16    | 17    | 19    | 18    | 22    | 23    | 21    | 20    |\n'  # noqa
                '    |   0   |   0   |   0   |   0   |   0   |   0   |   0   |   0   |\n'  # noqa
                '    |       |       |       |       |       |       |       |       |\n'  # noqa
                '    +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                '\n'
                ),
            expected_stderr='')

    def test_six_vars(self):
        # python -m tt --kmap "out = op1 or op2 or op3 or op4 or op5 or op6"
        self.functional_test_helper(
            cl_args=['--kmap', 'out = op1 or op2 or op3 or op4 or op5 or op6'],
            expected_stdout=(
                'op1 op2 op3 \ op4 op5 op6\n'
                '\n'
                '       000     001     011     010     110     111     101     100\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 000 | 0     | 1     | 3     | 2     | 6     | 7     | 5     | 4     |\n'  # noqa
                '     |   0   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 001 | 8     | 9     | 11    | 10    | 14    | 15    | 13    | 12    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 011 | 24    | 25    | 27    | 26    | 30    | 31    | 29    | 28    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 010 | 16    | 17    | 19    | 18    | 22    | 23    | 21    | 20    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 110 | 48    | 49    | 51    | 50    | 54    | 55    | 53    | 52    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 111 | 56    | 57    | 59    | 58    | 62    | 63    | 61    | 60    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 101 | 40    | 41    | 43    | 42    | 46    | 47    | 45    | 44    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 100 | 32    | 33    | 35    | 34    | 38    | 39    | 37    | 36    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                '\n'
                ),
            expected_stderr='')

    def test_seven_vars(self):
        # python -m tt --kmap "F = A or B or C or D nand (E xor G) or H"
        self.functional_test_helper(
            cl_args=['--kmap', 'F = A or B or C or D nand (E xor G) or H'],
            expected_stdout=(
                'A B C \ D E G H\n'
                '\n'
                '       0000    0001    0011    0010    0110    0111    0101    0100    1100    1101    1111    1110    1010    1011    1001    1000\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 000 | 0     | 1     | 3     | 2     | 6     | 7     | 5     | 4     | 12    | 13    | 15    | 14    | 10    | 11    | 9     | 8     |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   0   |   1   |   1   |   1   |   0   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 001 | 16    | 17    | 19    | 18    | 22    | 23    | 21    | 20    | 28    | 29    | 31    | 30    | 26    | 27    | 25    | 24    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 011 | 48    | 49    | 51    | 50    | 54    | 55    | 53    | 52    | 60    | 61    | 63    | 62    | 58    | 59    | 57    | 56    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 010 | 32    | 33    | 35    | 34    | 38    | 39    | 37    | 36    | 44    | 45    | 47    | 46    | 42    | 43    | 41    | 40    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 110 | 96    | 97    | 99    | 98    | 102   | 103   | 101   | 100   | 108   | 109   | 111   | 110   | 106   | 107   | 105   | 104   |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 111 | 112   | 113   | 115   | 114   | 118   | 119   | 117   | 116   | 124   | 125   | 127   | 126   | 122   | 123   | 121   | 120   |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 101 | 80    | 81    | 83    | 82    | 86    | 87    | 85    | 84    | 92    | 93    | 95    | 94    | 90    | 91    | 89    | 88    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 100 | 64    | 65    | 67    | 66    | 70    | 71    | 69    | 68    | 76    | 77    | 79    | 78    | 74    | 75    | 73    | 72    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                '\n'
                ),
            expected_stderr='')

d8b15e7cdaf4d1419d962375d4fc48b9438a1862
<mask token>


class TestKmaps(FunctionalTestCase):
    <mask token>

    def test_one_input_constant(self):
        self.functional_test_helper(cl_args=['--kmap', 'F = 1'],
            expected_stdout='', expected_stderr=
            """ERROR: Karnaugh Map generation requires an equation of at least 2 variables.
"""
            )
    <mask token>

    def test_two_vars_or(self):
        self.functional_test_helper(cl_args=['--kmap', 'F = A or B'],
            expected_stdout=
            """A \\ B

     0       1
   +-------+-------+
 0 | 0     | 1     |
   |   0   |   1   |
   |       |       |
   +-------+-------+
 1 | 2     | 3     |
   |   1   |   1   |
   |       |       |
   +-------+-------+

"""
            , expected_stderr='')

    def test_two_vars_all_results_low(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = (A and B) and 0'], expected_stdout=
            """A \\ B

     0       1
   +-------+-------+
 0 | 0     | 1     |
   |   0   |   0   |
   |       |       |
   +-------+-------+
 1 | 2     | 3     |
   |   0   |   0   |
   |       |       |
   +-------+-------+

"""
            , expected_stderr='')

    def test_two_vars_all_results_high(self):
        self.functional_test_helper(cl_args=['--kmap', 'F = (A and B) or 1'
            ], expected_stdout=
            """A \\ B

     0       1
   +-------+-------+
 0 | 0     | 1     |
   |   1   |   1   |
   |       |       |
   +-------+-------+
 1 | 2     | 3     |
   |   1   |   1   |
   |       |       |
   +-------+-------+

"""
            , expected_stderr='')

    def test_three_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = (op1 xor op2) and op3'], expected_stdout=
            """op1 \\ op2 op3

     00      01      11      10
   +-------+-------+-------+-------+
 0 | 0     | 1     | 3     | 2     |
   |   0   |   0   |   1   |   0   |
   |       |       |       |       |
   +-------+-------+-------+-------+
 1 | 4     | 5     | 7     | 6     |
   |   0   |   1   |   0   |   0   |
   |       |       |       |       |
   +-------+-------+-------+-------+

"""
            , expected_stderr='')

    def test_four_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = (op1 nand op2) or (op3 xor op4)'], expected_stdout=
            """op1 op2 \\ op3 op4

      00      01      11      10
    +-------+-------+-------+-------+
 00 | 0     | 1     | 3     | 2     |
    |   1   |   1   |   1   |   1   |
    |       |       |       |       |
    +-------+-------+-------+-------+
 01 | 4     | 5     | 7     | 6     |
    |   1   |   1   |   1   |   1   |
    |       |       |       |       |
    +-------+-------+-------+-------+
 11 | 12    | 13    | 15    | 14    |
    |   0   |   1   |   0   |   1   |
    |       |       |       |       |
    +-------+-------+-------+-------+
 10 | 8     | 9     | 11    | 10    |
    |   1   |   1   |   1   |   1   |
    |       |       |       |       |
    +-------+-------+-------+-------+

"""
            , expected_stderr='')

    def test_five_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = (A xor ~B) and ((C or D) xnor E)'], expected_stdout=
            """A B \\ C D E

      000     001     011     010     110     111     101     100
    +-------+-------+-------+-------+-------+-------+-------+-------+
 00 | 0     | 1     | 3     | 2     | 6     | 7     | 5     | 4     |
    |   1   |   0   |   1   |   0   |   0   |   1   |   1   |   0   |
    |       |       |       |       |       |       |       |       |
    +-------+-------+-------+-------+-------+-------+-------+-------+
 01 | 8     | 9     | 11    | 10    | 14    | 15    | 13    | 12    |
    |   0   |   0   |   0   |   0   |   0   |   0   |   0   |   0   |
    |       |       |       |       |       |       |       |       |
    +-------+-------+-------+-------+-------+-------+-------+-------+
 11 | 24    | 25    | 27    | 26    | 30    | 31    | 29    | 28    |
    |   1   |   0   |   1   |   0   |   0   |   1   |   1   |   0   |
    |       |       |       |       |       |       |       |       |
    +-------+-------+-------+-------+-------+-------+-------+-------+
 10 | 16    | 17    | 19    | 18    | 22    | 23    | 21    | 20    |
    |   0   |   0   |   0   |   0   |   0   |   0   |   0   |   0   |
    |       |       |       |       |       |       |       |       |
    +-------+-------+-------+-------+-------+-------+-------+-------+

"""
            , expected_stderr='')

    def test_six_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'out = op1 or op2 or op3 or op4 or op5 or op6'],
            expected_stdout=
            """op1 op2 op3 \\ op4 op5 op6

       000     001     011     010     110     111     101     100
     +-------+-------+-------+-------+-------+-------+-------+-------+
 000 | 0     | 1     | 3     | 2     | 6     | 7     | 5     | 4     |
     |   0   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 001 | 8     | 9     | 11    | 10    | 14    | 15    | 13    | 12    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 011 | 24    | 25    | 27    | 26    | 30    | 31    | 29    | 28    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 010 | 16    | 17    | 19    | 18    | 22    | 23    | 21    | 20    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 110 | 48    | 49    | 51    | 50    | 54    | 55    | 53    | 52    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 111 | 56    | 57    | 59    | 58    | 62    | 63    | 61    | 60    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 101 | 40    | 41    | 43    | 42    | 46    | 47    | 45    | 44    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 100 | 32    | 33    | 35    | 34    | 38    | 39    | 37    | 36    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+

"""
            , expected_stderr='')

    def test_seven_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = A or B or C or D nand (E xor G) or H'], expected_stdout=
            """A B C \\ D E G H

       0000    0001    0011    0010    0110    0111    0101    0100    1100    1101    1111    1110    1010    1011    1001    1000
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 000 | 0     | 1     | 3     | 2     | 6     | 7     | 5     | 4     | 12    | 13    | 15    | 14    | 10    | 11    | 9     | 8     |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   0   |   1   |   1   |   1   |   0   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 001 | 16    | 17    | 19    | 18    | 22    | 23    | 21    | 20    | 28    | 29    | 31    | 30    | 26    | 27    | 25    | 24    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 011 | 48    | 49    | 51    | 50    | 54    | 55    | 53    | 52    | 60    | 61    | 63    | 62    | 58    | 59    | 57    | 56    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 010 | 32    | 33    | 35    | 34    | 38    | 39    | 37    | 36    | 44    | 45    | 47    | 46    | 42    | 43    | 41    | 40    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 110 | 96    | 97    | 99    | 98    | 102   | 103   | 101   | 100   | 108   | 109   | 111   | 110   | 106   | 107   | 105   | 104   |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 111 | 112   | 113   | 115   | 114   | 118   | 119   | 117   | 116   | 124   | 125   | 127   | 126   | 122   | 123   | 121   | 120   |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 101 | 80    | 81    | 83    | 82    | 86    | 87    | 85    | 84    | 92    | 93    | 95    | 94    | 90    | 91    | 89    | 88    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 100 | 64    | 65    | 67    | 66    | 70    | 71    | 69    | 68    | 76    | 77    | 79    | 78    | 74    | 75    | 73    | 72    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+

"""
            , expected_stderr='')

<mask token>


class TestKmaps(FunctionalTestCase):

    def test_one_input_variable(self):
        self.functional_test_helper(cl_args=['--kmap', 'F = A'],
            expected_stdout='', expected_stderr=
            """ERROR: Karnaugh Map generation requires an equation of at least 2 variables.
"""
            )

    def test_one_input_constant(self):
        self.functional_test_helper(cl_args=['--kmap', 'F = 1'],
            expected_stdout='', expected_stderr=
            """ERROR: Karnaugh Map generation requires an equation of at least 2 variables.
"""
            )
    <mask token>

    def test_two_vars_or(self):
        self.functional_test_helper(cl_args=['--kmap', 'F = A or B'],
            expected_stdout=
            """A \\ B

     0       1
   +-------+-------+
 0 | 0     | 1     |
   |   0   |   1   |
   |       |       |
   +-------+-------+
 1 | 2     | 3     |
   |   1   |   1   |
   |       |       |
   +-------+-------+

"""
            , expected_stderr='')

    def test_two_vars_all_results_low(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = (A and B) and 0'], expected_stdout=
            """A \\ B

     0       1
   +-------+-------+
 0 | 0     | 1     |
   |   0   |   0   |
   |       |       |
   +-------+-------+
 1 | 2     | 3     |
   |   0   |   0   |
   |       |       |
   +-------+-------+

"""
            , expected_stderr='')

    def test_two_vars_all_results_high(self):
        self.functional_test_helper(cl_args=['--kmap', 'F = (A and B) or 1'
            ], expected_stdout=
            """A \\ B

     0       1
   +-------+-------+
 0 | 0     | 1     |
   |   1   |   1   |
   |       |       |
   +-------+-------+
 1 | 2     | 3     |
   |   1   |   1   |
   |       |       |
   +-------+-------+

"""
            , expected_stderr='')

    def test_three_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = (op1 xor op2) and op3'], expected_stdout=
            """op1 \\ op2 op3

     00      01      11      10
   +-------+-------+-------+-------+
 0 | 0     | 1     | 3     | 2     |
   |   0   |   0   |   1   |   0   |
   |       |       |       |       |
   +-------+-------+-------+-------+
 1 | 4     | 5     | 7     | 6     |
   |   0   |   1   |   0   |   0   |
   |       |       |       |       |
   +-------+-------+-------+-------+

"""
            , expected_stderr='')

    def test_four_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = (op1 nand op2) or (op3 xor op4)'], expected_stdout=
            """op1 op2 \\ op3 op4

      00      01      11      10
    +-------+-------+-------+-------+
 00 | 0     | 1     | 3     | 2     |
    |   1   |   1   |   1   |   1   |
    |       |       |       |       |
    +-------+-------+-------+-------+
 01 | 4     | 5     | 7     | 6     |
    |   1   |   1   |   1   |   1   |
    |       |       |       |       |
    +-------+-------+-------+-------+
 11 | 12    | 13    | 15    | 14    |
    |   0   |   1   |   0   |   1   |
    |       |       |       |       |
    +-------+-------+-------+-------+
 10 | 8     | 9     | 11    | 10    |
    |   1   |   1   |   1   |   1   |
    |       |       |       |       |
    +-------+-------+-------+-------+

"""
            , expected_stderr='')

    def test_five_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = (A xor ~B) and ((C or D) xnor E)'], expected_stdout=
            """A B \\ C D E

      000     001     011     010     110     111     101     100
    +-------+-------+-------+-------+-------+-------+-------+-------+
 00 | 0     | 1     | 3     | 2     | 6     | 7     | 5     | 4     |
    |   1   |   0   |   1   |   0   |   0   |   1   |   1   |   0   |
    |       |       |       |       |       |       |       |       |
    +-------+-------+-------+-------+-------+-------+-------+-------+
 01 | 8     | 9     | 11    | 10    | 14    | 15    | 13    | 12    |
    |   0   |   0   |   0   |   0   |   0   |   0   |   0   |   0   |
    |       |       |       |       |       |       |       |       |
    +-------+-------+-------+-------+-------+-------+-------+-------+
 11 | 24    | 25    | 27    | 26    | 30    | 31    | 29    | 28    |
    |   1   |   0   |   1   |   0   |   0   |   1   |   1   |   0   |
    |       |       |       |       |       |       |       |       |
    +-------+-------+-------+-------+-------+-------+-------+-------+
 10 | 16    | 17    | 19    | 18    | 22    | 23    | 21    | 20    |
    |   0   |   0   |   0   |   0   |   0   |   0   |   0   |   0   |
    |       |       |       |       |       |       |       |       |
    +-------+-------+-------+-------+-------+-------+-------+-------+

"""
            , expected_stderr='')

    def test_six_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'out = op1 or op2 or op3 or op4 or op5 or op6'],
            expected_stdout=
            """op1 op2 op3 \\ op4 op5 op6

       000     001     011     010     110     111     101     100
     +-------+-------+-------+-------+-------+-------+-------+-------+
 000 | 0     | 1     | 3     | 2     | 6     | 7     | 5     | 4     |
     |   0   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 001 | 8     | 9     | 11    | 10    | 14    | 15    | 13    | 12    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 011 | 24    | 25    | 27    | 26    | 30    | 31    | 29    | 28    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 010 | 16    | 17    | 19    | 18    | 22    | 23    | 21    | 20    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 110 | 48    | 49    | 51    | 50    | 54    | 55    | 53    | 52    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 111 | 56    | 57    | 59    | 58    | 62    | 63    | 61    | 60    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 101 | 40    | 41    | 43    | 42    | 46    | 47    | 45    | 44    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 100 | 32    | 33    | 35    | 34    | 38    | 39    | 37    | 36    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+

"""
            , expected_stderr='')

    def test_seven_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = A or B or C or D nand (E xor G) or H'], expected_stdout=
            """A B C \\ D E G H

       0000    0001    0011    0010    0110    0111    0101    0100    1100    1101    1111    1110    1010    1011    1001    1000
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 000 | 0     | 1     | 3     | 2     | 6     | 7     | 5     | 4     | 12    | 13    | 15    | 14    | 10    | 11    | 9     | 8     |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   0   |   1   |   1   |   1   |   0   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 001 | 16    | 17    | 19    | 18    | 22    | 23    | 21    | 20    | 28    | 29    | 31    | 30    | 26    | 27    | 25    | 24    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 011 | 48    | 49    | 51    | 50    | 54    | 55    | 53    | 52    | 60    | 61    | 63    | 62    | 58    | 59    | 57    | 56    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 010 | 32    | 33    | 35    | 34    | 38    | 39    | 37    | 36    | 44    | 45    | 47    | 46    | 42    | 43    | 41    | 40    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 110 | 96    | 97    | 99    | 98    | 102   | 103   | 101   | 100   | 108   | 109   | 111   | 110   | 106   | 107   | 105   | 104   |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 111 | 112   | 113   | 115   | 114   | 118   | 119   | 117   | 116   | 124   | 125   | 127   | 126   | 122   | 123   | 121   | 120   |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 101 | 80    | 81    | 83    | 82    | 86    | 87    | 85    | 84    | 92    | 93    | 95    | 94    | 90    | 91    | 89    | 88    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 100 | 64    | 65    | 67    | 66    | 70    | 71    | 69    | 68    | 76    | 77    | 79    | 78    | 74    | 75    | 73    | 72    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+

"""
            , expected_stderr='')

<mask token>


class TestKmaps(FunctionalTestCase):

    def test_one_input_variable(self):
        self.functional_test_helper(cl_args=['--kmap', 'F = A'],
            expected_stdout='', expected_stderr=
            """ERROR: Karnaugh Map generation requires an equation of at least 2 variables.
"""
            )

    def test_one_input_constant(self):
        self.functional_test_helper(cl_args=['--kmap', 'F = 1'],
            expected_stdout='', expected_stderr=
            """ERROR: Karnaugh Map generation requires an equation of at least 2 variables.
"""
            )

    def test_two_inputs_constant(self):
        self.functional_test_helper(cl_args=['--kmap', 'F = 1 or 0'],
            expected_stdout='', expected_stderr=
            """ERROR: Karnaugh Map generation requires an equation of at least 2 variables.
"""
            )

    def test_two_vars_or(self):
        self.functional_test_helper(cl_args=['--kmap', 'F = A or B'],
            expected_stdout=
            """A \\ B

     0       1
   +-------+-------+
 0 | 0     | 1     |
   |   0   |   1   |
   |       |       |
   +-------+-------+
 1 | 2     | 3     |
   |   1   |   1   |
   |       |       |
   +-------+-------+

"""
            , expected_stderr='')

    def test_two_vars_all_results_low(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = (A and B) and 0'], expected_stdout=
            """A \\ B

     0       1
   +-------+-------+
 0 | 0     | 1     |
   |   0   |   0   |
   |       |       |
   +-------+-------+
 1 | 2     | 3     |
   |   0   |   0   |
   |       |       |
   +-------+-------+

"""
            , expected_stderr='')

    def test_two_vars_all_results_high(self):
        self.functional_test_helper(cl_args=['--kmap', 'F = (A and B) or 1'
            ], expected_stdout=
            """A \\ B

     0       1
   +-------+-------+
 0 | 0     | 1     |
   |   1   |   1   |
   |       |       |
   +-------+-------+
 1 | 2     | 3     |
   |   1   |   1   |
   |       |       |
   +-------+-------+

"""
            , expected_stderr='')

    def test_three_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = (op1 xor op2) and op3'], expected_stdout=
            """op1 \\ op2 op3

     00      01      11      10
   +-------+-------+-------+-------+
 0 | 0     | 1     | 3     | 2     |
   |   0   |   0   |   1   |   0   |
   |       |       |       |       |
   +-------+-------+-------+-------+
 1 | 4     | 5     | 7     | 6     |
   |   0   |   1   |   0   |   0   |
   |       |       |       |       |
   +-------+-------+-------+-------+

"""
            , expected_stderr='')

    def test_four_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = (op1 nand op2) or (op3 xor op4)'], expected_stdout=
            """op1 op2 \\ op3 op4

      00      01      11      10
    +-------+-------+-------+-------+
 00 | 0     | 1     | 3     | 2     |
    |   1   |   1   |   1   |   1   |
    |       |       |       |       |
    +-------+-------+-------+-------+
 01 | 4     | 5     | 7     | 6     |
    |   1   |   1   |   1   |   1   |
    |       |       |       |       |
    +-------+-------+-------+-------+
 11 | 12    | 13    | 15    | 14    |
    |   0   |   1   |   0   |   1   |
    |       |       |       |       |
    +-------+-------+-------+-------+
 10 | 8     | 9     | 11    | 10    |
    |   1   |   1   |   1   |   1   |
    |       |       |       |       |
    +-------+-------+-------+-------+

"""
            , expected_stderr='')

    def test_five_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = (A xor ~B) and ((C or D) xnor E)'], expected_stdout=
            """A B \\ C D E

      000     001     011     010     110     111     101     100
    +-------+-------+-------+-------+-------+-------+-------+-------+
 00 | 0     | 1     | 3     | 2     | 6     | 7     | 5     | 4     |
    |   1   |   0   |   1   |   0   |   0   |   1   |   1   |   0   |
    |       |       |       |       |       |       |       |       |
    +-------+-------+-------+-------+-------+-------+-------+-------+
 01 | 8     | 9     | 11    | 10    | 14    | 15    | 13    | 12    |
    |   0   |   0   |   0   |   0   |   0   |   0   |   0   |   0   |
    |       |       |       |       |       |       |       |       |
    +-------+-------+-------+-------+-------+-------+-------+-------+
 11 | 24    | 25    | 27    | 26    | 30    | 31    | 29    | 28    |
    |   1   |   0   |   1   |   0   |   0   |   1   |   1   |   0   |
    |       |       |       |       |       |       |       |       |
    +-------+-------+-------+-------+-------+-------+-------+-------+
 10 | 16    | 17    | 19    | 18    | 22    | 23    | 21    | 20    |
    |   0   |   0   |   0   |   0   |   0   |   0   |   0   |   0   |
    |       |       |       |       |       |       |       |       |
    +-------+-------+-------+-------+-------+-------+-------+-------+

"""
            , expected_stderr='')

    def test_six_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'out = op1 or op2 or op3 or op4 or op5 or op6'],
            expected_stdout=
            """op1 op2 op3 \\ op4 op5 op6

       000     001     011     010     110     111     101     100
     +-------+-------+-------+-------+-------+-------+-------+-------+
 000 | 0     | 1     | 3     | 2     | 6     | 7     | 5     | 4     |
     |   0   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 001 | 8     | 9     | 11    | 10    | 14    | 15    | 13    | 12    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 011 | 24    | 25    | 27    | 26    | 30    | 31    | 29    | 28    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 010 | 16    | 17    | 19    | 18    | 22    | 23    | 21    | 20    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 110 | 48    | 49    | 51    | 50    | 54    | 55    | 53    | 52    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 111 | 56    | 57    | 59    | 58    | 62    | 63    | 61    | 60    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 101 | 40    | 41    | 43    | 42    | 46    | 47    | 45    | 44    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 100 | 32    | 33    | 35    | 34    | 38    | 39    | 37    | 36    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+

"""
            , expected_stderr='')

    def test_seven_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = A or B or C or D nand (E xor G) or H'], expected_stdout=
            """A B C \\ D E G H

       0000    0001    0011    0010    0110    0111    0101    0100    1100    1101    1111    1110    1010    1011    1001    1000
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 000 | 0     | 1     | 3     | 2     | 6     | 7     | 5     | 4     | 12    | 13    | 15    | 14    | 10    | 11    | 9     | 8     |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   0   |   1   |   1   |   1   |   0   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 001 | 16    | 17    | 19    | 18    | 22    | 23    | 21    | 20    | 28    | 29    | 31    | 30    | 26    | 27    | 25    | 24    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 011 | 48    | 49    | 51    | 50    | 54    | 55    | 53    | 52    | 60    | 61    | 63    | 62    | 58    | 59    | 57    | 56    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 010 | 32    | 33    | 35    | 34    | 38    | 39    | 37    | 36    | 44    | 45    | 47    | 46    | 42    | 43    | 41    | 40    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 110 | 96    | 97    | 99    | 98    | 102   | 103   | 101   | 100   | 108   | 109   | 111   | 110   | 106   | 107   | 105   | 104   |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 111 | 112   | 113   | 115   | 114   | 118   | 119   | 117   | 116   | 124   | 125   | 127   | 126   | 122   | 123   | 121   | 120   |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 101 | 80    | 81    | 83    | 82    | 86    | 87    | 85    | 84    | 92    | 93    | 95    | 94    | 90    | 91    | 89    | 88    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 100 | 64    | 65    | 67    | 66    | 70    | 71    | 69    | 68    | 76    | 77    | 79    | 78    | 74    | 75    | 73    | 72    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+

"""
            , expected_stderr='')

from tt.tests.utils import FunctionalTestCase


class TestKmaps(FunctionalTestCase):

    def test_one_input_variable(self):
        self.functional_test_helper(cl_args=['--kmap', 'F = A'],
            expected_stdout='', expected_stderr=
            """ERROR: Karnaugh Map generation requires an equation of at least 2 variables.
"""
            )

    def test_one_input_constant(self):
        self.functional_test_helper(cl_args=['--kmap', 'F = 1'],
            expected_stdout='', expected_stderr=
            """ERROR: Karnaugh Map generation requires an equation of at least 2 variables.
"""
            )

    def test_two_inputs_constant(self):
        self.functional_test_helper(cl_args=['--kmap', 'F = 1 or 0'],
            expected_stdout='', expected_stderr=
            """ERROR: Karnaugh Map generation requires an equation of at least 2 variables.
"""
            )

    def test_two_vars_or(self):
        self.functional_test_helper(cl_args=['--kmap', 'F = A or B'],
            expected_stdout=
            """A \\ B

     0       1
   +-------+-------+
 0 | 0     | 1     |
   |   0   |   1   |
   |       |       |
   +-------+-------+
 1 | 2     | 3     |
   |   1   |   1   |
   |       |       |
   +-------+-------+

"""
            , expected_stderr='')

    def test_two_vars_all_results_low(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = (A and B) and 0'], expected_stdout=
            """A \\ B

     0       1
   +-------+-------+
 0 | 0     | 1     |
   |   0   |   0   |
   |       |       |
   +-------+-------+
 1 | 2     | 3     |
   |   0   |   0   |
   |       |       |
   +-------+-------+

"""
            , expected_stderr='')

    def test_two_vars_all_results_high(self):
        self.functional_test_helper(cl_args=['--kmap', 'F = (A and B) or 1'
            ], expected_stdout=
            """A \\ B

     0       1
   +-------+-------+
 0 | 0     | 1     |
   |   1   |   1   |
   |       |       |
   +-------+-------+
 1 | 2     | 3     |
   |   1   |   1   |
   |       |       |
   +-------+-------+

"""
            , expected_stderr='')

    def test_three_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = (op1 xor op2) and op3'], expected_stdout=
            """op1 \\ op2 op3

     00      01      11      10
   +-------+-------+-------+-------+
 0 | 0     | 1     | 3     | 2     |
   |   0   |   0   |   1   |   0   |
   |       |       |       |       |
   +-------+-------+-------+-------+
 1 | 4     | 5     | 7     | 6     |
   |   0   |   1   |   0   |   0   |
   |       |       |       |       |
   +-------+-------+-------+-------+

"""
            , expected_stderr='')

    def test_four_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = (op1 nand op2) or (op3 xor op4)'], expected_stdout=
            """op1 op2 \\ op3 op4

      00      01      11      10
    +-------+-------+-------+-------+
 00 | 0     | 1     | 3     | 2     |
    |   1   |   1   |   1   |   1   |
    |       |       |       |       |
    +-------+-------+-------+-------+
 01 | 4     | 5     | 7     | 6     |
    |   1   |   1   |   1   |   1   |
    |       |       |       |       |
    +-------+-------+-------+-------+
 11 | 12    | 13    | 15    | 14    |
    |   0   |   1   |   0   |   1   |
    |       |       |       |       |
    +-------+-------+-------+-------+
 10 | 8     | 9     | 11    | 10    |
    |   1   |   1   |   1   |   1   |
    |       |       |       |       |
    +-------+-------+-------+-------+

"""
            , expected_stderr='')

    def test_five_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = (A xor ~B) and ((C or D) xnor E)'], expected_stdout=
            """A B \\ C D E

      000     001     011     010     110     111     101     100
    +-------+-------+-------+-------+-------+-------+-------+-------+
 00 | 0     | 1     | 3     | 2     | 6     | 7     | 5     | 4     |
    |   1   |   0   |   1   |   0   |   0   |   1   |   1   |   0   |
    |       |       |       |       |       |       |       |       |
    +-------+-------+-------+-------+-------+-------+-------+-------+
 01 | 8     | 9     | 11    | 10    | 14    | 15    | 13    | 12    |
    |   0   |   0   |   0   |   0   |   0   |   0   |   0   |   0   |
    |       |       |       |       |       |       |       |       |
    +-------+-------+-------+-------+-------+-------+-------+-------+
 11 | 24    | 25    | 27    | 26    | 30    | 31    | 29    | 28    |
    |   1   |   0   |   1   |   0   |   0   |   1   |   1   |   0   |
    |       |       |       |       |       |       |       |       |
    +-------+-------+-------+-------+-------+-------+-------+-------+
 10 | 16    | 17    | 19    | 18    | 22    | 23    | 21    | 20    |
    |   0   |   0   |   0   |   0   |   0   |   0   |   0   |   0   |
    |       |       |       |       |       |       |       |       |
    +-------+-------+-------+-------+-------+-------+-------+-------+

"""
            , expected_stderr='')

    def test_six_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'out = op1 or op2 or op3 or op4 or op5 or op6'],
            expected_stdout=
            """op1 op2 op3 \\ op4 op5 op6

       000     001     011     010     110     111     101     100
     +-------+-------+-------+-------+-------+-------+-------+-------+
 000 | 0     | 1     | 3     | 2     | 6     | 7     | 5     | 4     |
     |   0   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 001 | 8     | 9     | 11    | 10    | 14    | 15    | 13    | 12    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 011 | 24    | 25    | 27    | 26    | 30    | 31    | 29    | 28    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 010 | 16    | 17    | 19    | 18    | 22    | 23    | 21    | 20    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 110 | 48    | 49    | 51    | 50    | 54    | 55    | 53    | 52    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 111 | 56    | 57    | 59    | 58    | 62    | 63    | 61    | 60    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 101 | 40    | 41    | 43    | 42    | 46    | 47    | 45    | 44    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+
 100 | 32    | 33    | 35    | 34    | 38    | 39    | 37    | 36    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+

"""
            , expected_stderr='')

    def test_seven_vars(self):
        self.functional_test_helper(cl_args=['--kmap',
            'F = A or B or C or D nand (E xor G) or H'], expected_stdout=
            """A B C \\ D E G H

       0000    0001    0011    0010    0110    0111    0101    0100    1100    1101    1111    1110    1010    1011    1001    1000
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 000 | 0     | 1     | 3     | 2     | 6     | 7     | 5     | 4     | 12    | 13    | 15    | 14    | 10    | 11    | 9     | 8     |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   0   |   1   |   1   |   1   |   0   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 001 | 16    | 17    | 19    | 18    | 22    | 23    | 21    | 20    | 28    | 29    | 31    | 30    | 26    | 27    | 25    | 24    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 011 | 48    | 49    | 51    | 50    | 54    | 55    | 53    | 52    | 60    | 61    | 63    | 62    | 58    | 59    | 57    | 56    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 010 | 32    | 33    | 35    | 34    | 38    | 39    | 37    | 36    | 44    | 45    | 47    | 46    | 42    | 43    | 41    | 40    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 110 | 96    | 97    | 99    | 98    | 102   | 103   | 101   | 100   | 108   | 109   | 111   | 110   | 106   | 107   | 105   | 104   |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 111 | 112   | 113   | 115   | 114   | 118   | 119   | 117   | 116   | 124   | 125   | 127   | 126   | 122   | 123   | 121   | 120   |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 101 | 80    | 81    | 83    | 82    | 86    | 87    | 85    | 84    | 92    | 93    | 95    | 94    | 90    | 91    | 89    | 88    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
 100 | 64    | 65    | 67    | 66    | 70    | 71    | 69    | 68    | 76    | 77    | 79    | 78    | 74    | 75    | 73    | 72    |
     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |
     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |
     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+

"""
            , expected_stderr='')

from tt.tests.utils import FunctionalTestCase


class TestKmaps(FunctionalTestCase):

    # === Error tests =========================================================

    def test_one_input_variable(self):
        # python -m tt --kmap F = A
        self.functional_test_helper(
            cl_args=['--kmap', 'F = A'],
            expected_stdout='',
            expected_stderr=(
                'ERROR: Karnaugh Map generation requires an equation of at '
                'least 2 variables.\n'))

    def test_one_input_constant(self):
        # python -m tt --kmap F = 1
        self.functional_test_helper(
            cl_args=['--kmap', 'F = 1'],
            expected_stdout='',
            expected_stderr=(
                'ERROR: Karnaugh Map generation requires an equation of at '
                'least 2 variables.\n'))

    def test_two_inputs_constant(self):
        # python -m tt --kmap F = 1 or 0
        self.functional_test_helper(
            cl_args=['--kmap', 'F = 1 or 0'],
            expected_stdout='',
            expected_stderr=(
                'ERROR: Karnaugh Map generation requires an equation of at '
                'least 2 variables.\n'))

    # === Non-error tests =====================================================

    def test_two_vars_or(self):
        # python -m tt --kmap F = A or B
        self.functional_test_helper(
            cl_args=['--kmap', 'F = A or B'],
            expected_stdout=(
                'A \\ B\n'
                '\n'
                '     0       1\n'
                '   +-------+-------+\n'
                ' 0 | 0     | 1     |\n'
                '   |   0   |   1   |\n'
                '   |       |       |\n'
                '   +-------+-------+\n'
                ' 1 | 2     | 3     |\n'
                '   |   1   |   1   |\n'
                '   |       |       |\n'
                '   +-------+-------+\n'
                '\n'),
            expected_stderr='')

    def test_two_vars_all_results_low(self):
        # python -m tt --kmap F = (A and B) and 0
        self.functional_test_helper(
            cl_args=['--kmap', 'F = (A and B) and 0'],
            expected_stdout=(
                'A \\ B\n'
                '\n'
                '     0       1\n'
                '   +-------+-------+\n'
                ' 0 | 0     | 1     |\n'
                '   |   0   |   0   |\n'
                '   |       |       |\n'
                '   +-------+-------+\n'
                ' 1 | 2     | 3     |\n'
                '   |   0   |   0   |\n'
                '   |       |       |\n'
                '   +-------+-------+\n'
                '\n'),
            expected_stderr='')

    def test_two_vars_all_results_high(self):
        # python -m tt --kmap F = (A and B) or 1
        self.functional_test_helper(
            cl_args=['--kmap', 'F = (A and B) or 1'],
            expected_stdout=(
                'A \\ B\n'
                '\n'
                '     0       1\n'
                '   +-------+-------+\n'
                ' 0 | 0     | 1     |\n'
                '   |   1   |   1   |\n'
                '   |       |       |\n'
                '   +-------+-------+\n'
                ' 1 | 2     | 3     |\n'
                '   |   1   |   1   |\n'
                '   |       |       |\n'
                '   +-------+-------+\n'
                '\n'),
            expected_stderr='')

    def test_three_vars(self):
        # python -m tt --kmap F = (op1 xor op2) and op3
        self.functional_test_helper(
            cl_args=['--kmap', 'F = (op1 xor op2) and op3'],
            expected_stdout=(
                'op1 \ op2 op3\n'
                '\n'
                '     00      01      11      10\n'
                '   +-------+-------+-------+-------+\n'
                ' 0 | 0     | 1     | 3     | 2     |\n'
                '   |   0   |   0   |   1   |   0   |\n'
                '   |       |       |       |       |\n'
                '   +-------+-------+-------+-------+\n'
                ' 1 | 4     | 5     | 7     | 6     |\n'
                '   |   0   |   1   |   0   |   0   |\n'
                '   |       |       |       |       |\n'
                '   +-------+-------+-------+-------+\n'
                '\n'
                ),
            expected_stderr='')

    def test_four_vars(self):
        # python -m tt --kmap F = (op1 nand op2) or (op3 xor op4)
        self.functional_test_helper(
            cl_args=['--kmap', 'F = (op1 nand op2) or (op3 xor op4)'],
            expected_stdout=(
                'op1 op2 \ op3 op4\n'
                '\n'
                '      00      01      11      10\n'
                '    +-------+-------+-------+-------+\n'
                ' 00 | 0     | 1     | 3     | 2     |\n'
                '    |   1   |   1   |   1   |   1   |\n'
                '    |       |       |       |       |\n'
                '    +-------+-------+-------+-------+\n'
                ' 01 | 4     | 5     | 7     | 6     |\n'
                '    |   1   |   1   |   1   |   1   |\n'
                '    |       |       |       |       |\n'
                '    +-------+-------+-------+-------+\n'
                ' 11 | 12    | 13    | 15    | 14    |\n'
                '    |   0   |   1   |   0   |   1   |\n'
                '    |       |       |       |       |\n'
                '    +-------+-------+-------+-------+\n'
                ' 10 | 8     | 9     | 11    | 10    |\n'
                '    |   1   |   1   |   1   |   1   |\n'
                '    |       |       |       |       |\n'
                '    +-------+-------+-------+-------+\n'
                '\n'
                ),
            expected_stderr='')

    def test_five_vars(self):
        # python -m tt --kmap "F = (A xor ~B) and ((C or D) xnor E)"
        self.functional_test_helper(
            cl_args=['--kmap', 'F = (A xor ~B) and ((C or D) xnor E)'],
            expected_stdout=(
                'A B \ C D E\n'
                '\n'
                '      000     001     011     010     110     111     101     100\n'  # noqa
                '    +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 00 | 0     | 1     | 3     | 2     | 6     | 7     | 5     | 4     |\n'  # noqa
                '    |   1   |   0   |   1   |   0   |   0   |   1   |   1   |   0   |\n'  # noqa
                '    |       |       |       |       |       |       |       |       |\n'  # noqa
                '    +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 01 | 8     | 9     | 11    | 10    | 14    | 15    | 13    | 12    |\n'  # noqa
                '    |   0   |   0   |   0   |   0   |   0   |   0   |   0   |   0   |\n'  # noqa
                '    |       |       |       |       |       |       |       |       |\n'  # noqa
                '    +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 11 | 24    | 25    | 27    | 26    | 30    | 31    | 29    | 28    |\n'  # noqa
                '    |   1   |   0   |   1   |   0   |   0   |   1   |   1   |   0   |\n'  # noqa
                '    |       |       |       |       |       |       |       |       |\n'  # noqa
                '    +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 10 | 16    | 17    | 19    | 18    | 22    | 23    | 21    | 20    |\n'  # noqa
                '    |   0   |   0   |   0   |   0   |   0   |   0   |   0   |   0   |\n'  # noqa
                '    |       |       |       |       |       |       |       |       |\n'  # noqa
                '    +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                '\n'
                ),
            expected_stderr='')

    def test_six_vars(self):
        # python -m tt --kmap "out = op1 or op2 or op3 or op4 or op5 or op6"
        self.functional_test_helper(
            cl_args=['--kmap', 'out = op1 or op2 or op3 or op4 or op5 or op6'],
            expected_stdout=(
                'op1 op2 op3 \ op4 op5 op6\n'
                '\n'
                '       000     001     011     010     110     111     101     100\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 000 | 0     | 1     | 3     | 2     | 6     | 7     | 5     | 4     |\n'  # noqa
                '     |   0   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 001 | 8     | 9     | 11    | 10    | 14    | 15    | 13    | 12    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 011 | 24    | 25    | 27    | 26    | 30    | 31    | 29    | 28    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 010 | 16    | 17    | 19    | 18    | 22    | 23    | 21    | 20    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 110 | 48    | 49    | 51    | 50    | 54    | 55    | 53    | 52    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 111 | 56    | 57    | 59    | 58    | 62    | 63    | 61    | 60    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 101 | 40    | 41    | 43    | 42    | 46    | 47    | 45    | 44    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 100 | 32    | 33    | 35    | 34    | 38    | 39    | 37    | 36    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                '\n'
                ),
            expected_stderr='')

    def test_seven_vars(self):
        # python -m tt --kmap "F = A or B or C or D nand (E xor G) or H"
        self.functional_test_helper(
            cl_args=['--kmap', 'F = A or B or C or D nand (E xor G) or H'],
            expected_stdout=(
                'A B C \ D E G H\n'
                '\n'
                '       0000    0001    0011    0010    0110    0111    0101    0100    1100    1101    1111    1110    1010    1011    1001    1000\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 000 | 0     | 1     | 3     | 2     | 6     | 7     | 5     | 4     | 12    | 13    | 15    | 14    | 10    | 11    | 9     | 8     |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   0   |   1   |   1   |   1   |   0   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 001 | 16    | 17    | 19    | 18    | 22    | 23    | 21    | 20    | 28    | 29    | 31    | 30    | 26    | 27    | 25    | 24    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 011 | 48    | 49    | 51    | 50    | 54    | 55    | 53    | 52    | 60    | 61    | 63    | 62    | 58    | 59    | 57    | 56    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 010 | 32    | 33    | 35    | 34    | 38    | 39    | 37    | 36    | 44    | 45    | 47    | 46    | 42    | 43    | 41    | 40    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 110 | 96    | 97    | 99    | 98    | 102   | 103   | 101   | 100   | 108   | 109   | 111   | 110   | 106   | 107   | 105   | 104   |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 111 | 112   | 113   | 115   | 114   | 118   | 119   | 117   | 116   | 124   | 125   | 127   | 126   | 122   | 123   | 121   | 120   |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 101 | 80    | 81    | 83    | 82    | 86    | 87    | 85    | 84    | 92    | 93    | 95    | 94    | 90    | 91    | 89    | 88    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                ' 100 | 64    | 65    | 67    | 66    | 70    | 71    | 69    | 68    | 76    | 77    | 79    | 78    | 74    | 75    | 73    | 72    |\n'  # noqa
                '     |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |   1   |\n'  # noqa
                '     |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |       |\n'  # noqa
                '     +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+\n'  # noqa
                '\n'
                ),
            expected_stderr='')
