示例#1
0
def test_equal_with_different_insertion_order():
    x = pmap([(i, i) for i in range(50)], 10)
    y = pmap([(i, i) for i in range(49, -1, -1)], 10)

    assert x == y
    assert not (x != y)

    assert y == x
    assert not (y != x)
示例#2
0
def test_set_with_relocation():
    x = pmap({"a": 1000})
    x = x.set("b", 3000)
    x = x.set("c", 4000)
    x = x.set("d", 5000)
    x = x.set("d", 6000)

    assert len(x) == 4
    assert x == pmap({"a": 1000, "b": 3000, "c": 4000, "d": 6000})
示例#3
0
def test_equal_with_different_bucket_sizes():
    x = pmap({"a": 1, "b": 2}, 50)
    y = pmap({"a": 1, "b": 2}, 10)

    assert x == y
    assert not (x != y)

    assert y == x
    assert not (y != x)
示例#4
0
def test_evolver_update_with_relocation():
    x = pmap({"a": 1000}, pre_size=1)
    e = x.evolver()
    e["b"] = 3000
    e["c"] = 4000
    e["d"] = 5000
    e["d"] = 6000

    assert len(e) == 4
    assert e.persistent() == pmap({"a": 1000, "b": 3000, "c": 4000, "d": 6000})
示例#5
0
def test_same_hash_when_content_the_same_but_underlying_vector_size_differs():
    x = pmap(dict((x, x) for x in range(1000)))
    y = pmap({10: 10, 200: 200, 700: 700})

    for z in x:
        if z not in y:
            x = x.remove(z)

    assert x == y
    assert hash(x) == hash(y)
示例#6
0
def test_iter():
    container = pmap()
    for key in range(100):
        value = key
        container.set(key, value)
    for index, key in enumerate(sorted(container)):
        assert index == key and index == container[key]
示例#7
0
def test_access_non_existing_element():
    map1 = pmap()
    assert len(map1) == 0

    map2 = map1.set("1", 1)
    assert "1" not in map1
    assert map2["1"] == 1
    assert "2" not in map2
示例#8
0
def test_initialization_with_two_elements():
    map = pmap({"a": 2, "b": 3})
    assert len(map) == 2
    assert map["a"] == 2
    assert map["b"] == 3

    map2 = map.remove("a")
    assert "a" not in map2
    assert map2["b"] == 3
示例#9
0
def test_initialization_with_one_element():
    the_map = pmap({"a": 2})
    assert len(the_map) == 1
    assert the_map["a"] == 2
    # assert the_map.a == 2 # TODO: Implement __getattr__
    assert "a" in the_map

    assert the_map == the_map.discard("b")

    empty_map = the_map.remove("a")
    assert len(empty_map) == 0
    assert "a" not in empty_map
示例#10
0
def test_map():
    map_0 = pmap()
    assert len(map_0) == 0
    assert hash(map_0) == -4800647303603446203
    assert set(map_0.values()) == set()

    map_1 = map_0.set(0, 0)
    assert 0 in map_1
    assert map_1.get(0) == 0
    assert map_1[0] == 0
    assert len(map_1) == 1
    assert hash(map_1) == 2885792717948792520
    assert set(map_1.values()) == {0}

    map_2 = map_1.set("1", 1)
    assert 0 in map_2
    assert map_2.get(0) == 0
    assert map_2[0] == 0
    assert "1" in map_2
    assert map_2.get("1") == 1
    assert map_2["1"] == 1
    assert len(map_2) == 2
    assert set(map_2.keys()) == {0, "1"}
    assert set(map_2.values()) == {0, 1}
    assert set(map_2.items()) == {(0, 0), ("1", 1)}

    map_3 = map_2.set(2, "2")
    assert 0 in map_3
    assert map_3.get(0) == 0
    assert "1" in map_3
    assert map_3.get("1") == 1
    assert 2 in map_3
    assert map_3.get(2) == "2"
    assert len(map_3) == 3
    assert set(map_3.values()) == {0, "2", 1}

    map_4 = map_3.remove(2)
    assert 0 in map_4
    assert map_4.get(0) == 0
    assert "1" in map_4
    assert map_4.get("1") == 1
    assert len(map_4) == 2
    assert set(map_4.values()) == {0, 1}

    assert map_0 != map_1
    assert map_1 != map_2
    assert map_2 != map_3
    assert map_3 != map_4
    assert map_2 == map_4
    assert hash(map_2) == hash(map_4)
示例#11
0
def test_bitmap_indexed_iteration():
    map = pmap({"a": 2, "b": 1})
    keys = set()
    values = set()

    count = 0
    for k, v in map.iteritems():
        count += 1
        keys.add(k)
        values.add(v)

    assert count == 2
    assert keys == set(["a", "b"])
    assert values == set([2, 1])
示例#12
0
def test_evolver_set_with_reallocation_edge_case():
    # Demonstrates a bug in evolver that also affects updates. Under certain
    # circumstances, the result of `x.update(y)` will **not** have all the
    # keys from `y`.
    foo = object()
    x = pmap({"a": foo}, pre_size=1)
    e = x.evolver()
    e["b"] = 3000
    # Bug is triggered when we do a reallocation and the new value is
    # identical to the old one.
    e["a"] = foo

    y = e.persistent()
    assert "b" in y
    assert y is e.persistent()
示例#13
0
def test_hash_collision_is_correctly_resolved():
    dummy1 = HashDummy()
    dummy2 = HashDummy()
    dummy3 = HashDummy()
    dummy4 = HashDummy()

    map = pmap({dummy1: 1, dummy2: 2, dummy3: 3})
    assert map[dummy1] == 1
    assert map[dummy2] == 2
    assert map[dummy3] == 3
    assert dummy4 not in map

    keys = set()
    values = set()
    for k, v in map.iteritems():
        keys.add(k)
        values.add(v)

    assert keys == set([dummy1, dummy2, dummy3])
    assert values == set([1, 2, 3])

    map2 = map.set(dummy1, 11)
    assert map2[dummy1] == 11

    # Re-use existing structure when inserted element is the same
    assert map2.set(dummy1, 11) == map2

    map3 = map.set("a", 22)
    assert map3["a"] == 22
    assert map3[dummy3] == 3

    # Remove elements
    map4 = map.discard(dummy2)
    assert len(map4) == 2
    assert map4[dummy1] == 1
    assert dummy2 not in map4
    assert map4[dummy3] == 3

    assert map.discard(dummy4) == map

    # Empty map handling
    empty_map = map4.remove(dummy1).remove(dummy3)
    assert len(empty_map) == 0
    assert empty_map.discard(dummy1) == empty_map
示例#14
0
def test_initialization_with_many_elements():
    init_dict = dict([(str(x), x) for x in range(1700)])
    the_map = pmap(init_dict)

    assert len(the_map) == 1700
    assert the_map["16"] == 16
    assert the_map["1699"] == 1699
    assert the_map.set("256", 256) == the_map

    new_map = the_map.remove("1600")
    assert len(new_map) == 1699
    assert "1600" not in new_map
    assert new_map["1601"] == 1601

    # Some NOP properties
    assert new_map.discard("18888") == new_map
    assert "19999" not in new_map
    assert new_map["1500"] == 1500
    assert new_map.set("1500", new_map["1500"]) == new_map
示例#15
0
def test_iteration_with_many_elements():
    values = list(range(0, 2000))
    keys = [str(x) for x in values]
    init_dict = dict(zip(keys, values))

    hash_dummy1 = HashDummy()
    hash_dummy2 = HashDummy()

    # Throw in a couple of hash collision nodes to tests
    # those properly as well
    init_dict[hash_dummy1] = 12345
    init_dict[hash_dummy2] = 54321
    map = pmap(init_dict)

    actual_values = set()
    actual_keys = set()

    for k, v in map.iteritems():
        actual_values.add(v)
        actual_keys.add(k)

    assert actual_keys == set(keys + [hash_dummy1, hash_dummy2])
    assert actual_values == set(values + [12345, 54321])
示例#16
0
def test_overwrite_existing_element():
    map1 = pmap({"a": 2})
    map2 = map1.set("a", 3)

    assert len(map2) == 1
    assert map2["a"] == 3
示例#17
0
def test_literalish_works():
    # assert m() is pmap()
    assert m(a=1, b=2) == pmap({"a": 1, "b": 2})
示例#18
0
def test_iterable():
    """
    PMaps can be created from iterables even though they can't be len() hinted.
    """

    assert pmap(iter([("a", "b")])) == pmap([("a", "b")])
示例#19
0
def test_map_does_not_hash_values_on_second_hash_invocation():
    hashable = HashabilityControlled()
    x = pmap(dict(el=hashable))
    hash(x)
    hashable.hashable = False
    hash(x)
示例#20
0
def test_pmap_constructor():
    container = pmap()
    assert len(container) == 0

    container = pmap({0: "0", "1": 1, "2": "2"})
    assert len(container) == 3
示例#21
0
def test_empty_initialization():
    map = pmap()
    assert len(map) == 0
示例#22
0
def test_str():
    # assert str(pmap({1: 2, 3: 4})) == "pmap({1: 2, 3: 4})"
    assert str(pmap({1: 2})) == "pmap({1: 2})"
示例#23
0
def test_get_item():
    nodes_container = get_test_nodes_container()

    for node in nodes_container:
        assert nodes_container[node] == pyrpds.pmap()
示例#24
0
def test_get_item():
    graph = get_test_graph()

    for node in graph:
        assert graph[node] == pyrpds.pmap()