diff --git a/README.md b/README.md index 54d0893..28ae78c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/tests/conftest.py b/tests/conftest.py index aae94a5..1ab91a9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,6 @@ import pytest pytest_plugins = [ - "tries.simple_trie" + "tries.simple_trie", + "tries.degenerated_trie" ] diff --git a/tests/test_bytetrie_blackbox.py b/tests/test_bytetrie_blackbox.py index 19f84a8..3227aa7 100644 --- a/tests/test_bytetrie_blackbox.py +++ b/tests/test_bytetrie_blackbox.py @@ -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" diff --git a/tests/tries/degenerated_trie.png b/tests/tries/degenerated_trie.png new file mode 100644 index 0000000..0d45684 Binary files /dev/null and b/tests/tries/degenerated_trie.png differ diff --git a/tests/tries/degenerated_trie.py b/tests/tries/degenerated_trie.py new file mode 100644 index 0000000..c93901f --- /dev/null +++ b/tests/tries/degenerated_trie.py @@ -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