|
| 1 | +from unification import var |
| 2 | + |
| 3 | +from symbolic_pymc.meta import MetaSymbol, MetaOp |
| 4 | +from symbolic_pymc.utils import meta_diff, eq_lvar |
| 5 | + |
| 6 | + |
| 7 | +class SomeOp(object): |
| 8 | + def __repr__(self): |
| 9 | + return "<SomeOp>" |
| 10 | + |
| 11 | + |
| 12 | +class SomeType(object): |
| 13 | + def __init__(self, field1, field2): |
| 14 | + self.field1 = field1 |
| 15 | + self.field2 = field2 |
| 16 | + |
| 17 | + def __repr__(self): |
| 18 | + return f"SomeType({self.field1}, {self.field2})" |
| 19 | + |
| 20 | + def __str__(self): |
| 21 | + return f"SomeType<{self.field1}, {self.field2}>" |
| 22 | + |
| 23 | + |
| 24 | +class SomeMetaSymbol(MetaSymbol): |
| 25 | + __slots__ = ("field1", "field2", "_blah") |
| 26 | + base = SomeType |
| 27 | + |
| 28 | + def __init__(self, obj=None): |
| 29 | + super().__init__(obj) |
| 30 | + self.field1 = 1 |
| 31 | + self.field2 = 2 |
| 32 | + self._blah = "a" |
| 33 | + |
| 34 | + |
| 35 | +class SomeMetaOp(MetaOp): |
| 36 | + __slots__ = () |
| 37 | + base = SomeOp |
| 38 | + |
| 39 | + def output_meta_types(self): |
| 40 | + return [SomeMetaSymbol] |
| 41 | + |
| 42 | + def __call__(self, *args, **kwargs): |
| 43 | + return SomeMetaSymbol(*args, **kwargs) |
| 44 | + |
| 45 | + |
| 46 | +class SomeOtherMetaSymbol(MetaSymbol): |
| 47 | + __slots__ = ("field1", "field2") |
| 48 | + base = SomeType |
| 49 | + |
| 50 | + def __init__(self, field1, field2, obj=None): |
| 51 | + super().__init__(obj) |
| 52 | + self.field1 = field1 |
| 53 | + self.field2 = field2 |
| 54 | + |
| 55 | + |
| 56 | +class SomeOtherOp(object): |
| 57 | + def __repr__(self): |
| 58 | + return "<SomeOp>" |
| 59 | + |
| 60 | + |
| 61 | +class SomeOtherMetaOp(SomeMetaOp): |
| 62 | + base = SomeOtherOp |
| 63 | + |
| 64 | + |
| 65 | +def test_parts_unequal(): |
| 66 | + s0 = SomeMetaSymbol() |
| 67 | + s1 = SomeOtherMetaSymbol(1, 2) |
| 68 | + |
| 69 | + res = meta_diff(s0, s1) |
| 70 | + assert res.reason == "types" |
| 71 | + assert res.path(s0) is s0 |
| 72 | + assert res.objects == (s0, s1) |
| 73 | + |
| 74 | + res = meta_diff(s0, s1, cmp_types=False) |
| 75 | + assert res is None |
| 76 | + |
| 77 | + s2 = SomeOtherMetaSymbol(1, 3) |
| 78 | + res = meta_diff(s0, s2, cmp_types=False) |
| 79 | + assert res.path(s2) == 3 |
| 80 | + assert res.path(s1) == 2 |
| 81 | + assert res.reason == "ne_fn" |
| 82 | + assert res.objects == (2, 3) |
| 83 | + |
| 84 | + res = meta_diff(SomeMetaOp(), SomeMetaOp()) |
| 85 | + assert res is None |
| 86 | + |
| 87 | + op1 = SomeMetaOp() |
| 88 | + op2 = SomeOtherMetaOp() |
| 89 | + res = meta_diff(op1, op2, cmp_types=False) |
| 90 | + assert res.path(op1) is op1 |
| 91 | + assert res.reason == "bases" |
| 92 | + assert res.objects == (op1.base, op2.base) |
| 93 | + |
| 94 | + a = SomeOtherMetaSymbol(1, [2, SomeOtherMetaSymbol(3, 4)]) |
| 95 | + b = SomeOtherMetaSymbol(1, [2, SomeOtherMetaSymbol(3, 5)]) |
| 96 | + res = meta_diff(a, b) |
| 97 | + |
| 98 | + assert res.path(a) == 4 |
| 99 | + assert res.path(b) == 5 |
| 100 | + assert res.reason == "ne_fn" |
| 101 | + assert res.objects == (4, 5) |
| 102 | + |
| 103 | + a = SomeOtherMetaSymbol(1, [2, SomeOtherMetaSymbol(3, 4)]) |
| 104 | + b = SomeOtherMetaSymbol(1, [2, SomeOtherMetaSymbol(3, 4)]) |
| 105 | + res = meta_diff(a, b) |
| 106 | + assert res is None |
| 107 | + |
| 108 | + a = SomeOtherMetaSymbol(1, [2, SomeOtherMetaSymbol(3, 4), 5]) |
| 109 | + b = SomeOtherMetaSymbol(1, [2, SomeOtherMetaSymbol(3, 4)]) |
| 110 | + res = meta_diff(a, b) |
| 111 | + assert res is not None |
| 112 | + assert res.reason == "seq len" |
| 113 | + |
| 114 | + a = SomeOtherMetaSymbol(1, ["a", "b"]) |
| 115 | + b = SomeOtherMetaSymbol(1, 2) |
| 116 | + res = meta_diff(a, b, cmp_types=False) |
| 117 | + assert res is not None |
| 118 | + assert res.reason == "ne_fn" |
| 119 | + |
| 120 | + a = SomeOtherMetaSymbol(1, ["a", "b"]) |
| 121 | + b = SomeOtherMetaSymbol(1, "ab") |
| 122 | + res = meta_diff(a, b, cmp_types=False) |
| 123 | + assert res is not None |
| 124 | + |
| 125 | + a = SomeOtherMetaSymbol(1, {"a": 1, "b": 2}) |
| 126 | + b = SomeOtherMetaSymbol(1, {"b": 2, "a": 1}) |
| 127 | + res = meta_diff(a, b) |
| 128 | + assert res is None |
| 129 | + |
| 130 | + a = SomeOtherMetaSymbol(1, {"a": 1, "b": 2}) |
| 131 | + b = SomeOtherMetaSymbol(1, {"b": 3, "a": 1}) |
| 132 | + res = meta_diff(a, b) |
| 133 | + assert res.reason == "ne_fn" |
| 134 | + assert res.objects == (2, 3) |
| 135 | + assert res.path(a) == 2 |
| 136 | + assert res.path(b) == 3 |
| 137 | + |
| 138 | + a = SomeOtherMetaSymbol(1, {"a": 1, "b": 2}) |
| 139 | + b = SomeOtherMetaSymbol(1, {"a": 1, "c": 2}) |
| 140 | + res = meta_diff(a, b) |
| 141 | + assert res.reason == "map keys" |
| 142 | + assert res.path(a) == {"a": 1, "b": 2} |
| 143 | + assert res.objects == ([("a", 1), ("b", 2)], [("a", 1), ("c", 2)]) |
| 144 | + |
| 145 | + |
| 146 | +def test_eq_lvar(): |
| 147 | + a = SomeOtherMetaSymbol(1, [2, SomeOtherMetaSymbol(3, 4)]) |
| 148 | + b = SomeOtherMetaSymbol(1, [2, SomeOtherMetaSymbol(3, 4)]) |
| 149 | + assert eq_lvar(a, b) is True |
| 150 | + |
| 151 | + a = SomeOtherMetaSymbol(1, [2, SomeOtherMetaSymbol(3, 4)]) |
| 152 | + b = SomeOtherMetaSymbol(1, [2, var()]) |
| 153 | + assert eq_lvar(a, b) is False |
| 154 | + |
| 155 | + a = SomeOtherMetaSymbol(1, [2, var()]) |
| 156 | + b = SomeOtherMetaSymbol(1, [2, var()]) |
| 157 | + assert eq_lvar(a, b) is True |
| 158 | + |
| 159 | + a = SomeOtherMetaSymbol(1, [2, {"a": var()}]) |
| 160 | + b = SomeOtherMetaSymbol(1, [2, {"a": var()}]) |
| 161 | + assert eq_lvar(a, b) is True |
| 162 | + |
| 163 | + a = SomeOtherMetaSymbol(1, [3, var()]) |
| 164 | + b = SomeOtherMetaSymbol(1, [2, var()]) |
| 165 | + assert eq_lvar(a, b) is False |
0 commit comments