forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rolling_hash.py
More file actions
49 lines (33 loc) · 1.21 KB
/
test_rolling_hash.py
File metadata and controls
49 lines (33 loc) · 1.21 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
41
42
43
44
45
46
47
48
49
"""Tests for rolling hash Rabin-Karp implementation."""
import pytest
from rolling_hash.rabin_karp import rabin_karp
def test_basic_matches():
assert rabin_karp("abracadabra", "abra") == [0, 7]
assert rabin_karp("aaaaa", "aa") == [0, 1, 2, 3]
assert rabin_karp("hello world", "world") == [6]
def test_no_match():
assert rabin_karp("abcdef", "gh") == []
assert rabin_karp("abc", "abcd") == []
def test_empty_pattern():
# Empty pattern matches at every position (including end)
assert rabin_karp("abc", "") == [0, 1, 2, 3]
assert rabin_karp("", "") == [0]
def test_single_character():
assert rabin_karp("a", "a") == [0]
assert rabin_karp("ab", "a") == [0]
assert rabin_karp("ab", "b") == [1]
def test_overlapping():
text = "aaa"
pattern = "aa"
assert rabin_karp(text, pattern) == [0, 1]
def test_case_sensitive():
assert rabin_karp("ABCabc", "abc") == [3]
assert rabin_karp("ABCabc", "ABC") == [0]
def test_unicode():
# Unicode characters
assert rabin_karp("你好世界你好", "你好") == [0, 4]
def test_long_pattern():
text = "a" * 1000
pattern = "a" * 100
expected = list(range(0, 901))
assert rabin_karp(text, pattern) == expected