-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_engine.py
More file actions
40 lines (30 loc) · 1.57 KB
/
Copy pathtest_engine.py
File metadata and controls
40 lines (30 loc) · 1.57 KB
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
from engine import levenshtein_distance
from bktree import BKTree
class TestAutocompleteEngine(unittest.TestCase):
def test_levenshtein_distance(self):
# 1. Identity: Distance to itself should be 0
self.assertEqual(levenshtein_distance("apple", "apple"), 0)
# 2. Symmetry: Order shouldn't matter
self.assertEqual(levenshtein_distance("kitten", "sitting"), 3)
self.assertEqual(levenshtein_distance("sitting", "kitten"), 3)
# 3. Known cases (Insertions, Deletions, Substitutions)
self.assertEqual(levenshtein_distance("book", "books"), 1) # 1 insertion
self.assertEqual(levenshtein_distance("cake", "bake"), 1) # 1 substitution
self.assertEqual(levenshtein_distance("", "hello"), 5) # Empty string
def test_bktree_search(self):
tree = BKTree(levenshtein_distance)
# Add a mini-dictionary
for word in ["book", "books", "cake", "boo", "cook"]:
tree.add(word)
# Search for 'kook' with a tolerance of 1
# The only valid matches should be 'book' and 'cook'
results = tree.search("kook", tolerance=1)
# Extract just the words from the (distance, word) tuples
matched_words = [word for dist, word in results]
# Verify the tree pruned properly and found the right matches
self.assertEqual(len(matched_words), 2)
self.assertIn("book", matched_words)
self.assertIn("cook", matched_words)
if __name__ == '__main__':
unittest.main()