|
| 1 | +# Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) |
| 2 | +# in Pull Request: #11554 |
| 3 | +# https://github.com/TheAlgorithms/Python/pull/11554 |
| 4 | +# |
| 5 | +# Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request |
| 6 | +# addressing bugs/corrections to this file. |
| 7 | +# Thank you! |
| 8 | + |
| 9 | +import unittest |
| 10 | + |
| 11 | +from data_structures.suffix_tree.suffix_tree import SuffixTree |
| 12 | + |
| 13 | + |
| 14 | +class TestSuffixTree(unittest.TestCase): |
| 15 | + def setUp(self) -> None: |
| 16 | + """Set up the initial conditions for each test.""" |
| 17 | + self.text = "banana" |
| 18 | + self.suffix_tree = SuffixTree(self.text) |
| 19 | + |
| 20 | + def test_search_existing_patterns(self) -> None: |
| 21 | + """Test searching for patterns that exist in the suffix tree.""" |
| 22 | + patterns = ["ana", "ban", "na"] |
| 23 | + for pattern in patterns: |
| 24 | + with self.subTest(pattern=pattern): |
| 25 | + assert self.suffix_tree.search( |
| 26 | + pattern |
| 27 | + ), f"Pattern '{pattern}' should be found." |
| 28 | + |
| 29 | + def test_search_non_existing_patterns(self) -> None: |
| 30 | + """Test searching for patterns that do not exist in the suffix tree.""" |
| 31 | + patterns = ["xyz", "apple", "cat"] |
| 32 | + for pattern in patterns: |
| 33 | + with self.subTest(pattern=pattern): |
| 34 | + assert not self.suffix_tree.search( |
| 35 | + pattern |
| 36 | + ), f"Pattern '{pattern}' should not be found." |
| 37 | + |
| 38 | + def test_search_empty_pattern(self) -> None: |
| 39 | + """Test searching for an empty pattern.""" |
| 40 | + assert self.suffix_tree.search(""), "An empty pattern should be found." |
| 41 | + |
| 42 | + def test_search_full_text(self) -> None: |
| 43 | + """Test searching for the full text.""" |
| 44 | + assert self.suffix_tree.search( |
| 45 | + self.text |
| 46 | + ), "The full text should be found in the suffix tree." |
| 47 | + |
| 48 | + def test_search_substrings(self) -> None: |
| 49 | + """Test searching for substrings of the full text.""" |
| 50 | + substrings = ["ban", "ana", "a", "na"] |
| 51 | + for substring in substrings: |
| 52 | + with self.subTest(substring=substring): |
| 53 | + assert self.suffix_tree.search( |
| 54 | + substring |
| 55 | + ), f"Substring '{substring}' should be found." |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + unittest.main() |
0 commit comments