1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.diff;
45
46 import static java.nio.charset.StandardCharsets.UTF_8;
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertTrue;
49
50 import org.junit.Test;
51
52 public abstract class AbstractDiffTestCase {
53 @Test
54 public void testEmptyInputs() {
55 EditList r = diff(t(""), t(""));
56 assertTrue("is empty", r.isEmpty());
57 }
58
59 @Test
60 public void testCreateFile() {
61 EditList r = diff(t(""), t("AB"));
62 assertEquals(1, r.size());
63 assertEquals(new Edit(0, 0, 0, 2), r.get(0));
64 }
65
66 @Test
67 public void testDeleteFile() {
68 EditList r = diff(t("AB"), t(""));
69 assertEquals(1, r.size());
70 assertEquals(new Edit(0, 2, 0, 0), r.get(0));
71 }
72
73 @Test
74 public void testDegenerate_InsertMiddle() {
75 EditList r = diff(t("ac"), t("aBc"));
76 assertEquals(1, r.size());
77 assertEquals(new Edit(1, 1, 1, 2), r.get(0));
78 }
79
80 @Test
81 public void testDegenerate_DeleteMiddle() {
82 EditList r = diff(t("aBc"), t("ac"));
83 assertEquals(1, r.size());
84 assertEquals(new Edit(1, 2, 1, 1), r.get(0));
85 }
86
87 @Test
88 public void testDegenerate_ReplaceMiddle() {
89 EditList r = diff(t("bCd"), t("bEd"));
90 assertEquals(1, r.size());
91 assertEquals(new Edit(1, 2, 1, 2), r.get(0));
92 }
93
94 @Test
95 public void testDegenerate_InsertsIntoMidPosition() {
96 EditList r = diff(t("aaaa"), t("aaXaa"));
97 assertEquals(1, r.size());
98 assertEquals(new Edit(2, 2, 2, 3), r.get(0));
99 }
100
101 @Test
102 public void testDegenerate_InsertStart() {
103 EditList r = diff(t("bc"), t("Abc"));
104 assertEquals(1, r.size());
105 assertEquals(new Edit(0, 0, 0, 1), r.get(0));
106 }
107
108 @Test
109 public void testDegenerate_DeleteStart() {
110 EditList r = diff(t("Abc"), t("bc"));
111 assertEquals(1, r.size());
112 assertEquals(new Edit(0, 1, 0, 0), r.get(0));
113 }
114
115 @Test
116 public void testDegenerate_InsertEnd() {
117 EditList r = diff(t("bc"), t("bcD"));
118 assertEquals(1, r.size());
119 assertEquals(new Edit(2, 2, 2, 3), r.get(0));
120 }
121
122 @Test
123 public void testDegenerate_DeleteEnd() {
124 EditList r = diff(t("bcD"), t("bc"));
125 assertEquals(1, r.size());
126 assertEquals(new Edit(2, 3, 2, 2), r.get(0));
127 }
128
129 @Test
130 public void testEdit_ReplaceCommonDelete() {
131 EditList r = diff(t("RbC"), t("Sb"));
132 assertEquals(2, r.size());
133 assertEquals(new Edit(0, 1, 0, 1), r.get(0));
134 assertEquals(new Edit(2, 3, 2, 2), r.get(1));
135 }
136
137 @Test
138 public void testEdit_CommonReplaceCommonDeleteCommon() {
139 EditList r = diff(t("aRbCd"), t("aSbd"));
140 assertEquals(2, r.size());
141 assertEquals(new Edit(1, 2, 1, 2), r.get(0));
142 assertEquals(new Edit(3, 4, 3, 3), r.get(1));
143 }
144
145 @Test
146 public void testEdit_MoveBlock() {
147 EditList r = diff(t("aYYbcdz"), t("abcdYYz"));
148 assertEquals(2, r.size());
149 assertEquals(new Edit(1, 3, 1, 1), r.get(0));
150 assertEquals(new Edit(6, 6, 4, 6), r.get(1));
151 }
152
153 @Test
154 public void testEdit_InvertBlocks() {
155 EditList r = diff(t("aYYbcdXXz"), t("aXXbcdYYz"));
156 assertEquals(2, r.size());
157 assertEquals(new Edit(1, 3, 1, 3), r.get(0));
158 assertEquals(new Edit(6, 8, 6, 8), r.get(1));
159 }
160
161 @Test
162 public void testEdit_UniqueCommonLargerThanMatchPoint() {
163
164
165 EditList r = diff(t("AbdeZ"), t("PbdeQR"));
166 assertEquals(2, r.size());
167 assertEquals(new Edit(0, 1, 0, 1), r.get(0));
168 assertEquals(new Edit(4, 5, 4, 6), r.get(1));
169 }
170
171 @Test
172 public void testEdit_CommonGrowsPrefixAndSuffix() {
173
174
175 EditList r = diff(t("AaabccZ"), t("PaabccR"));
176 assertEquals(2, r.size());
177 assertEquals(new Edit(0, 1, 0, 1), r.get(0));
178 assertEquals(new Edit(6, 7, 6, 7), r.get(1));
179 }
180
181 @Test
182 public void testEdit_DuplicateAButCommonUniqueInB() {
183 EditList r = diff(t("AbbcR"), t("CbcS"));
184 assertEquals(2, r.size());
185 assertEquals(new Edit(0, 2, 0, 1), r.get(0));
186 assertEquals(new Edit(4, 5, 3, 4), r.get(1));
187 }
188
189 @Test
190 public void testEdit_InsertNearCommonTail() {
191 EditList r = diff(t("aq}nb"), t("aCq}nD}nb"));
192 assertEquals(new Edit(1, 1, 1, 2), r.get(0));
193 assertEquals(new Edit(4, 4, 5, 8), r.get(1));
194 assertEquals(2, r.size());
195 }
196
197 @Test
198 public void testEdit_DeleteNearCommonTail() {
199 EditList r = diff(t("aCq}nD}nb"), t("aq}nb"));
200 assertEquals(new Edit(1, 2, 1, 1), r.get(0));
201 assertEquals(new Edit(5, 8, 4, 4), r.get(1));
202 assertEquals(2, r.size());
203 }
204
205 @Test
206 public void testEdit_DeleteNearCommonCenter() {
207 EditList r = diff(t("abcd123123uvwxpq"), t("aBcd123uvwxPq"));
208 assertEquals(new Edit(1, 2, 1, 2), r.get(0));
209 assertEquals(new Edit(7, 10, 7, 7), r.get(1));
210 assertEquals(new Edit(14, 15, 11, 12), r.get(2));
211 assertEquals(3, r.size());
212 }
213
214 @Test
215 public void testEdit_InsertNearCommonCenter() {
216 EditList r = diff(t("aBcd123uvwxPq"), t("abcd123123uvwxpq"));
217 assertEquals(new Edit(1, 2, 1, 2), r.get(0));
218 assertEquals(new Edit(7, 7, 7, 10), r.get(1));
219 assertEquals(new Edit(11, 12, 14, 15), r.get(2));
220 assertEquals(3, r.size());
221 }
222
223 @Test
224 public void testEdit_LinuxBug() {
225 EditList r = diff(t("a{bcdE}z"), t("a{0bcdEE}z"));
226 assertEquals(new Edit(2, 2, 2, 3), r.get(0));
227 assertEquals(new Edit(6, 6, 7, 8), r.get(1));
228 assertEquals(2, r.size());
229 }
230
231 public EditList diff(RawText a, RawText b) {
232 return algorithm().diff(RawTextComparator.DEFAULT, a, b);
233 }
234
235 protected abstract DiffAlgorithm algorithm();
236
237 public static RawText t(String text) {
238 StringBuilder r = new StringBuilder();
239 for (int i = 0; i < text.length(); i++) {
240 r.append(text.charAt(i));
241 r.append('\n');
242 }
243 return new RawText(r.toString().getBytes(UTF_8));
244 }
245 }