예제 #1
0
        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 TestFindModificationByPrimaryIndex()
        {
            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.AddNoChanged(4, 2);

            Modification wanted = modifications.FindModificationByPrimaryIndex(1);
            Assert.AreEqual(wanted.Index, 0);

            wanted = modifications.FindModificationByPrimaryIndex(3);
            Assert.AreEqual(wanted.Index, 1);

            wanted = modifications.FindModificationByPrimaryIndex(4);
            Assert.AreEqual(wanted.Index, 2);

            wanted = modifications.FindModificationByPrimaryIndex(5);
            Assert.AreEqual(wanted.Index, 2);
        }
예제 #3
0
파일: Merger.cs 프로젝트: gilind/workshop
        public Merger()
        {
            ServerFile = new TextFile();
            User1File = new TextFile();
            User2File = new TextFile();

            ServerUser1Modifications = new ModificationCollection(ServerFile, User1File);
            ServerUser2Modifications = new ModificationCollection(ServerFile, User2File);
            MergedModifications = new ModificationCollection();
        }
        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);
        }
예제 #5
0
        public void TestAdded()
        {
            TextFile file1 = new TextFile();
            file1.AddLine("1");
            file1.AddLine("3");

            TextFile file2 = new TextFile();
            file2.AddLine("1");
            file2.AddLine("2");
            file2.AddLine("3");

            FileComparer fileComparer = new FileComparer();
            ModificationCollection report = fileComparer.Compare(file1, file2);

            ModificationAreEqual(report[0], "NoChanged", 0, 0, 1);
            ModificationAreEqual(report[1], "Added", -1, 1, 1);
            ModificationAreEqual(report[2], "NoChanged", 1, 2, 1);
        }
예제 #6
0
        public void TestEmptyFiles()
        {
            TextFile emptyFile = new TextFile();

            TextFile file2 = new TextFile();
            file2.AddLine("public class MyClass");
            file2.AddLine("{");
            file2.AddLine("}");

            FileComparer fileComparer = new FileComparer();

            ModificationCollection report = fileComparer.Compare(emptyFile, file2);
            ModificationAreEqual(report[0], "Added", -1, 0, 3);

            report = fileComparer.Compare(file2, emptyFile);
            ModificationAreEqual(report[0], "Removed", 0, -1, 3);

            report = fileComparer.Compare(emptyFile, emptyFile);
            Assert.AreEqual(report.Count, 0);
        }
예제 #7
0
        public void TestReplaced2()
        {
            TextFile file1 = new TextFile();
            file1.AddLine("1");
            file1.AddLine("4");
            file1.AddLine("3");

            TextFile file2 = new TextFile();
            file2.AddLine("2");

            FileComparer fileComparer = new FileComparer();
            ModificationCollection report = fileComparer.Compare(file1, file2);

            ModificationAreEqual(report[0], "Replaced", 0, 0, 1);
            ModificationAreEqual(report[1], "Removed", 1, -1, 2);
        }
예제 #8
0
        public void TestMethod1()
        {
            TextFile file1 = new TextFile();
            file1.AddLine("public class MyClass");
            file1.AddLine("{");
            file1.AddLine("    public MyClass()");
            file1.AddLine("    { }");
            file1.AddLine();
            file1.AddLine("    public string StringProperty");
            file1.AddLine("    {");
            file1.AddLine("        get;");
            file1.AddLine("        set;");
            file1.AddLine("    }");
            file1.AddLine("}");

            TextFile file2 = new TextFile();
            file2.AddLine("public class MyClass");
            file2.AddLine("{");
            file2.AddLine("    public MyClass()");
            file2.AddLine("    { }");
            file2.AddLine();
            file2.AddLine("    public int MyMethod(int value)");
            file2.AddLine("    {");
            file2.AddLine("        Console.WriteLine(value);");
            file2.AddLine("    }");
            file2.AddLine("}");

            FileComparer fileComparer = new FileComparer();
            ModificationCollection report = fileComparer.Compare(file1, file2);

            ModificationAreEqual(report[0], "NoChanged", 0, 0, 5);
            ModificationAreEqual(report[1], "Replaced", 5, 5, 1);
            ModificationAreEqual(report[2], "NoChanged", 6, 6, 1);
            ModificationAreEqual(report[3], "Replaced", 7, 7, 1);
            ModificationAreEqual(report[4], "Removed", 8, -1, 1);
            ModificationAreEqual(report[5], "NoChanged", 9, 8, 2);
        }
예제 #9
0
 /// <summary>
 /// Найти длину совпадения.
 /// </summary>
 private static int GetPrimaryMatchLength(TextFile primary, int primaryIndex, TextFile secondary, int secondaryIndex, int maxLength)
 {
     int matchCount;
     for (matchCount = 0; matchCount < maxLength; matchCount++)
     {
         if (secondary[secondaryIndex + matchCount].CompareTo(primary[primaryIndex + matchCount]) != 0)
         {
             break;
         }
     }
     return matchCount;
 }
예제 #10
0
        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;
        }
예제 #11
0
        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);
        }