test_trie.py 3.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
"""
Tests for the trie library.
"""
#from __future__ import (
#    absolute_import, division, print_function, unicode_literals)
#from future.builtins import str, zip

from dict_trie import Trie

class TestTrie(object):
    def setup(self):
        self._trie = Trie(['abc', 'abd', 'test', 'te'])

Jeroen F.J. Laros's avatar
Jeroen F.J. Laros committed
14
    def test_root(self):
15 16 17 18 19 20 21 22 23
        assert self._trie.root == {
            'a': {
                'b': {
                    'c': {'': {}},
                    'd': {'': {}}}},
            't': {'e': {
                '': {},
                's': {'t': {'': {}}}}}}

Jeroen F.J. Laros's avatar
Jeroen F.J. Laros committed
24
    def test_word_present(self):
25 26
        assert 'abc' in self._trie

Jeroen F.J. Laros's avatar
Jeroen F.J. Laros committed
27 28 29 30 31 32 33 34 35 36 37
    def test_word_absent(self):
        assert 'abx' not in self._trie

    def test_empty_string_present(self):
        assert '' in Trie([''])

    def test_empty_string_absent(self):
        assert '' not in self._trie

    def test_prefix_absent_as_word(self):
        assert 'ab' not in self._trie
38

Jeroen F.J. Laros's avatar
Jeroen F.J. Laros committed
39 40
    def test_too_long_absent(self):
        assert 'abcd' not in self._trie
41

Jeroen F.J. Laros's avatar
Jeroen F.J. Laros committed
42
    def test_prefix_present(self):
43 44
        assert self._trie.has_prefix('ab')

Jeroen F.J. Laros's avatar
Jeroen F.J. Laros committed
45 46 47
    def test_prefix_absent(self):
        assert not self._trie.has_prefix('ac')

48 49 50
    def test_word_is_prefix(self):
        assert self._trie.has_prefix('abc')

Jeroen F.J. Laros's avatar
Jeroen F.J. Laros committed
51
    def test_too_long_prefix_absent(self):
52 53 54 55
        assert not self._trie.has_prefix('abcd')

    def test_prefix_order(self):
        assert Trie(['test', 'te']).root == Trie(['te', 'test']).root
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

    def test_hamming_0_no_prefix(self):
        assert self._trie.hamming('ab', 0) == ''

    def test_hamming_0_match(self):
        assert self._trie.hamming('abc', 0) == 'abc'

    def test_hamming_0_match_sub(self):
        assert self._trie.hamming('te', 0) == 'te'

    def test_hamming_0_too_long(self):
        assert self._trie.hamming('abcd', 0) == ''

    def test_hamming_1_match(self):
        assert self._trie.hamming('abc', 1) == 'abc'

    def test_hamming_1_match_sub(self):
        assert self._trie.hamming('te', 1) == 'te'

    def test_hamming_1_match_1(self):
        assert self._trie.hamming('xbc', 1) == 'abc'

    def test_hamming_1_match_2(self):
        assert self._trie.hamming('axc', 1) == 'abc'

    def test_hamming_1_match_3(self):
        assert self._trie.hamming('abx', 1) == 'abc'

84 85 86
    def test_hamming_1_match_4(self):
        assert self._trie.hamming('abd', 1) == 'abc'

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    def test_hamming_1_no_prefix(self):
        assert self._trie.hamming('ab', 1) == ''

    def test_hamming_1_too_long(self):
        assert self._trie.hamming('abcd', 1) == ''

    def test_hamming_1_match_sub_1(self):
        assert self._trie.hamming('tx', 1) == 'te'

    def test_hamming_1_match_sub_2(self):
        assert self._trie.hamming('xe', 1) == 'te'

    def test_hamming_1_mismatch(self):
        assert self._trie.hamming('txxt', 1) == ''

    def test_hamming_2_match(self):
        assert self._trie.hamming('txxt', 2) == 'test'
104 105 106

    def test_best_hamming_match(self):
        assert self._trie.best_hamming('abd', 1) == 'abd'
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

    def test_levenshtein_0_match_1(self):
        assert self._trie.levenshtein('abc', 0) == 'abc'

    def test_levenshtein_0_match_2(self):
        assert self._trie.levenshtein('te', 0) == 'te'

    def test_levenshtein_1_subst(self):
        assert self._trie.levenshtein('axc', 1) == 'abc'

    def test_levenshtein_1_del(self):
        assert self._trie.levenshtein('ac', 1) == 'abc'

    def test_levenshtein_1_ins(self):
        assert self._trie.levenshtein('abbc', 1) == 'abc'