-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_converter.py
68 lines (55 loc) · 2.71 KB
/
test_converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# pylint: disable=missing-docstring
import unittest
from forseti.formula import Symbol, Not, And, Or, Iff
from forseti import converter, parser
class TestConverter(unittest.TestCase):
def test_cnf_converter_symbol(self):
statement = Symbol("a")
statement = converter.convert_formula(statement)
expected = Symbol("a")
self.assertEqual(expected, statement)
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_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_not_distribution_2(self):
statement = Not(Or(And(Symbol("a"), Not(Symbol("b"))), Not(Symbol("c"))))
statement = converter.convert_formula(statement)
expected = And(Or(Not(Symbol("a")), Symbol("b")), Symbol("c"))
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_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_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_convert_to_cnf_2(self):
statement = parser.parse("forall(x,if(A(x),and(B(x),C(x))))")
statement = converter.convert_formula(statement)
expected = "((B(Herbrand1) | ~A(Herbrand1)) & (C(Herbrand1) | ~A(Herbrand1)))"
self.assertEqual(expected, str(statement))
def test_cnf_error_on_string(self):
with self.assertRaises(TypeError):
converter.convert_formula("invalid")
if __name__ == "__main__":
unittest.main()