blob: 5791542cd3144c909986c1966c9579c7815ec7d1 [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
Zack Williams9a42f872019-02-15 17:56:04 -070016from __future__ import absolute_import
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040017import unittest
18from xosgenx.jinja2_extensions.fol2 import FOL2Python
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040019
Zack Williams045b63d2019-01-22 16:30:57 -070020
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040021class XProtoOptimizeTest(unittest.TestCase):
22 def setUp(self):
23 self.f2p = FOL2Python()
Zack Williams045b63d2019-01-22 16:30:57 -070024 self.maxDiff = None
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040025
26 def test_constant(self):
Zack Williams045b63d2019-01-22 16:30:57 -070027 input = "True"
Sapan Bhatiab69f4702017-07-31 16:03:33 -040028 output = self.f2p.hoist_outer(input)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040029 self.assertEqual(output, input)
30
31 def test_exists(self):
Zack Williams045b63d2019-01-22 16:30:57 -070032 input = {"exists": ["X", {"|": ["X.foo", "y"]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040033
Sapan Bhatiab69f4702017-07-31 16:03:33 -040034 output = self.f2p.hoist_outer(input)
Zack Williams045b63d2019-01-22 16:30:57 -070035 expected = {"|": ["y", {"&": [{"not": "y"}, {"exists": ["X", "X.foo"]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040036 self.assertEqual(output, expected)
Zack Williams045b63d2019-01-22 16:30:57 -070037
Sapan Bhatiab69f4702017-07-31 16:03:33 -040038 def test_exists_implies(self):
Zack Williams045b63d2019-01-22 16:30:57 -070039 input = {
40 "exists": [
41 "Foo",
42 {
43 "&": [
44 {"=": ("Foo.a", "1")},
45 {"->": ["write_access", {"=": ("Foo.b", "1")}]},
46 ]
47 },
48 ]
49 }
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040050
Sapan Bhatiab69f4702017-07-31 16:03:33 -040051 output = self.f2p.hoist_outer(input)
Zack Williams045b63d2019-01-22 16:30:57 -070052 expected = {
53 "|": [
54 {
55 "&": [
56 "write_access",
57 {
58 "exists": [
59 "Foo",
60 {"&": [{"=": ["Foo.a", "1"]}, {"=": ["Foo.b", "1"]}]},
61 ]
62 },
63 ]
64 },
65 {
66 "&": [
67 {"not": "write_access"},
68 {"exists": ["Foo", {"=": ["Foo.a", "1"]}]},
69 ]
70 },
71 ]
72 }
Sapan Bhatiab69f4702017-07-31 16:03:33 -040073 self.assertEqual(output, expected)
74
75 def test_forall(self):
Zack Williams045b63d2019-01-22 16:30:57 -070076 input = {"forall": ["X", {"|": ["X.foo", "y"]}]}
Sapan Bhatiab69f4702017-07-31 16:03:33 -040077
78 output = self.f2p.hoist_outer(input)
Zack Williams045b63d2019-01-22 16:30:57 -070079 expected = {"|": ["y", {"&": [{"not": "y"}, {"forall": ["X", "X.foo"]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040080 self.assertEqual(output, expected)
81
82 def test_exists_embedded(self):
Zack Williams045b63d2019-01-22 16:30:57 -070083 input = {"&": ["True", {"exists": ["X", {"|": ["X.foo", "y"]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040084
Sapan Bhatiab69f4702017-07-31 16:03:33 -040085 output = self.f2p.hoist_outer(input)
Zack Williams045b63d2019-01-22 16:30:57 -070086 expected = {"|": ["y", {"&": [{"not": "y"}, {"exists": ["X", "X.foo"]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040087 self.assertEqual(output, expected)
Zack Williams045b63d2019-01-22 16:30:57 -070088
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040089 def test_exists_equals(self):
Zack Williams045b63d2019-01-22 16:30:57 -070090 input = {"&": ["True", {"exists": ["X", {"|": ["X.foo", {"=": ["y", "z"]}]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040091
Sapan Bhatiab69f4702017-07-31 16:03:33 -040092 output = self.f2p.hoist_outer(input)
Zack Williams045b63d2019-01-22 16:30:57 -070093 expected = {
94 "|": [
95 {"=": ["y", "z"]},
96 {"&": [{"not": {"=": ["y", "z"]}}, {"exists": ["X", "X.foo"]}]},
97 ]
98 }
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -040099 self.assertEqual(output, expected)
100
101 def test_exists_nested_constant(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700102 input = {"&": ["True", {"exists": ["X", {"|": ["y", {"=": ["y", "X.foo"]}]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400103
Sapan Bhatiab69f4702017-07-31 16:03:33 -0400104 output = self.f2p.hoist_outer(input)
Zack Williams045b63d2019-01-22 16:30:57 -0700105 expected = {
106 "|": [
107 "y",
108 {"&": [{"not": "y"}, {"exists": ["X", {"=": ["False", "X.foo"]}]}]},
109 ]
110 }
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400111 self.assertEqual(output, expected)
112
113 def test_exists_nested(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700114 input = {"exists": ["X", {"exists": ["Y", {"=": ["Y.foo", "X.foo"]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400115
Sapan Bhatiab69f4702017-07-31 16:03:33 -0400116 output = self.f2p.hoist_outer(input)
117 expected = input
118 self.assertEqual(input, output)
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400119
120 def test_exists_nested2(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700121 input = {"exists": ["X", {"exists": ["Y", {"=": ["Z", "Y"]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400122
Sapan Bhatiab69f4702017-07-31 16:03:33 -0400123 output = self.f2p.hoist_outer(input)
Zack Williams045b63d2019-01-22 16:30:57 -0700124 expected = {"exists": ["Y", {"=": ["Z", "Y"]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400125 self.assertEqual(output, expected)
126
127 def test_exists_nested3(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700128 input = {"exists": ["X", {"exists": ["Y", {"=": ["Z", "X"]}]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400129
Sapan Bhatiab69f4702017-07-31 16:03:33 -0400130 output = self.f2p.hoist_outer(input)
Zack Williams045b63d2019-01-22 16:30:57 -0700131 expected = {"exists": ["X", {"=": ["Z", "X"]}]}
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400132 self.assertEqual(output, expected)
133
134
Zack Williams045b63d2019-01-22 16:30:57 -0700135if __name__ == "__main__":
Sapan Bhatia3e3c1cd2017-07-15 01:35:44 -0400136 unittest.main()