Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | # 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 Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 16 | import unittest |
| 17 | from xosgenx.jinja2_extensions.fol2 import FOL2Python |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 18 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 19 | |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 20 | class XProtoOptimizeTest(unittest.TestCase): |
| 21 | def setUp(self): |
| 22 | self.f2p = FOL2Python() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 23 | self.maxDiff = None |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 24 | |
| 25 | def test_constant(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 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): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [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) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 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) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 36 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 37 | def test_exists_implies(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 38 | input = { |
| 39 | "exists": [ |
| 40 | "Foo", |
| 41 | { |
| 42 | "&": [ |
| 43 | {"=": ("Foo.a", "1")}, |
| 44 | {"->": ["write_access", {"=": ("Foo.b", "1")}]}, |
| 45 | ] |
| 46 | }, |
| 47 | ] |
| 48 | } |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 49 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 50 | output = self.f2p.hoist_outer(input) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 51 | 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 Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 72 | self.assertEqual(output, expected) |
| 73 | |
| 74 | def test_forall(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 75 | input = {"forall": ["X", {"|": ["X.foo", "y"]}]} |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 76 | |
| 77 | output = self.f2p.hoist_outer(input) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 78 | expected = {"|": ["y", {"&": [{"not": "y"}, {"forall": ["X", "X.foo"]}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 79 | self.assertEqual(output, expected) |
| 80 | |
| 81 | def test_exists_embedded(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 82 | input = {"&": ["True", {"exists": ["X", {"|": ["X.foo", "y"]}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 83 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 84 | output = self.f2p.hoist_outer(input) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 85 | expected = {"|": ["y", {"&": [{"not": "y"}, {"exists": ["X", "X.foo"]}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 86 | self.assertEqual(output, expected) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 87 | |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 88 | def test_exists_equals(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 89 | input = {"&": ["True", {"exists": ["X", {"|": ["X.foo", {"=": ["y", "z"]}]}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 90 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 91 | output = self.f2p.hoist_outer(input) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 92 | expected = { |
| 93 | "|": [ |
| 94 | {"=": ["y", "z"]}, |
| 95 | {"&": [{"not": {"=": ["y", "z"]}}, {"exists": ["X", "X.foo"]}]}, |
| 96 | ] |
| 97 | } |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 98 | self.assertEqual(output, expected) |
| 99 | |
| 100 | def test_exists_nested_constant(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 101 | input = {"&": ["True", {"exists": ["X", {"|": ["y", {"=": ["y", "X.foo"]}]}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 102 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 103 | output = self.f2p.hoist_outer(input) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 104 | expected = { |
| 105 | "|": [ |
| 106 | "y", |
| 107 | {"&": [{"not": "y"}, {"exists": ["X", {"=": ["False", "X.foo"]}]}]}, |
| 108 | ] |
| 109 | } |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 110 | self.assertEqual(output, expected) |
| 111 | |
| 112 | def test_exists_nested(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 113 | input = {"exists": ["X", {"exists": ["Y", {"=": ["Y.foo", "X.foo"]}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 114 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 115 | output = self.f2p.hoist_outer(input) |
| 116 | expected = input |
| 117 | self.assertEqual(input, output) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 118 | |
| 119 | def test_exists_nested2(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 120 | input = {"exists": ["X", {"exists": ["Y", {"=": ["Z", "Y"]}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 121 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 122 | output = self.f2p.hoist_outer(input) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 123 | expected = {"exists": ["Y", {"=": ["Z", "Y"]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 124 | self.assertEqual(output, expected) |
| 125 | |
| 126 | def test_exists_nested3(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 127 | input = {"exists": ["X", {"exists": ["Y", {"=": ["Z", "X"]}]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 128 | |
Sapan Bhatia | b69f470 | 2017-07-31 16:03:33 -0400 | [diff] [blame] | 129 | output = self.f2p.hoist_outer(input) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 130 | expected = {"exists": ["X", {"=": ["Z", "X"]}]} |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 131 | self.assertEqual(output, expected) |
| 132 | |
| 133 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 134 | if __name__ == "__main__": |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 135 | unittest.main() |