Example #1
0
def test_dissoc():
    N = 32**3
    m = phm((i, 0) for i in range(N))
    assert len(m) == N
    for i in range(N):
        m = m.dissoc(i)
        assert i not in m
    assert m == phm()
    assert phm().dissoc(10) == phm()
Example #2
0
def test_transient_collisions():
    N = 32**3
    tm = phm((cs(i), None) for i in range(N)).transient()
    for i in range(N):
        si = cs(i)
        assert si in tm
        tm = tm.tdissoc(cs(i))
        assert si not in tm
    assert tm.persistent() == phm()
Example #3
0
def test_transient_collisions():
    N = 32**3
    tm = phm((cs(i), None) for i in range(N)).transient()
    for i in range(N):
        si = cs(i)
        assert si in tm
        tm = tm.tdissoc(cs(i))
        assert si not in tm
    assert tm.persistent() == phm()
Example #4
0
def test_dissoc():
    N = 32**3
    m = phm((i, 0) for i in range(N))
    assert len(m) == N
    for i in range(N):
        m = m.dissoc(i)
        assert i not in m
    assert m == phm()
    assert phm().dissoc(10) == phm()
Example #5
0
def test_transient():
    N = 32**3
    tm = phm().transient()
    for i in range(N):
        assert len(tm) == i
        tm = tm.tassoc(i, i)
        assert i in tm
        assert tm[i] == i
    for i in range(N):
        tm = tm.tdissoc(i)
        assert len(tm) == (N - i - 1)
    pm = tm.persistent()
    assert len(pm) == 0
    assert isinstance(pm, PersistentHashMap)
    assert pm == phm()
Example #6
0
def test_transient():
    N = 32**3
    tm = phm().transient()
    for i in range(N):
        assert len(tm) == i
        tm = tm.tassoc(i, i)
        assert i in tm
        assert tm[i] == i
    for i in range(N):
        tm = tm.tdissoc(i)
        assert len(tm) == (N-i-1)
    pm = tm.persistent()
    assert len(pm) == 0
    assert isinstance(pm, PersistentHashMap)
    assert pm == phm()
Example #7
0
def test_pathological():
    m = phm()
    for i in range(100):
        m = m.assoc(Pathological(i), i)
    assert len(m) == 100
    for i in range(100):
        assert m[Pathological(i)] == i
Example #8
0
def test_replace():
    m = phm()
    m = m.assoc(1, 2)
    assert m[1] == 2
    m2 = m.assoc(1, 10)
    assert m2[1] == 10
    assert m[1] == 2
Example #9
0
def test_iteration():
    d = {i: i**2 for i in range(1000)}
    m = phm(d)
    assert set(m) == set(d)
    assert set(m.keys()) == set(d.keys())
    assert set(m.values()) == set(d.values())
    assert set(m.items()) == set(d.items())
Example #10
0
def test_replace():
    m = phm()
    m = m.assoc(1, 2)
    assert m[1] == 2
    m2 = m.assoc(1, 10)
    assert m2[1] == 10
    assert m[1] == 2
Example #11
0
def test_iteration():
    d = {i: i**2 for i in range(1000)}
    m = phm(d)
    assert set(m) == set(d)
    assert set(m.keys()) == set(d.keys())
    assert set(m.values()) == set(d.values())
    assert set(m.items()) == set(d.items())
Example #12
0
def test_pathological():
    m = phm()
    for i in range(100):
        m = m.assoc(Pathological(i), i)
    assert len(m) == 100
    for i in range(100):
        assert m[Pathological(i)] == i
Example #13
0
def test_assoc():
    m = phm()
    m2 = m.assoc(None, None)
    assert len(m2) == 1
    for i in range(32**3):
        m = m.assoc(i, i)
        assert len(m) == i + 1
        assert m[i] == i
Example #14
0
def test_assoc():
    m = phm()
    m2 = m.assoc(None, None)
    assert len(m2) == 1
    for i in range(32**3):
        m = m.assoc(i, i)
        assert len(m) == i+1
        assert m[i] == i
Example #15
0
def test_key_error():
    m = phm()
    # Test path when root is None.
    with pytest.raises(KeyError):
        m[1]
    m = m.assoc(10, 20)
    # Test path when root is not None.
    with pytest.raises(KeyError):
        m[1]
Example #16
0
def test_key_error():
    m = phm()
    # Test path when root is None.
    with pytest.raises(KeyError):
        m[1]
    m = m.assoc(10, 20)
    # Test path when root is not None.
    with pytest.raises(KeyError):
        m[1]
Example #17
0
def test_transient_persistent():
    pm = phm({1:2})
    tm = pm.transient()
    assert len(pm) == len(tm) == 1
    assert pm[1] == tm[1] == 2
    pm2 = tm.persistent()
    assert pm2 == pm
    with pytest.raises(RuntimeError):
        len(tm)
    with pytest.raises(RuntimeError):
        tm[1]
Example #18
0
def test_transient_persistent():
    pm = phm({1: 2})
    tm = pm.transient()
    assert len(pm) == len(tm) == 1
    assert pm[1] == tm[1] == 2
    pm2 = tm.persistent()
    assert pm2 == pm
    with pytest.raises(RuntimeError):
        len(tm)
    with pytest.raises(RuntimeError):
        tm[1]
Example #19
0
def test_hash():
    m = phm()
    assert isinstance(hash(m), int)
    a = phm((i, None) for i in range(100))
    b = phm((i, None) for i in range(99, -1, -1))
    assert a == b and hash(a) == hash(b)
    c = phm(((i,), phm(a=i, b=2*i)) for i in range(100))
    assert isinstance(hash(c), int)
    with pytest.raises(TypeError):
        hash(phm(a=[1,2,3]))
Example #20
0
def test_hash():
    m = phm()
    assert isinstance(hash(m), int)
    a = phm((i, None) for i in range(100))
    b = phm((i, None) for i in range(99, -1, -1))
    assert a == b and hash(a) == hash(b)
    c = phm(((i, ), phm(a=i, b=2 * i)) for i in range(100))
    assert isinstance(hash(c), int)
    with pytest.raises(TypeError):
        hash(phm(a=[1, 2, 3]))
Example #21
0
def test_creation_mixed():
    m = phm([('a', 1)], b=2, c=3)
    assert m['a'] == 1
    assert m['b'] == 2
    assert m['c'] == 3
Example #22
0
def test_creation_dict():
    m = phm({'a': 1, 'b': 2, 'c': 3})
    assert m['a'] == 1
    assert m['b'] == 2
    assert m['c'] == 3
Example #23
0
def test_creation_seq_of_tuples():
    m = phm([('a', 1), ('b', 2), ('c', 3)])
    assert m['a'] == 1
    assert m['b'] == 2
    assert m['c'] == 3
Example #24
0
def test_get():
    m = phm((i, i**2) for i in range(10))
    assert m.get(0) == 0
    assert m.get(8) == 8**2
    assert m.get('a') == None
    assert m.get('b', 'notfound') == 'notfound'
Example #25
0
def test_creation():
    m = phm()
    assert len(m) == 0
Example #26
0
def test_creation_kwargs():
    m = phm(a=1, b=2, c=3)
    assert m['a'] == 1
    assert m['b'] == 2
    assert m['c'] == 3
Example #27
0
def test_str():
    m = phm({i: i**2 for i in range(1000)})
    assert eval(str(m).replace('map', 'phm')) == m
Example #28
0
def test_creation_raises():
    with pytest.raises(TypeError):
        phm([('a', 1)], {'b': 2})
    with pytest.raises(TypeError):
        phm({'b': 2}, [('a', 1)])
Example #29
0
def test_get():
    m = phm((i, i**2) for i in range(10))
    assert m.get(0) == 0
    assert m.get(8) == 8**2
    assert m.get('a') == None
    assert m.get('b', 'notfound') == 'notfound'
Example #30
0
def test_creation_raises():
    with pytest.raises(TypeError):
        phm([('a', 1)], {'b': 2})
    with pytest.raises(TypeError):
        phm({'b': 2}, [('a', 1)])
Example #31
0
def test_equals():
    assert phm() == phm()
    assert phm(a=1, b=2) == phm(b=2, a=1)
    assert phm(a=1, b=1) != phm(a=2, b=2)
Example #32
0
def test_getitem():
    m = phm()
    m2 = m.assoc(1, 42)
    assert m2[1] == 42
Example #33
0
def test_creation_seq_of_tuples():
    m = phm([('a', 1), ('b', 2), ('c', 3)])
    assert m['a'] == 1
    assert m['b'] == 2
    assert m['c'] == 3
Example #34
0
def test_creation_dict():
    m = phm({'a': 1, 'b': 2, 'c': 3})
    assert m['a'] == 1
    assert m['b'] == 2
    assert m['c'] == 3
Example #35
0
def test_equals():
    assert phm() == phm()
    assert phm(a=1, b=2) == phm(b=2, a=1)
    assert phm(a=1, b=1) != phm(a=2, b=2)
Example #36
0
def test_creation_mixed():
    m = phm([('a', 1)], b=2, c=3)
    assert m['a'] == 1
    assert m['b'] == 2
    assert m['c'] == 3
Example #37
0
def test_getitem():
    m = phm()
    m2 = m.assoc(1, 42)
    assert m2[1] == 42
Example #38
0
def test_str():
    m = phm({i: i**2 for i in range(1000)})
    assert eval(str(m).replace('map', 'phm')) == m
Example #39
0
def test_creation():
    m = phm()
    assert len(m) == 0
Example #40
0
def test_creation_kwargs():
    m = phm(a=1, b=2, c=3)
    assert m['a'] == 1
    assert m['b'] == 2
    assert m['c'] == 3