forked from biopython/biopython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_trie.py
150 lines (136 loc) · 5.47 KB
/
test_trie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
import unittest
try:
from Bio import trie
except ImportError:
import os
from Bio import MissingPythonDependencyError
if os.name=="java":
message = "Not available on Jython, Bio.trie requires compiled C code."
else:
message = "Could not import Bio.trie, check C code was compiled."
raise MissingPythonDependencyError(message)
class TestTrie(unittest.TestCase):
def test_get_set(self):
trieobj = trie.trie()
trieobj["hello world"] = "s1"
trieobj["bye"] = "s2"
trieobj["hell sucks"] = "s3"
trieobj["hebee"] = "s4"
self.assertEqual(trieobj["hello world"], "s1")
self.assertEqual(trieobj["bye"], "s2")
self.assertEqual(trieobj["hell sucks"], "s3")
self.assertEqual(trieobj["hebee"], "s4")
trieobj["blah"] = "s5"
self.assertEqual(trieobj["blah"], "s5")
self.assertEqual(trieobj.get("foobar"), None)
self.assertEqual(len(trieobj), 5)
trieobj["blah"] = "snew"
self.assertEqual(trieobj["blah"], "snew")
def test_prefix(self):
trieobj = trie.trie()
trieobj["hello"] = 5
trieobj["he"] = 7
trieobj["hej"] = 9
trieobj["foo"] = "bar"
k = trieobj.keys()
k.sort()
self.assertEqual(k, ["foo", "he", "hej", "hello"])
self.assertEqual(trieobj["hello"], 5)
self.assertEqual(trieobj.get("bye"), None)
self.assertEqual(trieobj.has_key("hello"), True)
self.assertEqual(trieobj.has_key("he"), True)
self.assertEqual(trieobj.has_key("bye"), False)
self.assertEqual(trieobj.has_prefix("h"), True)
self.assertEqual(trieobj.has_prefix("hel"), True)
self.assertEqual(trieobj.has_prefix("foa"), False)
self.assertEqual(trieobj.has_prefix("hello world"), False)
self.assertEqual(len(trieobj), 4)
k = trieobj.with_prefix("he")
k.sort()
self.assertEqual(k, ["he", "hej", "hello"])
k = trieobj.with_prefix("l")
self.assertEqual(k, [])
k = trieobj.with_prefix("hej")
self.assertEqual(k, ["hej"])
k = trieobj.with_prefix("hejk")
self.assertEqual(k, [])
def test_save(self):
import StringIO
trieobj = trie.trie()
trieobj["foo"] = 1
k = trieobj.keys()
self.assertEqual(k, ["foo"])
v = trieobj.values()
self.assertEqual(v, [1])
self.assertEqual(trieobj.get("bar", 99), 99)
trieobj["hello"] = '55a'
self.assertEqual(trieobj.get_approximate("foo", 0), [("foo", 1, 0)])
self.assertEqual(trieobj.get_approximate("foo", 1), [("foo", 1, 0)])
self.assertEqual(trieobj.get_approximate("foa", 0), [])
self.assertEqual(trieobj.get_approximate("foa", 1), [("foo", 1, 1)])
x = trieobj.get_approximate("foa", 2)
x.sort()
self.assertEqual(x, [("foo", 1, 1), ("foo", 1, 2), ("foo", 1, 2)])
# foo foo- foo-
# foa f-oa fo-a
# mismatch a->o
# insertion after f, deletion of o
# insertion after o, deletion of o
x = trieobj.get_approximate("foo", 4)
y = {}
for z in x:
y[z] = y.get(z, 0) + 1
x = y.items()
x.sort()
self.assertEqual(x,[(('foo', 1, 0), 1), (('hello', '55a', 4), 6)])
h = StringIO.StringIO()
trie.save(h, trieobj)
h.seek(0)
trieobj = trie.load(h)
k = trieobj.keys()
self.assertTrue("foo" in k)
self.assertTrue("hello" in k)
self.assertEqual(repr(trieobj["foo"]), '1')
self.assertEqual(repr(trieobj["hello"]), "'55a'")
def test_get_approximate(self):
# Found bug, doesn't handle insertions and deletions at end properly.
trieobj = trie.trie()
trieobj["hello"] = 1
self.assertEqual(trieobj.get_approximate('he', 2), [])
self.assertEqual(trieobj.get_approximate('he', 3), [('hello', 1, 3)])
self.assertEqual(trieobj.get_approximate('hello me!', 3), [])
self.assertEqual(trieobj.get_approximate('hello me!', 4), [('hello', 1, 4)])
self.assertEqual(trieobj.get_approximate('hello me!', 5), [('hello', 1, 4)])
class TestTrieFind(unittest.TestCase):
def test_find(self):
from Bio import triefind
trieobj = trie.trie()
trieobj["hello"] = 5
trieobj["he"] = 7
trieobj["hej"] = 9
trieobj["foo"] = "bar"
trieobj["wor"] = "ld"
self.assertEqual(triefind.match("hello world!", trieobj), "hello")
k = triefind.match_all("hello world!", trieobj)
k.sort()
self.assertEqual(k, ["he", "hello"])
k = triefind.find("hello world!", trieobj)
k.sort()
self.assertEqual(k, [("he", 0, 2), ("hello", 0, 5), ("wor", 6, 9)])
k = triefind.find_words("hello world!", trieobj)
k.sort()
self.assertEqual(k, [("hello", 0, 5)])
trieobj["world"] = "full"
k = triefind.find("hello world!", trieobj)
k.sort()
self.assertEqual(k, [("he", 0, 2), ("hello", 0, 5), ("wor", 6, 9), ("world", 6, 11)])
k = triefind.find_words("hello world!", trieobj)
k.sort()
self.assertEqual(k, [("hello", 0, 5), ("world", 6, 11)])
if __name__ == "__main__":
runner = unittest.TextTestRunner(verbosity = 2)
unittest.main(testRunner=runner)