Exemplo n.º 1
0
def test_sizeof():
    # NOTE: this test may fail because it assumes to know the size in bytes of
    # the root node and of non-root nodes. So failure of this test may not
    # necessarily indicate that the trie is reporting the wrong size.
    t = Trie()
    rs = 96 # size of root node in bytes
    ns = 56 # size of trie node in bytes
    sizeof = lambda t:t.__sizeof__()
    assert t.__sizeof__() == sizeof(t) == rs
    t[b"a"] = 1
    assert sizeof(t) == rs + ns + 1 + 1
    del t[b"a"]
    assert sizeof(t) == rs
    t[b"hello"] = 1
    assert sizeof(t) == rs + ns*5 + 5 + 1
    t[b"world"] = 1
    assert sizeof(t) == rs + ns*10 + 10 + 2
    t[b"h3llo"] = 1
    assert sizeof(t) == rs + ns*14 + 15 + 3
    t[b"hello"] = 2 # modifying existing value should not change the size
    assert sizeof(t) == rs + ns*14 + 15 + 3
    t[b"here"] = 3 # should add 2 nodes
    assert sizeof(t) == rs + ns*16 + 19 + 4
    del t[b"hello"] # can only remove "llo" nodes
    assert sizeof(t) == rs + ns*13 + 14 + 3
Exemplo n.º 2
0
def test_reduce():
    t = Trie()
    t[b"hello"] = 1
    t[b"world"] = [1,2,3]
    for i in xrange(100):
        t[b(str(i))] = i
    t2 = pickle.loads(pickle.dumps(t))
    assert len(t) == len(t2)
    assert t.__sizeof__() == t2.__sizeof__()
    for e in t:
        assert t[b(e)] == t2[b(e)]