37# include <gtest/gtest.h>
43TEST(StringAlgorithmsUmbrella, BasicInteroperability)
57TEST(StringAlgorithmsUmbrella, SearchAlgorithmsAgreeOnSameInput)
59 const std::string text =
"the quick brown fox jumps over the lazy dog fox";
60 const std::string pattern =
"fox";
63 const auto z =
z_search(text, pattern);
67 ASSERT_EQ(kmp.size(), z.size());
68 ASSERT_EQ(kmp.size(), bmh.size());
69 ASSERT_EQ(kmp.size(), rk.size());
71 for (
size_t i = 0; i < kmp.size(); ++i)
73 EXPECT_EQ(kmp[i], z[i]);
74 EXPECT_EQ(kmp[i], bmh[i]);
75 EXPECT_EQ(kmp[i], rk[i]);
79TEST(StringAlgorithmsUmbrella, AhoCorasickMatchesKmpForSinglePattern)
81 const std::string text =
"ababababab";
82 const std::string pattern =
"abab";
84 const auto kmp_matches =
kmp_search(text, pattern);
89 const auto ac_matches = ac.
search(text);
92 ASSERT_EQ(ac_matches.size(), kmp_matches.size());
95 for (
size_t i = 0; i < ac_matches.size(); ++i)
96 EXPECT_EQ(ac_matches[i].position, kmp_matches[i]);
99TEST(StringAlgorithmsUmbrella, SuffixTreeMatchesKmpForSearch)
101 const std::string text =
"abracadabra";
102 const std::string pattern =
"abra";
104 const auto kmp_matches =
kmp_search(text, pattern);
107 auto tree_matches = st.
find_all(pattern);
109 ASSERT_EQ(tree_matches.size(), kmp_matches.size());
111 for (
size_t i = 0; i < tree_matches.size(); ++i)
112 EXPECT_EQ(tree_matches[i], kmp_matches[i]);
115TEST(StringAlgorithmsUmbrella, SamContainsAgreesWithSuffixTree)
117 const std::string text =
"mississippi";
123 const char * patterns[] = {
"issi",
"miss",
"pi",
"ppi",
"xyz",
"sip",
""};
124 for (
const char * p : patterns)
126 <<
"Disagreement on pattern: \"" << p <<
"\"";
129TEST(StringAlgorithmsUmbrella, LcsVsSamLcs)
131 const std::string a =
"xabxac";
132 const std::string b =
"abcabxabcd";
138 EXPECT_EQ(dp_result.length, sam_result.size());
139 EXPECT_EQ(dp_result.substring, sam_result);
Umbrella header for classical string algorithms in Aleph-w.
Aho-Corasick multi-pattern automaton.
bool contains_any(const std::string_view text) const
Return true if at least one pattern appears in the text.
void build()
Build failure links and transition completion.
size_t add_pattern(std::string pattern)
Add one pattern to the automaton.
Array< Match > search(const std::string_view text) const
Search all patterns in a text.
Naive compressed suffix tree (didactic implementation).
Array< size_t > find_all(const std::string_view pattern) const
Return all occurrences of a pattern.
bool contains(const std::string_view pattern) const
Return true if the pattern appears in the text.
Suffix automaton (SAM) over byte alphabet.
void build(const std::string_view text)
Build the SAM from an entire string.
bool contains(const std::string_view pattern) const
Return true if pattern is a substring of the built text.
Main namespace for Aleph-w library functions.
Array< size_t > z_search(const std::string_view text, const std::string_view pattern)
Find all occurrences of a pattern using the Z-algorithm.
Array< size_t > rabin_karp_search(const std::string_view text, const std::string_view pattern, const uint64_t base=911382323ull)
Find all occurrences using Rabin-Karp with rolling hash.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
size_t edit_distance(const std::string_view a, const std::string_view b)
Alias for Levenshtein distance.
std::string longest_common_substring_sam(const std::string_view a, const std::string_view b)
Convenience function: LCS via suffix automaton.
Longest_Common_Substring_Result longest_common_substring(const std::string_view a, const std::string_view b)
Compute the longest common substring (contiguous) between two strings.
std::string longest_palindromic_substring(const std::string_view text)
Convenience wrapper returning only the longest palindromic substring.
Array< size_t > kmp_search(const std::string_view text, const std::string_view pattern)
Find all occurrences of a pattern using KMP.
Array< size_t > boyer_moore_horspool_search(const std::string_view text, const std::string_view pattern)
Find all occurrences using Boyer-Moore-Horspool.
Array< size_t > suffix_array(const std::string_view text)
Build suffix array with doubling algorithm.