You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractDiffTestCase.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.diff;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertTrue;
  46. import java.io.UnsupportedEncodingException;
  47. import org.junit.Test;
  48. public abstract class AbstractDiffTestCase {
  49. @Test
  50. public void testEmptyInputs() {
  51. EditList r = diff(t(""), t(""));
  52. assertTrue("is empty", r.isEmpty());
  53. }
  54. @Test
  55. public void testCreateFile() {
  56. EditList r = diff(t(""), t("AB"));
  57. assertEquals(1, r.size());
  58. assertEquals(new Edit(0, 0, 0, 2), r.get(0));
  59. }
  60. @Test
  61. public void testDeleteFile() {
  62. EditList r = diff(t("AB"), t(""));
  63. assertEquals(1, r.size());
  64. assertEquals(new Edit(0, 2, 0, 0), r.get(0));
  65. }
  66. @Test
  67. public void testDegenerate_InsertMiddle() {
  68. EditList r = diff(t("ac"), t("aBc"));
  69. assertEquals(1, r.size());
  70. assertEquals(new Edit(1, 1, 1, 2), r.get(0));
  71. }
  72. @Test
  73. public void testDegenerate_DeleteMiddle() {
  74. EditList r = diff(t("aBc"), t("ac"));
  75. assertEquals(1, r.size());
  76. assertEquals(new Edit(1, 2, 1, 1), r.get(0));
  77. }
  78. @Test
  79. public void testDegenerate_ReplaceMiddle() {
  80. EditList r = diff(t("bCd"), t("bEd"));
  81. assertEquals(1, r.size());
  82. assertEquals(new Edit(1, 2, 1, 2), r.get(0));
  83. }
  84. @Test
  85. public void testDegenerate_InsertsIntoMidPosition() {
  86. EditList r = diff(t("aaaa"), t("aaXaa"));
  87. assertEquals(1, r.size());
  88. assertEquals(new Edit(2, 2, 2, 3), r.get(0));
  89. }
  90. @Test
  91. public void testDegenerate_InsertStart() {
  92. EditList r = diff(t("bc"), t("Abc"));
  93. assertEquals(1, r.size());
  94. assertEquals(new Edit(0, 0, 0, 1), r.get(0));
  95. }
  96. @Test
  97. public void testDegenerate_DeleteStart() {
  98. EditList r = diff(t("Abc"), t("bc"));
  99. assertEquals(1, r.size());
  100. assertEquals(new Edit(0, 1, 0, 0), r.get(0));
  101. }
  102. @Test
  103. public void testDegenerate_InsertEnd() {
  104. EditList r = diff(t("bc"), t("bcD"));
  105. assertEquals(1, r.size());
  106. assertEquals(new Edit(2, 2, 2, 3), r.get(0));
  107. }
  108. @Test
  109. public void testDegenerate_DeleteEnd() {
  110. EditList r = diff(t("bcD"), t("bc"));
  111. assertEquals(1, r.size());
  112. assertEquals(new Edit(2, 3, 2, 2), r.get(0));
  113. }
  114. @Test
  115. public void testEdit_ReplaceCommonDelete() {
  116. EditList r = diff(t("RbC"), t("Sb"));
  117. assertEquals(2, r.size());
  118. assertEquals(new Edit(0, 1, 0, 1), r.get(0));
  119. assertEquals(new Edit(2, 3, 2, 2), r.get(1));
  120. }
  121. @Test
  122. public void testEdit_CommonReplaceCommonDeleteCommon() {
  123. EditList r = diff(t("aRbCd"), t("aSbd"));
  124. assertEquals(2, r.size());
  125. assertEquals(new Edit(1, 2, 1, 2), r.get(0));
  126. assertEquals(new Edit(3, 4, 3, 3), r.get(1));
  127. }
  128. @Test
  129. public void testEdit_MoveBlock() {
  130. EditList r = diff(t("aYYbcdz"), t("abcdYYz"));
  131. assertEquals(2, r.size());
  132. assertEquals(new Edit(1, 3, 1, 1), r.get(0));
  133. assertEquals(new Edit(6, 6, 4, 6), r.get(1));
  134. }
  135. @Test
  136. public void testEdit_InvertBlocks() {
  137. EditList r = diff(t("aYYbcdXXz"), t("aXXbcdYYz"));
  138. assertEquals(2, r.size());
  139. assertEquals(new Edit(1, 3, 1, 3), r.get(0));
  140. assertEquals(new Edit(6, 8, 6, 8), r.get(1));
  141. }
  142. @Test
  143. public void testEdit_UniqueCommonLargerThanMatchPoint() {
  144. // We are testing 3 unique common matches, but two of
  145. // them are consumed as part of the 1st's LCS region.
  146. EditList r = diff(t("AbdeZ"), t("PbdeQR"));
  147. assertEquals(2, r.size());
  148. assertEquals(new Edit(0, 1, 0, 1), r.get(0));
  149. assertEquals(new Edit(4, 5, 4, 6), r.get(1));
  150. }
  151. @Test
  152. public void testEdit_CommonGrowsPrefixAndSuffix() {
  153. // Here there is only one common unique point, but we can grow it
  154. // in both directions to find the LCS in the middle.
  155. EditList r = diff(t("AaabccZ"), t("PaabccR"));
  156. assertEquals(2, r.size());
  157. assertEquals(new Edit(0, 1, 0, 1), r.get(0));
  158. assertEquals(new Edit(6, 7, 6, 7), r.get(1));
  159. }
  160. @Test
  161. public void testEdit_DuplicateAButCommonUniqueInB() {
  162. EditList r = diff(t("AbbcR"), t("CbcS"));
  163. assertEquals(2, r.size());
  164. assertEquals(new Edit(0, 2, 0, 1), r.get(0));
  165. assertEquals(new Edit(4, 5, 3, 4), r.get(1));
  166. }
  167. @Test
  168. public void testEdit_InsertNearCommonTail() {
  169. EditList r = diff(t("aq}nb"), t("aCq}nD}nb"));
  170. assertEquals(new Edit(1, 1, 1, 2), r.get(0));
  171. assertEquals(new Edit(3, 3, 4, 7), r.get(1));
  172. assertEquals(2, r.size());
  173. }
  174. public EditList diff(RawText a, RawText b) {
  175. return algorithm().diff(RawTextComparator.DEFAULT, a, b);
  176. }
  177. protected abstract DiffAlgorithm algorithm();
  178. public static RawText t(String text) {
  179. StringBuilder r = new StringBuilder();
  180. for (int i = 0; i < text.length(); i++) {
  181. r.append(text.charAt(i));
  182. r.append('\n');
  183. }
  184. try {
  185. return new RawText(r.toString().getBytes("UTF-8"));
  186. } catch (UnsupportedEncodingException e) {
  187. throw new RuntimeException(e);
  188. }
  189. }
  190. }