kfuzzymatchertest.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. This file is part of the KDE libraries
  3. SPDX-FileCopyrightText: 2021 Waqar Ahmed <waqar.17a@gmail.com>
  4. SPDX-License-Identifier: LGPL-2.0-or-later
  5. */
  6. #include "kfuzzymatchertest.h"
  7. #include <QString>
  8. #include <QStringList>
  9. #include <QTest>
  10. #include <algorithm>
  11. #include "kfuzzymatcher.h"
  12. QTEST_MAIN(KFuzzyMatcherTest)
  13. void KFuzzyMatcherTest::testMatchSimple_data()
  14. {
  15. QTest::addColumn<QString>("pattern");
  16. QTest::addColumn<QString>("inputstr");
  17. QTest::addColumn<bool>("expected");
  18. QTest::newRow("AbcD") << QStringLiteral("AbcD") << QStringLiteral("AbCdefg") << true;
  19. QTest::newRow("WithSpace") << QStringLiteral("Wa qa") << QStringLiteral("Wa qar") << true;
  20. QTest::newRow("RTL") << QStringLiteral("ارو") << QStringLiteral("اردو") << true;
  21. QTest::newRow("WithSep") << QStringLiteral("tf") << QStringLiteral("the_file") << true;
  22. QTest::newRow("Umlaut") << QStringLiteral("Häu") << QStringLiteral("Häuser") << true;
  23. QTest::newRow("Unmatched") << QStringLiteral("Name") << QStringLiteral("Nam") << false;
  24. QTest::newRow("Empty Pattern") << QString("") << QStringLiteral("Nam") << true;
  25. }
  26. void KFuzzyMatcherTest::testMatchSimple()
  27. {
  28. QFETCH(QString, pattern);
  29. QFETCH(QString, inputstr);
  30. QFETCH(bool, expected);
  31. QVERIFY(KFuzzyMatcher::matchSimple(pattern, inputstr) == expected);
  32. }
  33. void KFuzzyMatcherTest::testMatch_data()
  34. {
  35. QTest::addColumn<QString>("pattern");
  36. QTest::addColumn<QStringList>("input");
  37. QTest::addColumn<QStringList>("expected");
  38. QTest::addColumn<int>("size");
  39. // clang-format off
  40. QTest::newRow("pattern=sort") << QStringLiteral("sort")
  41. << QStringList{
  42. QStringLiteral("Sort"),
  43. QStringLiteral("Some other right test"),
  44. QStringLiteral("Soup rate"),
  45. QStringLiteral("Someother"),
  46. QStringLiteral("irrelevant"),
  47. }
  48. << QStringList{
  49. QStringLiteral("Sort"),
  50. QStringLiteral("Some other right test"),
  51. QStringLiteral("Soup rate"),
  52. }
  53. << 3;
  54. QTest::newRow("pattern=kateapp") << QStringLiteral("kaapp")
  55. << QStringList{
  56. QStringLiteral("kateapp.cpp"),
  57. QStringLiteral("kate_application"),
  58. QStringLiteral("kateapp.h"),
  59. QStringLiteral("katepap.c")
  60. }
  61. << QStringList{
  62. QStringLiteral("kate_application"),
  63. QStringLiteral("kateapp.h"),
  64. QStringLiteral("kateapp.cpp")
  65. }
  66. << 3;
  67. QTest::newRow("pattern=this") << QStringLiteral("this")
  68. << QStringList{
  69. QStringLiteral("th"),
  70. QStringLiteral("ths"),
  71. QStringLiteral("thsi")
  72. }
  73. << QStringList{
  74. }
  75. << 0;
  76. QTest::newRow("pattern=marath") << QStringLiteral("marath")
  77. << QStringList{
  78. QStringLiteral("Maralen of the Mornsong"),
  79. QStringLiteral("Silumgar, the Drifting Death"),
  80. QStringLiteral("Maralen of the Mornsong Avatar"),
  81. QStringLiteral("Marshaling the Troops"),
  82. QStringLiteral("Homeward Path"),
  83. QStringLiteral("Marath, Will of the Wild"),
  84. QStringLiteral("Marshal's Anthem"),
  85. QStringLiteral("Marchesa, the Black Rose"),
  86. QStringLiteral("Mark for Death"),
  87. QStringLiteral("Master Apothecary"),
  88. QStringLiteral("Mazirek, Kraul Death Priest"),
  89. QStringLiteral("Akroma, Angel of Wrath"),
  90. QStringLiteral("Akroma, Angel of Wrath Avatar"),
  91. QStringLiteral("Commander's Authority"),
  92. QStringLiteral("Shaman of the Great Hunt"),
  93. QStringLiteral("Halimar Wavewatch"),
  94. QStringLiteral("Pyromancer's Swath")
  95. }
  96. << QStringList{
  97. QStringLiteral("Marath, Will of the Wild"),
  98. QStringLiteral("Maralen of the Mornsong"),
  99. QStringLiteral("Maralen of the Mornsong Avatar"),
  100. QStringLiteral("Marshal's Anthem"),
  101. QStringLiteral("Marshaling the Troops"),
  102. QStringLiteral("Marchesa, the Black Rose"),
  103. QStringLiteral("Mark for Death"),
  104. QStringLiteral("Master Apothecary"),
  105. QStringLiteral("Mazirek, Kraul Death Priest"),
  106. QStringLiteral("Akroma, Angel of Wrath"),
  107. QStringLiteral("Akroma, Angel of Wrath Avatar"),
  108. QStringLiteral("Commander's Authority"),
  109. QStringLiteral("Homeward Path"),
  110. QStringLiteral("Shaman of the Great Hunt"),
  111. QStringLiteral("Halimar Wavewatch"),
  112. QStringLiteral("Pyromancer's Swath"),
  113. QStringLiteral("Silumgar, the Drifting Death")
  114. }
  115. << 17;
  116. // This tests our recursive best match
  117. QTest::newRow("pattern=lll") << QStringLiteral("lll")
  118. << QStringList{
  119. QStringLiteral("SVisualLoggerLogsList.h"),
  120. QStringLiteral("SimpleFileLogger.cpp"),
  121. QStringLiteral("StringHandlerLogList.txt"),
  122. QStringLiteral("LeapFromLostAllan"),
  123. QStringLiteral("BumpLLL"),
  124. }
  125. << QStringList{
  126. QStringLiteral("SVisualLoggerLogsList.h"),
  127. QStringLiteral("LeapFromLostAllan"),
  128. QStringLiteral("BumpLLL"),
  129. QStringLiteral("StringHandlerLogList.txt"),
  130. QStringLiteral("SimpleFileLogger.cpp"),
  131. }
  132. << 5;
  133. QTest::newRow("pattern=") << QString("")
  134. << QStringList{
  135. QStringLiteral("th"),
  136. QStringLiteral("ths"),
  137. QStringLiteral("thsi")
  138. }
  139. << QStringList{
  140. QStringLiteral("th"),
  141. QStringLiteral("ths"),
  142. QStringLiteral("thsi")
  143. }
  144. << 3;
  145. // clang-format on
  146. }
  147. static QStringList matchHelper(const QString &pattern, const QStringList &input)
  148. {
  149. QVector<QPair<QString, int>> actual;
  150. for (int i = 0; i < input.size(); ++i) {
  151. KFuzzyMatcher::Result res = KFuzzyMatcher::match(pattern, input.at(i));
  152. if (res.matched) {
  153. actual.push_back({input.at(i), res.score});
  154. }
  155. }
  156. // sort descending based on score
  157. std::sort(actual.begin(), actual.end(), [](const QPair<QString, int> &l, const QPair<QString, int> &r) {
  158. return l.second > r.second;
  159. });
  160. QStringList actualOut;
  161. for (const auto &s : actual) {
  162. actualOut << s.first;
  163. }
  164. return actualOut;
  165. }
  166. void KFuzzyMatcherTest::testMatch()
  167. {
  168. QFETCH(QString, pattern);
  169. QFETCH(QStringList, input);
  170. QFETCH(QStringList, expected);
  171. QFETCH(int, size);
  172. const QStringList actual = matchHelper(pattern, input);
  173. QCOMPARE(actual.size(), size);
  174. QCOMPARE(actual, expected);
  175. }
  176. void KFuzzyMatcherTest::testMatchedRanges_data()
  177. {
  178. QTest::addColumn<QString>("pattern");
  179. QTest::addColumn<QString>("string");
  180. using Range = QPair<int, int>;
  181. QTest::addColumn<QVector<Range>>("expectedRanges");
  182. QTest::addColumn<bool>("matchingOnly");
  183. QTest::newRow("Emtpy") << QString("") << QString("") << QVector<Range>{} << true;
  184. QTest::newRow("Hello") << QStringLiteral("Hlo") << QStringLiteral("Hello") << QVector<Range>{{0, 1}, {3, 2}} << true;
  185. QTest::newRow("lll") << QStringLiteral("lll") << QStringLiteral("SVisualLoggerLogsList") << QVector<Range>{{7, 1}, {13, 1}, {17, 1}} << true;
  186. QTest::newRow("Sort") << QStringLiteral("sort") << QStringLiteral("SorT") << QVector<Range>{{0, 4}} << true;
  187. QTest::newRow("Unmatching") << QStringLiteral("git") << QStringLiteral("gti") << QVector<Range>{} << true;
  188. QTest::newRow("UnmatchingWithAllMatches") << QStringLiteral("git") << QStringLiteral("gti") << QVector<Range>{{0, 1}, {2, 1}} << false;
  189. }
  190. void KFuzzyMatcherTest::testMatchedRanges()
  191. {
  192. QFETCH(QString, pattern);
  193. QFETCH(QString, string);
  194. QFETCH(bool, matchingOnly);
  195. using Range = QPair<int, int>;
  196. QFETCH(QVector<Range>, expectedRanges);
  197. const auto matchMode = matchingOnly ? KFuzzyMatcher::RangeType::FullyMatched : KFuzzyMatcher::RangeType::All;
  198. auto resultRanges = KFuzzyMatcher::matchedRanges(pattern, string, matchMode);
  199. QCOMPARE(resultRanges.size(), expectedRanges.size());
  200. bool res = std::equal(expectedRanges.begin(), expectedRanges.end(), resultRanges.begin(), [](const Range &l, const KFuzzyMatcher::Range &r) {
  201. return l.first == r.start && l.second == r.length;
  202. });
  203. QVERIFY(res);
  204. }
  205. #include "moc_kfuzzymatchertest.cpp"