Skip to content

Commit e588dad

Browse files
committed
Added tests
1 parent 007a1aa commit e588dad

File tree

4 files changed

+69
-31
lines changed

4 files changed

+69
-31
lines changed

pydatastructs/strings/algorithms.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ def find(text, query, algorithm):
5353
5454
.. [1] https://www.inf.hs-flensburg.de/lang/algorithmen/pattern/kmpen.htm
5555
"""
56-
import pydatastructs.strings.algorithms as algorithms
56+
import pydatastructs.strings.algorithms as algorithms
5757
func = "_" + algorithm
58-
if not hasattr(algorithms, func):
59-
raise NotImplementedError(
58+
if not hasattr(algorithms, func):
59+
raise NotImplementedError(
6060
"Currently %s algoithm for searching strings "
6161
"inside a text isn't implemented yet."
62-
%(algorithm))
62+
%(algorithm))
6363
return getattr(algorithms, func)(text, query)
6464

6565

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from pydatastructs.strings import find
2+
3+
import random, string
4+
5+
def test_kmp():
6+
_test_common_string_matching('kmp')
7+
8+
9+
def _test_common_string_matching(algorithm):
10+
true_text_pattern_dictionary = {
11+
"Knuth-Morris-Pratt": "-Morris-",
12+
"abcabcabcabdabcabdabcabca": "abcabdabcabca",
13+
"aefcdfaecdaefaefcdaefeaefcdcdeae": "aefcdaefeaefcd",
14+
"aaaaaaaa": "aaa",
15+
"fullstringmatch": "fullstringmatch"
16+
}
17+
for test_case_key in true_text_pattern_dictionary:
18+
text = test_case_key
19+
query = true_text_pattern_dictionary[test_case_key]
20+
positions = find(text, query, algorithm)
21+
for i in range(positions._last_pos_filled):
22+
p = positions[i]
23+
assert text[p:p + len(query)] == query
24+
25+
false_text_pattern_dictionary = {
26+
"Knuth-Morris-Pratt": "-Pratt-",
27+
"abcabcabcabdabcabdabcabca": "qwertyuiopzxcvbnm",
28+
"aefcdfaecdaefaefcdaefeaefcdcdeae": "cdaefaefe",
29+
"fullstringmatch": "fullstrinmatch"
30+
}
31+
32+
for test_case_key in false_text_pattern_dictionary:
33+
text = test_case_key
34+
query = false_text_pattern_dictionary[test_case_key]
35+
positions = find(text, query, algorithm)
36+
assert positions.size == 0
37+
38+
random.seed(1000)
39+
40+
def gen_random_string(length):
41+
ascii = string.ascii_uppercase
42+
digits = string.digits
43+
return ''.join(random.choices(ascii + digits, k=length))
44+
45+
for _ in range(100):
46+
query = gen_random_string(random.randint(3, 10))
47+
num_times = random.randint(1, 10)
48+
freq = 0
49+
text = ""
50+
while freq < num_times:
51+
rand_str = gen_random_string(random.randint(5, 10))
52+
if rand_str != query:
53+
freq += 1
54+
text += query + rand_str + query
55+
positions = find(text, query, algorithm="kmp")
56+
assert positions._num == num_times * 2
57+
for i in range(positions._last_pos_filled):
58+
p = positions[i]
59+
assert text[p:p + len(query)] == query
60+
61+
text = gen_random_string(len(query))
62+
if text != query:
63+
positions = find(text, query, algorithm="kmp")
64+
assert positions.size == 0

pydatastructs/strings/tests/test_string_matching_algorithms.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

pydatastructs/utils/tests/test_code_quality.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_trailing_white_spaces():
2222
while line != "":
2323
if line.endswith(" \n") or line.endswith("\t\n") \
2424
or line.endswith(" ") or line.endswith("\t"):
25-
assert False, "%s contains trailing whitespace at line number %d: %s"\
25+
assert False, "%s:%d : %s"\
2626
%(file_path, line_number, line)
2727
line = file.readline()
2828
line_number += 1

0 commit comments

Comments
 (0)