blob: 0d2c1b1408a4c1561394b6ac902ff080eeb84d74 [file] [log] [blame]
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -04001import unittest
2from xosgenx.jinja2_extensions.fol2 import FOL2Python
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -04003
4class XProtoOptimizeTest(unittest.TestCase):
5 def setUp(self):
6 self.f2p = FOL2Python()
Sapan Bhatiab69f4702017-07-31 16:03:33 -04007 self.maxDiff=None
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -04008
9 def test_constant(self):
10 input = 'True'
Sapan Bhatiab69f4702017-07-31 16:03:33 -040011 output = self.f2p.hoist_outer(input)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040012 self.assertEqual(output, input)
13
14 def test_exists(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040015 input = {'exists': ['X',{'|':['X.foo','y']}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040016
Sapan Bhatiab69f4702017-07-31 16:03:33 -040017 output = self.f2p.hoist_outer(input)
18 expected = {'|': ['y', {'&': [{'not': 'y'}, {'exists': ['X', 'X.foo']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040019 self.assertEqual(output, expected)
20
Sapan Bhatiab69f4702017-07-31 16:03:33 -040021 def test_exists_implies(self):
22 input = {'exists': ['Foo', {'&': [{'=': ('Foo.a', '1')}, {'->': ['write_access', {'=': ('Foo.b', '1')}]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040023
Sapan Bhatiab69f4702017-07-31 16:03:33 -040024 output = self.f2p.hoist_outer(input)
25 expected = {'|': [{'&': ['write_access', {'exists': ['Foo', {'&': [{'=': ['Foo.a', '1']}, {'=': ['Foo.b', '1']}]}]}]}, {'&': [{'not': 'write_access'}, {'exists': ['Foo', {'=': ['Foo.a', '1']}]}]}]}
26 self.assertEqual(output, expected)
27
28 def test_forall(self):
29 input = {'forall': ['X',{'|':['X.foo','y']}]}
30
31 output = self.f2p.hoist_outer(input)
32 expected = {'|': ['y', {'&': [{'not': 'y'}, {'forall': ['X', 'X.foo']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040033 self.assertEqual(output, expected)
34
35 def test_exists_embedded(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040036 input = {'&':['True',{'exists': ['X',{'|':['X.foo','y']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040037
Sapan Bhatiab69f4702017-07-31 16:03:33 -040038 output = self.f2p.hoist_outer(input)
39 expected = {'|': ['y', {'&': [{'not': 'y'}, {'exists': ['X', 'X.foo']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040040 self.assertEqual(output, expected)
41
42 def test_exists_equals(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040043 input = {'&':['True',{'exists': ['X',{'|':['X.foo',{'=':['y','z']}]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040044
Sapan Bhatiab69f4702017-07-31 16:03:33 -040045 output = self.f2p.hoist_outer(input)
46 expected = {'|': [{'=': ['y', 'z']}, {'&': [{'not': {'=': ['y', 'z']}}, {'exists': ['X', 'X.foo']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040047 self.assertEqual(output, expected)
48
49 def test_exists_nested_constant(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040050 input = {'&':['True',{'exists': ['X',{'|':['y',{'=':['y','X.foo']}]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040051
Sapan Bhatiab69f4702017-07-31 16:03:33 -040052 output = self.f2p.hoist_outer(input)
53 expected = {'|': ['y', {'&': [{'not': 'y'}, {'exists': ['X', {'=': ['False', 'X.foo']}]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040054 self.assertEqual(output, expected)
55
56 def test_exists_nested(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040057 input = {'exists': ['X',{'exists':['Y',{'=':['Y.foo','X.foo']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040058
Sapan Bhatiab69f4702017-07-31 16:03:33 -040059 output = self.f2p.hoist_outer(input)
60 expected = input
61 self.assertEqual(input, output)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040062
63 def test_exists_nested2(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040064 input = {'exists': ['X',{'exists':['Y',{'=':['Z','Y']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040065
Sapan Bhatiab69f4702017-07-31 16:03:33 -040066 output = self.f2p.hoist_outer(input)
67 expected = {'exists': ['Y', {'=': ['Z', 'Y']}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040068 self.assertEqual(output, expected)
69
70 def test_exists_nested3(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040071 input = {'exists': ['X',{'exists':['Y',{'=':['Z','X']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040072
Sapan Bhatiab69f4702017-07-31 16:03:33 -040073 output = self.f2p.hoist_outer(input)
74 expected = {'exists': ['X', {'=': ['Z', 'X']}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040075 self.assertEqual(output, expected)
76
77
78if __name__ == '__main__':
79 unittest.main()
80
81