public void DiffMainTest() { diff_match_patchTest dmp = new diff_match_patchTest(); // Perform a trivial diff. List<Diff> diffs = new List<Diff> {}; CollectionAssert.AreEqual(diffs, dmp.DiffMain("", "", false), "DiffMain: Null case."); diffs = new List<Diff> {new Diff(Operation.EQUAL, "abc")}; CollectionAssert.AreEqual(diffs, dmp.DiffMain("abc", "abc", false), "DiffMain: Equality."); diffs = new List<Diff> {new Diff(Operation.EQUAL, "ab"), new Diff(Operation.INSERT, "123"), new Diff(Operation.EQUAL, "c")}; CollectionAssert.AreEqual(diffs, dmp.DiffMain("abc", "ab123c", false), "DiffMain: Simple insertion."); diffs = new List<Diff> {new Diff(Operation.EQUAL, "a"), new Diff(Operation.DELETE, "123"), new Diff(Operation.EQUAL, "bc")}; CollectionAssert.AreEqual(diffs, dmp.DiffMain("a123bc", "abc", false), "DiffMain: Simple deletion."); diffs = new List<Diff> {new Diff(Operation.EQUAL, "a"), new Diff(Operation.INSERT, "123"), new Diff(Operation.EQUAL, "b"), new Diff(Operation.INSERT, "456"), new Diff(Operation.EQUAL, "c")}; CollectionAssert.AreEqual(diffs, dmp.DiffMain("abc", "a123b456c", false), "DiffMain: Two insertions."); diffs = new List<Diff> {new Diff(Operation.EQUAL, "a"), new Diff(Operation.DELETE, "123"), new Diff(Operation.EQUAL, "b"), new Diff(Operation.DELETE, "456"), new Diff(Operation.EQUAL, "c")}; CollectionAssert.AreEqual(diffs, dmp.DiffMain("a123b456c", "abc", false), "DiffMain: Two deletions."); // Perform a real diff. // Switch off the timeout. dmp.Diff_Timeout = 0; diffs = new List<Diff> {new Diff(Operation.DELETE, "a"), new Diff(Operation.INSERT, "b")}; CollectionAssert.AreEqual(diffs, dmp.DiffMain("a", "b", false), "DiffMain: Simple case #1."); diffs = new List<Diff> {new Diff(Operation.DELETE, "Apple"), new Diff(Operation.INSERT, "Banana"), new Diff(Operation.EQUAL, "s are a"), new Diff(Operation.INSERT, "lso"), new Diff(Operation.EQUAL, " fruit.")}; CollectionAssert.AreEqual(diffs, dmp.DiffMain("Apples are a fruit.", "Bananas are also fruit.", false), "DiffMain: Simple case #2."); diffs = new List<Diff> {new Diff(Operation.DELETE, "a"), new Diff(Operation.INSERT, "\u0680"), new Diff(Operation.EQUAL, "x"), new Diff(Operation.DELETE, "\t"), new Diff(Operation.INSERT, new string (new char[]{(char)0}))}; CollectionAssert.AreEqual(diffs, dmp.DiffMain("ax\t", "\u0680x" + (char)0, false), "DiffMain: Simple case #3."); diffs = new List<Diff> {new Diff(Operation.DELETE, "1"), new Diff(Operation.EQUAL, "a"), new Diff(Operation.DELETE, "y"), new Diff(Operation.EQUAL, "b"), new Diff(Operation.DELETE, "2"), new Diff(Operation.INSERT, "xab")}; CollectionAssert.AreEqual(diffs, dmp.DiffMain("1ayb2", "abxab", false), "DiffMain: Overlap #1."); diffs = new List<Diff> {new Diff(Operation.INSERT, "xaxcx"), new Diff(Operation.EQUAL, "abc"), new Diff(Operation.DELETE, "y")}; CollectionAssert.AreEqual(diffs, dmp.DiffMain("abcy", "xaxcxabc", false), "DiffMain: Overlap #2."); diffs = new List<Diff> {new Diff(Operation.DELETE, "ABCD"), new Diff(Operation.EQUAL, "a"), new Diff(Operation.DELETE, "="), new Diff(Operation.INSERT, "-"), new Diff(Operation.EQUAL, "bcd"), new Diff(Operation.DELETE, "="), new Diff(Operation.INSERT, "-"), new Diff(Operation.EQUAL, "efghijklmnopqrs"), new Diff(Operation.DELETE, "EFGHIJKLMNOefg")}; CollectionAssert.AreEqual(diffs, dmp.DiffMain("ABCDa=bcd=efghijklmnopqrsEFGHIJKLMNOefg", "a-bcd-efghijklmnopqrs", false), "DiffMain: Overlap #3."); diffs = new List<Diff> {new Diff(Operation.INSERT, " "), new Diff(Operation.EQUAL, "a"), new Diff(Operation.INSERT, "nd"), new Diff(Operation.EQUAL, " [[Pennsylvania]]"), new Diff(Operation.DELETE, " and [[New")}; CollectionAssert.AreEqual(diffs, dmp.DiffMain("a [[Pennsylvania]] and [[New", " and [[Pennsylvania]]", false), "DiffMain: Large equality."); dmp.Diff_Timeout = 0.1f; // 100ms string a = "`Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe:\nAll mimsy were the borogoves,\nAnd the mome raths outgrabe.\n"; string b = "I am the very model of a modern major general,\nI've information vegetable, animal, and mineral,\nI know the kings of England, and I quote the fights historical,\nFrom Marathon to Waterloo, in order categorical.\n"; // Increase the text lengths by 1024 times to ensure a timeout. for (int x = 0; x < 10; x++) { a = a + a; b = b + b; } DateTime startTime = DateTime.Now; dmp.DiffMain(a, b); DateTime endTime = DateTime.Now; // Test that we took at least the timeout period. Assert.IsTrue(new TimeSpan(((long)(dmp.Diff_Timeout * 1000)) * 10000) <= endTime - startTime); // Test that we didn't take forever (be forgiving). // Theoretically this test could fail very occasionally if the // OS task swaps or locks up for a second at the wrong moment. // Assert.IsTrue(new TimeSpan(((long)(dmp.Diff_Timeout * 1000)) * 10000 * 2) > endTime - startTime); dmp.Diff_Timeout = 0; // Test the linemode speedup. // Must be long to pass the 100 char cutoff. a = "1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n"; b = "abcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\n"; CollectionAssert.AreEqual(dmp.DiffMain(a, b, true), dmp.DiffMain(a, b, false), "DiffMain: Simple line-mode."); a = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; b = "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij"; CollectionAssert.AreEqual(dmp.DiffMain(a, b, true), dmp.DiffMain(a, b, false), "DiffMain: Single line-mode."); a = "1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n"; b = "abcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n"; string[] texts_linemode = diff_rebuildtexts(dmp.DiffMain(a, b, true)); string[] texts_textmode = diff_rebuildtexts(dmp.DiffMain(a, b, false)); CollectionAssert.AreEqual(texts_textmode, texts_linemode, "DiffMain: Overlap line-mode."); // Test null inputs -- not needed because nulls can't be passed in C#. }
public void PatchMakeTest() { diff_match_patchTest dmp = new diff_match_patchTest(); List<Patch> patches; patches = dmp.PatchMake("", ""); Assert.AreEqual("", dmp.PatchToText(patches), "PatchMake: Null case."); string text1 = "The quick brown fox jumps over the lazy dog."; string text2 = "That quick brown fox jumped over a lazy dog."; string expectedPatch = "@@ -1,8 +1,7 @@\n Th\n-at\n+e\n qui\n@@ -21,17 +21,18 @@\n jump\n-ed\n+s\n over \n-a\n+the\n laz\n"; // The second patch must be "-21,17 +21,18", not "-22,17 +21,18" due to rolling context. patches = dmp.PatchMake(text2, text1); Assert.AreEqual(expectedPatch, dmp.PatchToText(patches), "PatchMake: Text2+Text1 inputs."); expectedPatch = "@@ -1,11 +1,12 @@\n Th\n-e\n+at\n quick b\n@@ -22,18 +22,17 @@\n jump\n-s\n+ed\n over \n-the\n+a\n laz\n"; patches = dmp.PatchMake(text1, text2); Assert.AreEqual(expectedPatch, dmp.PatchToText(patches), "PatchMake: Text1+Text2 inputs."); List<Diff> diffs = dmp.DiffMain(text1, text2, false); patches = dmp.PatchMake(diffs); Assert.AreEqual(expectedPatch, dmp.PatchToText(patches), "PatchMake: Diff input."); patches = dmp.PatchMake(text1, diffs); Assert.AreEqual(expectedPatch, dmp.PatchToText(patches), "PatchMake: Text1+Diff inputs."); patches = dmp.PatchMake(text1, text2, diffs); Assert.AreEqual(expectedPatch, dmp.PatchToText(patches), "PatchMake: Text1+Text2+Diff inputs (deprecated)."); patches = dmp.PatchMake("`1234567890-=[]\\;',./", "~!@#$%^&*()_+{}|:\"<>?"); Assert.AreEqual("@@ -1,21 +1,21 @@\n-%601234567890-=%5b%5d%5c;',./\n+~!@#$%25%5e&*()_+%7b%7d%7c:%22%3c%3e?\n", dmp.PatchToText(patches), "PatchToText: Character encoding."); diffs = new List<Diff> { new Diff(Operation.DELETE, "`1234567890-=[]\\;',./"), new Diff(Operation.INSERT, "~!@#$%^&*()_+{}|:\"<>?")}; CollectionAssert.AreEqual(diffs, dmp.PatchFromText("@@ -1,21 +1,21 @@\n-%601234567890-=%5B%5D%5C;',./\n+~!@#$%25%5E&*()_+%7B%7D%7C:%22%3C%3E?\n") [0].diffs, "PatchFromText: Character decoding."); text1 = ""; for (int x = 0; x < 100; x++) { text1 += "abcdef"; } text2 = text1 + "123"; expectedPatch = "@@ -573,28 +573,31 @@\n cdefabcdefabcdefabcdefabcdef\n+123\n"; patches = dmp.PatchMake(text1, text2); Assert.AreEqual(expectedPatch, dmp.PatchToText(patches), "PatchMake: Long string with repeats."); // Test null inputs -- not needed because nulls can't be passed in C#. }