diff_ut.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "diff.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. using namespace NDiff;
  4. struct TDiffTester {
  5. TStringStream Res;
  6. TVector<TChunk<char>> Chunks;
  7. TStringBuf Special(const TStringBuf& str) const {
  8. return str;
  9. }
  10. TStringBuf Common(const TConstArrayRef<const char>& str) const {
  11. return TStringBuf(str.begin(), str.end());
  12. }
  13. TStringBuf Left(const TConstArrayRef<const char>& str) const {
  14. return TStringBuf(str.begin(), str.end());
  15. }
  16. TStringBuf Right(const TConstArrayRef<const char>& str) const {
  17. return TStringBuf(str.begin(), str.end());
  18. }
  19. void Test(const TStringBuf& a, const TStringBuf& b, const TString& delims = " \t\n") {
  20. Chunks.clear();
  21. InlineDiff(Chunks, a, b, delims);
  22. Res.clear();
  23. PrintChunks(Res, *this, Chunks);
  24. }
  25. const TString& Result() const {
  26. return Res.Str();
  27. }
  28. };
  29. Y_UNIT_TEST_SUITE(DiffTokens) {
  30. Y_UNIT_TEST(ReturnValue) {
  31. TVector<TChunk<char>> res;
  32. UNIT_ASSERT_VALUES_EQUAL(InlineDiff(res, "aaa", "aaa"), 0);
  33. UNIT_ASSERT_VALUES_EQUAL(InlineDiff(res, "aaa", "aa"), 1);
  34. UNIT_ASSERT_VALUES_EQUAL(InlineDiff(res, "aaa", "a"), 2);
  35. UNIT_ASSERT_VALUES_EQUAL(InlineDiff(res, "aaa", "abc"), 2);
  36. UNIT_ASSERT_VALUES_EQUAL(InlineDiff(res, "aaa", "aba"), 1);
  37. UNIT_ASSERT_VALUES_EQUAL(InlineDiff(res, "", "aba"), 3);
  38. UNIT_ASSERT_VALUES_EQUAL(InlineDiff(res, "aaa", "aaaa"), 1);
  39. UNIT_ASSERT_VALUES_EQUAL(InlineDiff(res, "abc", "xyz"), 3);
  40. }
  41. Y_UNIT_TEST(EqualStringsOneToken) {
  42. TDiffTester tester;
  43. tester.Test("aaa", "aaa");
  44. //~ Cerr << tester.Result() << Endl;
  45. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "aaa");
  46. }
  47. Y_UNIT_TEST(NonCrossingStringsOneToken) {
  48. TDiffTester tester;
  49. tester.Test("aaa", "bbb");
  50. //~ Cerr << tester.Result() << Endl;
  51. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aaa|bbb)");
  52. tester.Test("aaa", "bbbb");
  53. //~ Cerr << tester.Result() << Endl;
  54. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aaa|bbbb)");
  55. }
  56. Y_UNIT_TEST(Simple) {
  57. TDiffTester tester;
  58. tester.Test("aaa", "abb", "");
  59. //~ Cerr << tester.Result() << Endl;
  60. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "a(aa|bb)");
  61. tester.Test("aac", "abc", "");
  62. //~ Cerr << tester.Result() << Endl;
  63. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "a(a|b)c");
  64. tester.Test("123", "133", "");
  65. //~ Cerr << tester.Result() << Endl;
  66. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "1(2|3)3");
  67. tester.Test("[1, 2, 3]", "[1, 3, 3]", "");
  68. //~ Cerr << tester.Result() << Endl;
  69. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "[1, (2|3), 3]");
  70. }
  71. Y_UNIT_TEST(CommonCharOneToken) {
  72. TDiffTester tester;
  73. tester.Test("abcde", "accfg");
  74. //~ Cerr << tester.Result() << Endl;
  75. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(abcde|accfg)");
  76. }
  77. Y_UNIT_TEST(EqualStringsTwoTokens) {
  78. TDiffTester tester;
  79. TStringBuf str("aaa bbb");
  80. tester.Test(str, str);
  81. //~ Cerr << tester.Result() << Endl;
  82. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "aaa bbb");
  83. }
  84. Y_UNIT_TEST(NonCrossingStringsTwoTokens) {
  85. TDiffTester tester;
  86. tester.Test("aaa bbb", "ccc ddd");
  87. //~ Cerr << tester.Result() << Endl;
  88. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aaa|ccc) (bbb|ddd)");
  89. tester.Test("aaa bbb", "c d");
  90. //~ Cerr << tester.Result() << Endl;
  91. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aaa|c) (bbb|d)");
  92. }
  93. Y_UNIT_TEST(SimpleTwoTokens) {
  94. TDiffTester tester;
  95. tester.Test("aaa ccd", "abb cce");
  96. //~ Cerr << tester.Result() << Endl;
  97. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aaa|abb) (ccd|cce)");
  98. tester.Test("aac cbb", "aa bb");
  99. //~ Cerr << tester.Result() << Endl;
  100. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aac|aa) (cbb|bb)");
  101. }
  102. Y_UNIT_TEST(MixedTwoTokens) {
  103. TDiffTester tester;
  104. tester.Test("aaa bbb", "bbb aaa");
  105. //~ Cerr << tester.Result() << Endl;
  106. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(|bbb )aaa( bbb|)");
  107. tester.Test("aaa bbb", " bbb aaa");
  108. //~ Cerr << tester.Result() << Endl;
  109. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aaa|) bbb(| aaa)");
  110. tester.Test(" aaa bbb ", " bbb aaa ");
  111. //~ Cerr << tester.Result() << Endl;
  112. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(| bbb) aaa (bbb |)");
  113. tester.Test("aaa bb", " bbb aa");
  114. //~ Cerr << tester.Result() << Endl;
  115. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aaa|) (bb|bbb aa)");
  116. }
  117. Y_UNIT_TEST(TwoTokensInOneString) {
  118. TDiffTester tester;
  119. tester.Test("aaa bbb", "aaa");
  120. //~ Cerr << tester.Result() << Endl;
  121. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "aaa( bbb|)");
  122. tester.Test("aaa bbb", "aaa ");
  123. //~ Cerr << tester.Result() << Endl;
  124. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "aaa (bbb|)");
  125. tester.Test("aaa bbb", " bbb");
  126. //~ Cerr << tester.Result() << Endl;
  127. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aaa|) bbb");
  128. tester.Test("aaa bbb", "bbb");
  129. //~ Cerr << tester.Result() << Endl;
  130. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "(aaa |)bbb");
  131. }
  132. Y_UNIT_TEST(Multiline) {
  133. TDiffTester tester;
  134. tester.Test("aaa\nabc\nbbb", "aaa\nacc\nbbb");
  135. //~ Cerr << tester.Result() << Endl;
  136. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "aaa\n(abc|acc)\nbbb");
  137. tester.Test("aaa\nabc\nbbb", "aaa\nac\nbbb");
  138. //~ Cerr << tester.Result() << Endl;
  139. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "aaa\n(abc|ac)\nbbb");
  140. }
  141. Y_UNIT_TEST(DifferentDelimiters) {
  142. TDiffTester tester;
  143. tester.Test("aaa bbb", "aaa\tbbb");
  144. //~ Cerr << tester.Result() << Endl;
  145. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "aaa( |\t)bbb");
  146. tester.Test(" aaa\tbbb\n", "\taaa\nbbb ");
  147. //~ Cerr << tester.Result() << Endl;
  148. UNIT_ASSERT_VALUES_EQUAL(tester.Result(), "( |\t)aaa(\t|\n)bbb(\n| )");
  149. }
  150. }