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