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_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_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_extern_types(): class X(): pass hdt.Set((X(), X(), 10)) hdt.Map(((X(), X()), (X(), 10)))
def test_map_conversion(): m = hdt.Map((("a", 11), ("b", 12))) assert m.to_dict() == {"a": 11, "b": 12}
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)