Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
string_dp_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
37# include <gtest/gtest.h>
38
39# include <String_DP.H>
40
41using namespace Aleph;
42
44{
45 EXPECT_EQ(levenshtein_distance("kitten", "sitting"), 3u);
46 EXPECT_EQ(edit_distance("kitten", "sitting"), 3u);
47 EXPECT_EQ(levenshtein_distance("", "abc"), 3u);
48 EXPECT_EQ(levenshtein_distance("abc", ""), 3u);
49 EXPECT_EQ(levenshtein_distance("abc", "abc"), 0u);
50}
51
58
60{
61 const auto r = longest_common_subsequence("AGGTAB", "GXTXAYB");
62 EXPECT_EQ(r.length, 4u);
63 EXPECT_EQ(r.subsequence, "GTAB");
64}
65
67{
68 const auto r = longest_common_substring("xabxac", "abcabxabcd");
69 EXPECT_EQ(r.length, 4u);
70 EXPECT_EQ(r.substring, "abxa");
71 EXPECT_EQ(r.begin_a, 1u);
72 EXPECT_EQ(r.begin_b, 3u);
73}
74
76{
77 const auto r = longest_common_substring("abc", "XYZ");
78 EXPECT_EQ(r.length, 0u);
79 EXPECT_EQ(r.substring, "");
80}
81
89
91{
92 // Insertion
93 EXPECT_EQ(levenshtein_distance("abc", "abxc"), 1u);
94 // Deletion
95 EXPECT_EQ(levenshtein_distance("abcd", "abd"), 1u);
96 // Substitution
97 EXPECT_EQ(levenshtein_distance("abc", "axc"), 1u);
98}
99
101{
102 EXPECT_EQ(levenshtein_distance("hello world", "hello world"), 0u);
103}
104
112
117
119{
120 // Transposition: DL = 1, Lev = 2
122 EXPECT_EQ(levenshtein_distance("ab", "ba"), 2u);
123}
124
126{
127 const auto r1 = longest_common_subsequence("", "abc");
128 EXPECT_EQ(r1.length, 0u);
129 EXPECT_EQ(r1.subsequence, "");
130
131 const auto r2 = longest_common_subsequence("abc", "");
132 EXPECT_EQ(r2.length, 0u);
133 EXPECT_EQ(r2.subsequence, "");
134}
135
137{
138 const auto r = longest_common_subsequence("a", "a");
139 EXPECT_EQ(r.length, 1u);
140 EXPECT_EQ(r.subsequence, "a");
141}
142
144{
145 const auto r = longest_common_substring("a", "a");
146 EXPECT_EQ(r.length, 1u);
147 EXPECT_EQ(r.substring, "a");
148}
149
151{
152 // Two strings of length 100 that differ in exactly 10 positions
153 std::string a(100, 'a');
154 std::string b = a;
155 for (int i = 0; i < 10; ++i)
156 b[i * 10] = 'b';
157
159}
Dynamic-programming algorithms for string similarity and alignment.
#define TEST(name)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
LCS_Result longest_common_subsequence(const std::string_view a, const std::string_view b)
Compute Longest Common Subsequence.
Definition String_DP.H:241
size_t levenshtein_distance(const std::string_view a, const std::string_view b)
Levenshtein distance (insert/delete/substitute each cost 1).
Definition String_DP.H:94
size_t edit_distance(const std::string_view a, const std::string_view b)
Alias for Levenshtein distance.
Definition String_DP.H:147
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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.
Definition String_DP.H:301
size_t damerau_levenshtein_distance(const std::string_view a, const std::string_view b)
Damerau-Levenshtein distance with adjacent transpositions.
Definition String_DP.H:167
gsl_rng * r