def test_mapped_trie(): tree = NoAho() tree.add(anchor(".a..b..c."), 0) tree.add(anchor(".b."), 1) tree.add(anchor(".a..c."), 2) tree.add(anchor(".a..b."), 3) tree.add(anchor(".é."), 4) tree.compile() with tempfile.TemporaryDirectory(prefix="noahong-") as tmpdir: path = os.path.join(tmpdir, "mapped") tree.write(path) with contextlib.closing(Mapped(path)) as m: assert m.nodes_count() == tree.nodes_count() matches = list(m.findall_anchored(anchor(".a..b..c."))) assert matches == [(0, 9, 0)] matches = list(m.findall_anchored(anchor(".b."))) assert matches == [(0, 3, 1)] matches = list(m.findall_anchored(anchor(".a..c."))) assert matches == [(0, 6, 2)] matches = list(m.findall_anchored(anchor(".z."))) assert matches == [] matches = list(m.findall_anchored(anchor(".z..a..b..z."))) assert matches == [(3, 9, 3)] matches = list(m.findall_anchored(anchor(".é."))) assert matches == [(0, 3, 4)]
def test_mapped_trie_payload(): trie = NoAho() trie.add("foo", None) trie.compile() with tempfile.TemporaryDirectory(prefix="noahong-") as tmpdir: path = os.path.join(tmpdir, "mapped") with pytest.raises(PayloadWriteError): trie.write(path)
def test_empty_mapped_trie(): tree = NoAho() tree.compile() with tempfile.TemporaryDirectory(prefix="noahong-") as tmpdir: path = os.path.join(tmpdir, "mapped") tree.write(path) with contextlib.closing(Mapped(path)) as m: assert m.nodes_count() == 1 assert m.nodes_count() == tree.nodes_count() matches = list(m.findall_anchored(anchor(".a..b..c."))) assert matches == []