Skip to content

Commit 5b5d4df

Browse files
Chipe1norvig
authored andcommitted
test cases for logic.py (aimacode#451)
1 parent 60a4285 commit 5b5d4df

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

tests/test_logic.py

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,34 @@
33
from utils import expr_handle_infix_ops, count, Symbol
44

55

6+
def test_is_symbol():
7+
assert is_symbol('x')
8+
assert is_symbol('X')
9+
assert is_symbol('N245')
10+
assert not is_symbol('')
11+
assert not is_symbol('1L')
12+
assert not is_symbol([1, 2, 3])
13+
14+
15+
def test_is_var_symbol():
16+
assert is_var_symbol('xt')
17+
assert not is_var_symbol('Txt')
18+
assert not is_var_symbol('')
19+
assert not is_var_symbol('52')
20+
21+
22+
def test_is_prop_symbol():
23+
assert not is_prop_symbol('xt')
24+
assert is_prop_symbol('Txt')
25+
assert not is_prop_symbol('')
26+
assert not is_prop_symbol('52')
27+
28+
29+
def test_variables():
30+
assert variables(expr('F(x, x) & G(x, y) & H(y, z) & R(A, z, 2)')) == {x, y, z}
31+
assert variables(expr('(x ==> y) & B(x, y) & A')) == {x, y}
32+
33+
634
def test_expr():
735
assert repr(expr('P <=> Q(1)')) == '(P <=> Q(1))'
836
assert repr(expr('P & Q | ~R(x, F(x))')) == '((P & Q) | ~R(x, F(x)))'
@@ -14,6 +42,10 @@ def test_extend():
1442
assert extend({x: 1}, y, 2) == {x: 1, y: 2}
1543

1644

45+
def test_subst():
46+
assert subst({x: 42, y:0}, F(x) + y) == (F(42) + 0)
47+
48+
1749
def test_PropKB():
1850
kb = PropKB()
1951
assert count(kb.ask(expr) for expr in [A, C, D, E, Q]) is 0
@@ -68,7 +100,7 @@ def test_KB_wumpus():
68100
assert kb_wumpus.ask(P[2, 2] | P[3, 1]) == {}
69101

70102

71-
def test_definite_clause():
103+
def test_is_definite_clause():
72104
assert is_definite_clause(expr('A & B & C & D ==> E'))
73105
assert is_definite_clause(expr('Farmer(Mac)'))
74106
assert not is_definite_clause(expr('~Farmer(Mac)'))
@@ -77,6 +109,12 @@ def test_definite_clause():
77109
assert not is_definite_clause(expr('(Farmer(f) | Rabbit(r)) ==> Hates(f, r)'))
78110

79111

112+
def test_parse_definite_clause():
113+
assert parse_definite_clause(expr('A & B & C & D ==> E')) == ([A, B, C, D], E)
114+
assert parse_definite_clause(expr('Farmer(Mac)')) == ([], expr('Farmer(Mac)'))
115+
assert parse_definite_clause(expr('(Farmer(f) & Rabbit(r)) ==> Hates(f, r)')) == ([expr('Farmer(f)'), expr('Rabbit(r)')], expr('Hates(f, r)'))
116+
117+
80118
def test_pl_true():
81119
assert pl_true(P, {}) is None
82120
assert pl_true(P, {P: False}) is False
@@ -115,6 +153,22 @@ def test_dpll():
115153
assert dpll_satisfiable(P & ~P) is False
116154

117155

156+
def test_find_pure_symbol():
157+
assert find_pure_symbol([A, B, C], [A|~B,~B|~C,C|A]) == (A, True)
158+
assert find_pure_symbol([A, B, C], [~A|~B,~B|~C,C|A]) == (B, False)
159+
assert find_pure_symbol([A, B, C], [~A|B,~B|~C,C|A]) == (None, None)
160+
161+
162+
def test_unit_clause_assign():
163+
assert unit_clause_assign(A|B|C, {A:True}) == (None, None)
164+
assert unit_clause_assign(B|C, {A:True}) == (None, None)
165+
assert unit_clause_assign(B|~A, {A:True}) == (B, True)
166+
167+
168+
def test_find_unit_clause():
169+
assert find_unit_clause([A|B|C, B|~C, ~A|~B], {A:True}) == (B, False)
170+
171+
118172
def test_unify():
119173
assert unify(x, x, {}) == {}
120174
assert unify(x, 3, {}) == {x: 3}
@@ -131,6 +185,11 @@ def test_tt_entails():
131185
assert tt_entails(A & (B | C) & E & F & ~(P | Q), A & E & F & ~P & ~Q)
132186

133187

188+
def test_prop_symbols():
189+
assert set(prop_symbols(expr('x & y & z | A'))) == {A}
190+
assert set(prop_symbols(expr('(x & B(z)) ==> Farmer(y) | A'))) == {A, expr('Farmer(y)'), expr('B(z)')}
191+
192+
134193
def test_eliminate_implications():
135194
assert repr(eliminate_implications('A ==> (~B <== C)')) == '((~B | ~C) | ~A)'
136195
assert repr(eliminate_implications(A ^ B)) == '((A & ~B) | (~A & B))'
@@ -156,6 +215,18 @@ def test_move_not_inwards():
156215
assert repr(move_not_inwards(~(~(A | ~B) | ~~C))) == '((A | ~B) & ~C)'
157216

158217

218+
def test_distribute_and_over_or():
219+
def test_enatilment(s, has_and = False):
220+
result = distribute_and_over_or(s)
221+
if has_and:
222+
assert result.op == '&'
223+
assert tt_entails(s, result)
224+
assert tt_entails(result, s)
225+
test_enatilment((A & B) | C, True)
226+
test_enatilment((A | B) & C, True)
227+
test_enatilment((A | B) | C, False)
228+
test_enatilment((A & B) | (C | D), True)
229+
159230
def test_to_cnf():
160231
assert (repr(to_cnf(wumpus_world_inference & ~expr('~P12'))) ==
161232
"((~P12 | B11) & (~P21 | B11) & (P12 | P21 | ~B11) & ~B11 & P12)")

0 commit comments

Comments
 (0)