Exemplo n.º 1
0
    def test_chained_unary_operators_ending_in_operand(self):
        """Test unary operators chained ending in an operand."""
        t = ExpressionTreeNode.build_tree(['A', '~'])
        self.assertTrue(t.is_really_unary)

        t = ExpressionTreeNode.build_tree(['A', '~', '~'])
        self.assertTrue(t.is_really_unary)

        t = ExpressionTreeNode.build_tree(['A', '~', '~', '~'])
        self.assertTrue(t.is_really_unary)
Exemplo n.º 2
0
    def test_chained_unary_operators_with_binary_operator(self):
        """Test a chain of unary operators containing a binary operator."""
        t = ExpressionTreeNode.build_tree(['A', 'B', 'and', 'not'])
        self.assertFalse(t.is_really_unary)

        t = ExpressionTreeNode.build_tree(
            ['A', 'B', 'or', 'C', 'D', 'E', 'not', 'and', 'and', 'not', 'xor'])
        self.assertFalse(t.is_really_unary)
        self.assertFalse(t.l_child.is_really_unary)
        self.assertFalse(t.r_child.is_really_unary)
        self.assertTrue(t.r_child.l_child.r_child.l_child.is_really_unary)
Exemplo n.º 3
0
Arquivo: bexpr.py Projeto: GDApsy/tt
    def __init__(self, expr):
        if not isinstance(expr, (str, ExpressionTreeNode)):
            raise InvalidArgumentTypeError(
                'expr must be a str or ExpressionTreeNode')

        self._symbols = []
        self._symbol_set = set()
        self._tokens = []
        self._postfix_tokens = []

        if isinstance(expr, str):
            self._init_from_str(expr)
        elif isinstance(expr, ExpressionTreeNode):
            self._init_from_expr_node(expr)

        self._symbol_vals_factory = boolean_variables_factory(self._symbols)
        self._tree = ExpressionTreeNode.build_tree(self._postfix_tokens)
        self._constraints = {}
        self._constrained_symbol_set = set()
Exemplo n.º 4
0
 def test_postfix_tokens_empty(self):
     """Test passing an empty list for postfix_tokens."""
     with self.assertRaises(InvalidArgumentValueError):
         ExpressionTreeNode.build_tree([])
Exemplo n.º 5
0
 def test_postfix_tokens_contains_non_str(self):
     """Test passing a list containing a non-str for postfix_tokens."""
     with self.assertRaises(InvalidArgumentTypeError):
         ExpressionTreeNode.build_tree(['A', 1, 'or'])
Exemplo n.º 6
0
 def test_postfix_tokens_not_a_list(self):
     """Test passing a non-list for postfix_tokens."""
     with self.assertRaises(InvalidArgumentTypeError):
         ExpressionTreeNode.build_tree('should cause an exception')
Exemplo n.º 7
0
 def test_single_operand(self):
     """Test unary detection of single operand."""
     for token in ('0', '1', 'token'):
         t = ExpressionTreeNode.build_tree([token])
         self.assertTrue(t.is_really_unary)
Exemplo n.º 8
0
 def test_binary_operator(self):
     """Test an tree containing a binary operator."""
     t = ExpressionTreeNode.build_tree(['A', 'B', 'or'])
     self.assertFalse(t.is_really_unary)
Exemplo n.º 9
0
 def _bexpr_from_postfix_tokens(self, postfix_tokens):
     return BooleanExpression(ExpressionTreeNode.build_tree(postfix_tokens))
Exemplo n.º 10
0
 def assert_is_cnf(self, postfix_tokens):
     """Assert the passed tokens are in conjunctive normal form."""
     self.assertTrue(ExpressionTreeNode.build_tree(postfix_tokens).is_cnf)
Exemplo n.º 11
0
 def assert_not_dnf(self, postfix_tokens):
     """Assert the passed tokens are not in disjunctive normal form."""
     self.assertFalse(ExpressionTreeNode.build_tree(postfix_tokens).is_dnf)