37# include <gtest/gtest.h>
48TEST(SuffixStructures, SuffixArrayBanana)
54TEST(SuffixStructures, LcpKasaiBanana)
61TEST(SuffixStructures, LcpKasaiRejectsInvalidInput)
68TEST(SuffixStructures, LcpKasaiRejectsDuplicates)
76TEST(SuffixStructures, LcpKasaiRejectsOutOfRange)
84TEST(SuffixStructures, NaiveSuffixTreeContainsAndFindAll)
102TEST(SuffixStructures, NaiveSuffixTreeSentinelExhaustion)
105 std::string full_alphabet;
106 for (
int i = 0; i < 256; ++i)
107 full_alphabet.push_back(
static_cast<char>(i));
113TEST(SuffixStructures, SuffixAutomatonContainsAndDistinctCount)
126TEST(SuffixStructures, SuffixAutomatonLongestCommonSubstring)
135TEST(SuffixStructures, SuffixArrayLongerText)
137 const std::string text =
"mississippi";
140 ASSERT_EQ(sa.size(), text.size());
143 for (
size_t i = 1; i < sa.size(); ++i)
145 const std::string_view prev_suffix(text.data() + sa[i - 1],
146 text.size() - sa[i - 1]);
147 const std::string_view curr_suffix(text.data() + sa[i],
148 text.size() - sa[i]);
149 EXPECT_LT(prev_suffix, curr_suffix);
153TEST(SuffixStructures, SuffixArraySingleChar)
159TEST(SuffixStructures, SuffixArrayEmpty)
162 EXPECT_TRUE(sa.is_empty());
165TEST(SuffixStructures, SuffixTreeRepeatedPattern)
174 const auto matches = st.
find_all(
"abc");
175 ASSERT_EQ(matches.size(), 3u);
179TEST(SuffixStructures, SuffixTreeSingleChar)
187TEST(SuffixStructures, SuffixAutomatonEmptyAndSingleChar)
201TEST(SuffixStructures, SuffixAutomatonLcsNoCommon)
206TEST(SuffixStructures, StressLongerSuffixArray)
211 for (
int i = 0; i < 5000; ++i)
212 text.push_back(
'a' + (i * 7 + 3) % 26);
214 const auto start = std::chrono::steady_clock::now();
216 const auto end = std::chrono::steady_clock::now();
217 const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
221 if (std::getenv(
"ENABLE_PERF_TESTS"))
222 EXPECT_LT(elapsed, 500) <<
"Performance regression: suffix_array(5000) took " << elapsed <<
"ms";
223 else if (elapsed >= 500)
224 std::cout <<
"[ PERF ] Warning: suffix_array(5000) took " << elapsed <<
"ms\n";
226 ASSERT_EQ(sa.size(), text.size());
228 for (
size_t i = 1; i < sa.size(); ++i)
230 const std::string_view prev_suffix(text.data() + sa[i - 1],
231 text.size() - sa[i - 1]);
232 const std::string_view curr_suffix(text.data() + sa[i],
233 text.size() - sa[i]);
234 EXPECT_LT(prev_suffix, curr_suffix);
238TEST(SuffixStructures, BinaryContentSuffixAutomaton)
241 for (
int i = 0; i < 256; ++i)
242 text.push_back(
static_cast<char>(i));
248 for (
int i = 0; i < 256; ++i)
250 std::string pat(1,
static_cast<char>(i));
254 EXPECT_FALSE(sam.
contains(text +
"x"));
257TEST(SuffixStructures, SuffixAutomatonCloneLinkInvariant)
263 sam.
build(
"bcaaaccaaacccaacca");
266 const auto & states = sam.
states();
267 for (
size_t i = 1; i < states.size(); ++i)
268 EXPECT_GE(states[i].link, 0) <<
"state " << i <<
" has link == -1";
273 EXPECT_TRUE(sam.
contains(
"caaaccaaaccc"));
Suffix structures: suffix array/LCP, suffix tree, suffix automaton.
Simple dynamic array with automatic resizing and functional operations.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
T & append(const T &data)
Append a copy of data
Naive compressed suffix tree (didactic implementation).
Array< size_t > find_all(const std::string_view pattern) const
Return all occurrences of a pattern.
size_t node_count() const noexcept
Return the number of nodes in the tree.
size_t text_size() const noexcept
Return the text size (without terminal marker).
bool contains(const std::string_view pattern) const
Return true if the pattern appears in the text.
const Array< Node > & nodes() const noexcept
Return the internal node array for inspection/debugging.
Suffix automaton (SAM) over byte alphabet.
std::string longest_common_substring(const std::string_view other) const
Find the longest common substring between the built SAM text and other.
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.
const Array< State > & states() const noexcept
Access the internal states array for testing/inspection.
size_t distinct_substring_count() const
Count the number of distinct substrings in the built text.
size_t state_count() const noexcept
Return the number of states in the SAM.
void expect_array_eq(const Array< size_t > &a, std::initializer_list< size_t > expected)
Assert that an Aleph::Array contains exactly the elements in an initializer_list.
Main namespace for Aleph-w library functions.
Array< size_t > lcp_array_kasai(const std::string_view text, const Array< size_t > &sa)
Compute LCP array from text and suffix array using Kasai.
std::string longest_common_substring_sam(const std::string_view a, const std::string_view b)
Convenience function: LCS via suffix automaton.
Array< size_t > suffix_array(const std::string_view text)
Build suffix array with doubling algorithm.
Shared helper functions for Aleph-w unit tests.