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
|
"""
unit tests for module logilab.common.patricia
"""
__revision__ = "$Id: unittest_patricia.py,v 1.3 2003-09-05 10:22:35 syt Exp $"
from logilab.common.patricia import *
from logilab.common.testlib import TestCase, unittest_main
class PatriciaTrieClassTest(TestCase):
def test_knownValues(self):
"""
remove a child node
"""
p = PatriciaTrie()
i = 0
words_list = ['maitre', 'maman', 'mange', 'manger', 'mangouste',
'manigance', 'manitou']
words_list.sort()
#
for i in range(len(words_list)):
p.insert(words_list[i], i)
for i in range(len(words_list)):
assert p.lookup(words_list[i]) == [i]
try:
p.lookup('not in list')
raise AssertionError()
except KeyError:
pass
#
l = p.pfx_search('')
l.sort()
assert l == words_list
l = p.pfx_search('ma')
l.sort()
assert l == words_list
l = p.pfx_search('mai')
assert l == ['maitre']
l = p.pfx_search('not in list')
assert l == []
l = p.pfx_search('man', 2)
assert l == ['mange']
l = p.pfx_search('man', 1)
assert l == []
p.remove('maitre')
try:
p.lookup('maitre')
raise AssertionError()
except KeyError:
pass
#print p
if __name__ == '__main__':
unittest_main()
|