blob: e31deb8df2acbf08faa61ac39f2267a04e21c5fe [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
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 Bhatia3e3c1cd2017-07-15 01:35:44 -040017import unittest
18from xosgenx.jinja2_extensions.fol2 import FOL2Python
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040019
20class XProtoOptimizeTest(unittest.TestCase):
21 def setUp(self):
22 self.f2p = FOL2Python()
Sapan Bhatiab69f4702017-07-31 16:03:33 -040023 self.maxDiff=None
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040024
25 def test_constant(self):
26 input = 'True'
Sapan Bhatiab69f4702017-07-31 16:03:33 -040027 output = self.f2p.hoist_outer(input)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040028 self.assertEqual(output, input)
29
30 def test_exists(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040031 input = {'exists': ['X',{'|':['X.foo','y']}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040032
Sapan Bhatiab69f4702017-07-31 16:03:33 -040033 output = self.f2p.hoist_outer(input)
34 expected = {'|': ['y', {'&': [{'not': 'y'}, {'exists': ['X', 'X.foo']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040035 self.assertEqual(output, expected)
36
Sapan Bhatiab69f4702017-07-31 16:03:33 -040037 def test_exists_implies(self):
38 input = {'exists': ['Foo', {'&': [{'=': ('Foo.a', '1')}, {'->': ['write_access', {'=': ('Foo.b', '1')}]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040039
Sapan Bhatiab69f4702017-07-31 16:03:33 -040040 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 Bhatia3e3c1cd2017-07-15 01:35:44 -040049 self.assertEqual(output, expected)
50
51 def test_exists_embedded(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040052 input = {'&':['True',{'exists': ['X',{'|':['X.foo','y']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040053
Sapan Bhatiab69f4702017-07-31 16:03:33 -040054 output = self.f2p.hoist_outer(input)
55 expected = {'|': ['y', {'&': [{'not': 'y'}, {'exists': ['X', 'X.foo']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040056 self.assertEqual(output, expected)
57
58 def test_exists_equals(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040059 input = {'&':['True',{'exists': ['X',{'|':['X.foo',{'=':['y','z']}]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040060
Sapan Bhatiab69f4702017-07-31 16:03:33 -040061 output = self.f2p.hoist_outer(input)
62 expected = {'|': [{'=': ['y', 'z']}, {'&': [{'not': {'=': ['y', 'z']}}, {'exists': ['X', 'X.foo']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040063 self.assertEqual(output, expected)
64
65 def test_exists_nested_constant(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040066 input = {'&':['True',{'exists': ['X',{'|':['y',{'=':['y','X.foo']}]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040067
Sapan Bhatiab69f4702017-07-31 16:03:33 -040068 output = self.f2p.hoist_outer(input)
69 expected = {'|': ['y', {'&': [{'not': 'y'}, {'exists': ['X', {'=': ['False', 'X.foo']}]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040070 self.assertEqual(output, expected)
71
72 def test_exists_nested(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040073 input = {'exists': ['X',{'exists':['Y',{'=':['Y.foo','X.foo']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040074
Sapan Bhatiab69f4702017-07-31 16:03:33 -040075 output = self.f2p.hoist_outer(input)
76 expected = input
77 self.assertEqual(input, output)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040078
79 def test_exists_nested2(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040080 input = {'exists': ['X',{'exists':['Y',{'=':['Z','Y']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040081
Sapan Bhatiab69f4702017-07-31 16:03:33 -040082 output = self.f2p.hoist_outer(input)
83 expected = {'exists': ['Y', {'=': ['Z', 'Y']}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040084 self.assertEqual(output, expected)
85
86 def test_exists_nested3(self):
Sapan Bhatiab69f4702017-07-31 16:03:33 -040087 input = {'exists': ['X',{'exists':['Y',{'=':['Z','X']}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040088
Sapan Bhatiab69f4702017-07-31 16:03:33 -040089 output = self.f2p.hoist_outer(input)
90 expected = {'exists': ['X', {'=': ['Z', 'X']}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040091 self.assertEqual(output, expected)
92
93
94if __name__ == '__main__':
95 unittest.main()
96
97