-
Notifications
You must be signed in to change notification settings - Fork 3
/
test-noaho.py
413 lines (331 loc) · 10.9 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# !/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 contextlib
import os
import tempfile
import pytest
from noahong import Mapped, NoAho, PayloadWriteError
def test_compile_before_use():
tree = NoAho()
tree.add("bar")
# Cannot be used before compilation
with pytest.raises(AssertionError):
tree.find_short("xxxbaryyy")
tree.compile()
tree.find_short("xxxbaryyy")
# Cannot add after compilation
with pytest.raises(AssertionError):
tree.add("foo")
def test_keyword_as_prefix_of_another():
"""According to John, there's a problem with the matcher.
this test case should expose the bug."""
tree = NoAho()
tree.add("foobar")
tree.add("foo")
tree.add("bar")
tree.compile()
assert (3, 6, None) == tree.find_short("xxxfooyyy")
assert (0, 3, None) == tree.find_short("foo")
assert (3, 6, None) == tree.find_short("xxxbaryyy")
def test_another_find():
"""Just to triangulate the search code. We want to make sure
that the implementation can do more than one search, at
least."""
tree = NoAho()
tree.add("Python")
tree.add("PLT Scheme")
tree.compile()
assert (19, 25, None) == tree.find_short(
"I am learning both Python and PLT Scheme"
) # NOQA
assert (0, 10, None) == tree.find_short(
"PLT Scheme is an interesting language."
) # NOQA
def test_simple_construction():
tree = NoAho()
tree.add("foo")
tree.add("bar")
tree.compile()
(10, 13, None) == tree.find_short("this is a foo message")
tree.children_count() == 6
def test_counts():
tree = NoAho()
tree.add("foo")
tree.compile()
assert tree.nodes_count() == 4
assert tree.children_count() == 3
tree = NoAho()
tree.add("foo")
tree.add("bar")
tree.compile()
assert tree.nodes_count() == 7
assert tree.children_count() == 6
tree = NoAho()
tree.add("fo")
tree.add("foo")
tree.compile()
assert tree.nodes_count() == 4
assert tree.children_count() == 3
def test_find_longest():
tree = NoAho()
tree.add("a")
tree.add("alphabet")
tree.compile()
assert (0, 1, None) == tree.find_short("alphabet soup")
assert (0, 8, None) == tree.find_long("alphabet soup")
assert (13, 14, None) == tree.find_long(
"yummy, I see an alphabet soup bowl"
) # NOQA
def test_find_with_whole_match():
"""Make sure that longest search will match the whole string."""
tree = NoAho()
longString = "supercalifragilisticexpialidocious"
tree.add(longString)
tree.compile()
assert (0, len(longString), None) == tree.find_short(longString)
def test_find_longest_with_whole_match():
"""Make sure that longest search will match the whole string."""
tree = NoAho()
longString = "supercalifragilisticexpialidocious"
tree.add(longString)
tree.compile()
assert (0, len(longString), None) == tree.find_long(longString)
def test_find_longest_with_no_match():
tree = NoAho()
tree.add("foobar")
tree.compile()
assert (None, None, None) == tree.find_long("fooba")
def test_with_expected_non_match():
"""Check to see that we don't always get a successful match."""
tree = NoAho()
tree.add("wise man")
tree.compile()
assert (None, None, None) == tree.find_short(
"where fools and wise men fear to tread"
)
def test_reject_empty_key():
tree = NoAho()
with pytest.raises(ValueError):
tree.add("")
def test_empty_construction():
"""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():
"""Check to see if we can accept embedded nulls"""
tree = NoAho()
tree.add("hell\0 world")
tree.compile()
assert (None, None, None) == tree.find_short("ello\0 world")
assert (0, 11, None) == tree.find_short("hell\0 world")
def test_embedded_nulls_again():
tree = NoAho()
tree.add("\0\0\0")
tree.compile()
assert (0, 3, None) == tree.find_short("\0\0\0\0\0\0\0\0")
def test_findall_and_findall_longest():
tree = NoAho()
tree.add("python")
tree.add("perl")
tree.add("scheme")
tree.add("java")
tree.add("pythonperl")
tree.compile()
assert [
(0, 6, None),
(6, 10, None),
(10, 16, None),
(16, 20, None),
] == list( # NOQA
tree.findall_short("pythonperlschemejava")
)
assert [(0, 10, None), (10, 16, None), (16, 20, None)] == list(
tree.findall_long("pythonperlschemejava")
)
assert [] == list(tree.findall_short("no pascal here"))
assert [] == list(tree.findall_long("no pascal here"))
def test_bug2_competing_longests():
"""Previously we'd return the /last/ key found, now we look forward
while there are contiguous candidate keys, and actually return the
longest.
"""
tree = NoAho()
tree.add("cisco", "cisco")
tree.add("em", "em")
tree.add("cisco systems australia", "cisco systems")
tree.compile()
assert [(0, 5, "cisco"), (10, 12, "em")] == list(
tree.findall_long("cisco systems")
) # NOQA
def test_bug3_false_terminal_nodes():
tree = NoAho()
tree.add("an", None)
tree.add("canal", None)
tree.add("e can oilfield", None)
tree.compile()
assert [(4, 4 + 5, None)], list(tree.findall_long("one canal"))
def test_payload():
tree = NoAho()
class RandomClass(object):
def __init__(self):
pass
obj = RandomClass()
tree.add("python", "yes-python")
tree.add("perl", "")
tree.add("scheme", None)
tree.add("lisp", [1, 2, 3])
# no payload, comes out None
tree.add("C++")
tree.add("dylan", obj)
tree.compile()
assert (0, 6, "yes-python") == tree.find_short("python")
assert (0, 4, "") == tree.find_short("perl")
assert (0, 6, None) == tree.find_short("scheme")
assert (0, 4, [1, 2, 3]) == tree.find_short("lisp")
assert (0, 3, None) == tree.find_short("C++")
assert (0, 5, obj) == tree.find_short("dylan")
def test_dict_style_get_and_set():
tree = NoAho()
tree["foo"] = 5
tree.compile()
assert 5 == tree["foo"]
def test_dict_style_set_empty_key():
tree = NoAho()
with pytest.raises(ValueError):
tree[""] = None
def test_dict_style_set_nonstring_key():
tree = NoAho()
with pytest.raises(ValueError):
tree[6] = None
with pytest.raises(ValueError):
tree[None] = None
with pytest.raises(ValueError):
tree[[]] = None
def test_dict_style_get_unseen_key():
tree = NoAho()
tree.compile()
with pytest.raises(KeyError):
tree["unseen"]
with pytest.raises(KeyError):
tree[""]
def test_dict_style_containment():
tree = NoAho()
tree["foo"] = 5
tree.compile()
assert "foo" in tree
assert "" not in tree
assert "fo" not in tree
assert "o" not in tree
assert "oo" not in tree
assert "f" not in tree
def test_dict_style_len():
tree = NoAho()
tree["a"] = None
tree["b"] = [1, 2]
tree["c"] = 12
tree.compile()
assert 3 == len(tree)
# reminder that we need to figure out which version we're in, and
# test Python 2 unicode explicitly
@pytest.mark.xfail
def test_unicode_in_python2():
assert False
# key iteration is unimplemented
@pytest.mark.xfail
def test_iteration():
tree = NoAho()
tree.add("Harry")
tree.add("Hermione")
tree.add("Ron")
assert set("Harry", "Hermione", "Ron") == set(tree.keys())
# reminder that we need to implement findall_short
@pytest.mark.xfail
def test_subset():
tree = NoAho()
tree.add("he")
tree.add("hers")
assert [(0, 2, None), (0, 4, None)] == list(tree.findall_short("hers"))
def anchor(s):
return s.replace(".", "\u001F")
def test_utf8():
tree = NoAho()
tree.add("étable")
tree.add("béret")
tree.add("blé")
tree.compile()
matches = list(tree.findall_long("étable béret blé"))
assert matches == [(0, 6, None), (7, 12, None), (13, 16, None)]
def test_anchored():
tree = NoAho()
tree.add(anchor(".a..b..c."))
tree.add(anchor(".b."))
tree.compile()
matches = list(tree.findall_anchored(anchor(".a..b..z.")))
assert matches == [(3, 6, None)]
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_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 == []
def test_bad_mapped_trie():
with tempfile.TemporaryDirectory(prefix="noahong-") as tmpdir:
path = os.path.join(tmpdir, "mapped")
# Input file is too short
with open(path, "wb") as fp:
fp.write(b"1")
with pytest.raises(AssertionError):
Mapped(path)
# Invalid BOM
with open(path, "wb") as fp:
fp.write(b"1234")
with pytest.raises(AssertionError):
Mapped(path)
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)