Exemplo n.º 1
0
    def test_trie_prefix_count(self):

        t = strutil.make_trie(self.strs, node_max_num=3)

        dd('==== trie:')
        dd(str(t))

        # all prefixes, might be also a whole string.
        for ks, v in dictutil.depth_iter(t, intermediate=True):

            dd('==== got keys:', ks, 'sub trie:')
            dd(v)

            prefix = ''.join(ks)
            expected = len([x for x in self.strs if x.startswith(prefix)])

            key_path = '.'.join(ks)
            subtrie = dictutil.get(t, key_path, default=-1)
            rst = subtrie.n

            dd('prefix:', prefix, 'trie key_path:', key_path, 'sub trie:')
            dd(str(subtrie))
            dd('expected:', expected)
            dd('rst:', rst)

            self.assertEqual(expected, rst)

            # Leaf node number limit
            if len(v) == 0:
                self.assertLessEqual(rst, 3)
Exemplo n.º 2
0
    def test_node_max_num(self):

        t = strutil.make_trie(self.strs, node_max_num=1)

        for ks, v in dictutil.depth_iter(t, empty_leaf=True):

            key_path = '.'.join(ks)
            subtrie = dictutil.get(t, key_path, default=-1)

            self.assertEqual(1, subtrie.n)

        # all in one node

        t = strutil.make_trie(self.strs, node_max_num=10000)

        dd()
        dd(str(t))

        self.assertEqual(len(self.strs), t.n)
        self.assertEqual(0, len(t))
Exemplo n.º 3
0
    def test_trie_iterable(self):

        t = strutil.make_trie(self.iterables, node_max_num=3)

        dd('trie:')
        dd(str(t))

        self.assertEqual(len(self.iterables), t.n)

        for ks, v in dictutil.depth_iter(t, is_allowed=lambda ks, v: v.is_eol):

            s = tuple(ks)
            dd('whole string: ', s, v)
            self.assertTrue(s in self.iterables)
Exemplo n.º 4
0
    def test_trie_whole_string(self):

        t = strutil.make_trie(self.strs, node_max_num=3)

        dd('trie:')
        dd(str(t))

        self.assertEqual(len(self.strs), t.n)

        # only whole strings
        for ks, v in dictutil.depth_iter(t, is_allowed=lambda ks, v: v.is_eol):

            s = ''.join(ks)
            dd('whole string: ', s, v)
            self.assertTrue(s in self.strs)