Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Test cases for logic.py #451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 72 additions & 1 deletion tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@
from utils import expr_handle_infix_ops, count, Symbol


def test_is_symbol():
assert is_symbol('x')
assert is_symbol('X')
assert is_symbol('N245')
assert not is_symbol('')
assert not is_symbol('1L')
assert not is_symbol([1, 2, 3])


def test_is_var_symbol():
assert is_var_symbol('xt')
assert not is_var_symbol('Txt')
assert not is_var_symbol('')
assert not is_var_symbol('52')


def test_is_prop_symbol():
assert not is_prop_symbol('xt')
assert is_prop_symbol('Txt')
assert not is_prop_symbol('')
assert not is_prop_symbol('52')


def test_variables():
assert variables(expr('F(x, x) & G(x, y) & H(y, z) & R(A, z, 2)')) == {x, y, z}
assert variables(expr('(x ==> y) & B(x, y) & A')) == {x, y}


def test_expr():
assert repr(expr('P <=> Q(1)')) == '(P <=> Q(1))'
assert repr(expr('P & Q | ~R(x, F(x))')) == '((P & Q) | ~R(x, F(x)))'
Expand All @@ -14,6 +42,10 @@ def test_extend():
assert extend({x: 1}, y, 2) == {x: 1, y: 2}


def test_subst():
assert subst({x: 42, y:0}, F(x) + y) == (F(42) + 0)


def test_PropKB():
kb = PropKB()
assert count(kb.ask(expr) for expr in [A, C, D, E, Q]) is 0
Expand Down Expand Up @@ -68,7 +100,7 @@ def test_KB_wumpus():
assert kb_wumpus.ask(P[2, 2] | P[3, 1]) == {}


def test_definite_clause():
def test_is_definite_clause():
assert is_definite_clause(expr('A & B & C & D ==> E'))
assert is_definite_clause(expr('Farmer(Mac)'))
assert not is_definite_clause(expr('~Farmer(Mac)'))
Expand All @@ -77,6 +109,12 @@ def test_definite_clause():
assert not is_definite_clause(expr('(Farmer(f) | Rabbit(r)) ==> Hates(f, r)'))


def test_parse_definite_clause():
assert parse_definite_clause(expr('A & B & C & D ==> E')) == ([A, B, C, D], E)
assert parse_definite_clause(expr('Farmer(Mac)')) == ([], expr('Farmer(Mac)'))
assert parse_definite_clause(expr('(Farmer(f) & Rabbit(r)) ==> Hates(f, r)')) == ([expr('Farmer(f)'), expr('Rabbit(r)')], expr('Hates(f, r)'))


def test_pl_true():
assert pl_true(P, {}) is None
assert pl_true(P, {P: False}) is False
Expand Down Expand Up @@ -115,6 +153,22 @@ def test_dpll():
assert dpll_satisfiable(P & ~P) is False


def test_find_pure_symbol():
assert find_pure_symbol([A, B, C], [A|~B,~B|~C,C|A]) == (A, True)
assert find_pure_symbol([A, B, C], [~A|~B,~B|~C,C|A]) == (B, False)
assert find_pure_symbol([A, B, C], [~A|B,~B|~C,C|A]) == (None, None)


def test_unit_clause_assign():
assert unit_clause_assign(A|B|C, {A:True}) == (None, None)
assert unit_clause_assign(B|C, {A:True}) == (None, None)
assert unit_clause_assign(B|~A, {A:True}) == (B, True)


def test_find_unit_clause():
assert find_unit_clause([A|B|C, B|~C, ~A|~B], {A:True}) == (B, False)


def test_unify():
assert unify(x, x, {}) == {}
assert unify(x, 3, {}) == {x: 3}
Expand All @@ -131,6 +185,11 @@ def test_tt_entails():
assert tt_entails(A & (B | C) & E & F & ~(P | Q), A & E & F & ~P & ~Q)


def test_prop_symbols():
assert set(prop_symbols(expr('x & y & z | A'))) == {A}
assert set(prop_symbols(expr('(x & B(z)) ==> Farmer(y) | A'))) == {A, expr('Farmer(y)'), expr('B(z)')}


def test_eliminate_implications():
assert repr(eliminate_implications('A ==> (~B <== C)')) == '((~B | ~C) | ~A)'
assert repr(eliminate_implications(A ^ B)) == '((A & ~B) | (~A & B))'
Expand All @@ -156,6 +215,18 @@ def test_move_not_inwards():
assert repr(move_not_inwards(~(~(A | ~B) | ~~C))) == '((A | ~B) & ~C)'


def test_distribute_and_over_or():
def test_enatilment(s, has_and = False):
result = distribute_and_over_or(s)
if has_and:
assert result.op == '&'
assert tt_entails(s, result)
assert tt_entails(result, s)
test_enatilment((A & B) | C, True)
test_enatilment((A | B) & C, True)
test_enatilment((A | B) | C, False)
test_enatilment((A & B) | (C | D), True)

def test_to_cnf():
assert (repr(to_cnf(wumpus_world_inference & ~expr('~P12'))) ==
"((~P12 | B11) & (~P21 | B11) & (P12 | P21 | ~B11) & ~B11 & P12)")
Expand Down