| Line | |
|---|
| 1 | """ |
|---|
| 2 | unit tests for module logilab.common.patricia |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | __revision__ = "$Id: unittest_patricia.py,v 1.3 2003-09-05 10:22:35 syt Exp $" |
|---|
| 6 | |
|---|
| 7 | from logilab.common.patricia import * |
|---|
| 8 | from logilab.common.testlib import TestCase, unittest_main |
|---|
| 9 | |
|---|
| 10 | class PatriciaTrieClassTest(TestCase): |
|---|
| 11 | |
|---|
| 12 | def test_knownValues(self): |
|---|
| 13 | """ |
|---|
| 14 | remove a child node |
|---|
| 15 | """ |
|---|
| 16 | p = PatriciaTrie() |
|---|
| 17 | i = 0 |
|---|
| 18 | words_list = ['maitre', 'maman', 'mange', 'manger', 'mangouste', |
|---|
| 19 | 'manigance', 'manitou'] |
|---|
| 20 | words_list.sort() |
|---|
| 21 | # |
|---|
| 22 | for i in range(len(words_list)): |
|---|
| 23 | p.insert(words_list[i], i) |
|---|
| 24 | for i in range(len(words_list)): |
|---|
| 25 | assert p.lookup(words_list[i]) == [i] |
|---|
| 26 | try: |
|---|
| 27 | p.lookup('not in list') |
|---|
| 28 | raise AssertionError() |
|---|
| 29 | except KeyError: |
|---|
| 30 | pass |
|---|
| 31 | # |
|---|
| 32 | l = p.pfx_search('') |
|---|
| 33 | l.sort() |
|---|
| 34 | assert l == words_list |
|---|
| 35 | l = p.pfx_search('ma') |
|---|
| 36 | l.sort() |
|---|
| 37 | assert l == words_list |
|---|
| 38 | l = p.pfx_search('mai') |
|---|
| 39 | assert l == ['maitre'] |
|---|
| 40 | l = p.pfx_search('not in list') |
|---|
| 41 | assert l == [] |
|---|
| 42 | l = p.pfx_search('man', 2) |
|---|
| 43 | assert l == ['mange'] |
|---|
| 44 | l = p.pfx_search('man', 1) |
|---|
| 45 | assert l == [] |
|---|
| 46 | p.remove('maitre') |
|---|
| 47 | try: |
|---|
| 48 | p.lookup('maitre') |
|---|
| 49 | raise AssertionError() |
|---|
| 50 | except KeyError: |
|---|
| 51 | pass |
|---|
| 52 | #print p |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | if __name__ == '__main__': |
|---|
| 56 | unittest_main() |
|---|