Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 17 | import unittest |
| 18 | from xosgenx.jinja2_extensions.fol2 import FOL2Python |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 19 | |
| 20 | class XProtoOptimizeTest(unittest.TestCase): |
| 21 | def setUp(self): |
| 22 | self.f2p = FOL2Python() |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 23 | self.maxDiff=None |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 24 | |
| 25 | def test_constant(self): |
| 26 | input = 'True' |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 27 | output = self.f2p.hoist_outer(input) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 28 | self.assertEqual(output, input) |
| 29 | |
| 30 | def test_exists(self): |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 31 | input = {'exists': ['X',{'|':['X.foo','y']}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 32 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 33 | output = self.f2p.hoist_outer(input) |
| 34 | expected = {'|': ['y', {'&': [{'not': 'y'}, {'exists': ['X', 'X.foo']}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 35 | self.assertEqual(output, expected) |
| 36 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 37 | def test_exists_implies(self): |
| 38 | input = {'exists': ['Foo', {'&': [{'=': ('Foo.a', '1')}, {'->': ['write_access', {'=': ('Foo.b', '1')}]}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 39 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 40 | output = self.f2p.hoist_outer(input) |
| 41 | expected = {'|': [{'&': ['write_access', {'exists': ['Foo', {'&': [{'=': ['Foo.a', '1']}, {'=': ['Foo.b', '1']}]}]}]}, {'&': [{'not': 'write_access'}, {'exists': ['Foo', {'=': ['Foo.a', '1']}]}]}]} |
| 42 | self.assertEqual(output, expected) |
| 43 | |
| 44 | def test_forall(self): |
| 45 | input = {'forall': ['X',{'|':['X.foo','y']}]} |
| 46 | |
| 47 | output = self.f2p.hoist_outer(input) |
| 48 | expected = {'|': ['y', {'&': [{'not': 'y'}, {'forall': ['X', 'X.foo']}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 49 | self.assertEqual(output, expected) |
| 50 | |
| 51 | def test_exists_embedded(self): |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 52 | input = {'&':['True',{'exists': ['X',{'|':['X.foo','y']}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 53 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 54 | output = self.f2p.hoist_outer(input) |
| 55 | expected = {'|': ['y', {'&': [{'not': 'y'}, {'exists': ['X', 'X.foo']}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 56 | self.assertEqual(output, expected) |
| 57 | |
| 58 | def test_exists_equals(self): |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 59 | input = {'&':['True',{'exists': ['X',{'|':['X.foo',{'=':['y','z']}]}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 60 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 61 | output = self.f2p.hoist_outer(input) |
| 62 | expected = {'|': [{'=': ['y', 'z']}, {'&': [{'not': {'=': ['y', 'z']}}, {'exists': ['X', 'X.foo']}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 63 | self.assertEqual(output, expected) |
| 64 | |
| 65 | def test_exists_nested_constant(self): |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 66 | input = {'&':['True',{'exists': ['X',{'|':['y',{'=':['y','X.foo']}]}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 67 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 68 | output = self.f2p.hoist_outer(input) |
| 69 | expected = {'|': ['y', {'&': [{'not': 'y'}, {'exists': ['X', {'=': ['False', 'X.foo']}]}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 70 | self.assertEqual(output, expected) |
| 71 | |
| 72 | def test_exists_nested(self): |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 73 | input = {'exists': ['X',{'exists':['Y',{'=':['Y.foo','X.foo']}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 74 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 75 | output = self.f2p.hoist_outer(input) |
| 76 | expected = input |
| 77 | self.assertEqual(input, output) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 78 | |
| 79 | def test_exists_nested2(self): |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 80 | input = {'exists': ['X',{'exists':['Y',{'=':['Z','Y']}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 81 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 82 | output = self.f2p.hoist_outer(input) |
| 83 | expected = {'exists': ['Y', {'=': ['Z', 'Y']}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 84 | self.assertEqual(output, expected) |
| 85 | |
| 86 | def test_exists_nested3(self): |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 87 | input = {'exists': ['X',{'exists':['Y',{'=':['Z','X']}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 88 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 89 | output = self.f2p.hoist_outer(input) |
| 90 | expected = {'exists': ['X', {'=': ['Z', 'X']}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 91 | self.assertEqual(output, expected) |
| 92 | |
| 93 | |
| 94 | if __name__ == '__main__': |
| 95 | unittest.main() |
| 96 | |
| 97 | |