]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tokenizer/token_analysis/test_simple_trie.py
release 5.0.0.post5
[nominatim.git] / test / python / tokenizer / token_analysis / test_simple_trie.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for simplified trie structure.
9 """
10
11 from nominatim_db.tokenizer.token_analysis.simple_trie import SimpleTrie
12
13 def test_single_item_trie():
14     t = SimpleTrie([('foob', 42)])
15
16     assert t.longest_prefix('afoobar') == (None, 0)
17     assert t.longest_prefix('afoobar', start=1) == (42, 5)
18     assert t.longest_prefix('foob') == (42, 4)
19     assert t.longest_prefix('123foofoo', 3) == (None, 3)
20
21 def test_complex_item_tree():
22     t = SimpleTrie([('a', 1),
23                     ('b', 2),
24                     ('auto', 3),
25                     ('buto', 4),
26                     ('automat', 5),
27                     ('bu', 6),
28                     ('bx', 7)])
29
30     assert t.longest_prefix('a') == (1, 1)
31     assert t.longest_prefix('au') == (1, 1)
32     assert t.longest_prefix('aut') == (1, 1)
33     assert t.longest_prefix('auto') == (3, 4)
34     assert t.longest_prefix('automat') == (5, 7)
35     assert t.longest_prefix('automatx') == (5, 7)
36     assert t.longest_prefix('butomat') == (4, 4)
37     assert t.longest_prefix('butomat', 1) == (None, 1)