def test_iff(self): statement_a = Symbol('a') statement_b = Symbol('b') statement_iff = Iff(statement_a, statement_b) self.assertEqual(statement_iff.arity, 2) self.assertEqual(repr(statement_iff), "iff(a, b)") self.assertEqual(str(statement_iff), "(a <-> b)")
def test_non_equality(self): statement_a = Symbol('a') statement_b = Symbol('b') statement_and = And(statement_a, statement_b) statement_or = Or(statement_and, statement_b) self.assertNotEqual(statement_or, Or(And(Symbol('a'), Symbol('b')), Symbol('a')))
def test_or(self): statement_a = Symbol('a') statement_b = Symbol('b') statement_or = Or(statement_a, statement_b) self.assertEqual(statement_or.arity, 2) self.assertEqual(repr(statement_or), "or(a, b)") self.assertEqual(str(statement_or), "(a | b)")
def test_and(self): statement_a = Symbol('a') statement_b = Symbol('b') statement_and = And(statement_a, statement_b) self.assertEqual(statement_and.arity, 2) self.assertEqual(repr(statement_and), "and(a, b)") self.assertEqual(str(statement_and), "(a & b)")
def test_convert_to_cnf(self): statement = Not(Iff(Symbol("a"), Symbol("c"))) statement = converter.convert_formula(statement) a_symbol = Symbol("a") c_symbol = Symbol("c") expected = And( And(Or(c_symbol, a_symbol), Or(Not(a_symbol), a_symbol)), And(Or(c_symbol, Not(c_symbol)), Or(Not(a_symbol), Not(c_symbol)))) self.assertEqual(expected, statement)
def test_parse_fol_1(self): Predicate.reset() Herbrand.reset() Skolem.reset() statement = parser.parse("forall(x,if(A(x),and(B(x),C(x))))") expected = Universal(Symbol("x"), If(Predicate("A", [Symbol("x")]), And(Predicate("B", [Symbol("x")]), Predicate("C", [Symbol("x")])))) self.assertEqual(expected, statement)
def test_complex(self): statement_a = Symbol('a') statement_b = Symbol('b') statement_and = And(statement_a, statement_b) statement_or = Or(statement_and, statement_a) statement_implies = If(statement_or, statement_and) statement_equiv = Iff(statement_implies, statement_a) self.assertEqual(repr(statement_equiv), "iff(if(or(and(a, b), a), " "and(a, b)), a)") self.assertEqual(str(statement_equiv), "((((a & b) | a) -> (a & b)) <-> a)")
def test_print_cnf_list(self): cnf = list() a_symbol = Symbol("a") b_symbol = Symbol("b") not_a = Not(a_symbol) not_b = Not(b_symbol) cnf.append([a_symbol, b_symbol]) cnf.append([not_b, a_symbol]) cnf.append([not_a, not_b]) writer = StringIO() util.print_cnf_list(cnf, out=writer) cnf_list = "[[a, b], [~b, a], [~a, ~b]]\n" self.assertEqual(cnf_list, writer.getvalue())
def test_equality(self): statement_a = Symbol('a') statement_b = Symbol('b') statement_and = And(statement_a, statement_b) statement_or = Or(statement_and, statement_a) statement_if = If(statement_or, statement_and) statement_iff = Iff(statement_if, statement_a) expected = Iff( If(Or(And(Symbol("a"), Symbol("b")), Symbol("a")), And(Symbol("a"), Symbol("b"))), Symbol("a")) self.assertTrue(expected == statement_iff) self.assertEqual(repr(expected), repr(statement_iff)) self.assertEqual(str(expected), str(statement_iff))
def test_cnf_converter_symbol(self): statement = Symbol("a") statement = converter.convert_formula(statement) expected = Symbol("a") self.assertEqual(expected, statement)
def test_cnf_negation(self): statement = Not(Not(And(Symbol("a"), Symbol("b")))) statement = converter.convert_formula(statement) expected = And(Symbol("a"), Symbol("b")) self.assertEqual(expected, statement)
def test_cnf_not_distribution(self): statement = Not(And(Symbol("a"), Symbol("b"))) statement = converter.convert_formula(statement) expected = Or(Not(Symbol("a")), Not(Symbol("b"))) self.assertEqual(expected, statement)
def test_cnf_or_distribution(self): statement = Or(And(Symbol("a"), Symbol("b")), Symbol("c")) statement = converter.convert_formula(statement) expected = And(Or(Symbol("a"), Symbol("c")), Or(Symbol("b"), Symbol("c"))) self.assertEqual(expected, statement)
def test_is_tautology_bad_input(self): self.assertFalse(util.is_tautology("a")) self.assertFalse(util.is_tautology([Symbol("a")]))
def test_symbol_gt_symbol(self): self.assertGreater(Symbol('b'), Symbol('a'))
def test_cnf_list_as_disjunction(self): cnf = [Symbol("a"), Symbol("b")] expected = util.cnf_list_as_disjunction(cnf) self.assertEqual(expected, Or(Symbol("a"), Symbol("b")))
def test_is_tautology(self): cnf = list() cnf.append(Symbol("a")) cnf.append(Not(Symbol("a"))) is_taut = util.is_tautology(cnf) self.assertTrue(is_taut)
def test_symbol(self): statement = Symbol('a') self.assertEqual(statement.arity, 0) self.assertEqual(repr(statement), "a") self.assertEqual(str(statement), "a")
def test_not(self): statement_a = Symbol('a') statement_not = Not(statement_a) self.assertEqual(statement_not.arity, 1) self.assertEqual(repr(statement_not), "not(a)") self.assertEqual(str(statement_not), "~a")
def test_operator_ge_symbol(self): self.assertGreaterEqual(Not(Symbol("a")), Not(Symbol("a")))
def test_operator_gt_symbol(self): self.assertGreater(Not(Symbol("a")), Symbol("b"))
def test_operator_le_symbol(self): self.assertLessEqual(Not(Symbol('a')), Not(Symbol('b')))
def test_operator_not_lt_symbol(self): self.assertFalse(Not(Symbol("a")) < Symbol("b"))
def test_symbol_ge_operator_1(self): self.assertGreaterEqual(Symbol('b'), Symbol('a'))
def test_negate_not(self): negate = util.negate_formula(Not(Symbol("a"))) self.assertEqual(Symbol("a"), negate)
def test_symbol_lt_operator_1(self): self.assertLess(Symbol('a'), Not(Symbol('a')))
def test_is_tautology_negative(self): cnf = list() cnf.append(Symbol("a")) cnf.append(Symbol("b")) is_taut = util.is_tautology(cnf) self.assertFalse(is_taut)
def test_symbol_lt_symbol_1(self): self.assertLess(Symbol('a'), Symbol('b'))
def test_cnf_converter_equiv(self): statement = Iff(Symbol("a"), Symbol("b")) statement = converter.convert_formula(statement) expected = And(Or(Not(Symbol("a")), Symbol("b")), Or(Not(Symbol("b")), Symbol("a"))) self.assertEqual(expected, statement)
def test_symbol_le_symbol(self): self.assertLessEqual(Symbol('a'), Symbol('b'))