Skip to content

Commit a41c680

Browse files
committed
large dataset
1 parent 5522b28 commit a41c680

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

src/main/java/difflib/DiffUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import difflib.patch.Equalizer;
2727
import difflib.patch.Patch;
2828
import difflib.patch.PatchFailedException;
29+
import java.util.Arrays;
2930
import java.util.Collections;
3031
import java.util.LinkedList;
3132
import java.util.List;
@@ -52,6 +53,13 @@ public final class DiffUtils {
5253
public static <T> Patch<T> diff(List<T> original, List<T> revised) throws DiffException {
5354
return DiffUtils.diff(original, revised, new MyersDiff<>());
5455
}
56+
57+
/**
58+
* Computes the difference between the original and revised text.
59+
*/
60+
public static Patch<String> diff(String originalText, String revisedText) throws DiffException {
61+
return DiffUtils.diff(Arrays.asList(originalText.split("\n")), Arrays.asList(revisedText.split("\n")));
62+
}
5563

5664
/**
5765
* Computes the difference between the original and revised list of elements with default diff

src/main/java/difflib/patch/Patch.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,9 @@ public List<Delta<T>> getDeltas() {
8585
Collections.sort(deltas, comparing(d -> d.getOriginal().getPosition()));
8686
return deltas;
8787
}
88+
89+
@Override
90+
public String toString() {
91+
return "Patch{" + "deltas=" + deltas + '}';
92+
}
8893
}

src/test/java/difflib/DiffUtilsTest.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@
77
import difflib.patch.Delta;
88
import difflib.patch.InsertDelta;
99
import difflib.patch.Patch;
10+
import java.io.BufferedReader;
11+
import java.io.IOException;
12+
import java.io.InputStream;
13+
import java.io.InputStreamReader;
14+
import java.nio.charset.Charset;
15+
import java.nio.charset.StandardCharsets;
1016
import java.util.ArrayList;
1117
import java.util.Arrays;
1218
import java.util.Collections;
1319
import java.util.List;
20+
import static java.util.stream.Collectors.toList;
21+
import java.util.zip.ZipFile;
1422
import static org.junit.Assert.assertEquals;
1523
import static org.junit.Assert.assertNotNull;
1624
import static org.junit.Assert.assertTrue;
25+
import org.junit.Ignore;
1726
import org.junit.Test;
1827

1928
public class DiffUtilsTest {
@@ -101,13 +110,46 @@ public void testDiffIntegerList() throws DiffException {
101110
List<Integer> revised = Arrays.asList(2, 3, 4, 6);
102111

103112
final Patch<Integer> patch = DiffUtils.diff(original, revised);
104-
113+
105114
for (Delta delta : patch.getDeltas()) {
106115
System.out.println(delta);
107116
}
108-
117+
109118
assertEquals(2, patch.getDeltas().size());
110119
assertEquals("[DeleteDelta, position: 0, lines: [1]]", patch.getDeltas().get(0).toString());
111120
assertEquals("[ChangeDelta, position: 4, lines: [5] to [6]]", patch.getDeltas().get(1).toString());
112121
}
122+
123+
@Test
124+
public void testDiffMissesChangeForkDnaumenkoIssue31() throws DiffException {
125+
List<String> original = Arrays.asList("line1", "line2", "line3");
126+
List<String> revised = Arrays.asList("line1", "line2-2", "line4");
127+
128+
Patch<String> patch = DiffUtils.diff(original, revised);
129+
assertEquals(1, patch.getDeltas().size());
130+
assertEquals("[ChangeDelta, position: 1, lines: [line2, line3] to [line2-2, line4]]", patch.getDeltas().get(0).toString());
131+
}
132+
133+
/**
134+
* To test this, the greedy meyer algorithm is not suitable.
135+
*/
136+
@Test
137+
@Ignore
138+
public void testPossibleDiffHangOnLargeDatasetDnaumenkoIssue26() throws IOException, DiffException {
139+
ZipFile zip = new ZipFile(TestConstants.MOCK_FOLDER + "/large_dataset1.zip");
140+
141+
Patch<String> patch = DiffUtils.diff(
142+
readStringListFromInputStream(zip.getInputStream(zip.getEntry("ta"))),
143+
readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb"))));
144+
145+
assertEquals(1, patch.getDeltas().size());
146+
}
147+
148+
private static List<String> readStringListFromInputStream(InputStream is) throws IOException {
149+
try (BufferedReader reader = new BufferedReader(
150+
new InputStreamReader(is, Charset.forName(StandardCharsets.UTF_8.name())))) {
151+
152+
return reader.lines().collect(toList());
153+
}
154+
}
113155
}
1.59 MB
Binary file not shown.

0 commit comments

Comments
 (0)