blob: c86b736aa5267d10cb3ca4be0d50f832cd922052 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040016import unittest
17from xosgenx.jinja2_extensions.fol2 import FOL2Python
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040018
Zack Williams045b63d2019-01-22 16:30:57 -070019
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040020class XProtoOptimizeTest(unittest.TestCase):
21 def setUp(self):
22 self.f2p = FOL2Python()
Zack Williams045b63d2019-01-22 16:30:57 -070023 self.maxDiff = None
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040024
25 def test_constant(self):
Zack Williams045b63d2019-01-22 16:30:57 -070026 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):
Zack Williams045b63d2019-01-22 16:30:57 -070031 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)
Zack Williams045b63d2019-01-22 16:30:57 -070034 expected = {"|": ["y", {"&": [{"not": "y"}, {"exists": ["X", "X.foo"]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040035 self.assertEqual(output, expected)
Zack Williams045b63d2019-01-22 16:30:57 -070036
Sapan Bhatiab69f4702017-07-31 16:03:33 -040037 def test_exists_implies(self):
Zack Williams045b63d2019-01-22 16:30:57 -070038 input = {
39 "exists": [
40 "Foo",
41 {
42 "&": [
43 {"=": ("Foo.a", "1")},
44 {"->": ["write_access", {"=": ("Foo.b", "1")}]},
45 ]
46 },
47 ]
48 }
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040049
Sapan Bhatiab69f4702017-07-31 16:03:33 -040050 output = self.f2p.hoist_outer(input)
Zack Williams045b63d2019-01-22 16:30:57 -070051 expected = {
52 "|": [
53 {
54 "&": [
55 "write_access",
56 {
57 "exists": [
58 "Foo",
59 {"&": [{"=": ["Foo.a", "1"]}, {"=": ["Foo.b", "1"]}]},
60 ]
61 },
62 ]
63 },
64 {
65 "&": [
66 {"not": "write_access"},
67 {"exists": ["Foo", {"=": ["Foo.a", "1"]}]},
68 ]
69 },
70 ]
71 }
Sapan Bhatiab69f4702017-07-31 16:03:33 -040072 self.assertEqual(output, expected)
73
74 def test_forall(self):
Zack Williams045b63d2019-01-22 16:30:57 -070075 input = {"forall": ["X", {"|": ["X.foo", "y"]}]}
Sapan Bhatiab69f4702017-07-31 16:03:33 -040076
77 output = self.f2p.hoist_outer(input)
Zack Williams045b63d2019-01-22 16:30:57 -070078 expected = {"|": ["y", {"&": [{"not": "y"}, {"forall": ["X", "X.foo"]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040079 self.assertEqual(output, expected)
80
81 def test_exists_embedded(self):
Zack Williams045b63d2019-01-22 16:30:57 -070082 input = {"&": ["True", {"exists": ["X", {"|": ["X.foo", "y"]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040083
Sapan Bhatiab69f4702017-07-31 16:03:33 -040084 output = self.f2p.hoist_outer(input)
Zack Williams045b63d2019-01-22 16:30:57 -070085 expected = {"|": ["y", {"&": [{"not": "y"}, {"exists": ["X", "X.foo"]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040086 self.assertEqual(output, expected)
Zack Williams045b63d2019-01-22 16:30:57 -070087
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040088 def test_exists_equals(self):
Zack Williams045b63d2019-01-22 16:30:57 -070089 input = {"&": ["True", {"exists": ["X", {"|": ["X.foo", {"=": ["y", "z"]}]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040090
Sapan Bhatiab69f4702017-07-31 16:03:33 -040091 output = self.f2p.hoist_outer(input)
Zack Williams045b63d2019-01-22 16:30:57 -070092 expected = {
93 "|": [
94 {"=": ["y", "z"]},
95 {"&": [{"not": {"=": ["y", "z"]}}, {"exists": ["X", "X.foo"]}]},
96 ]
97 }
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040098 self.assertEqual(output, expected)
99
100 def test_exists_nested_constant(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700101 input = {"&": ["True", {"exists": ["X", {"|": ["y", {"=": ["y", "X.foo"]}]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400102
Sapan Bhatiab69f4702017-07-31 16:03:33 -0400103 output = self.f2p.hoist_outer(input)
Zack Williams045b63d2019-01-22 16:30:57 -0700104 expected = {
105 "|": [
106 "y",
107 {"&": [{"not": "y"}, {"exists": ["X", {"=": ["False", "X.foo"]}]}]},
108 ]
109 }
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400110 self.assertEqual(output, expected)
111
112 def test_exists_nested(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700113 input = {"exists": ["X", {"exists": ["Y", {"=": ["Y.foo", "X.foo"]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400114
Sapan Bhatiab69f4702017-07-31 16:03:33 -0400115 output = self.f2p.hoist_outer(input)
116 expected = input
117 self.assertEqual(input, output)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400118
119 def test_exists_nested2(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700120 input = {"exists": ["X", {"exists": ["Y", {"=": ["Z", "Y"]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400121
Sapan Bhatiab69f4702017-07-31 16:03:33 -0400122 output = self.f2p.hoist_outer(input)
Zack Williams045b63d2019-01-22 16:30:57 -0700123 expected = {"exists": ["Y", {"=": ["Z", "Y"]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400124 self.assertEqual(output, expected)
125
126 def test_exists_nested3(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700127 input = {"exists": ["X", {"exists": ["Y", {"=": ["Z", "X"]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400128
Sapan Bhatiab69f4702017-07-31 16:03:33 -0400129 output = self.f2p.hoist_outer(input)
Zack Williams045b63d2019-01-22 16:30:57 -0700130 expected = {"exists": ["X", {"=": ["Z", "X"]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400131 self.assertEqual(output, expected)
132
133
Zack Williams045b63d2019-01-22 16:30:57 -0700134if __name__ == "__main__":
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400135 unittest.main()