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