-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest-noaho.py
256 lines (198 loc) · 9.29 KB
/
test-noaho.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
######!/usr/bin/env python
"""Unit tests for Aho Corasick keyword string searching.
Many tests copyright Danny Yoo, the rest copyright Jeff Donner.
Because I think Danny's tests were GPL, so, now, is this whole file.
So, if you distribute this test code you must also distribute .. uh,
this test code (this doesn't apply to the whole package).
Jeff Donner, [email protected]
"""
import sys
from noaho import NoAho
import unittest
class AhoCorasickTest(unittest.TestCase):
def setUp(self):
self.tree = NoAho()
def tearDown(self):
self.tree = None
def test_keyword_as_prefix_of_another(self):
"""According to John, there's a problem with the matcher.
this test case should expose the bug."""
self.tree.add('foobar')
self.tree.add('foo')
self.tree.add('bar')
self.assertEqual((3, 6, None), self.tree.find_short('xxxfooyyy'))
self.assertEqual((0, 3, None), self.tree.find_short('foo'))
self.assertEqual((3, 6, None), self.tree.find_short('xxxbaryyy'))
def test_another_find(self):
"""Just to triangulate the search code. We want to make sure
that the implementation can do more than one search, at
least."""
self.tree.add("Python")
self.tree.add("PLT Scheme")
self.assertEqual((19, 25, None),
self.tree.find_short("I am learning both Python and PLT Scheme"))
self.assertEqual((0, 10, None),
self.tree.find_short("PLT Scheme is an interesting language."))
def test_simple_construction(self):
self.tree.add("foo")
self.tree.add("bar")
self.assertEqual((10, 13, None),
self.tree.find_short("this is a foo message"))
def test_find_longest(self):
self.tree.add("a")
self.tree.add("alphabet");
self.assertEqual((0, 1, None), self.tree.find_short("alphabet soup"));
self.assertEqual((0, 8, None), self.tree.find_long("alphabet soup"))
self.assertEqual((13, 14, None), self.tree.find_long("yummy, I see an alphabet soup bowl"))
def test_find_with_whole_match(self):
"""Make sure that longest search will match the whole string."""
longString = "supercalifragilisticexpialidocious"
self.tree.add(longString)
self.assertEqual((0, len(longString), None),
self.tree.find_short(longString))
def test_find_longest_with_whole_match(self):
"""Make sure that longest search will match the whole string."""
longString = "supercalifragilisticexpialidocious"
self.tree.add(longString)
self.assertEqual((0, len(longString), None),
self.tree.find_long(longString))
def test_find_longest_with_no_match(self):
self.tree.add("foobar")
self.assertEqual((None, None, None), self.tree.find_long("fooba"))
def test_with_expected_non_match(self):
"""Check to see that we don't always get a successful match."""
self.tree.add("wise man")
self.assertEqual((None, None, None),
self.tree.find_short("where fools and wise men fear to tread"))
def test_reject_empty_key(self):
self.assertRaises(ValueError, self.tree.add, "")
def test_empty_construction(self):
"""Make sure that we can safely construct and dealloc a tree
with no initial keywords. Important because the C
implementation assumes keywords exist on its dealloc, so we
have to do some work on the back end to avoid silly segmentation
errors."""
tree = NoAho()
del tree
def test_embedded_nulls(self):
"""Check to see if we can accept embedded nulls"""
self.tree.add("hell\0 world")
self.assertEqual((None, None, None), self.tree.find_short("ello\0 world"))
self.assertEqual((0, 11, None), self.tree.find_short("hell\0 world"))
def test_embedded_nulls_again(self):
self.tree.add("\0\0\0")
self.assertEqual((0, 3, None), self.tree.find_short("\0\0\0\0\0\0\0\0"))
def test_findall_and_findall_longest(self):
self.tree.add("python")
self.tree.add("perl")
self.tree.add("scheme")
self.tree.add("java")
self.tree.add("pythonperl")
self.assertEqual([(0, 6, None), (6, 10, None), (10, 16, None), (16, 20, None)],
list(self.tree.findall_short("pythonperlschemejava")))
self.assertEqual([(0, 10, None), (10, 16, None), (16, 20, None)],
list(self.tree.findall_long("pythonperlschemejava")))
self.assertEqual([],
list(self.tree.findall_short("no pascal here")))
self.assertEqual([],
list(self.tree.findall_long("no pascal here")))
def test_bug2_competing_longests(self):
"""Previously we'd return the /last/ key found, now we look forward
while there are contiguous candidate keys, and actually return the
longest.
"""
self.tree.add('cisco', 'cisco')
self.tree.add('em', 'em')
self.tree.add('cisco systems australia', 'cisco systems')
self.assertEqual([(0, 5, 'cisco'), (10, 12, 'em')],
list(self.tree.findall_long('cisco systems')))
def test_bug3_false_terminal_nodes(self):
self.tree.add('an', None)
self.tree.add('canal', None)
self.tree.add('e can oilfield', None)
self.assertEqual([(4, 4+5, None)],
list(self.tree.findall_long('one canal')))
def test_add_and_find_mix_freely(self):
text = """We got pickles and crocks, We got bagels and lox"""
self.tree.add('lox')
self.assertEqual((45, 48, None), self.tree.find_long(text))
self.tree.add('pickles')
self.assertEqual((7, 14, None), self.tree.find_long(text))
def test_explicit_compilation_still_ok(self):
# ... but vestigial
text = """We got pickles and crocks, We got bagels and lox"""
self.tree["lox"] = None
self.tree.compile()
self.assertEqual((45, 48, None), self.tree.find_long(text))
self.tree["pickles"] = None
self.assertEqual((7, 14, None), self.tree.find_long(text))
def test_payload(self):
class RandomClass(object):
def __init__(self):
pass
obj = RandomClass()
self.tree.add("python", "yes-python")
self.tree.add("perl", "")
self.tree.add("scheme", None)
self.tree.add("lisp", [1,2,3])
# no payload, comes out None
self.tree.add("C++")
self.tree.add("dylan", obj)
self.assertEqual((0, 6, "yes-python"), self.tree.find_short("python"))
self.assertEqual((0, 4, ""), self.tree.find_short("perl"))
self.assertEqual((0, 6, None), self.tree.find_short("scheme"))
self.assertEqual((0, 4, [1,2,3]), self.tree.find_short("lisp"))
self.assertEqual((0, 3, None), self.tree.find_short("C++"))
self.assertEqual((0, 5, obj), self.tree.find_short("dylan"))
def test_dict_style_get_and_set(self):
self.tree['foo'] = 5
self.assertEqual(5, self.tree['foo'])
def test_dict_style_set_empty_key(self):
# equivalent to self.tree[''] = None
# __setitem__ implements this part of the [] protocol
self.assertRaises(ValueError, self.tree.__setitem__, '', None)
def test_dict_style_set_nonstring_key(self):
# equivalent to self.tree[''] = None
# __setitem__ implements this part of the [] protocol
self.assertRaises(ValueError, self.tree.__setitem__, 6, None)
self.assertRaises(ValueError, self.tree.__setitem__, None, None)
self.assertRaises(ValueError, self.tree.__setitem__, [], None)
def test_dict_style_get_unseen_key(self):
# __getitem__ implements this part of the [] protocol
self.assertRaises(KeyError, self.tree.__getitem__, 'unseen')
self.assertRaises(KeyError, self.tree.__getitem__, '')
def test_dict_style_containment(self):
self.tree['foo'] = 5
self.assertEqual(True, 'foo' in self.tree)
self.assertEqual(False, '' in self.tree)
self.assertEqual(False, 'fo' in self.tree)
self.assertEqual(False, 'o' in self.tree)
self.assertEqual(False, 'oo' in self.tree)
self.assertEqual(False, 'f' in self.tree)
def test_dict_style_len(self):
self.tree['a'] = None
self.tree['b'] = [1,2]
self.tree['c'] = 12
self.assertEqual(3, len(self.tree))
# reminder that we need to figure out which version we're in, and
# test Python 2 unicode explicitly
@unittest.expectedFailure
def test_unicode_in_python2(self):
self.assertEqual(True, False)
# key iteration is unimplemented
@unittest.expectedFailure
def test_iteration(self):
self.tree.add("Harry")
self.tree.add("Hermione")
self.tree.add("Ron")
self.assertEqual(set("Harry", "Hermione", "Ron"), set(self.tree.keys()))
# reminder that we need to implement findall_short
@unittest.expectedFailure
def test_subset(self):
self.tree.add("he")
self.tree.add("hers")
self.assertEqual([(0, 2, None), (0, 4, None)], list(self.tree.findall_short("hers")))
def main(args):
unittest.main()
if __name__ == '__main__':
main(sys.argv[1:])