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