Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 1 | import unittest |
| 2 | from xosgenx.jinja2_extensions.fol2 import FOL2Python |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 3 | |
| 4 | class XProtoOptimizeTest(unittest.TestCase): |
| 5 | def setUp(self): |
| 6 | self.f2p = FOL2Python() |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 7 | self.maxDiff=None |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 8 | |
| 9 | def test_constant(self): |
| 10 | input = 'True' |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 11 | output = self.f2p.hoist_outer(input) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 12 | self.assertEqual(output, input) |
| 13 | |
| 14 | def test_exists(self): |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 15 | input = {'exists': ['X',{'|':['X.foo','y']}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 16 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 17 | output = self.f2p.hoist_outer(input) |
| 18 | expected = {'|': ['y', {'&': [{'not': 'y'}, {'exists': ['X', 'X.foo']}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 19 | self.assertEqual(output, expected) |
| 20 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 21 | def test_exists_implies(self): |
| 22 | input = {'exists': ['Foo', {'&': [{'=': ('Foo.a', '1')}, {'->': ['write_access', {'=': ('Foo.b', '1')}]}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 23 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 24 | 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 Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 33 | self.assertEqual(output, expected) |
| 34 | |
| 35 | def test_exists_embedded(self): |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 36 | input = {'&':['True',{'exists': ['X',{'|':['X.foo','y']}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 37 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 38 | output = self.f2p.hoist_outer(input) |
| 39 | expected = {'|': ['y', {'&': [{'not': 'y'}, {'exists': ['X', 'X.foo']}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 40 | self.assertEqual(output, expected) |
| 41 | |
| 42 | def test_exists_equals(self): |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 43 | input = {'&':['True',{'exists': ['X',{'|':['X.foo',{'=':['y','z']}]}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 44 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 45 | output = self.f2p.hoist_outer(input) |
| 46 | expected = {'|': [{'=': ['y', 'z']}, {'&': [{'not': {'=': ['y', 'z']}}, {'exists': ['X', 'X.foo']}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 47 | self.assertEqual(output, expected) |
| 48 | |
| 49 | def test_exists_nested_constant(self): |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 50 | input = {'&':['True',{'exists': ['X',{'|':['y',{'=':['y','X.foo']}]}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 51 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 52 | output = self.f2p.hoist_outer(input) |
| 53 | expected = {'|': ['y', {'&': [{'not': 'y'}, {'exists': ['X', {'=': ['False', 'X.foo']}]}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 54 | self.assertEqual(output, expected) |
| 55 | |
| 56 | def test_exists_nested(self): |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 57 | input = {'exists': ['X',{'exists':['Y',{'=':['Y.foo','X.foo']}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 58 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 59 | output = self.f2p.hoist_outer(input) |
| 60 | expected = input |
| 61 | self.assertEqual(input, output) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 62 | |
| 63 | def test_exists_nested2(self): |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 64 | input = {'exists': ['X',{'exists':['Y',{'=':['Z','Y']}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 65 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 66 | output = self.f2p.hoist_outer(input) |
| 67 | expected = {'exists': ['Y', {'=': ['Z', 'Y']}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 68 | self.assertEqual(output, expected) |
| 69 | |
| 70 | def test_exists_nested3(self): |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 71 | input = {'exists': ['X',{'exists':['Y',{'=':['Z','X']}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 72 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 73 | output = self.f2p.hoist_outer(input) |
| 74 | expected = {'exists': ['X', {'=': ['Z', 'X']}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 75 | self.assertEqual(output, expected) |
| 76 | |
| 77 | |
| 78 | if __name__ == '__main__': |
| 79 | unittest.main() |
| 80 | |
| 81 | |