def test_compare_lists(): ax = USet(3, "a") bx = USet(5, "b") a1, a2, a3 = ax.all() b3 = bx.get(3) b4 = bx.get(4) assert hdt.compare((), ()) == 0 assert hdt.compare((a1,), (a1,)) == 0 assert hdt.compare((a1, b3), (a1, b3)) == 0 assert hdt.compare((a2, a2), (a2, a2)) == 0 assert hdt.compare((a1,), (a2,)) == -1 assert hdt.compare((a1, b3), (b3, a1)) == -1 assert hdt.compare((a1, a1), (a1, a2)) == -1 assert hdt.compare((a2,), (a1,)) == 1 assert hdt.compare((a3, b3), (a1, a1)) == 1 assert hdt.compare((a1, a3), (a1, a2)) == 1 assert hdt.compare((a2,), (a1, a3)) == -1 assert hdt.compare((a2,), (a3, a3)) == -1 assert hdt.compare((b3, b4), ()) == 1 assert hdt.compare((b3, b4), (a1,)) == 1
def test_compare_atoms(): ax = USet(3, "a") bx = USet(5, "b") a1, a2, a3 = ax.all() b3 = bx.get(3) b4 = bx.get(4) assert hdt.compare(a1, a1) == 0 assert hdt.compare(a2, a2) == 0 assert hdt.compare(a3, a3) == 0 assert hdt.compare(b3, b3) == 0 assert hdt.compare(b4, b4) == 0 assert hdt.compare(a1, a2) == -1 assert hdt.compare(a1, a3) == -1 assert hdt.compare(a2, a3) == -1 assert hdt.compare(a1, b3) == -1 assert hdt.compare(b3, b4) == -1 assert hdt.compare(a3, b3) == -1 assert hdt.compare(a2, a1) == 1 assert hdt.compare(a3, a1) == 1 assert hdt.compare(a3, a2) == 1 assert hdt.compare(b3, a1) == 1 assert hdt.compare(b4, b3) == 1 assert hdt.compare(b3, a3) == 1
def test_compare_types(): ax = USet(3, "a") a1 = ax.get(1) class X(object): def __init__(self, v): self.v = v def __cmp__(self, other): if isinstance(other, X): return cmp(self.v, other.v) raise Exception("Not comparable") class Y(object): pass assert hdt.compare(a1, 10) == -1 assert hdt.compare(a1, "A") == -1 assert hdt.compare(a1, (a1,)) == -1 assert hdt.compare("B", hdt.Map((a1, a1))) == -1 assert hdt.compare((a1,), "C") == 1 assert hdt.compare((a1,), X(10)) == -1 assert hdt.compare(Y(), 10) == 1 assert hdt.compare(X(11), Y()) != 0 assert hdt.compare(X(10), X(10)) == 0 assert hdt.compare(X(10), X(100)) == -1 assert hdt.compare(X(100), X(10)) == 1
def test_compare_types(): ax = USet(3, "a") a1 = ax.get(1) class X(object): def __init__(self, v): self.v = v def __cmp__(self, other): if isinstance(other, X): return cmp(self.v, other.v) raise Exception("Not comparable") class Y(object): pass assert hdt.compare(a1, 10) == -1 assert hdt.compare(a1, "A") == -1 assert hdt.compare(a1, (a1, )) == -1 assert hdt.compare("B", hdt.Map((a1, a1))) == -1 assert hdt.compare((a1, ), "C") == 1 assert hdt.compare((a1, ), X(10)) == -1 assert hdt.compare(Y(), 10) == 1 assert hdt.compare(X(11), Y()) != 0 assert hdt.compare(X(10), X(10)) == 0 assert hdt.compare(X(10), X(100)) == -1 assert hdt.compare(X(100), X(10)) == 1
def test_compare_maps(): ax = USet(3, "a") bx = USet(5, "b") a1, a2, a3 = ax.all() b3 = bx.get(3) b4 = bx.get(4) m1 = hdt.Map(( (a1, a2), (a2, a1), (a3, a3) )) m2 = hdt.Map(( (a1, a1), (a2, a2), (a3, a3) )) m3 = hdt.Map(( (a1, a1), (b3, a1), (a3, a1) )) m4 = hdt.Map(( (a1, a1), (b3, a1), (a3, a1), (b4, a1) )) assert hdt.compare(m1, m1) == 0 assert hdt.compare(m2, m2) == 0 assert hdt.compare(m1, m2) == 1 assert hdt.compare(m2, m1) == -1 assert hdt.compare(m3, m1) == -1 assert hdt.compare(m1, m3) == 1 assert hdt.compare(m1, m4) == -1 assert hdt.compare(m4, m1) == 1 assert hdt.compare(m3, m4) == -1 assert hdt.compare(m4, m2) == 1
def test_replace_atoms(): ax = USet(3, "a") a, b, c = ax.all() assert ((b, ), (b, b)) == hdt.replace_atoms([[a], [a, a]], lambda x: b) assert ((c, a), ((c, c), a)) == hdt.replace_atoms([[b, a], [[b, b], c]], lambda x: c if x == b else a)
def test_replace_atoms(): ax = USet(3, "a") a, b, c = ax.all() assert ((b,), (b, b)) == hdt.replace_atoms([[a], [a, a]], lambda x: b) assert ((c, a), ((c, c), a)) == hdt.replace_atoms( [[b, a], [[b, b], c]], lambda x: c if x == b else a)
def test_collect_atoms(): ax = USet(3, "a") a = ax.get(0) b = ax.get(1) assert {a} == set(hdt.collect_atoms(((a,), (a, a)))) assert {a, b} == set(hdt.collect_atoms(((b,), (b, a)))) assert set() == set(hdt.collect_atoms((1, 2, 3)))
def test_collect_atoms(): ax = USet(3, "a") a = ax.get(0) b = ax.get(1) assert {a} == set(hdt.collect_atoms(((a, ), (a, a)))) assert {a, b} == set(hdt.collect_atoms(((b, ), (b, a)))) assert set() == set(hdt.collect_atoms((1, 2, 3)))
def test_compare2_atoms(): ax = USet(3, "a") a1, a2, a3 = ax.all() assert hdt.compare2(a1, {}, a1, {}) == 0 assert hdt.compare2(a2, {}, a2, {}) == 0 assert hdt.compare2(a2, {}, a2, {a2: a1}) == 1 assert hdt.compare2(a1, {}, a2, {a1: a2}) == -1 assert hdt.compare2(a1, {a1: a2}, a2, {a2: a1}) == 1
def test_map_hash(): ax = USet(3, "a") m1 = hdt.Map(( (ax.get(2), 11), ("b", 12) )) m2 = hdt.Map(( (ax.get(2), 11), ("b", 12) )) assert m1 == m2 assert hash(m1) == hash(m2)
def test_compare2_tuples(): ax = USet(3, "a") bx = USet(2, "b") a1, a2, a3 = ax.all() b1, b2 = bx.all() assert hdt.compare2((a1, b1), {}, (a1, b1), {}) == 0 assert hdt.compare2((a1, b1), {}, (a2, b2), {}) == -1 assert hdt.compare2((a1, b1), {b1: b2}, (a2, b2), {}) == -1 assert hdt.compare2((a1, b1), {b1: b2, a1: a2}, (a2, b2), {}) == 0
def test_canonical_map_map(): ax = USet(2, "a") bx = USet(2, "b") m1 = hd.Mappings(ax, bx) m2 = hd.Mappings(ax, ax) m = hd.Mappings(m1, m2) bf_check(m) m = hd.Mappings(m1, m1) bf_check(m) m = hd.Mappings(m2, m1) bf_check(m)
def test_canonical_sequence(): ax = USet(2, "a") s = hd.Sequences(ax, 0, 4) bf_check(s) s = hd.Sequences(ax * ax, 0, 4) bf_check(s)
def test_compare2_maps(): ax = USet(3, "a") bx = USet(2, "b") a1, a2, a3 = ax.all() b1, b2 = bx.all() m1 = hdt.Map( [(a1, a1), (a2, a2), (a3, a3)]) m2 = hdt.Map( [(a1, b1), (a2, b2), (a3, b1)]) assert hdt.compare2(m1, {}, m1, {}) == 0 assert hdt.compare2(m1, {}, m2, {}) == -1 assert hdt.compare2(m1, {}, m1, {a1: a2, a2: a1}) == 0 assert hdt.compare2(m2, {}, m2, {a1: a2, a2: a1}) == -1 assert hdt.compare2(m2, {a1: a3, a3: a1}, m2, {a1: a2, a2: a1}) == -1
def test_canonical_map_prod(): ax = USet(3, "a") bx = USet(2, "b") cx = USet(1, "b") m1 = hd.Mappings(bx, cx) m2 = hd.Mappings(ax, ax) m3 = hd.Mappings(ax, bx) bf_check(m1 * bx) bf_check(m1 * cx) bf_check(bx * m1) bf_check(cx * m1) bf_check(m1 * m1) bf_check(m3 * m3) bf_check(m3 * m3 * m3) bf_check(m2 * m3)
def test_cannonical_prod_map(): ax = USet(3, "a") bx = USet(2, "b") pb = bx * bx pab = ax * bx m = hd.Mappings(bx, pb) bf_check(m) m = hd.Mappings(pb, bx) bf_check(m) m = hd.Mappings(pb, pb) bf_check(m) m = hd.Mappings(pab, bx) bf_check(m)
def test_canonical_mapping(): ax = USet(3, "a") a0, a1, a2 = ax.all() bx = USet(2, "b") b0, b1 = bx.all() m1 = hd.Map([(b0, b0), (b1, b0)]) m2 = hd.Map([(b0, b0), (b1, b1)]) m3 = hd.Map([(b0, b1), (b1, b0)]) result = list(hd.Mappings(bx, bx).create_cn_iter()) assert result == [m1, m2, m3] m1 = hd.Map([(b0, a0), (b1, a0)]) m2 = hd.Map([(b0, a0), (b1, a1)]) result = list(hd.Mappings(bx, ax).create_cn_iter()) assert result == [m1, m2] m1 = hd.Map([(0, a0), (1, a0)]) m2 = hd.Map([(0, a0), (1, a1)]) result = list(hd.Mappings(hd.Range(2), ax).create_cn_iter()) assert result == [m1, m2] m1 = hd.Map([(0, b0), (1, b0)]) m2 = hd.Map([(0, b0), (1, b1)]) result = list(hd.Mappings(hd.Range(2), bx).create_cn_iter()) assert result == [m1, m2]
def test_compare_maps(): ax = USet(3, "a") bx = USet(5, "b") a1, a2, a3 = ax.all() b3 = bx.get(3) b4 = bx.get(4) m1 = hdt.Map(((a1, a2), (a2, a1), (a3, a3))) m2 = hdt.Map(((a1, a1), (a2, a2), (a3, a3))) m3 = hdt.Map(((a1, a1), (b3, a1), (a3, a1))) m4 = hdt.Map(((a1, a1), (b3, a1), (a3, a1), (b4, a1))) assert hdt.compare(m1, m1) == 0 assert hdt.compare(m2, m2) == 0 assert hdt.compare(m1, m2) == 1 assert hdt.compare(m2, m1) == -1 assert hdt.compare(m3, m1) == -1 assert hdt.compare(m1, m3) == 1 assert hdt.compare(m1, m4) == -1 assert hdt.compare(m4, m1) == 1 assert hdt.compare(m3, m4) == -1 assert hdt.compare(m4, m2) == 1
def test_compare_lists(): ax = USet(3, "a") bx = USet(5, "b") a1, a2, a3 = ax.all() b3 = bx.get(3) b4 = bx.get(4) assert hdt.compare((), ()) == 0 assert hdt.compare((a1, ), (a1, )) == 0 assert hdt.compare((a1, b3), (a1, b3)) == 0 assert hdt.compare((a2, a2), (a2, a2)) == 0 assert hdt.compare((a1, ), (a2, )) == -1 assert hdt.compare((a1, b3), (b3, a1)) == -1 assert hdt.compare((a1, a1), (a1, a2)) == -1 assert hdt.compare((a2, ), (a1, )) == 1 assert hdt.compare((a3, b3), (a1, a1)) == 1 assert hdt.compare((a1, a3), (a1, a2)) == 1 assert hdt.compare((a2, ), (a1, a3)) == -1 assert hdt.compare((a2, ), (a3, a3)) == -1 assert hdt.compare((b3, b4), ()) == 1 assert hdt.compare((b3, b4), (a1, )) == 1
def test_is_isomoprhic(): assert hd.is_isomorphic(0, 0) assert not hd.is_isomorphic(0, 1) assert not hd.is_isomorphic("a", 1) a0, a1 = USet(2, "a") b0, b1 = USet(2, "b") assert not hd.is_isomorphic(a0, b0) assert not hd.is_isomorphic(a1, b0) assert hd.is_isomorphic(a0, a1) assert hd.is_isomorphic(b0, b1) x = (a0, a1) y = (a1, a0) z = (a0, a0) w = (b0, a0) assert hd.is_isomorphic(x, y) assert not hd.is_isomorphic(x, z) assert not hd.is_isomorphic(x, w)
def test_expand(): assert hd.expand(10) == [10] ax = USet(3, "a") a0, a1, a2 = ax assert set(hd.expand(a0)) == {a1, a2, a0} assert set(hd.expand(a1)) == {a1, a2, a0} assert set(hd.expand(a2)) == {a1, a2, a0} assert set(hd.expand((a1, a1))) == {(a0, a0), (a1, a1), (a2, a2)} assert set(hd.expand((a0, a0))) == {(a0, a0), (a1, a1), (a2, a2)} assert set(hd.expand((a1, a2))) == {(a0, a1), (a0, a2), (a1, a0), (a1, a2), (a2, a0), (a2, a1)}
def test_product_is_canonical(): ax = USet(5, "a") bx = USet(2, "b") cx = USet(1, "c") a1 = ax.get(0) a2 = ax.get(1) b1 = bx.get(0) c1 = cx.get(0) p = ax * ax c = [a for a in p if hdc.is_canonical(a)] assert c == [(a1, a1), (a1, a2)] p = ax * bx c = [a for a in p if hdc.is_canonical(a)] assert c == [(a1, b1)] p = cx * cx c = [a for a in p if hdc.is_canonical(a)] assert c == [(c1, c1)]
def test_canonize(): ax = USet(1000, "a") a0 = ax.get(0) a1 = ax.get(1) a2 = ax.get(2) a900 = ax.get(900) bx = USet(2, "b") b0 = bx.get(0) b1 = bx.get(1) assert hd.canonize(a0) == a0 assert hd.canonize(a1) == a0 assert hd.canonize(a900) == a0 assert hd.canonize((a900, a0, a1, a1, 100)) == (a0, a1, a2, a2, 100) assert hd.canonize((a0, b1, b1)) == (a0, b0, b0) assert hd.canonize((a0, b0, b1)) == (a0, b0, b1) assert hd.canonize((a0, b1, b0)) == (a0, b0, b1) assert hd.canonize((a900, b1, b0)) == (a0, b0, b1)
def test_compare2_maps(): ax = USet(3, "a") bx = USet(2, "b") a1, a2, a3 = ax.all() b1, b2 = bx.all() m1 = hdt.Map([(a1, a1), (a2, a2), (a3, a3)]) m2 = hdt.Map([(a1, b1), (a2, b2), (a3, b1)]) assert hdt.compare2(m1, {}, m1, {}) == 0 assert hdt.compare2(m1, {}, m2, {}) == -1 assert hdt.compare2(m1, {}, m1, {a1: a2, a2: a1}) == 0 assert hdt.compare2(m2, {}, m2, {a1: a2, a2: a1}) == -1 assert hdt.compare2(m2, {a1: a3, a3: a1}, m2, {a1: a2, a2: a1}) == -1
def test_candidates(): ax = USet(3, "a") a0, a1, a2 = ax.all() bx = USet(5, "b") b0, b1, b2, b3, b5 = bx.all() result = hdc.create_candidates(a0, {}) assert list(result) == [a0] result = hdc.create_candidates(a0, {bx: 1}) assert list(result) == [a0] result = hdc.create_candidates(a0, {ax: 1}) assert list(result) == [a0, a1] result = hdc.create_candidates(a0, {ax: 2}) assert list(result) == [a0, a1, a2] result = hdc.create_candidates(a0, {ax: 3}) assert list(result) == [a0, a1, a2] result = hdc.create_candidates((a0, a1), {}) assert list(result) == [(a0, a1), (a1, a0)] result = hdc.create_candidates((a0, a1), {bx: 1}) assert list(result) == [(a0, a1), (a1, a0)] result = hdc.create_candidates((a0, a1), {ax: 1}) assert set(result) == {(a0, a1), (a1, a0), (a1, a2), (a2, a1)} result = hdc.create_candidates((a1, a2), {ax: 2}) assert set(result) == {(a0, a1), (a1, a0), (a0, a2), (a2, a0), (a1, a2), (a2, a1)} result = hdc.create_candidates((a1, a2), {ax: 3}) assert set(result) == {(a0, a1), (a1, a0), (a0, a2), (a2, a0), (a1, a2), (a2, a1)} result = hdc.create_candidates((b0, b1), {bx: 2}) assert set(result) == {(b0, b1), (b0, b1), (b0, b2), (b1, b0), (b1, b2), (b2, b0), (b2, b1), (b2, b3), (b3, b2)} result = hdc.create_candidates((a0, b0), {ax: 1, bx: 2}) assert set(result) == {(a0, b0), (a0, b1), (a0, b2), (a1, b0), (a1, b1), (a1, b2)}
def test_map_is_canonical(): ax = USet(2, "a") a1, a2 = ax.all() bx = USet(2, "b") b1, b2 = bx.all() ms = hd.Mappings(ax, bx) c = [item for item in ms if hdc.is_canonical(item)] m1 = hd.Map(((a1, b1), (a2, b1))) m2 = hd.Map(((a1, b1), (a2, b2))) assert hdt.compare_sequence([m1, m2], c) == 0 ms = hd.Mappings(ax, ax) c = [item for item in ms if hdc.is_canonical(item)] m1 = hd.Map(((a1, a1), (a2, a1))) m2 = hd.Map(((a1, a1), (a2, a2))) m3 = hd.Map(((a1, a2), (a2, a1))) assert hdt.compare_sequence([m1, m2, m3], c) == 0
def test_canonical_product(): ax = USet(3, "a") a0, a1, a2 = ax.all() bx = USet(2, "b") b0, b1 = bx.all() result = list(hd.Product((ax, )).create_cn_iter()) assert result == [(a0, )] result = list((ax * ax).create_cn_iter()) assert result == [(a0, a0), (a0, a1)] result = list((ax * ax * ax).create_cn_iter()) assert result == [(a0, a0, a0), (a0, a0, a1), (a0, a1, a0), (a0, a1, a1), (a0, a1, a2)] result = list((bx * bx * bx).create_cn_iter()) assert result == [(b0, b0, b0), (b0, b0, b1), (b0, b1, b0), (b0, b1, b1)] result = list((ax * bx * bx).create_cn_iter()) assert result == [(a0, b0, b0), (a0, b0, b1)]
def test_uset_flags(): ax = USet(3, "a") assert not ax.filtered assert ax.step_jumps assert ax.strict
def test_set_hash(): ax = USet(3, "a") m1 = hdt.Set((ax.get(2), 11)) m2 = hdt.Set((ax.get(2), 11)) assert m1 == m2 assert hash(m1) == hash(m2)
def test_map_hash(): ax = USet(3, "a") m1 = hdt.Map(((ax.get(2), 11), ("b", 12))) m2 = hdt.Map(((ax.get(2), 11), ("b", 12))) assert m1 == m2 assert hash(m1) == hash(m2)
def test_canonical_sets(): ax = USet(4, "a") s = hd.Subsets(ax, 2) bf_check(s) s = hd.Subsets(ax, 0, 2) bf_check(s)
def test_atom_is_canonical(): ax = USet(3, "a") c = [a for a in ax.all() if hdc.is_canonical(a)] assert c == [ax.get(0)]