Exemplo n.º 1
0
def test_persistent_hashtrie():
    space = Space()
    t = PersistentHashTrie(space)
    t1 = t.assoc(String("a"), String("b"))
    assert len(t1.keys()) == 1
    assert space.eq(t1.keys()[0], String("a"))
    assert space.eq(t1.get_item(String("a")), String("b"))
    t2 = t1.assoc(String("b"), String("c"))
    assert len(t2.keys()) == 2
Exemplo n.º 2
0
def skip_test_persistent_hashtrie_collisions():
    space = Space()
    t0 = PersistentHashTrie(space)
    # two different objects with same hash
    a = HashAndEqSimulator(100, 1)
    b = HashAndEqSimulator(100, 2)
    t1 = t0.assoc(a, String("a"))
    t = t1.assoc(b, String("b"))
    assert space.eq(t.get_item(a), String("a"))
    assert space.eq(t.get_item(b), String("b"))
    assert len(t.keys()) == 2
Exemplo n.º 3
0
class MutableDict(Root):
    '''
    Mutable dictionary that should be based on PyPy's implementation,
    but I can't get it accepted by translator.

    So, for now, it's based on persistent hash trie.
    '''
    def __init__(self, space):
        self.container = PersistentHashTrie(space)

    def repr(self):
        return 'MutableDict{%s}' % ', '.join([
            '%s: %s' % (k.repr(), self.container.get_item(k).repr())
            for k in self.container.keys() ])

    def set_item(self, key, val):
        self.container = self.container.assoc(key, val)

    def get_item(self, key):
        return self.container.get_item(key)
Exemplo n.º 4
0
def test_hashtrie_like_mutable_with_many():
    space = Space()
    t = PersistentHashTrie(space)
    items = [String("foo%d" % i) for i in xrange(200)]
    for item in items:
        t._set_item(item, item)

    for item in items:
        assert space.eq(t.get_item(item), item)

    for item in items:
        t._set_item(item, item)
        assert space.eq(t.get_item(item), item)

    for item in items:
        assert space.eq(t.get_item(item), item)

    assert len(t.keys()) == len(items)
Exemplo n.º 5
0
 def __init__(self, space):
     self.container = PersistentHashTrie(space)
Exemplo n.º 6
0
def test_hashtrie_like_mutable():
    space = Space()
    t = PersistentHashTrie(space)
    t._set_item(String("foobar"), String("foobarval"))
    t._set_item(String("foobar2"), String("foobarval2"))
    assert space.eq(t.get_item(String("foobar")), String("foobarval"))
    assert space.eq(t.get_item(String("foobar2")), String("foobarval2"))
    assert len(t.keys()) == 2
    assert space.eq(t.keys()[0], String("foobar")) or space.eq(t.keys()[1], String("foobar"))
    assert space.eq(t.keys()[0], String("foobar2")) or space.eq(t.keys()[1], String("foobar2"))