|
| 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 |
0 commit comments