37# include <gtest/gtest.h>
45TEST(AhoCorasick, ClassicDictionary)
55 const auto matches = ac.
search(
"ahishers");
57 ASSERT_EQ(matches.size(), 4u);
64TEST(AhoCorasick, OverlappingPatterns)
72 const auto matches = ac.
search(
"aaaa");
78 for (
const auto &
m : matches)
80 if (
m.pattern_id == id_a)
82 else if (
m.pattern_id == id_aa)
84 else if (
m.pattern_id == id_aaa)
89 EXPECT_EQ(cnt_aa, 3u);
90 EXPECT_EQ(cnt_aaa, 2u);
94TEST(AhoCorasick, BuildRequiredBeforeSearch)
98 EXPECT_THROW((
void) ac.
search(
"abc"), std::runtime_error);
101TEST(AhoCorasick, IncrementalBuildAfterPreviousBuild)
107 const auto he_only = ac.
search(
"he");
108 ASSERT_EQ(he_only.size(), 1u);
114 const auto she_matches = ac.
search(
"she");
115 ASSERT_EQ(she_matches.size(), 2u);
120 const auto he_after_rebuild = ac.
search(
"he");
121 ASSERT_EQ(he_after_rebuild.size(), 1u);
125TEST(AhoCorasick, EmptyPatternRejected)
128 EXPECT_THROW((
void) ac.
add_pattern(
""), std::invalid_argument);
131TEST(AhoCorasick, ClearAndRebuild)
145 const auto matches = ac.
search(
"xxabcxx");
146 ASSERT_EQ(matches.size(), 1u);
148 EXPECT_EQ(ac.
pattern(
id),
"abc");
151TEST(AhoCorasick, LargePatternSet)
157 for (
int i = 0; i < 100; ++i)
159 std::string pat =
"p";
164 pat += std::to_string(i);
171 std::string text =
"xxp000xxp050xxp099xx";
172 const auto matches = ac.
search(text);
174 EXPECT_GE(matches.size(), 3u);
177 for (
size_t i = 0; i < matches.size(); ++i)
179 const auto &
m = matches[i];
180 EXPECT_LT(
m.pattern_id, 100u);
181 const auto & pat = ac.
pattern(
m.pattern_id);
182 EXPECT_EQ(text.substr(
m.position, pat.
size()), pat);
186TEST(AhoCorasick, LongTextSearch)
196 for (
int i = 0; i < 2000; ++i)
199 text.replace(100, 6,
"needle");
200 text.replace(5000, 6,
"needle");
202 const auto matches = ac.
search(text);
203 EXPECT_GE(matches.size(), 2u);
205 size_t needle_count = 0;
206 for (
size_t i = 0; i < matches.size(); ++i)
207 if (ac.
pattern(matches[i].pattern_id) ==
"needle")
209 EXPECT_EQ(needle_count, 2u);
212TEST(AhoCorasick, SingleCharPatterns)
219 const auto matches = ac.
search(
"abba");
220 ASSERT_EQ(matches.size(), 4u);
227TEST(AhoCorasick, BinaryPatterns)
231 pat.push_back(
static_cast<char>(0x00));
232 pat.push_back(
static_cast<char>(0xFF));
236 std::string text =
"abc";
237 text.push_back(
static_cast<char>(0x00));
238 text.push_back(
static_cast<char>(0xFF));
241 const auto matches = ac.
search(text);
242 ASSERT_EQ(matches.size(), 1u);
243 EXPECT_EQ(matches[0].position, 3u);
253 const auto matches = ac.
search(
"");
254 EXPECT_TRUE(matches.is_empty());
257TEST(AhoCorasick, NoMatchesFound)
264 const auto matches = ac.
search(
"xyzxyz");
265 EXPECT_TRUE(matches.is_empty());
270TEST(AhoCorasick, PrefixChainPatterns)
280 const auto matches = ac.
search(
"abcabc");
287 for (
size_t i = 0; i < matches.size(); ++i)
289 if (matches[i].pattern_id == id_a)
291 else if (matches[i].pattern_id == id_ab)
293 else if (matches[i].pattern_id == id_abc)
298 EXPECT_EQ(cnt_a, 2u);
300 EXPECT_EQ(cnt_ab, 2u);
302 EXPECT_EQ(cnt_abc, 2u);
304 EXPECT_EQ(matches.size(), 6u);
307TEST(AhoCorasick, PrefixChainWithSuffixOverlap)
319 const auto matches = ac.
search(
"abc");
327 for (
size_t i = 0; i < matches.size(); ++i)
329 if (matches[i].pattern_id == id_a) ++cnt_a;
330 else if (matches[i].pattern_id == id_ab) ++cnt_ab;
331 else if (matches[i].pattern_id == id_abc) ++cnt_abc;
332 else if (matches[i].pattern_id == id_bc) ++cnt_bc;
333 else if (matches[i].pattern_id == id_c) ++cnt_c;
336 EXPECT_EQ(cnt_a, 1u);
337 EXPECT_EQ(cnt_ab, 1u);
338 EXPECT_EQ(cnt_abc, 1u);
339 EXPECT_EQ(cnt_bc, 1u);
340 EXPECT_EQ(cnt_c, 1u);
341 EXPECT_EQ(matches.size(), 5u);
344TEST(AhoCorasick, LongPrefixChain)
349 for (
int len = 1; len <= 10; ++len)
354 const std::string text(15,
'x');
355 const auto matches = ac.
search(text);
359 for (
size_t i = 0; i < 10; ++i)
362 for (
size_t i = 0; i < matches.size(); ++i)
363 ++counts[matches[i].pattern_id];
366 for (
int k = 1;
k <= 10; ++
k)
367 EXPECT_EQ(counts[
static_cast<size_t>(
k - 1)],
368 static_cast<size_t>(15 -
k + 1))
369 <<
"pattern length " <<
k;
372TEST(AhoCorasick, PrefixPatternsMatchPositions)
381 const auto matches = ac.
search(
"xabcx");
387 for (
size_t i = 0; i < matches.size(); ++i)
389 if (matches[i].pattern_id == id_a)
390 pos_a.
append(matches[i].position);
391 else if (matches[i].pattern_id == id_ab)
392 pos_ab.
append(matches[i].position);
393 else if (matches[i].pattern_id == id_abc)
394 pos_abc.
append(matches[i].position);
397 ASSERT_EQ(pos_a.
size(), 1u);
398 EXPECT_EQ(pos_a[0], 1u);
400 ASSERT_EQ(pos_ab.
size(), 1u);
401 EXPECT_EQ(pos_ab[0], 1u);
403 ASSERT_EQ(pos_abc.
size(), 1u);
404 EXPECT_EQ(pos_abc[0], 1u);
407TEST(AhoCorasick, DeterministicStress)
410 const int num_patterns = 1000;
414 for (
int i = 0; i < num_patterns; ++i)
416 std::string p =
"pat" + std::to_string(i);
425 text.reserve(200000);
426 for (
int i = 0; i < 5000; ++i)
428 text +=
"some_random_filler_";
430 text += patterns[i % num_patterns];
433 const auto matches = ac.
search(text);
434 EXPECT_GT(matches.size(), 0u);
436 for (
const auto &
m : matches)
438 const auto & pat = ac.
pattern(
m.pattern_id);
439 EXPECT_EQ(text.substr(
m.position, pat.
size()), pat);
Multi-pattern string matching with the Aho-Corasick automaton.
Aho-Corasick multi-pattern automaton.
const std::string & pattern(const size_t id) const
Return the pattern text by id.
bool contains_any(const std::string_view text) const
Return true if at least one pattern appears in the text.
void clear()
Clear all patterns and reset to an empty automaton.
size_t pattern_count() const noexcept
Return the number of inserted patterns.
bool is_built() const noexcept
Return whether build() has been executed since last insertion.
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.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
static Array create(const size_t n)
Create an array with n logical elements.
T & append(const T &data)
Append a copy of data
constexpr size_t size() const noexcept
Returns the number of entries in the table.
Main namespace for Aleph-w library functions.
One match occurrence in the searched text.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)