Ejemplo n.º 1
0
    def test_dual(self):
        algebra = BooleanAlgebra()
        self.assertEqual(algebra.AND(algebra.Symbol('a'), algebra.Symbol('b')).dual, algebra.OR)
        self.assertEqual(algebra.OR(algebra.Symbol('a'), algebra.Symbol('b')).dual, algebra.AND)

        self.assertEqual(algebra.parse('a|b').dual, algebra.AND)
        self.assertEqual(algebra.parse('a&b').dual, algebra.OR)
Ejemplo n.º 2
0
 def test_parse(self):
     algebra = BooleanAlgebra()
     a, b, c = algebra.Symbol('a'), algebra.Symbol('b'), algebra.Symbol('c')
     self.assertEqual(algebra.parse('0'), algebra.FALSE)
     self.assertEqual(algebra.parse('(0)'), algebra.FALSE)
     self.assertEqual(algebra.parse('1') , algebra.TRUE)
     self.assertEqual(algebra.parse('(1)'), algebra.TRUE)
     self.assertEqual(algebra.parse('a'), a)
     self.assertEqual(algebra.parse('(a)'), a)
     self.assertEqual(algebra.parse('(a)'), a)
     self.assertEqual(algebra.parse('~a'), algebra.parse('~(a)'))
     self.assertEqual(algebra.parse('~(a)'), algebra.parse('(~a)'))
     self.assertEqual(algebra.parse('~a'), ~a)
     self.assertEqual(algebra.parse('(~a)'), ~a)
     self.assertEqual(algebra.parse('~~a', simplify=True), (~~a).simplify())
     self.assertEqual(algebra.parse('a&b'), a & b)
     self.assertEqual(algebra.parse('~a&b'), ~a & b)
     self.assertEqual(algebra.parse('a&~b'), a & ~b)
     self.assertEqual(algebra.parse('a&b&c'), algebra.parse('a&b&c'))
     self.assertEqual(algebra.parse('a&b&c'), algebra.AND(a, b, c))
     self.assertEqual(algebra.parse('~a&~b&~c'), algebra.parse('~a&~b&~c'))
     self.assertEqual(algebra.parse('~a&~b&~c'), algebra.AND(~a, ~b, ~c))
     self.assertEqual(algebra.parse('a|b'), a | b)
     self.assertEqual(algebra.parse('~a|b'), ~a | b)
     self.assertEqual(algebra.parse('a|~b'), a | ~b)
     self.assertEqual(algebra.parse('a|b|c'), algebra.parse('a|b|c'))
     self.assertEqual(algebra.parse('a|b|c'), algebra.OR(a, b, c))
     self.assertEqual(algebra.parse('~a|~b|~c'), algebra.OR(~a, ~b, ~c))
     self.assertEqual(algebra.parse('(a|b)'), a | b)
     self.assertEqual(algebra.parse('a&(a|b)', simplify=True), (a & (a | b)).simplify())
     self.assertEqual(algebra.parse('a&(a|~b)', simplify=True), (a & (a | ~b)).simplify())
     self.assertEqual(algebra.parse('(a&b)|(b&((c|a)&(b|(c&a))))', simplify=True), ((a & b) | (b & ((c | a) & (b | (c & a))))).simplify())
     self.assertEqual(algebra.parse('(a&b)|(b&((c|a)&(b|(c&a))))', simplify=True), algebra.parse('a&b | b&(c|a)&(b|c&a)', simplify=True))
Ejemplo n.º 3
0
 def test_subs_default(self):
     algebra = BooleanAlgebra()
     a, b, c = algebra.Symbol('a'), algebra.Symbol('b'), algebra.Symbol('c')
     expr = a & b | c
     self.assertEqual(expr.subs({}, default=algebra.TRUE).simplify(), algebra.TRUE)
     self.assertEqual(expr.subs({a: algebra.FALSE, c: algebra.FALSE}, default=algebra.TRUE).simplify(), algebra.FALSE)
     self.assertEqual(algebra.TRUE.subs({}, default=algebra.FALSE).simplify(), algebra.TRUE)
     self.assertEqual(algebra.FALSE.subs({}, default=algebra.TRUE).simplify(), algebra.FALSE)
Ejemplo n.º 4
0
 def test_subs(self):
     algebra = BooleanAlgebra()
     a, b, c = algebra.Symbol('a'), algebra.Symbol('b'), algebra.Symbol('c')
     expr = a & b | c
     self.assertEqual(expr.subs({a: b}).simplify(), b | c)
     self.assertEqual(expr.subs({a: a}).simplify(), expr)
     self.assertEqual(expr.subs({a: b | c}).simplify(), algebra.parse('(b|c)&b|c').simplify())
     self.assertEqual(expr.subs({a & b: a}).simplify(), a | c)
     self.assertEqual(expr.subs({c: algebra.TRUE}).simplify(), algebra.TRUE)
Ejemplo n.º 5
0
    def test_simplify(self):
        algebra1 = BooleanAlgebra()
        algebra2 = BooleanAlgebra()
        a = algebra1.Symbol('a')
        b = algebra1.Symbol('b')
        c = algebra1.Symbol('c')

        _0 = algebra1.FALSE
        _1 = algebra1.TRUE
        # Idempotence
        self.assertEqual(a, (a & a).simplify())
        # Idempotence + Associativity
        self.assertEqual(a | b, (a | (a | b)).simplify())
        # Annihilation
        self.assertEqual(_0, (a & _0).simplify())
        self.assertEqual(_1, (a | _1).simplify())
        # Identity
        self.assertEqual(a, (a & _1).simplify())
        self.assertEqual(a, (a | _0).simplify())
        # Complementation
        self.assertEqual(_0, (a & ~a).simplify())
        self.assertEqual(_1, (a | ~a).simplify())
        # Absorption
        self.assertEqual(a, (a & (a | b)).simplify())
        self.assertEqual(a, (a | (a & b)).simplify())
        self.assertEqual(b & a, ((b & a) | (b & a & c)).simplify())

        # Elimination
        self.assertEqual(a, ((a & ~b) | (a & b)).simplify())

        expected = algebra1.parse('(a&b)|(b&c)|(a&c)')
        result = algebra1.parse('(~a&b&c) | (a&~b&c) | (a&b&~c) | (a&b&c)', simplify=True)
        self.assertEqual(expected, result)

        expected = algebra1.parse('(a&b)|(b&c)|(a&c)')
        result = algebra2.parse('(~a&b&c) | (a&~b&c) | (a&b&~c) | (a&b&c)', simplify=True)
        self.assertEqual(expected, result)

        expected = algebra1.parse('b&d')
        result = algebra1.parse('(a&b&c&d) | (b&d)', simplify=True)
        self.assertEqual(expected, result)

        expected = algebra1.parse('b&d')
        result = algebra2.parse('(a&b&c&d) | (b&d)', simplify=True)
        self.assertEqual(expected, result)

        expected = algebra1.parse('(~b&~d&a) | (~c&~d&b) | (a&c&d)', simplify=True)
        result = algebra1.parse('''(~a&b&~c&~d) | (a&~b&~c&~d) | (a&~b&c&~d) |
                          (a&~b&c&d) | (a&b&~c&~d) | (a&b&c&d)''', simplify=True)
        self.assertEqual(expected.pretty(), result.pretty())

        expected = algebra1.parse('(~b&~d&a) | (~c&~d&b) | (a&c&d)', simplify=True)
        result = algebra2.parse('''(~a&b&~c&~d) | (a&~b&~c&~d) | (a&~b&c&~d) |
                          (a&~b&c&d) | (a&b&~c&~d) | (a&b&c&d)''', simplify=True)
        self.assertEqual(expected.pretty(), result.pretty())
Ejemplo n.º 6
0
 def test_simplify(self):
     algebra = BooleanAlgebra()
     a = algebra.Symbol('a')
     self.assertEqual(~a, ~a)
     assert algebra.Symbol('a') == algebra.Symbol('a')
     self.assertNotEqual(a, algebra.parse('~~a'))
     self.assertEqual(a, (~~a).simplify())
     self.assertEqual(~a, (~~~a).simplify())
     self.assertEqual(a, (~~~~a).simplify())
     self.assertEqual((~(a & a & a)).simplify(), (~(a & a & a)).simplify())
     self.assertEqual(a, algebra.parse('~~a', simplify=True))
Ejemplo n.º 7
0
 def test_distributive(self):
     algebra = BooleanAlgebra()
     a = algebra.Symbol('a')
     b = algebra.Symbol('b')
     c = algebra.Symbol('c')
     d = algebra.Symbol('d')
     e = algebra.Symbol('e')
     self.assertEqual((a & (b | c)).distributive(), (a & b) | (a & c))
     t1 = algebra.AND(a, (b | c), (d | e))
     t2 = algebra.OR(algebra.AND(a, b, d), algebra.AND(a, b, e), algebra.AND(a, c, d), algebra.AND(a, c, e))
     self.assertEqual(t1.distributive(), t2)
Ejemplo n.º 8
0
 def test_demorgan(self):
     algebra = BooleanAlgebra()
     a = algebra.Symbol('a')
     b = algebra.Symbol('b')
     c = algebra.Symbol('c')
     self.assertEqual(algebra.parse('~(a&b)').demorgan(), ~a | ~b)
     self.assertEqual(algebra.parse('~(a|b|c)').demorgan(), algebra.parse('~a&~b&~c'))
     self.assertEqual(algebra.parse('~(~a&b)').demorgan(), a | ~b)
     self.assertEqual((~~(a&b|c)).demorgan(), a&b|c)
     self.assertEqual((~~~(a&b|c)).demorgan(), ~(a&b)&~c)
     self.assertEqual(algebra.parse('~'*10 + '(a&b|c)').demorgan(), a&b|c)
     self.assertEqual(algebra.parse('~'*11 + '(a&b|c)').demorgan(), (~(a&b|c)).demorgan())
Ejemplo n.º 9
0
    def test_bool(self):
        algebra = BooleanAlgebra()
        a, b, c = algebra.Symbol('a'), algebra.Symbol('b'), algebra.Symbol('c')
        expr = a & b | c
        self.assertRaises(TypeError, bool, expr.subs({a: algebra.TRUE}))
        self.assertRaises(TypeError, bool, expr.subs({b: algebra.TRUE}))
        self.assertRaises(TypeError, bool, expr.subs({c: algebra.TRUE}))
        self.assertRaises(TypeError, bool, expr.subs({a: algebra.TRUE, b: algebra.TRUE}))
        result = expr.subs({c: algebra.TRUE}, simplify=True)
        result = result.simplify()
        self.assertEqual(algebra.TRUE, result)

        result = expr.subs({a: algebra.TRUE, b: algebra.TRUE}, simplify=True)
        result = result.simplify()
        self.assertEqual(algebra.TRUE, result)
Ejemplo n.º 10
0
    def test_class_order(self):
        # FIXME: this test is cryptic: what does it do?
        algebra = BooleanAlgebra()
        order = (
            (algebra.TRUE, algebra.FALSE),
            (algebra.Symbol('y'), algebra.Symbol('x')),
            (algebra.parse('x&y'), ),
            (algebra.parse('x|y'), ),
        )
        for i, tests in enumerate(order):
            for case1 in tests:
                for j in range(i + 1, len(order)):
                    for case2 in order[j]:

                        self.assertTrue(case1 < case2)
                        self.assertTrue(case2 > case1)
Ejemplo n.º 11
0
 def test_demorgan(self):
     algebra = BooleanAlgebra()
     a = algebra.Symbol('a')
     b = algebra.Symbol('b')
     self.assertEqual(algebra.parse('~(a&b)').demorgan(), ~a | ~b)
     self.assertEqual(
         algebra.parse('~(a|b|c)').demorgan(), algebra.parse('~a&~b&~c'))
     self.assertEqual(algebra.parse('~(~a&b)').demorgan(), a | ~b)
Ejemplo n.º 12
0
    def test_complex_expression_without_parens_parsed_or_built_in_python_should_be_identical(self):
        # FIXME: THIS SHOULD NOT FAIL
        algebra = BooleanAlgebra()
        a = algebra.Symbol('a')
        b = algebra.Symbol('b')
        c = algebra.Symbol('c')
        d = algebra.Symbol('d')

        test_expression_str = '''
            ~a&~b&~c&~d | ~a&~b&~c&d | ~a&b&~c&~d |
            ~a&b&c&d | ~a&b&~c&d | ~a&b&c&~d |
            a&~b&~c&d | ~a&b&c&d | a&~b&c&d | a&b&c&d
            '''

        parsed = algebra.parse(test_expression_str)

        test_expression = (
            ~a & ~b & ~c & ~d | ~a & ~b & ~c & d | ~a & b & ~c & ~d |
            ~ a & b & c & d | ~a & b & ~c & d | ~a & b & c & ~d |
            a & ~b & ~c & d | ~a & b & c & d | a & ~b & c & d | a & b & c & d
        )

        self.assertEqual(parsed.pretty(), test_expression.pretty())