Tests for a ByteTrie that degenerates to a doubly linked list
This commit is contained in:
parent
543c71b562
commit
9f47254090
5 changed files with 73 additions and 3 deletions
|
@ -3,7 +3,9 @@
|
|||
# Bytetrie
|
||||
A fast, dependency-free, self-compressing trie with radix 256 in pure python.
|
||||
|
||||
![](trie.png)
|
||||
| ![Artistic ByteTrie Render](trie.png) |
|
||||
|--------------------------------------------------------------------------------------------------------------------------:|
|
||||
| *Rendering of a ByteTrie containing [~200,000 cities with a population > 500](http://download.geonames.org/export/dump/)* |
|
||||
|
||||
Bytetrie allows fast prefix search in a large corpus of keys. Each key can be
|
||||
associated with arbitrary data. It features fast lookup times at the cost of
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import pytest
|
||||
|
||||
pytest_plugins = [
|
||||
"tries.simple_trie"
|
||||
"tries.simple_trie",
|
||||
"tries.degenerated_trie"
|
||||
]
|
||||
|
|
|
@ -2,11 +2,12 @@ import pytest
|
|||
|
||||
import sys
|
||||
import os
|
||||
sys.path.append("tests/tries/")
|
||||
|
||||
from bytetrie import ByteTrie
|
||||
|
||||
sys.path.append("tests/tries/")
|
||||
import simple_trie
|
||||
import degenerated_trie
|
||||
|
||||
def test_find_all(simple_trie):
|
||||
r = simple_trie.find(b"A")
|
||||
|
@ -47,3 +48,18 @@ def test_partial_prefix_find_terminal(simple_trie):
|
|||
assert len(r) == 1
|
||||
assert r[0].key() == b"ABCDE"
|
||||
assert r[0].value() == "ABCDE"
|
||||
|
||||
def test_degenerated_find_first_finds_all(degenerated_trie):
|
||||
r = degenerated_trie.find(b"A")
|
||||
assert len(r) == 26
|
||||
assert r[0].key() == b"A"
|
||||
assert r[0].value() == "A"
|
||||
|
||||
assert r[25].key() == b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
assert r[25].value() == "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
def test_degenerated_find_last_finds_one(degenerated_trie):
|
||||
r = degenerated_trie.find(b"ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
assert len(r) == 1
|
||||
assert r[0].key() == b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
assert r[0].value() == "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
|
BIN
tests/tries/degenerated_trie.png
Normal file
BIN
tests/tries/degenerated_trie.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 52 KiB |
51
tests/tries/degenerated_trie.py
Normal file
51
tests/tries/degenerated_trie.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
"""
|
||||
A degenerated ByteTrie fixture. ByteTrie resolves to a doubly-linked list. The
|
||||
worst case scenario.
|
||||
|
||||
See degenerated_trie.png
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from bytetrie import ByteTrie
|
||||
|
||||
def insert(trie):
|
||||
""" Shall only be used to insert strings """
|
||||
t = trie
|
||||
def _insert(*vals):
|
||||
for val in vals:
|
||||
t.insert(val.encode('utf-8'), val)
|
||||
return _insert
|
||||
|
||||
@pytest.fixture
|
||||
def degenerated_trie():
|
||||
t = ByteTrie()
|
||||
ins = insert(t)
|
||||
ins("A")
|
||||
ins("AB")
|
||||
ins("ABC")
|
||||
ins("ABCD")
|
||||
ins("ABCDE")
|
||||
ins("ABCDEF")
|
||||
ins("ABCDEFG")
|
||||
ins("ABCDEFGH")
|
||||
ins("ABCDEFGHI")
|
||||
ins("ABCDEFGHIJ")
|
||||
ins("ABCDEFGHIJK")
|
||||
ins("ABCDEFGHIJKL")
|
||||
ins("ABCDEFGHIJKLM")
|
||||
ins("ABCDEFGHIJKLMN")
|
||||
ins("ABCDEFGHIJKLMNO")
|
||||
ins("ABCDEFGHIJKLMNOP")
|
||||
ins("ABCDEFGHIJKLMNOPQ")
|
||||
ins("ABCDEFGHIJKLMNOPQR")
|
||||
ins("ABCDEFGHIJKLMNOPQRS")
|
||||
ins("ABCDEFGHIJKLMNOPQRST")
|
||||
ins("ABCDEFGHIJKLMNOPQRSTU")
|
||||
ins("ABCDEFGHIJKLMNOPQRSTUV")
|
||||
ins("ABCDEFGHIJKLMNOPQRSTUVW")
|
||||
ins("ABCDEFGHIJKLMNOPQRSTUVWX")
|
||||
ins("ABCDEFGHIJKLMNOPQRSTUVWXY")
|
||||
ins("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
|
||||
return t
|
Loading…
Reference in a new issue