-
Notifications
You must be signed in to change notification settings - Fork 2
/
automata_test.py
40 lines (31 loc) · 1.13 KB
/
automata_test.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
import unittest
import automata
import bisect
words = [x.strip().lower() for x in open('sowpods.txt')]
words.sort()
class Tests(unittest.TestCase):
def test_food(self):
m = Matcher(words)
self.assertEqual(len((list(automata.find_all_matches('food', 1, m)))), 18)
self.assertEqual(len((list(automata.find_all_matches('food', 2, m)))), 318)
def test_hello(self):
m = Matcher(words)
self.assertEqual(len((list(automata.find_all_matches('hello', 1, m)))), 12)
self.assertEqual(len((list(automata.find_all_matches('hello', 2, m)))), 128)
def test_slice(self):
m = Matcher(words)
self.assertEqual(len((list(automata.find_all_matches('slice', 1, m)))), 15)
self.assertEqual(len((list(automata.find_all_matches('slice', 2, m)))), 213)
class Matcher(object):
def __init__(self, l):
self.l = l
self.probes = 0
def __call__(self, w):
self.probes += 1
pos = bisect.bisect_left(self.l, w)
if pos < len(self.l):
return self.l[pos]
else:
return None
if __name__ == '__main__':
unittest.main()