33#include < catch2/catch_test_macros.hpp>
44#include < rapidfuzz/distance/JaroWinkler.hpp>
55
6+ #include " ../common.hpp"
7+
68using Catch::Approx;
79
810template <typename Sentence1, typename Sentence2>
@@ -86,9 +88,24 @@ double jaro_winkler_distance(const Sentence1& s1, const Sentence2& s2, double pr
8688 return res1;
8789}
8890
89- /* *
90- * @name JaroWinklerFlagCharsTest
91- */
91+ template <typename Sentence1, typename Sentence2>
92+ double jaro_winkler_sim_test (const Sentence1& s1, const Sentence2& s2, double score_cutoff = 0.0 )
93+ {
94+ INFO (" name1: " << s1 << " , name2: " << s2 << " , score_cutoff: " << score_cutoff);
95+ double Sim_original = rapidfuzz_reference::jaro_winkler_similarity (s1, s2, 0.1 , score_cutoff);
96+ double Sim_bitparallel = jaro_winkler_similarity (s1, s2, 0.1 , score_cutoff);
97+ double Dist_bitparallel = jaro_winkler_distance (s1, s2, 0.1 , 1.0 - score_cutoff);
98+ double Sim_bitparallel2 = jaro_winkler_similarity (s2, s1, 0.1 , score_cutoff);
99+ double Dist_bitparallel2 = jaro_winkler_distance (s2, s1, 0.1 , 1.0 - score_cutoff);
100+
101+
102+ REQUIRE (Sim_original == Approx (Sim_bitparallel));
103+ REQUIRE ((1.0 - Sim_original) == Approx (Dist_bitparallel));
104+ REQUIRE (Sim_original == Approx (Sim_bitparallel2));
105+ REQUIRE ((1.0 - Sim_original) == Approx (Dist_bitparallel2));
106+ return Sim_original;
107+ }
108+
92109TEST_CASE (" JaroWinklerTest" )
93110{
94111 std::array<std::string, 22 > names = {" james" , " robert" , " john" , " michael" , " william" ,
@@ -103,14 +120,26 @@ TEST_CASE("JaroWinklerTest")
103120
104121 for (double score_cutoff : score_cutoffs)
105122 for (const auto & name1 : names)
106- for (const auto & name2 : names) {
107- INFO (" name1: " << name1 << " , name2: " << name2 << " , score_cutoff: " << score_cutoff);
108- double Sim_original =
109- rapidfuzz_reference::jaro_winkler_similarity (name1, name2, 0.1 , score_cutoff);
110- double Sim_bitparallel = jaro_winkler_similarity (name1, name2, 0.1 , score_cutoff);
111- double Dist_bitparallel = jaro_winkler_distance (name1, name2, 0.1 , 1.0 - score_cutoff);
112- REQUIRE (Sim_original == Approx (Sim_bitparallel));
113- REQUIRE ((1.0 - Sim_original) == Approx (Dist_bitparallel));
114- }
123+ for (const auto & name2 : names)
124+ jaro_winkler_sim_test (name1, name2, score_cutoff);
125+ }
126+
127+ SECTION (" testEdgeCaseLengths" )
128+ {
129+ REQUIRE (jaro_winkler_sim_test (std::string (" " ), std::string (" " )) == Approx (1 ));
130+ REQUIRE (jaro_winkler_sim_test (std::string (" 0" ), std::string (" 0" )) == Approx (1 ));
131+ REQUIRE (jaro_winkler_sim_test (std::string (" 00" ), std::string (" 00" )) == Approx (1 ));
132+ REQUIRE (jaro_winkler_sim_test (std::string (" 0" ), std::string (" 00" )) == Approx (0.85 ));
133+
134+ REQUIRE (jaro_winkler_sim_test (str_multiply (std::string (" 0" ), 65 ), str_multiply (std::string (" 0" ), 65 )) == Approx (1 ));
135+ REQUIRE (jaro_winkler_sim_test (str_multiply (std::string (" 0" ), 64 ), str_multiply (std::string (" 0" ), 65 )) == Approx (0.996923 ));
136+ REQUIRE (jaro_winkler_sim_test (str_multiply (std::string (" 0" ), 63 ), str_multiply (std::string (" 0" ), 65 )) == Approx (0.993846 ));
137+
138+ REQUIRE (jaro_winkler_sim_test (std::string (" 10000000000000000000000000000000000000000000000000000000000000020" ), std::string (" 00000000000000000000000000000000000000000000000000000000000000000" )) == Approx (0.979487 ));
139+ REQUIRE (jaro_winkler_sim_test (std::string (" 00000000000000100000000000000000000000010000000000000000000000000" ), std::string (" 0000000000000000000000000000000000000000000000000000000000000000000000000000001" )) == Approx (0.95334 ));
140+ REQUIRE (jaro_winkler_sim_test (
141+ std::string (" 00000000000000000000000000000000000000000000000000000000000000000" ),
142+ std::string (" 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" )
143+ ) == Approx (0.852344 ));
115144 }
116145}
0 commit comments