1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import pickle
import string
import datrie
import hypothesis.strategies as st
from hypothesis import given
printable_strings = st.lists(st.text(string.printable))
@given(printable_strings)
def test_contains(words):
trie = datrie.Trie(string.printable)
for i, word in enumerate(set(words)):
trie[word] = i + 1
for i, word in enumerate(set(words)):
assert word in trie
assert trie[word] == trie.get(word) == i + 1
@given(printable_strings)
def test_len(words):
trie = datrie.Trie(string.printable)
for i, word in enumerate(set(words)):
trie[word] = i
assert len(trie) == len(set(words))
@given(printable_strings)
def test_pickle_unpickle(words):
trie = datrie.Trie(string.printable)
for i, word in enumerate(set(words)):
trie[word] = i
trie = pickle.loads(pickle.dumps(trie))
for i, word in enumerate(set(words)):
assert word in trie
assert trie[word] == i
@given(printable_strings)
def test_pop(words):
words = set(words)
trie = datrie.Trie(string.printable)
for i, word in enumerate(words):
trie[word] = i
for i, word in enumerate(words):
assert trie.pop(word) == i
assert trie.pop(word, 42) == trie.get(word, 42) == 42
@given(printable_strings)
def test_clear(words):
words = set(words)
trie = datrie.Trie(string.printable)
for i, word in enumerate(words):
trie[word] = i
assert len(trie) == len(words)
trie.clear()
assert not trie
assert len(trie) == 0
# make sure the trie works afterwards.
for i, word in enumerate(words):
trie[word] = i
assert trie[word] == i
|