def test_unify_svar(): v1 = var() v2 = var() v3 = var() sv1 = svar() sv2 = svar() d = TU.unify_raw(L([v1, (sv1, ), (v1, sv1)]), L([v2, v3, (v2, sv2)]), {v3: (1, 2, 3)}) assert d[sv2] == (1, 2, 3)
def test_clone(): fv1 = var(filter=lambda v: isinstance(v, int)) fv2 = TU.clone(fv1) assert fv1 is not fv2 assert fv1.filter is fv2.filter rv1 = var(filter=[None, 0]) rv2 = TU.clone(rv1) assert rv1 is not rv2 assert rv1.legal_values == rv2.legal_values sv1 = svar() sv2 = TU.clone(sv1) assert sv1 is not sv2 assert isinstance(sv2, SVar) v1 = var() v2 = var() uv1 = uvar([(v1, v2), L([v2])]) uv2 = TU.clone(uv1) assert uv1 is not uv2 assert isinstance(uv2, UnionVar) assert len(uv2.values) == 2 vv = None for it in uv2.values: if isinstance(it, tuple): if vv is None: vv = it[1] else: assert vv is it[1] elif isinstance(it, L): if vv is None: vv = it[0] else: assert vv is it[0] else: raise AssertionError("Bad item in clone") l1 = L([v1]) l2 = TU.clone(l1) assert l1 is not l2 assert TU.unify(l1, l2)[v1] is l2[0] t1 = (v1, v1, v2) t2 = TU.clone(t1) assert t1 is not t2 assert t2[0] is t2[1] assert t2[0] != t2[2] assert len(t2) == 3 b = TU.clone(None) assert b is None s1 = Seq((v1, 2, 3)) s2 = TU.clone(s1) assert s1[0] is not s2[0] assert s2[1:] == Seq((2, 3))
def test_reify(): v1 = var() v2 = var() v3 = var() v4 = var() sv = svar() d = {v1: 2.0, v2: None, v3: 3, v4: (v1, v3)} t = TU.reify(v4, d) assert t == (2.0, 3) d = {v1: 3.0} t = TU.reify(L([v1]), d) assert t == L([3.0]) d = {sv: Seq((3, 4))} t = TU.reify((1, 2, sv), d) assert t == (1, 2, 3, 4)
def test_unify_union(): v1 = var() v2 = var() sv1 = svar() uv1 = uvar((1, 2)) dd = {} d = TU.unify_union(uv1, 2, dd) assert len(d) == 0 assert d is dd with pytest.raises(UnificationError): TU.unify_union(uv1, 3, {}) dd = {} uv2 = uvar([(v1, ), L([v1])]) d = TU.unify_union(uv2, v2, dd) assert len(d) == 1 assert d is dd assert d[v2].values == uv2.values dd = {} uv2 = uvar([(v1, ), L([v1])]) d = TU.unify_union(uv2, (v2, ), dd) assert len(d) == 1 assert d is dd assert d[v1] == v2 dd = {} uv2 = uvar([(v1, v2, sv1), L([v2])]) d = TU.unify_union(uv2, (1, 2), dd) assert len(d) == 3 assert d is dd assert d[v2] == 2 uv3 = uvar([(v1, ), (v1, sv1)]) with pytest.raises(UnificationError): TU.unify_union(uv3, (v2, ), {})
def test_unify_raw(): v1 = var() v2 = var() v3 = var() d = TU.unify_raw(v1, None, {}) assert d[v1] is None d = TU.unify_raw(None, v1, {}) assert d[v1] is None d = TU.unify_raw(v1, v2, {}) assert d[v1] is v2 d = TU.unify_raw(v1, None, {v1: v2}) assert d[v2] is None d = TU.unify_raw(v1, v3, {v1: v2, v3: None}) assert d[v2] is None d = TU.unify_raw(L([v1]), L([None]), {}) assert len(d) == 1 assert d[v1] is None d = TU.unify_raw((v1, v1, v3), (v2, v3, None), {}) assert len(d) == 3 assert d[v1] == v2 assert d[v2] == v3 assert d[v3] is None with pytest.raises(UnificationError): TU.unify_raw(1, 2, {}) with pytest.raises(UnificationError): TU.unify_raw((v1, v1, v3), (v2, v3), {}) with pytest.raises(UnificationError): TU.unify_raw((v1, v1, v3), L([v2, v2, v3]), {}) sv1 = svar() sv2 = svar() d = TU.unify_raw((sv1, ), (v1, v2), {}) assert len(d) == 1 assert d[sv1] == Seq((v1, v2)) d = TU.unify_raw((v1, sv1), (v1, v2), {}) assert len(d) == 1 assert d[sv1] == Seq((v2, )) with pytest.raises(UnificationError): TU.unify_raw((v1, sv1), (sv2, v2), {}) with pytest.raises(UnificationError): TU.unify_raw((sv1, sv2), (v1, v2), {}) with pytest.raises(UnificationError): TU.unify_raw((v1, v2), (sv1, sv2), {}) d = TU.unify_raw((v1, sv1), (v2, sv2), {}) assert len(d) == 2 assert d[sv1] is sv2 assert d[v1] is v2 d = TU.unify_raw((v1, sv1), (v2, v2, v3), {sv1: Seq((v1, v1))}) assert len(d) == 3 assert d[v1] is v2 assert d[v2] is v3 uv = uvar([(v1, ), L([v1])]) d = TU.unify_raw(uv, (v2, ), {}) assert len(d) == 1 assert d[v1] is v2 d = TU.unify_raw((v2, ), uv, {}) assert len(d) == 1 assert d[v1] is v2
def test_svar(): sv = svar() assert type(sv) is SVar