예제 #1
0
def test_novaluecopy():
    mp = PersistentTreeMap()
    bar = []
    mp = mp.assoc("foo", bar)
    mv = mp.transient()
    bar.append("test")
    assert mp["foo"] == mv["foo"] == bar
예제 #2
0
def test_collision():
    HASH = 13465345
    mp = PersistentTreeMap()
    mp = mp.assoc(HashCollision("hello", HASH), "world")
    mp = mp.assoc(HashCollision("answer", HASH), 42)
    assert mp[HashCollision("hello", HASH)] == "world"
    assert mp[HashCollision("answer", HASH)] == 42
예제 #3
0
def test_collision():
    HASH = 13465345
    mp = PersistentTreeMap()
    mp = mp.assoc(HashCollision("hello", HASH), "world")
    mp = mp.assoc(HashCollision("answer", HASH), 42)
    assert mp[HashCollision("hello", HASH)] == "world"
    assert mp[HashCollision("answer", HASH)] == 42
예제 #4
0
def test_neq():
    some = random_dict(1000)
    some.update({'a': 'foo', 'b': 'bar', 'c': 'blub'})
    other = random_dict(1000)
    other.update({'a': 'blub', 'c': 'blab', 'd': 'quuz'})
    assert (PersistentTreeMap.from_dict(some) !=
            PersistentTreeMap.from_dict(other))
예제 #5
0
def test_novaluecopy():
    mp = PersistentTreeMap()
    bar = []
    mp = mp.assoc("foo", bar)
    mv = mp.transient()
    bar.append("test")
    assert mp["foo"] == mv["foo"] == bar
예제 #6
0
def test_neq():
    some = random_dict(1000)
    some.update({'a': 'foo', 'b': 'bar', 'c': 'blub'})
    other = random_dict(1000)
    other.update({'a': 'blub', 'c': 'blab', 'd': 'quuz'})
    assert (
        PersistentTreeMap.from_dict(some) != PersistentTreeMap.from_dict(other)
    )
예제 #7
0
def test_or():
    some = random_dict(1000)
    some.update({'a': 'foo', 'b': 'bar', 'c': 'blub'})
    other = random_dict(1000)
    other = {'a': 'blub', 'c': 'blab', 'd': 'quuz'}
    
    df = PersistentTreeMap.from_dict(some) | PersistentTreeMap.from_dict(other)
    some.update(other)
    assert set(df.iteritems()) == set(some.iteritems())
예제 #8
0
def test_eq():
    some = random_dict(1000)
    assert (
        PersistentTreeMap.from_dict(some) == PersistentTreeMap.from_dict(some)
    )
    assert (
        PersistentTreeMap.from_dict(some) ==
        PersistentTreeMap.from_itr(sorted(some.iteritems()))
    )
예제 #9
0
def test_or():
    some = random_dict(1000)
    some.update({'a': 'foo', 'b': 'bar', 'c': 'blub'})
    other = random_dict(1000)
    other = {'a': 'blub', 'c': 'blab', 'd': 'quuz'}

    df = PersistentTreeMap.from_dict(some) | PersistentTreeMap.from_dict(other)
    some.update(other)
    assert set(df.iteritems()) == set(some.iteritems())
예제 #10
0
def test_persistence():
    mp = PersistentTreeMap()
    mp1 = mp.assoc('a', 'hello')
    assert mp1['a'] == 'hello'
    mp2 = mp1.assoc('b', 'world')
    assert mp2['a'] == 'hello'
    assert mp2['b'] == 'world'
    mp3 = mp2.without('a')
    assert mp3['b'] == 'world'
    excinfo = pytest.raises(KeyError, lambda: mp3['a'])
    assert excinfo.value.args[0] == 'a'
예제 #11
0
def test_persistence():
    mp = PersistentTreeMap()
    mp1 = mp.assoc('a', 'hello')
    assert mp1['a'] == 'hello'
    mp2 = mp1.assoc('b', 'world')
    assert mp2['a'] == 'hello'
    assert mp2['b'] == 'world'
    mp3 = mp2.without('a')
    assert mp3['b'] == 'world'
    excinfo = pytest.raises(KeyError, lambda: mp3['a'])
    assert excinfo.value.args[0] == 'a'
예제 #12
0
def test_xor():
    some = random_dict(1000)
    some.update({'a': 'foo', 'b': 'bar', 'c': 'blub'})
    other = random_dict(1000)
    other.update({'a': 'blub', 'c': 'blab', 'd': 'quuz'})

    df = PersistentTreeMap.from_dict(some) ^ PersistentTreeMap.from_dict(other)

    for key, value in df.iteritems():
        assert (key in some and key not in other and some[key] == value
                ) or key in other and key not in some and other[key] == value
예제 #13
0
def test_xor():
    some = random_dict(1000)
    some.update({'a': 'foo', 'b': 'bar', 'c': 'blub'})
    other = random_dict(1000)
    other.update({'a': 'blub', 'c': 'blab', 'd': 'quuz'})
    
    df = PersistentTreeMap.from_dict(some) ^ PersistentTreeMap.from_dict(other)
    
    for key, value in df.iteritems():
        assert (
            key in some and key not in other and some[key] == value
        ) or key in other and key not in some and other[key] == value
예제 #14
0
def test_and():
    some = random_dict(1000)
    some.update({'a': 'foo', 'b': 'bar', 'c': 'blub'})
    other = random_dict(1000)
    other.update({'a': 'blub', 'c': 'blab', 'd': 'quuz'})

    third = copy(some)
    third.update(other)

    df = PersistentTreeMap.from_dict(some) & PersistentTreeMap.from_dict(other)

    for key in third:
        if key in some and key in other:
            assert df[key] == other[key]
예제 #15
0
def test_and():
    some = random_dict(1000)
    some.update({'a': 'foo', 'b': 'bar', 'c': 'blub'})
    other = random_dict(1000)
    other.update({'a': 'blub', 'c': 'blab', 'd': 'quuz'})
    
    third = copy(some)
    third.update(other)
    
    df = PersistentTreeMap.from_dict(some) & PersistentTreeMap.from_dict(other)
    
    for key in third:
        if key in some and key in other:
            assert df[key] == other[key]
예제 #16
0
def test_transient():
    mp = PersistentTreeMap.from_dict({'foo': 'baz'})
    mp2 = mp.transient()
    mp3 = mp2.assoc('foo', 'bar')
    assert mp2['foo'] == 'bar'
    assert mp3['foo'] == 'bar'
    assert mp2 is mp3
    assert mp['foo'] == 'baz'
    
    mp4 = mp3.persistent()
    mp5 = mp4.assoc('foo', 'spam')
    assert mp2['foo'] == 'bar'
    assert mp3['foo'] == 'bar'
    assert mp4['foo'] == 'bar'
    assert mp5['foo'] == 'spam'
예제 #17
0
def main():
    import os
    import time
    
    mp = PersistentTreeMap().transient()
    for _ in xrange(22500):
        one, other = os.urandom(20), os.urandom(25)
        mp2 = mp.assoc(one, other)
        assert mp[one] == other
        assert mp2[one] == other
        mp = mp2
    pmp = mp.persistent()    
    
    s = time.time()
    mp = PersistentTreeMap()
    for _ in xrange(225000):
        one, other = os.urandom(20), os.urandom(25)
        mp2 = mp.assoc(one, other)
        try:
            mp[one]
        except KeyError:
            assert True
        else:
            assert False
        try:
            mp2.without(one)[one]
        except KeyError:
            assert True
        else:
            assert False
        mp = mp2
        assert mp[one] == other
    print 'PersistentHashMap:', time.time() - s
    assert mp[one] == other
    # This /may/ actually fail if we are unlucky, but it's a good start.
    assert len(list(iter(mp))) == 225000
예제 #18
0
def test_transient():
    mp = PersistentTreeMap.from_dict({'foo': 'baz'})
    mp2 = mp.transient()
    mp3 = mp2.assoc('foo', 'bar')
    assert mp2['foo'] == 'bar'
    assert mp3['foo'] == 'bar'
    assert mp2 is mp3
    assert mp['foo'] == 'baz'

    mp4 = mp3.persistent()
    mp5 = mp4.assoc('foo', 'spam')
    assert mp2['foo'] == 'bar'
    assert mp3['foo'] == 'bar'
    assert mp4['foo'] == 'bar'
    assert mp5['foo'] == 'spam'
예제 #19
0
def main():
    import os
    import time

    mp = PersistentTreeMap().transient()
    for _ in xrange(22500):
        one, other = os.urandom(20), os.urandom(25)
        mp2 = mp.assoc(one, other)
        assert mp[one] == other
        assert mp2[one] == other
        mp = mp2
    pmp = mp.persistent()

    s = time.time()
    mp = PersistentTreeMap()
    for _ in xrange(225000):
        one, other = os.urandom(20), os.urandom(25)
        mp2 = mp.assoc(one, other)
        try:
            mp[one]
        except KeyError:
            assert True
        else:
            assert False
        try:
            mp2.without(one)[one]
        except KeyError:
            assert True
        else:
            assert False
        mp = mp2
        assert mp[one] == other
    print 'PersistentHashMap:', time.time() - s
    assert mp[one] == other
    # This /may/ actually fail if we are unlucky, but it's a good start.
    assert len(list(iter(mp))) == 225000
예제 #20
0
def test_eq():
    some = random_dict(1000)
    assert (
        PersistentTreeMap.from_dict(some) == PersistentTreeMap.from_dict(some))
    assert (PersistentTreeMap.from_dict(some) == PersistentTreeMap.from_itr(
        sorted(some.iteritems())))
예제 #21
0
def test_fromdict():
    dct = random_dict(1000)
    mp = PersistentTreeMap.from_dict(dct)
    for key, value in dct.iteritems():
        assert mp[key] == value
예제 #22
0
def test_fromdict():
    dct = random_dict(1000)
    mp = PersistentTreeMap.from_dict(dct)
    for key, value in dct.iteritems():
        assert mp[key] == value
예제 #23
0
def test_iteration():
    dct = random_dict(1000)
    mp = PersistentTreeMap.from_dict(dct)
    assert set(mp.iterkeys()) == set(mp) == set(dct.iterkeys())
    assert set(mp.itervalues()) == set(dct.itervalues())
    assert set(mp.iteritems()) == set(dct.iteritems())
예제 #24
0
def test_iteration():
    dct = random_dict(1000)
    mp = PersistentTreeMap.from_dict(dct)
    assert set(mp.iterkeys()) == set(mp) == set(dct.iterkeys())
    assert set(mp.itervalues()) == set(dct.itervalues())
    assert set(mp.iteritems()) == set(dct.iteritems())