def test_internal_guts_2_variables(self):
        # chromeos can be: 0 or 1.
        # OS can be: linux, mac or win.
        variables = ('chromeos', 'OS')
        values = ((0, 1), ('linux', 'mac', 'win'))

        # 1. is input data to _get_expr. It is all the possibilities that should be
        #    be matched for.
        # 2. is (bits, values) output from _get_expr, that is input in
        #    _find_reduced_cost.
        # 3. is (assertions) output from _find_reduced_cost, that is input in
        #    _format_expr.
        # 4. is string output from _format_expr.
        expectations = [
            (
                [(0, 'linux')],
                1L,
                ((1, 1), ),
                ('chromeos==0 and OS=="linux"', 2),
            ),
            (
                [(1, 'linux'), (0, 'mac')],
                6L,
                ((2, 1), (1, 2)),
                ('(chromeos==1 and OS=="linux") or (chromeos==0 and OS=="mac")',
                 2),
            ),
            (
                [(1, 'linux'), (0, 'mac'), (0, 'win')],
                22L,
                ((2, 1), (1, 6)),
                (
                    '(chromeos==1 and OS=="linux") or '
                    '(chromeos==0 and (OS=="mac" or OS=="win"))',
                    2,
                ),
            ),
        ]
        for line in expectations:
            # Note that none of these functions support free variable. It's
            # implemented by ShortExpressionFinder.get_expr(.
            to_get_expr, bits, to_format_expr, final_expected = line
            self.assertEqual(
                bits, short_expression_finder._get_expr(values, to_get_expr))
            self.assertEqual(
                to_format_expr,
                short_expression_finder._find_reduced_cost(1, 1, values, bits))
            self.assertEqual(
                final_expected,
                short_expression_finder._format_expr(variables, values,
                                                     to_format_expr))
  def test_internal_guts_2_variables(self):
    # chromeos can be: 0 or 1.
    # OS can be: linux, mac or win.
    variables = ('chromeos', 'OS')
    values = ((0, 1), ('linux', 'mac', 'win'))

    # 1. is input data to _get_expr. It is all the possibilities that should be
    #    be matched for.
    # 2. is (bits, values) output from _get_expr, that is input in
    #    _find_reduced_cost.
    # 3. is (assertions) output from _find_reduced_cost, that is input in
    #    _format_expr.
    # 4. is string output from _format_expr.
    expectations = [
        (
          [(0, 'linux')],
          1L,
          ((1, 1),),
          ('chromeos==0 and OS=="linux"', 2),
        ),
        (
          [(1, 'linux'), (0, 'mac')],
          6L,
          ((2, 1), (1, 2)),
          ('(chromeos==1 and OS=="linux") or (chromeos==0 and OS=="mac")', 2),
        ),
        (
          [(1, 'linux'), (0, 'mac'), (0, 'win')],
          22L,
          ((2, 1), (1, 6)),
          (
            '(chromeos==1 and OS=="linux") or '
            '(chromeos==0 and (OS=="mac" or OS=="win"))',
            2,
          ),
        ),
    ]
    for line in expectations:
      # Note that none of these functions support free variable. It's
      # implemented by ShortExpressionFinder.get_expr(.
      to_get_expr, bits, to_format_expr, final_expected = line
      self.assertEqual(
          bits,
          short_expression_finder._get_expr(values, to_get_expr))
      self.assertEqual(
          to_format_expr,
          short_expression_finder._find_reduced_cost(1, 1, values, bits))
      self.assertEqual(
          final_expected,
          short_expression_finder._format_expr(
            variables, values, to_format_expr))
 def test_internal_guts_4_variables(self):
     # Create a 4D matrix.
     # See test_internal_guts_2_variables for the explanations.
     variables = ('lib', 'cros', 'OS', 'brand')
     values = (('s', 'c'), (0, 1), ('l', 'm', 'w'), ('C', 'GC', 'Y'))
     expectations = [
         (
             [('s', 0, 'l', 'C')],
             1L,
             ((1, 1, 1, 1), ),
             ('lib=="s" and cros==0 and OS=="l" and brand=="C"', 4),
         ),
         (
             # 2nd value for 1th variable.
             [('c', 0, 'l', 'C')],
             2L,
             ((2, 1, 1, 1), ),
             ('lib=="c" and cros==0 and OS=="l" and brand=="C"', 4),
         ),
         (
             # 2nd value for 2th variable.
             [('s', 1, 'l', 'C')],
             4L,
             ((1, 2, 1, 1), ),
             ('lib=="s" and cros==1 and OS=="l" and brand=="C"', 4),
         ),
         (
             # 2nd value for 3th variable.
             [('s', 0, 'm', 'C')],
             16L,
             ((1, 1, 2, 1), ),
             ('lib=="s" and cros==0 and OS=="m" and brand=="C"', 4),
         ),
         (
             # 3nd value for 3th variable.
             [('s', 0, 'w', 'C')],
             256L,
             ((1, 1, 4, 1), ),  # bitfields, not numbers.
             ('lib=="s" and cros==0 and OS=="w" and brand=="C"', 4),
         ),
         (
             # 2nd value for 4th variable.
             [('s', 0, 'l', 'GC')],
             4096L,
             ((1, 1, 1, 2), ),
             ('lib=="s" and cros==0 and OS=="l" and brand=="GC"', 4),
         ),
         (
             # Last bit that can be set, all values are the last.
             # 100000000000000000000000000000000000 is 36th bit == 2*2*3*3.
             [('c', 1, 'w', 'Y')],
             34359738368L,
             ((2, 2, 4, 4), ),
             ('lib=="c" and cros==1 and OS=="w" and brand=="Y"', 4),
         ),
         (
             # Same condition twice doesn't affect the result.
             [('s', 0, 'l', 'C'), ('s', 0, 'l', 'C')],
             # One real condition, only one bit set.
             1L,
             ((1, 1, 1, 1), ),
             ('lib=="s" and cros==0 and OS=="l" and brand=="C"', 4),
         ),
         (
             # All values for 1st variable.
             [('s', 0, 'l', 'C'), ('c', 0, 'l', 'C')],
             # It has 2 bits set.
             3L,
             ((3, 1, 1, 1), ),
             ('(lib=="s" or lib=="c") and cros==0 and OS=="l" and brand=="C"',
              4),
         ),
         (
             # All values for 2nd variable.
             [('s', 0, 'l', 'C'), ('s', 1, 'l', 'C')],
             # It has 2 bits set.
             5L,
             ((1, 3, 1, 1), ),
             ('lib=="s" and (cros==0 or cros==1) and OS=="l" and brand=="C"',
              4),
         ),
     ]
     for line in expectations:
         # Note that none of these functions support free variable. It's
         # implemented by ShortExpressionFinder.get_expr().
         to_get_expr, bits, to_format_expr, final_expected = line
         self.assertEqual(
             bits, short_expression_finder._get_expr(values, to_get_expr))
         self.assertEqual(
             to_format_expr,
             short_expression_finder._find_reduced_cost(1, 1, values, bits))
         self.assertEqual(
             final_expected,
             short_expression_finder._format_expr(variables, values,
                                                  to_format_expr))
 def test_internal_guts_4_variables(self):
   # Create a 4D matrix.
   # See test_internal_guts_2_variables for the explanations.
   variables = ('lib', 'cros', 'OS', 'brand')
   values = (('s', 'c'), (0, 1), ('l', 'm', 'w'), ('C', 'GC', 'Y'))
   expectations = [
       (
         [('s', 0, 'l', 'C')],
         1L,
         ((1, 1, 1, 1),),
         ('lib=="s" and cros==0 and OS=="l" and brand=="C"', 4),
       ),
       (
         # 2nd value for 1th variable.
         [('c', 0, 'l', 'C')],
         2L,
         ((2, 1, 1, 1),),
         ('lib=="c" and cros==0 and OS=="l" and brand=="C"', 4),
       ),
       (
         # 2nd value for 2th variable.
         [('s', 1, 'l', 'C')],
         4L,
         ((1, 2, 1, 1),),
         ('lib=="s" and cros==1 and OS=="l" and brand=="C"', 4),
       ),
       (
         # 2nd value for 3th variable.
         [('s', 0, 'm', 'C')],
         16L,
         ((1, 1, 2, 1),),
         ('lib=="s" and cros==0 and OS=="m" and brand=="C"', 4),
       ),
       (
         # 3nd value for 3th variable.
         [('s', 0, 'w', 'C')],
         256L,
         ((1, 1, 4, 1),),  # bitfields, not numbers.
         ('lib=="s" and cros==0 and OS=="w" and brand=="C"', 4),
       ),
       (
         # 2nd value for 4th variable.
         [('s', 0, 'l', 'GC')],
         4096L,
         ((1, 1, 1, 2),),
         ('lib=="s" and cros==0 and OS=="l" and brand=="GC"', 4),
       ),
       (
         # Last bit that can be set, all values are the last.
         # 100000000000000000000000000000000000 is 36th bit == 2*2*3*3.
         [('c', 1, 'w', 'Y')],
         34359738368L,
         ((2, 2, 4, 4),),
         ('lib=="c" and cros==1 and OS=="w" and brand=="Y"', 4),
       ),
       (
         # Same condition twice doesn't affect the result.
         [('s', 0, 'l', 'C'), ('s', 0, 'l', 'C')],
         # One real condition, only one bit set.
         1L,
         ((1, 1, 1, 1),),
         ('lib=="s" and cros==0 and OS=="l" and brand=="C"', 4),
       ),
       (
         # All values for 1st variable.
         [('s', 0, 'l', 'C'), ('c', 0, 'l', 'C')],
         # It has 2 bits set.
         3L,
         ((3, 1, 1, 1),),
         ('(lib=="s" or lib=="c") and cros==0 and OS=="l" and brand=="C"', 4),
       ),
       (
         # All values for 2nd variable.
         [('s', 0, 'l', 'C'), ('s', 1, 'l', 'C')],
         # It has 2 bits set.
         5L,
         ((1, 3, 1, 1),),
         ('lib=="s" and (cros==0 or cros==1) and OS=="l" and brand=="C"', 4),
       ),
   ]
   for line in expectations:
     # Note that none of these functions support free variable. It's
     # implemented by ShortExpressionFinder.get_expr().
     to_get_expr, bits, to_format_expr, final_expected = line
     self.assertEqual(
         bits,
         short_expression_finder._get_expr(values, to_get_expr))
     self.assertEqual(
         to_format_expr,
         short_expression_finder._find_reduced_cost(1, 1, values, bits))
     self.assertEqual(
         final_expected,
         short_expression_finder._format_expr(
           variables, values, to_format_expr))