diff options
author | Shawn O. Pearce <spearce@spearce.org> | 2010-09-02 12:53:27 -0700 |
---|---|---|
committer | Chris Aniszczyk <caniszczyk@gmail.com> | 2010-09-06 21:37:11 -0500 |
commit | 67263e2056108e471d684c3cef9e719724b51220 (patch) | |
tree | e4ea7623e4b0884b542c77167ab456e3643eb5ed /org.eclipse.jgit.test/tst/org/eclipse/jgit/diff | |
parent | 18aadc826dae7c6308c2a6c68d48ab75c8dd9b6d (diff) | |
download | jgit-67263e2056108e471d684c3cef9e719724b51220.tar.gz jgit-67263e2056108e471d684c3cef9e719724b51220.zip |
Refactor diff sequence API
Instead of making the sequence itself responsible for the equivalence
function, use an external function that is supplied by the caller.
This cleans up the code because we now say cmp.equals(a, ai, b, bi)
instead of a.equals(ai, b, bi).
This refactoring also removes the odd concept of creating different
types of sequences to have different behaviors for whitespace
ignoring. Instead DiffComparator now supports singleton functions
that apply a particular equivalence algorithm to a type of sequence.
Change-Id: I559f494d81cdc6f06bfb4208f60780c0ae251df9
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'org.eclipse.jgit.test/tst/org/eclipse/jgit/diff')
7 files changed, 126 insertions, 101 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/MyersDiffPerformanceTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/MyersDiffPerformanceTest.java index b97c1633f7..38fac3cc96 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/MyersDiffPerformanceTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/MyersDiffPerformanceTest.java @@ -163,14 +163,15 @@ public class MyersDiffPerformanceTest extends TestCase { String b = DiffTestDataGenerator.generateSequence(characters, 1621, 5); CharArray ac = new CharArray(a); CharArray bc = new CharArray(b); - MyersDiff myersDiff = null; + CharCmp cmp = new CharCmp(); + MyersDiff<CharArray> myersDiff = null; int cpuTimeChanges = 0; long lastReadout = 0; long interimTime = 0; int repetitions = 0; stopwatch.start(); while (cpuTimeChanges < minCPUTimerTicks && interimTime < longTaskBoundary) { - myersDiff = new MyersDiff(ac, bc); + myersDiff = new MyersDiff<CharArray>(cmp, ac, bc); repetitions++; interimTime = stopwatch.readout(); if (interimTime != lastReadout) { @@ -179,26 +180,34 @@ public class MyersDiffPerformanceTest extends TestCase { } } ret.runningTime = stopwatch.stop() / repetitions; - ret.N = (ac.size() + bc.size()); + ret.N = ac.size() + bc.size(); ret.D = myersDiff.getEdits().size(); return ret; } - private static class CharArray implements Sequence { - private final char[] array; + private static class CharArray extends Sequence { + final char[] array; public CharArray(String s) { array = s.toCharArray(); } + @Override public int size() { return array.length; } + } + + private static class CharCmp extends SequenceComparator<CharArray> { + @Override + public boolean equals(CharArray a, int ai, CharArray b, int bi) { + return a.array[ai] == b.array[bi]; + } - public boolean equals(int i, Sequence other, int j) { - CharArray o = (CharArray) other; - return array[i] == o.array[j]; + @Override + public int hash(CharArray seq, int ptr) { + return seq.array[ptr]; } } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/MyersDiffTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/MyersDiffTest.java index c91348ffe6..60c1b47280 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/MyersDiffTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/MyersDiffTest.java @@ -63,7 +63,8 @@ public class MyersDiffTest extends TestCase { } public void assertDiff(String a, String b, String edits) { - MyersDiff diff = new MyersDiff(toCharArray(a), toCharArray(b)); + MyersDiff diff = new MyersDiff<CharArray>(new CharCmp(), + toCharArray(a), toCharArray(b)); assertEquals(edits, toString(diff.getEdits())); } @@ -80,8 +81,7 @@ public class MyersDiffTest extends TestCase { return new CharArray(s); } - protected static String toString(Sequence seq, int begin, int end) { - CharArray a = (CharArray)seq; + protected static String toString(CharArray a, int begin, int end) { return new String(a.array, begin, end - begin); } @@ -97,13 +97,21 @@ public class MyersDiffTest extends TestCase { + ")"; } - private static class CharArray implements Sequence { + private static class CharArray extends Sequence { char[] array; public CharArray(String s) { array = s.toCharArray(); } public int size() { return array.length; } - public boolean equals(int i, Sequence other, int j) { - CharArray o = (CharArray)other; - return array[i] == o.array[j]; + } + + private static class CharCmp extends SequenceComparator<CharArray> { + @Override + public boolean equals(CharArray a, int ai, CharArray b, int bi) { + return a.array[ai] == b.array[bi]; + } + + @Override + public int hash(CharArray seq, int ptr) { + return seq.array[ptr]; } } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextIgnoreAllWhitespaceTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextIgnoreAllWhitespaceTest.java index 5e1a238a72..c59f3780b1 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextIgnoreAllWhitespaceTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextIgnoreAllWhitespaceTest.java @@ -49,48 +49,50 @@ import org.eclipse.jgit.lib.Constants; import junit.framework.TestCase; public class RawTextIgnoreAllWhitespaceTest extends TestCase { + private final RawTextComparator cmp = RawTextComparator.WS_IGNORE_ALL; + public void testEqualsWithoutWhitespace() { - final RawText a = new RawTextIgnoreAllWhitespace(Constants + final RawText a = new RawText(cmp, Constants .encodeASCII("foo-a\nfoo-b\nfoo\n")); - final RawText b = new RawTextIgnoreAllWhitespace(Constants + final RawText b = new RawText(cmp, Constants .encodeASCII("foo-b\nfoo-c\nf\n")); assertEquals(3, a.size()); assertEquals(3, b.size()); // foo-a != foo-b - assertFalse(a.equals(0, b, 0)); - assertFalse(b.equals(0, a, 0)); + assertFalse(cmp.equals(a, 0, b, 0)); + assertFalse(cmp.equals(b, 0, a, 0)); // foo-b == foo-b - assertTrue(a.equals(1, b, 0)); - assertTrue(b.equals(0, a, 1)); + assertTrue(cmp.equals(a, 1, b, 0)); + assertTrue(cmp.equals(b, 0, a, 1)); // foo != f - assertFalse(a.equals(2, b, 2)); - assertFalse(b.equals(2, a, 2)); + assertFalse(cmp.equals(a, 2, b, 2)); + assertFalse(cmp.equals(b, 2, a, 2)); } public void testEqualsWithWhitespace() { - final RawText a = new RawTextIgnoreAllWhitespace(Constants + final RawText a = new RawText(cmp, Constants .encodeASCII("foo-a\n \n a b c\na \n")); - final RawText b = new RawTextIgnoreAllWhitespace(Constants + final RawText b = new RawText(cmp, Constants .encodeASCII("foo-a b\n\nab c\na\n")); // "foo-a" != "foo-a b" - assertFalse(a.equals(0, b, 0)); - assertFalse(b.equals(0, a, 0)); + assertFalse(cmp.equals(a, 0, b, 0)); + assertFalse(cmp.equals(b, 0, a, 0)); // " " == "" - assertTrue(a.equals(1, b, 1)); - assertTrue(b.equals(1, a, 1)); + assertTrue(cmp.equals(a, 1, b, 1)); + assertTrue(cmp.equals(b, 1, a, 1)); // " a b c" == "ab c" - assertTrue(a.equals(2, b, 2)); - assertTrue(b.equals(2, a, 2)); + assertTrue(cmp.equals(a, 2, b, 2)); + assertTrue(cmp.equals(b, 2, a, 2)); // "a " == "a" - assertTrue(a.equals(3, b, 3)); - assertTrue(b.equals(3, a, 3)); + assertTrue(cmp.equals(a, 3, b, 3)); + assertTrue(cmp.equals(b, 3, a, 3)); } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextIgnoreLeadingWhitespaceTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextIgnoreLeadingWhitespaceTest.java index 9ab18873dc..647fa30d2e 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextIgnoreLeadingWhitespaceTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextIgnoreLeadingWhitespaceTest.java @@ -49,52 +49,54 @@ import org.eclipse.jgit.lib.Constants; import junit.framework.TestCase; public class RawTextIgnoreLeadingWhitespaceTest extends TestCase { + private final RawTextComparator cmp = RawTextComparator.WS_IGNORE_LEADING; + public void testEqualsWithoutWhitespace() { - final RawText a = new RawTextIgnoreLeadingWhitespace(Constants + final RawText a = new RawText(cmp, Constants .encodeASCII("foo-a\nfoo-b\nfoo\n")); - final RawText b = new RawTextIgnoreLeadingWhitespace(Constants + final RawText b = new RawText(cmp, Constants .encodeASCII("foo-b\nfoo-c\nf\n")); assertEquals(3, a.size()); assertEquals(3, b.size()); // foo-a != foo-b - assertFalse(a.equals(0, b, 0)); - assertFalse(b.equals(0, a, 0)); + assertFalse(cmp.equals(a, 0, b, 0)); + assertFalse(cmp.equals(b, 0, a, 0)); // foo-b == foo-b - assertTrue(a.equals(1, b, 0)); - assertTrue(b.equals(0, a, 1)); + assertTrue(cmp.equals(a, 1, b, 0)); + assertTrue(cmp.equals(b, 0, a, 1)); // foo != f - assertFalse(a.equals(2, b, 2)); - assertFalse(b.equals(2, a, 2)); + assertFalse(cmp.equals(a, 2, b, 2)); + assertFalse(cmp.equals(b, 2, a, 2)); } public void testEqualsWithWhitespace() { - final RawText a = new RawTextIgnoreLeadingWhitespace(Constants + final RawText a = new RawText(cmp, Constants .encodeASCII("foo-a\n \n a b c\n a\nb \n")); - final RawText b = new RawTextIgnoreLeadingWhitespace(Constants + final RawText b = new RawText(cmp, Constants .encodeASCII("foo-a b\n\nab c\na\nb\n")); // "foo-a" != "foo-a b" - assertFalse(a.equals(0, b, 0)); - assertFalse(b.equals(0, a, 0)); + assertFalse(cmp.equals(a, 0, b, 0)); + assertFalse(cmp.equals(b, 0, a, 0)); // " " == "" - assertTrue(a.equals(1, b, 1)); - assertTrue(b.equals(1, a, 1)); + assertTrue(cmp.equals(a, 1, b, 1)); + assertTrue(cmp.equals(b, 1, a, 1)); // " a b c" != "ab c" - assertFalse(a.equals(2, b, 2)); - assertFalse(b.equals(2, a, 2)); + assertFalse(cmp.equals(a, 2, b, 2)); + assertFalse(cmp.equals(b, 2, a, 2)); // " a" == "a" - assertTrue(a.equals(3, b, 3)); - assertTrue(b.equals(3, a, 3)); + assertTrue(cmp.equals(a, 3, b, 3)); + assertTrue(cmp.equals(b, 3, a, 3)); // "b " != "b" - assertFalse(a.equals(4, b, 4)); - assertFalse(b.equals(4, a, 4)); + assertFalse(cmp.equals(a, 4, b, 4)); + assertFalse(cmp.equals(b, 4, a, 4)); } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextIgnoreTrailingWhitespaceTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextIgnoreTrailingWhitespaceTest.java index ad6a2397be..b6999cd031 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextIgnoreTrailingWhitespaceTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextIgnoreTrailingWhitespaceTest.java @@ -49,52 +49,54 @@ import org.eclipse.jgit.lib.Constants; import junit.framework.TestCase; public class RawTextIgnoreTrailingWhitespaceTest extends TestCase { + private final RawTextComparator cmp = RawTextComparator.WS_IGNORE_TRAILING; + public void testEqualsWithoutWhitespace() { - final RawText a = new RawTextIgnoreTrailingWhitespace(Constants + final RawText a = new RawText(cmp, Constants .encodeASCII("foo-a\nfoo-b\nfoo\n")); - final RawText b = new RawTextIgnoreTrailingWhitespace(Constants + final RawText b = new RawText(cmp, Constants .encodeASCII("foo-b\nfoo-c\nf\n")); assertEquals(3, a.size()); assertEquals(3, b.size()); // foo-a != foo-b - assertFalse(a.equals(0, b, 0)); - assertFalse(b.equals(0, a, 0)); + assertFalse(cmp.equals(a, 0, b, 0)); + assertFalse(cmp.equals(b, 0, a, 0)); // foo-b == foo-b - assertTrue(a.equals(1, b, 0)); - assertTrue(b.equals(0, a, 1)); + assertTrue(cmp.equals(a, 1, b, 0)); + assertTrue(cmp.equals(b, 0, a, 1)); // foo != f - assertFalse(a.equals(2, b, 2)); - assertFalse(b.equals(2, a, 2)); + assertFalse(cmp.equals(a, 2, b, 2)); + assertFalse(cmp.equals(b, 2, a, 2)); } public void testEqualsWithWhitespace() { - final RawText a = new RawTextIgnoreTrailingWhitespace(Constants + final RawText a = new RawText(cmp, Constants .encodeASCII("foo-a\n \n a b c\na \n b\n")); - final RawText b = new RawTextIgnoreTrailingWhitespace(Constants + final RawText b = new RawText(cmp, Constants .encodeASCII("foo-a b\n\nab c\na\nb\n")); // "foo-a" != "foo-a b" - assertFalse(a.equals(0, b, 0)); - assertFalse(b.equals(0, a, 0)); + assertFalse(cmp.equals(a, 0, b, 0)); + assertFalse(cmp.equals(b, 0, a, 0)); // " " == "" - assertTrue(a.equals(1, b, 1)); - assertTrue(b.equals(1, a, 1)); + assertTrue(cmp.equals(a, 1, b, 1)); + assertTrue(cmp.equals(b, 1, a, 1)); // " a b c" != "ab c" - assertFalse(a.equals(2, b, 2)); - assertFalse(b.equals(2, a, 2)); + assertFalse(cmp.equals(a, 2, b, 2)); + assertFalse(cmp.equals(b, 2, a, 2)); // "a " == "a" - assertTrue(a.equals(3, b, 3)); - assertTrue(b.equals(3, a, 3)); + assertTrue(cmp.equals(a, 3, b, 3)); + assertTrue(cmp.equals(b, 3, a, 3)); // " b" != "b" - assertFalse(a.equals(4, b, 4)); - assertFalse(b.equals(4, a, 4)); + assertFalse(cmp.equals(a, 4, b, 4)); + assertFalse(cmp.equals(b, 4, a, 4)); } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextIgnoreWhitespaceChangeTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextIgnoreWhitespaceChangeTest.java index 4a71c44885..62a079f395 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextIgnoreWhitespaceChangeTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextIgnoreWhitespaceChangeTest.java @@ -49,57 +49,58 @@ import org.eclipse.jgit.lib.Constants; import junit.framework.TestCase; public class RawTextIgnoreWhitespaceChangeTest extends TestCase { + private final RawTextComparator cmp = RawTextComparator.WS_IGNORE_CHANGE; + public void testEqualsWithoutWhitespace() { - final RawText a = new RawTextIgnoreWhitespaceChange(Constants + final RawText a = new RawText(cmp, Constants .encodeASCII("foo-a\nfoo-b\nfoo\n")); - final RawText b = new RawTextIgnoreWhitespaceChange(Constants + final RawText b = new RawText(cmp, Constants .encodeASCII("foo-b\nfoo-c\nf\n")); assertEquals(3, a.size()); assertEquals(3, b.size()); // foo-a != foo-b - assertFalse(a.equals(0, b, 0)); - assertFalse(b.equals(0, a, 0)); + assertFalse(cmp.equals(a, 0, b, 0)); + assertFalse(cmp.equals(b, 0, a, 0)); // foo-b == foo-b - assertTrue(a.equals(1, b, 0)); - assertTrue(b.equals(0, a, 1)); + assertTrue(cmp.equals(a, 1, b, 0)); + assertTrue(cmp.equals(b, 0, a, 1)); // foo != f - assertFalse(a.equals(2, b, 2)); - assertFalse(b.equals(2, a, 2)); + assertFalse(cmp.equals(a, 2, b, 2)); + assertFalse(cmp.equals(b, 2, a, 2)); } public void testEqualsWithWhitespace() { - final RawText a = new RawTextIgnoreWhitespaceChange( - Constants - .encodeASCII("foo-a\n \n a b c\na \n foo\na b c\n")); - final RawText b = new RawTextIgnoreWhitespaceChange(Constants + final RawText a = new RawText(cmp, Constants + .encodeASCII("foo-a\n \n a b c\na \n foo\na b c\n")); + final RawText b = new RawText(cmp, Constants .encodeASCII("foo-a b\n\nab c\na\nfoo\na b c \n")); // "foo-a" != "foo-a b" - assertFalse(a.equals(0, b, 0)); - assertFalse(b.equals(0, a, 0)); + assertFalse(cmp.equals(a, 0, b, 0)); + assertFalse(cmp.equals(b, 0, a, 0)); // " " == "" - assertTrue(a.equals(1, b, 1)); - assertTrue(b.equals(1, a, 1)); + assertTrue(cmp.equals(a, 1, b, 1)); + assertTrue(cmp.equals(b, 1, a, 1)); // " a b c" != "ab c" - assertFalse(a.equals(2, b, 2)); - assertFalse(b.equals(2, a, 2)); + assertFalse(cmp.equals(a, 2, b, 2)); + assertFalse(cmp.equals(b, 2, a, 2)); // "a " == "a" - assertTrue(a.equals(3, b, 3)); - assertTrue(b.equals(3, a, 3)); + assertTrue(cmp.equals(a, 3, b, 3)); + assertTrue(cmp.equals(b, 3, a, 3)); // " foo" != "foo" - assertFalse(a.equals(4, b, 4)); - assertFalse(b.equals(4, a, 4)); + assertFalse(cmp.equals(a, 4, b, 4)); + assertFalse(cmp.equals(b, 4, a, 4)); // "a b c" == "a b c " - assertTrue(a.equals(5, b, 5)); - assertTrue(b.equals(5, a, 5)); + assertTrue(cmp.equals(a, 5, b, 5)); + assertTrue(cmp.equals(b, 5, a, 5)); } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java index 5cb8bc3945..f30e07f22d 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java @@ -61,17 +61,18 @@ public class RawTextTest extends TestCase { public void testEquals() { final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b\n")); final RawText b = new RawText(Constants.encodeASCII("foo-b\nfoo-c\n")); + RawTextComparator cmp = RawTextComparator.DEFAULT; assertEquals(2, a.size()); assertEquals(2, b.size()); // foo-a != foo-b - assertFalse(a.equals(0, b, 0)); - assertFalse(b.equals(0, a, 0)); + assertFalse(cmp.equals(a, 0, b, 0)); + assertFalse(cmp.equals(b, 0, a, 0)); // foo-b == foo-b - assertTrue(a.equals(1, b, 0)); - assertTrue(b.equals(0, a, 1)); + assertTrue(cmp.equals(a, 1, b, 0)); + assertTrue(cmp.equals(b, 0, a, 1)); } public void testWriteLine1() throws IOException { |