Ejemplo n.º 1
0
    def test_single_pair(self) -> None:
        x0 = NameExpr('x0')
        x1 = NameExpr('x1')

        single_comparison = [('==', x0, x1)]
        expected_output = [('==', [0, 1])]

        assignable_combinations = [
            {},
            {
                0: x0
            },
            {
                1: x1
            },
            {
                0: x0,
                1: x1
            },
        ]  # type: List[Dict[int, NameExpr]]
        to_group_by = [set(), {'=='}, {'is'}]  # type: List[Set[str]]

        for combo in assignable_combinations:
            for operators in to_group_by:
                keymap = self.literal_keymap(combo)
                self.assertEqual(
                    group_comparison_operands(single_comparison, keymap,
                                              operators),
                    expected_output,
                )
Ejemplo n.º 2
0
    def test_multiple_groups(self) -> None:
        x0 = NameExpr('x0')
        x1 = NameExpr('x1')
        x2 = NameExpr('x2')
        x3 = NameExpr('x3')
        x4 = NameExpr('x4')
        x5 = NameExpr('x5')

        self.assertEqual(
            group_comparison_operands(
                [('==', x0, x1), ('==', x1, x2), ('is', x2, x3),
                 ('is', x3, x4)],
                self.literal_keymap({}),
                {'==', 'is'},
            ),
            [('==', [0, 1, 2]), ('is', [2, 3, 4])],
        )
        self.assertEqual(
            group_comparison_operands(
                [('==', x0, x1), ('==', x1, x2), ('==', x2, x3),
                 ('==', x3, x4)],
                self.literal_keymap({}),
                {'==', 'is'},
            ),
            [('==', [0, 1, 2, 3, 4])],
        )
        self.assertEqual(
            group_comparison_operands(
                [('is', x0, x1), ('==', x1, x2), ('==', x2, x3),
                 ('==', x3, x4)],
                self.literal_keymap({}),
                {'==', 'is'},
            ),
            [('is', [0, 1]), ('==', [1, 2, 3, 4])],
        )
        self.assertEqual(
            group_comparison_operands(
                [('is', x0, x1), ('is', x1, x2), ('<', x2, x3), ('==', x3, x4),
                 ('==', x4, x5)],
                self.literal_keymap({}),
                {'==', 'is'},
            ),
            [('is', [0, 1, 2]), ('<', [2, 3]), ('==', [3, 4, 5])],
        )
Ejemplo n.º 3
0
    def test_basic_cases(self) -> None:
        # Note: the grouping function doesn't actually inspect the input exprs, so we
        # just default to using NameExprs for simplicity.
        x0 = NameExpr('x0')
        x1 = NameExpr('x1')
        x2 = NameExpr('x2')
        x3 = NameExpr('x3')
        x4 = NameExpr('x4')

        basic_input = [('==', x0, x1), ('==', x1, x2), ('<', x2, x3),
                       ('==', x3, x4)]

        none_assignable = self.literal_keymap({})
        all_assignable = self.literal_keymap({
            0: x0,
            1: x1,
            2: x2,
            3: x3,
            4: x4
        })

        for assignable in [none_assignable, all_assignable]:
            self.assertEqual(
                group_comparison_operands(basic_input, assignable, set()),
                [('==', [0, 1]), ('==', [1, 2]), ('<', [2, 3]),
                 ('==', [3, 4])],
            )
            self.assertEqual(
                group_comparison_operands(basic_input, assignable, {'=='}),
                [('==', [0, 1, 2]), ('<', [2, 3]), ('==', [3, 4])],
            )
            self.assertEqual(
                group_comparison_operands(basic_input, assignable, {'<'}),
                [('==', [0, 1]), ('==', [1, 2]), ('<', [2, 3]),
                 ('==', [3, 4])],
            )
            self.assertEqual(
                group_comparison_operands(basic_input, assignable,
                                          {'==', '<'}),
                [('==', [0, 1, 2]), ('<', [2, 3]), ('==', [3, 4])],
            )
Ejemplo n.º 4
0
    def test_multiple_groups_different_operators(self) -> None:
        x0 = NameExpr('x0')
        x1 = NameExpr('x1')
        x2 = NameExpr('x2')
        x3 = NameExpr('x3')

        groups = [('==', x0, x1), ('==', x1, x2), ('is', x2, x3),
                  ('is', x3, x0)]
        keymap = self.literal_keymap({0: x0, 1: x1, 2: x2, 3: x3, 4: x0})
        self.assertEqual(
            group_comparison_operands(groups, keymap,
                                      {'==', 'is'}), [('==', [0, 1, 2]),
                                                      ('is', [2, 3, 4])],
            "Different operators can never be combined")
Ejemplo n.º 5
0
    def test_empty_pair_list(self) -> None:
        # This case should never occur in practice -- ComparisionExprs
        # always contain at least one comparision. But in case it does...

        self.assertEqual(group_comparison_operands([], {}, set()), [])
        self.assertEqual(group_comparison_operands([], {}, {'=='}), [])
Ejemplo n.º 6
0
    def test_multiple_groups_coalescing(self) -> None:
        x0 = NameExpr('x0')
        x1 = NameExpr('x1')
        x2 = NameExpr('x2')
        x3 = NameExpr('x3')
        x4 = NameExpr('x4')

        nothing_combined = [('==', [0, 1, 2]), ('<', [2, 3]), ('==', [3, 4,
                                                                      5])]
        everything_combined = [('==', [0, 1, 2, 3, 4, 5]), ('<', [2, 3])]

        # Note: We do 'x4 == x0' at the very end!
        two_groups = [
            ('==', x0, x1),
            ('==', x1, x2),
            ('<', x2, x3),
            ('==', x3, x4),
            ('==', x4, x0),
        ]
        self.assertEqual(
            group_comparison_operands(
                two_groups,
                self.literal_keymap({
                    0: x0,
                    1: x1,
                    2: x2,
                    3: x3,
                    4: x4,
                    5: x0
                }),
                {'=='},
            ), everything_combined,
            "All vars are assignable, everything is combined")
        self.assertEqual(
            group_comparison_operands(
                two_groups,
                self.literal_keymap({
                    1: x1,
                    2: x2,
                    3: x3,
                    4: x4
                }),
                {'=='},
            ), nothing_combined, "x0 is unassignable, so no combining")
        self.assertEqual(
            group_comparison_operands(
                two_groups,
                self.literal_keymap({
                    0: x0,
                    1: x1,
                    3: x3,
                    5: x0
                }),
                {'=='},
            ), everything_combined,
            "Some vars are unassignable but x0 is, so we combine")
        self.assertEqual(
            group_comparison_operands(
                two_groups,
                self.literal_keymap({
                    0: x0,
                    5: x0
                }),
                {'=='},
            ), everything_combined,
            "All vars are unassignable but x0 is, so we combine")