public void TestSplit() { TextFile primary = new TextFile(); primary.AddLines(new[] { "a", "b", "c", "d", "e", "f" }); TextFile secondary = new TextFile(); secondary.AddLines(new[] { "1", "2", "3", "4", "5", "6" }); ModificationCollection modifications = new ModificationCollection(primary, secondary); modifications.AddNoChanged(0, 2); modifications.AddReplaced(2, 0, 2); modifications.AddAdded(4, 2); Modification[] splited = modifications[1].Split(1); // неверный индекс Assert.AreEqual(splited.Length, 1); Assert.AreSame(splited[0], modifications[1]); splited = modifications[1].Split(2); // граничный индекс Assert.AreEqual(splited.Length, 1); Assert.AreSame(splited[0], modifications[1]); splited = modifications[1].Split(3); // индекс в середине Assert.AreEqual(splited.Length, 2); }
public void TestSplit() { TextFile primary = new TextFile(); primary.AddLines(new[] { "a", "b", "c", "d", "e", "f" }); TextFile secondary = new TextFile(); secondary.AddLines(new[] { "1", "2", "3", "4", "5", "6" }); ModificationCollection modifications = new ModificationCollection(primary, secondary); modifications.AddNoChanged(0, 2); modifications.AddReplaced(2, 0, 2); modifications.AddAdded(4, 2); Modification[] splited = modifications[1].Split(1); // неверный индекс Assert.AreEqual(splited.Length, 1); Assert.AreSame(splited[0], modifications[1]); splited = modifications[1].Split(2); // граничный индекс Assert.AreEqual(splited.Length, 1); Assert.AreSame(splited[0], modifications[1]); splited = modifications[1].Split(3); // индекс в середине Assert.AreEqual(splited.Length, 2); }
private bool DetermineModification(int primaryStart, int primaryEnd, int secondaryStart, int secondaryEnd, ModificationCollection modifications) { bool result = false; int primaryLength = primaryEnd - primaryStart; int secondaryLength = secondaryEnd - secondaryStart; if (secondaryLength > 0) { if (primaryLength > 0) { var minLength = Math.Min(secondaryLength, primaryLength); modifications.AddReplaced(primaryStart, secondaryStart, minLength); if (secondaryLength > primaryLength) { secondaryStart += minLength; modifications.AddAdded(secondaryStart, secondaryLength - primaryLength); } else { if (primaryLength > secondaryLength) { primaryStart += minLength; modifications.AddRemoved(primaryStart, primaryLength - secondaryLength); } } } else { modifications.AddAdded(secondaryStart, secondaryLength); } result = true; } else { if (primaryLength > 0) { modifications.AddRemoved(primaryStart, primaryLength); result = true; } } return(result); }
public void TestSplit() { TextFile primary = new TextFile(); primary.AddLines(new[] { "a", "b", "c", "d", "e", "f" }); TextFile secondary = new TextFile(); secondary.AddLines(new[] { "1", "2", "3", "4", "5", "6" }); ModificationCollection modifications = new ModificationCollection(primary, secondary); modifications.AddNoChanged(0, 2); modifications.AddReplaced(2, 0, 2); modifications.AddAdded(4, 2); modifications.Split(3); // индекс в середине Assert.AreEqual(modifications.Count, 4); }
public void TestSplit() { TextFile primary = new TextFile(); primary.AddLines(new[] { "a", "b", "c", "d", "e", "f" }); TextFile secondary = new TextFile(); secondary.AddLines(new[] { "1", "2", "3", "4", "5", "6" }); ModificationCollection modifications = new ModificationCollection(primary, secondary); modifications.AddNoChanged(0, 2); modifications.AddReplaced(2, 0, 2); modifications.AddAdded(4, 2); modifications.Split(3); // индекс в середине Assert.AreEqual(modifications.Count, 4); }
public ModificationCollection Compare(TextFile primary, TextFile secondary) { _primary = primary; _secondary = secondary; ModificationCollection result = new ModificationCollection(primary, secondary); if (primary.LineCount == 0 && secondary.LineCount == 0) { // два пустых файла _primary = null; _secondary = null; return result; } if (primary.LineCount == 0) { if (secondary.LineCount > 0) { // первый файл пустой, а второй имеет строки result.AddAdded(0, secondary.LineCount); } _primary = null; _secondary = null; return result; } if (secondary.LineCount == 0) { if (primary.LineCount > 0) { // первый файл имеет строки, а второй пустой result.AddRemoved(0, primary.LineCount); } _primary = null; _secondary = null; return result; } List<Area> areas = new List<Area>(); DivideIntoAreas(0, primary.LineCount - 1, 0, secondary.LineCount - 1, areas); areas.Sort(Area.SecondaryIndexComparer); int primaryIndex = 0; int secondaryIndex = 0; Area last = null; foreach (Area area in areas) { if (DetermineModification(primaryIndex, area.PrimaryIndex, secondaryIndex, area.SecondaryIndex, result) || last == null) { result.AddNoChanged(area.PrimaryIndex, area.Length); } primaryIndex = area.PrimaryIndex + area.Length; secondaryIndex = area.SecondaryIndex + area.Length; last = area; } // этот вызов решает проблему, когда файлы совершенно разные // это граничная ситуация, областей пересечения нет DetermineModification(primaryIndex, primary.LineCount, secondaryIndex, secondary.LineCount, result); return result; }
private bool DetermineModification(int primaryStart, int primaryEnd, int secondaryStart, int secondaryEnd, ModificationCollection modifications) { bool result = false; int primaryLength = primaryEnd - primaryStart; int secondaryLength = secondaryEnd - secondaryStart; if (secondaryLength > 0) { if (primaryLength > 0) { var minLength = Math.Min(secondaryLength, primaryLength); modifications.AddReplaced(primaryStart, secondaryStart, minLength); if (secondaryLength > primaryLength) { secondaryStart += minLength; modifications.AddAdded(secondaryStart, secondaryLength - primaryLength); } else { if (primaryLength > secondaryLength) { primaryStart += minLength; modifications.AddRemoved(primaryStart, primaryLength - secondaryLength); } } } else { modifications.AddAdded(secondaryStart, secondaryLength); } result = true; } else { if (primaryLength > 0) { modifications.AddRemoved(primaryStart, primaryLength); result = true; } } return result; }
public ModificationCollection Compare(TextFile primary, TextFile secondary) { _primary = primary; _secondary = secondary; ModificationCollection result = new ModificationCollection(primary, secondary); if (primary.LineCount == 0 && secondary.LineCount == 0) { // два пустых файла _primary = null; _secondary = null; return(result); } if (primary.LineCount == 0) { if (secondary.LineCount > 0) { // первый файл пустой, а второй имеет строки result.AddAdded(0, secondary.LineCount); } _primary = null; _secondary = null; return(result); } if (secondary.LineCount == 0) { if (primary.LineCount > 0) { // первый файл имеет строки, а второй пустой result.AddRemoved(0, primary.LineCount); } _primary = null; _secondary = null; return(result); } List <Area> areas = new List <Area>(); DivideIntoAreas(0, primary.LineCount - 1, 0, secondary.LineCount - 1, areas); areas.Sort(Area.SecondaryIndexComparer); int primaryIndex = 0; int secondaryIndex = 0; Area last = null; foreach (Area area in areas) { if (DetermineModification(primaryIndex, area.PrimaryIndex, secondaryIndex, area.SecondaryIndex, result) || last == null) { result.AddNoChanged(area.PrimaryIndex, area.Length); } primaryIndex = area.PrimaryIndex + area.Length; secondaryIndex = area.SecondaryIndex + area.Length; last = area; } // этот вызов решает проблему, когда файлы совершенно разные // это граничная ситуация, областей пересечения нет DetermineModification(primaryIndex, primary.LineCount, secondaryIndex, secondary.LineCount, result); return(result); }