blob: af405752b9ca42175a4aa1bc6ed1704e5fb4f639 [file] [log] [blame]
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -04001import unittest
2from xosgenx.jinja2_extensions.fol2 import FOL2Python
Sapan Bhatiab69f4702017-07-31 16:03:33 -04003import vimpdb
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -04004
5class XProtoOptimizeTest(unittest.TestCase):
6 def setUp(self):
7 self.f2p = FOL2Python()
Sapan Bhatiab69f4702017-07-31 16:03:33 -04008 self.maxDiff=None
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -04009
10 def test_constant(self):
11 input = 'True'
Sapan Bhatiab69f4702017-07-31 16:03:33 -040012 output = self.f2p.hoist_outer(input)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040013 self.assertEqual(output, input)
14
15 def test_exists(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040016 input = {'exists': ['X',{'|':['X.foo','y']}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040017
Sapan Bhatiab69f4702017-07-31 16:03:33 -040018 output = self.f2p.hoist_outer(input)
19 expected = {'|': ['y', {'&': [{'not': 'y'}, {'exists': ['X', 'X.foo']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040020 self.assertEqual(output, expected)
21
Sapan Bhatiab69f4702017-07-31 16:03:33 -040022 def test_exists_implies(self):
23 input = {'exists': ['Foo', {'&': [{'=': ('Foo.a', '1')}, {'->': ['write_access', {'=': ('Foo.b', '1')}]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040024
Sapan Bhatiab69f4702017-07-31 16:03:33 -040025 output = self.f2p.hoist_outer(input)
26 expected = {'|': [{'&': ['write_access', {'exists': ['Foo', {'&': [{'=': ['Foo.a', '1']}, {'=': ['Foo.b', '1']}]}]}]}, {'&': [{'not': 'write_access'}, {'exists': ['Foo', {'=': ['Foo.a', '1']}]}]}]}
27 self.assertEqual(output, expected)
28
29 def test_forall(self):
30 input = {'forall': ['X',{'|':['X.foo','y']}]}
31
32 output = self.f2p.hoist_outer(input)
33 expected = {'|': ['y', {'&': [{'not': 'y'}, {'forall': ['X', 'X.foo']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040034 self.assertEqual(output, expected)
35
36 def test_exists_embedded(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040037 input = {'&':['True',{'exists': ['X',{'|':['X.foo','y']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040038
Sapan Bhatiab69f4702017-07-31 16:03:33 -040039 output = self.f2p.hoist_outer(input)
40 expected = {'|': ['y', {'&': [{'not': 'y'}, {'exists': ['X', 'X.foo']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040041 self.assertEqual(output, expected)
42
43 def test_exists_equals(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040044 input = {'&':['True',{'exists': ['X',{'|':['X.foo',{'=':['y','z']}]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040045
Sapan Bhatiab69f4702017-07-31 16:03:33 -040046 output = self.f2p.hoist_outer(input)
47 expected = {'|': [{'=': ['y', 'z']}, {'&': [{'not': {'=': ['y', 'z']}}, {'exists': ['X', 'X.foo']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040048 self.assertEqual(output, expected)
49
50 def test_exists_nested_constant(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040051 input = {'&':['True',{'exists': ['X',{'|':['y',{'=':['y','X.foo']}]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040052
Sapan Bhatiab69f4702017-07-31 16:03:33 -040053 output = self.f2p.hoist_outer(input)
54 expected = {'|': ['y', {'&': [{'not': 'y'}, {'exists': ['X', {'=': ['False', 'X.foo']}]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040055 self.assertEqual(output, expected)
56
57 def test_exists_nested(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040058 input = {'exists': ['X',{'exists':['Y',{'=':['Y.foo','X.foo']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040059
Sapan Bhatiab69f4702017-07-31 16:03:33 -040060 output = self.f2p.hoist_outer(input)
61 expected = input
62 self.assertEqual(input, output)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040063
64 def test_exists_nested2(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040065 input = {'exists': ['X',{'exists':['Y',{'=':['Z','Y']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040066
Sapan Bhatiab69f4702017-07-31 16:03:33 -040067 output = self.f2p.hoist_outer(input)
68 expected = {'exists': ['Y', {'=': ['Z', 'Y']}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040069 self.assertEqual(output, expected)
70
71 def test_exists_nested3(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040072 input = {'exists': ['X',{'exists':['Y',{'=':['Z','X']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040073
Sapan Bhatiab69f4702017-07-31 16:03:33 -040074 output = self.f2p.hoist_outer(input)
75 expected = {'exists': ['X', {'=': ['Z', 'X']}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040076 self.assertEqual(output, expected)
77
78
79if __name__ == '__main__':
80 unittest.main()
81
82