def test_assoc(): space = Space() v = space.make_vector([ String(i) for i in ["a", "b", "c"] ]) a = v.assoc_at(1, String("fuu")) assert a.size() == 3 and space.eq( a.get_at(0), String('a') ) assert space.eq( a.get_at(1), String('fuu') ) assert space.eq( a.get_at(2), String('c') )
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
def test_vector(): space = Space() v = PersistentVector() assert v.size() == 0 v1 = v.with_appended(String('abc')) assert v1.size() == 1 assert space.eq( v1.to_list()[0], String('abc') ) assert len(v1.to_list()) == 1 assert space.eq( v1.get_at(0), String('abc') ) assert space.eq( v1.get_item(space.make_int(0)), String('abc') )
def test_many_append(): space = Space() count = 2000 v = PersistentVector() for i in xrange(count): v = v.with_appended(String('%d' % i)) assert v.size() == count for i in xrange(count): assert space.eq( v.get_at(i), String('%d' % i) ) l = v.to_list() for i in xrange(count): assert space.eq( l[i], String('%d' % i) )
def test_serialization(): space = Space() zero = unserialize(space, 'I\n0') assert space.eq( space.make_int(0), zero) print
def test_repr(): space = Space() d = MutableDict(space) assert d.repr() == 'MutableDict{}' d.set_item(String('foobar'), String('needle')) assert d.repr() == 'MutableDict{"foobar": "needle"}' assert space.eq(d.get_item(String('foobar')), String('needle'))
def test_dict_with_many(): space = Space() t = MutableDict(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)
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)
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
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"))
def test_string(): space = Space() assert space.hash(String('a')) == space.hash(String('a')) assert space.eq(String('a'), String('a')) assert not space.eq(String('a'), String('b'))