large dataset

This commit is contained in:
wumpz
2017-04-12 11:21:31 +02:00
parent 5522b28695
commit a41c680e9d
4 changed files with 57 additions and 2 deletions

View File

@@ -26,6 +26,7 @@ import difflib.patch.Delta;
import difflib.patch.Equalizer;
import difflib.patch.Patch;
import difflib.patch.PatchFailedException;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@@ -52,6 +53,13 @@ public final class DiffUtils {
public static <T> Patch<T> diff(List<T> original, List<T> revised) throws DiffException {
return DiffUtils.diff(original, revised, new MyersDiff<>());
}
/**
* Computes the difference between the original and revised text.
*/
public static Patch<String> diff(String originalText, String revisedText) throws DiffException {
return DiffUtils.diff(Arrays.asList(originalText.split("\n")), Arrays.asList(revisedText.split("\n")));
}
/**
* Computes the difference between the original and revised list of elements with default diff

View File

@@ -85,4 +85,9 @@ public final class Patch<T> {
Collections.sort(deltas, comparing(d -> d.getOriginal().getPosition()));
return deltas;
}
@Override
public String toString() {
return "Patch{" + "deltas=" + deltas + '}';
}
}

View File

@@ -7,13 +7,22 @@ import difflib.patch.DeleteDelta;
import difflib.patch.Delta;
import difflib.patch.InsertDelta;
import difflib.patch.Patch;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static java.util.stream.Collectors.toList;
import java.util.zip.ZipFile;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Ignore;
import org.junit.Test;
public class DiffUtilsTest {
@@ -101,13 +110,46 @@ public class DiffUtilsTest {
List<Integer> revised = Arrays.asList(2, 3, 4, 6);
final Patch<Integer> patch = DiffUtils.diff(original, revised);
for (Delta delta : patch.getDeltas()) {
System.out.println(delta);
}
assertEquals(2, patch.getDeltas().size());
assertEquals("[DeleteDelta, position: 0, lines: [1]]", patch.getDeltas().get(0).toString());
assertEquals("[ChangeDelta, position: 4, lines: [5] to [6]]", patch.getDeltas().get(1).toString());
}
@Test
public void testDiffMissesChangeForkDnaumenkoIssue31() throws DiffException {
List<String> original = Arrays.asList("line1", "line2", "line3");
List<String> revised = Arrays.asList("line1", "line2-2", "line4");
Patch<String> patch = DiffUtils.diff(original, revised);
assertEquals(1, patch.getDeltas().size());
assertEquals("[ChangeDelta, position: 1, lines: [line2, line3] to [line2-2, line4]]", patch.getDeltas().get(0).toString());
}
/**
* To test this, the greedy meyer algorithm is not suitable.
*/
@Test
@Ignore
public void testPossibleDiffHangOnLargeDatasetDnaumenkoIssue26() throws IOException, DiffException {
ZipFile zip = new ZipFile(TestConstants.MOCK_FOLDER + "/large_dataset1.zip");
Patch<String> patch = DiffUtils.diff(
readStringListFromInputStream(zip.getInputStream(zip.getEntry("ta"))),
readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb"))));
assertEquals(1, patch.getDeltas().size());
}
private static List<String> readStringListFromInputStream(InputStream is) throws IOException {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(is, Charset.forName(StandardCharsets.UTF_8.name())))) {
return reader.lines().collect(toList());
}
}
}

View File

Binary file not shown.