Beispiel #1
0
        public virtual void TestEdit_InsertNearCommonTail()
        {
            EditList r = Diff(T("aq}nb"), T("aCq}nD}nb"));

            NUnit.Framework.Assert.AreEqual(new Edit(1, 1, 1, 2), r[0]);
            NUnit.Framework.Assert.AreEqual(new Edit(3, 3, 4, 7), r[1]);
            NUnit.Framework.Assert.AreEqual(2, r.Count);
        }
Beispiel #2
0
        public virtual void TestEdit_InvertBlocks()
        {
            EditList r = Diff(T("aYYbcdXXz"), T("aXXbcdYYz"));

            NUnit.Framework.Assert.AreEqual(2, r.Count);
            NUnit.Framework.Assert.AreEqual(new Edit(1, 3, 1, 3), r[0]);
            NUnit.Framework.Assert.AreEqual(new Edit(6, 8, 6, 8), r[1]);
        }
Beispiel #3
0
        public virtual void TestEdit_CommonReplaceCommonDeleteCommon()
        {
            EditList r = Diff(T("aRbCd"), T("aSbd"));

            NUnit.Framework.Assert.AreEqual(2, r.Count);
            NUnit.Framework.Assert.AreEqual(new Edit(1, 2, 1, 2), r[0]);
            NUnit.Framework.Assert.AreEqual(new Edit(3, 4, 3, 3), r[1]);
        }
Beispiel #4
0
        public virtual void TestEdit_MoveBlock()
        {
            EditList r = Diff(T("aYYbcdz"), T("abcdYYz"));

            NUnit.Framework.Assert.AreEqual(2, r.Count);
            NUnit.Framework.Assert.AreEqual(new Edit(1, 3, 1, 1), r[0]);
            NUnit.Framework.Assert.AreEqual(new Edit(6, 6, 4, 6), r[1]);
        }
Beispiel #5
0
        public virtual void TestEdit_DuplicateAButCommonUniqueInB()
        {
            EditList r = Diff(T("AbbcR"), T("CbcS"));

            NUnit.Framework.Assert.AreEqual(2, r.Count);
            NUnit.Framework.Assert.AreEqual(new Edit(0, 2, 0, 1), r[0]);
            NUnit.Framework.Assert.AreEqual(new Edit(4, 5, 3, 4), r[1]);
        }
Beispiel #6
0
        public virtual void TestEdit_ReplaceCommonDelete()
        {
            EditList r = Diff(T("RbC"), T("Sb"));

            NUnit.Framework.Assert.AreEqual(2, r.Count);
            NUnit.Framework.Assert.AreEqual(new Edit(0, 1, 0, 1), r[0]);
            NUnit.Framework.Assert.AreEqual(new Edit(2, 3, 2, 2), r[1]);
        }
Beispiel #7
0
 /// <summary>Adjust the Edits to reflect positions in the base sequence.</summary>
 /// <remarks>Adjust the Edits to reflect positions in the base sequence.</remarks>
 /// <?></?>
 /// <param name="edits">
 /// edits to adjust in-place. Prior to invocation the indexes are
 /// in terms of the two subsequences; after invocation the indexes
 /// are in terms of the base sequences.
 /// </param>
 /// <param name="a">the A sequence.</param>
 /// <param name="b">the B sequence.</param>
 /// <returns>
 /// always
 /// <code>edits</code>
 /// (as the list was updated in-place).
 /// </returns>
 public static EditList ToBase <S>(EditList edits, NGit.Diff.Subsequence <S> a, NGit.Diff.Subsequence
                                   <S> b) where S : Sequence
 {
     foreach (Edit e in edits)
     {
         ToBase(e, a, b);
     }
     return(edits);
 }
Beispiel #8
0
 internal State(HistogramDiff _enclosing, EditList edits, HashedSequenceComparator
                <S> cmp, HashedSequence <S> a, HashedSequence <S> b)
 {
     this._enclosing = _enclosing;
     this.cmp        = cmp;
     this.a          = a;
     this.b          = b;
     this.edits      = edits;
 }
        public virtual void TestEdit_NoUniqueMiddleSide_FlipAndExpand()
        {
            EditList r = Diff(T("aRSz"), T("aSSRRz"));

            NUnit.Framework.Assert.AreEqual(2, r.Count);
            NUnit.Framework.Assert.AreEqual(new Edit(1, 2, 1, 1), r[0]);
            // DELETE "R"
            NUnit.Framework.Assert.AreEqual(new Edit(3, 3, 2, 5), r[1]);
        }
        public virtual void TestEdit_NoUniqueMiddleSide_FlipBlocks()
        {
            EditList r = Diff(T("aRRSSz"), T("aSSRRz"));

            NUnit.Framework.Assert.AreEqual(2, r.Count);
            NUnit.Framework.Assert.AreEqual(new Edit(1, 3, 1, 1), r[0]);
            // DELETE "RR"
            NUnit.Framework.Assert.AreEqual(new Edit(5, 5, 3, 5), r[1]);
        }
        public virtual void TestEdit_UniqueCommonLargerThanMatchPoint()
        {
            // We are testing 3 unique common matches, but two of
            // them are consumed as part of the 1st's LCS region.
            EditList r = Diff(T("AbdeZ"), T("PbdeQR"));

            NUnit.Framework.Assert.AreEqual(2, r.Count);
            NUnit.Framework.Assert.AreEqual(new Edit(0, 1, 0, 1), r[0]);
            NUnit.Framework.Assert.AreEqual(new Edit(4, 5, 4, 6), r[1]);
        }
        public virtual void TestEdit_CommonGrowsPrefixAndSuffix()
        {
            // Here there is only one common unique point, but we can grow it
            // in both directions to find the LCS in the middle.
            EditList r = Diff(T("AaabccZ"), T("PaabccR"));

            NUnit.Framework.Assert.AreEqual(2, r.Count);
            NUnit.Framework.Assert.AreEqual(new Edit(0, 1, 0, 1), r[0]);
            NUnit.Framework.Assert.AreEqual(new Edit(6, 7, 6, 7), r[1]);
        }
Beispiel #13
0
 /// <summary>Formats a list of edits in unified diff format</summary>
 /// <param name="edits">some differences which have been calculated between A and B</param>
 /// <param name="a">the text A which was compared</param>
 /// <param name="b">the text B which was compared</param>
 /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
 public virtual void Format(EditList edits, RawText a, RawText b)
 {
     for (int curIdx = 0; curIdx < edits.Count;)
     {
         Edit curEdit = edits[curIdx];
         int  endIdx  = FindCombinedEnd(edits, curIdx);
         Edit endEdit = edits[endIdx];
         int  aCur    = Math.Max(0, curEdit.GetBeginA() - context);
         int  bCur    = Math.Max(0, curEdit.GetBeginB() - context);
         int  aEnd    = Math.Min(a.Size(), endEdit.GetEndA() + context);
         int  bEnd    = Math.Min(b.Size(), endEdit.GetEndB() + context);
         WriteHunkHeader(aCur, aEnd, bCur, bEnd);
         while (aCur < aEnd || bCur < bEnd)
         {
             if (aCur < curEdit.GetBeginA() || endIdx + 1 < curIdx)
             {
                 WriteContextLine(a, aCur);
                 if (IsEndOfLineMissing(a, aCur))
                 {
                     @out.Write(noNewLine);
                 }
                 aCur++;
                 bCur++;
             }
             else
             {
                 if (aCur < curEdit.GetEndA())
                 {
                     WriteRemovedLine(a, aCur);
                     if (IsEndOfLineMissing(a, aCur))
                     {
                         @out.Write(noNewLine);
                     }
                     aCur++;
                 }
                 else
                 {
                     if (bCur < curEdit.GetEndB())
                     {
                         WriteAddedLine(b, bCur);
                         if (IsEndOfLineMissing(b, bCur))
                         {
                             @out.Write(noNewLine);
                         }
                         bCur++;
                     }
                 }
             }
             if (End(curEdit, aCur, bCur) && ++curIdx < edits.Count)
             {
                 curEdit = edits[curIdx];
             }
         }
     }
 }
Beispiel #14
0
        private MyersDiff(EditList edits, HashedSequenceComparator <S> cmp, HashedSequence
                          <S> a, HashedSequence <S> b, Edit region)
        {
            middle = new MyersDiff <S> .MiddleEdit(this);

            this.edits = edits;
            this.cmp   = cmp;
            this.a     = a;
            this.b     = b;
            CalculateEdits(region);
        }
        public virtual void TestExceedsChainLength_DuringScanOfB()
        {
            HistogramDiff hd = new HistogramDiff();

            hd.SetFallbackAlgorithm(null);
            hd.SetMaxChainLength(1);
            EditList r = hd.Diff(RawTextComparator.DEFAULT, T("RaaS"), T("QaaT"));

            NUnit.Framework.Assert.AreEqual(1, r.Count);
            NUnit.Framework.Assert.AreEqual(new Edit(0, 4, 0, 4), r[0]);
        }
Beispiel #16
0
        public virtual void TestSet()
        {
            Edit     e1 = new Edit(1, 2, 1, 1);
            Edit     e2 = new Edit(3, 4, 3, 3);
            EditList l  = new EditList();

            l.AddItem(e1);
            NUnit.Framework.Assert.AreSame(e1, l[0]);
            NUnit.Framework.Assert.AreSame(e1, l.Set(0, e2));
            NUnit.Framework.Assert.AreSame(e2, l[0]);
        }
        public virtual void TestExceedsChainLength_DuringScanOfA()
        {
            HistogramDiff hd = new HistogramDiff();

            hd.SetFallbackAlgorithm(null);
            hd.SetMaxChainLength(3);
            SequenceComparator <RawText> cmp = new _SequenceComparator_97();
            EditList r = hd.Diff(cmp, T("RabS"), T("QabT"));

            NUnit.Framework.Assert.AreEqual(1, r.Count);
            NUnit.Framework.Assert.AreEqual(new Edit(0, 4, 0, 4), r[0]);
        }
Beispiel #18
0
        public virtual void TestEmpty()
        {
            EditList l = new EditList();

            NUnit.Framework.Assert.AreEqual(0, l.Count);
            NUnit.Framework.Assert.IsTrue(l.IsEmpty());
            NUnit.Framework.Assert.AreEqual("EditList[]", l.ToString());
            NUnit.Framework.Assert.AreEqual(l, l);
            NUnit.Framework.Assert.AreEqual(new EditList(), l);
            NUnit.Framework.Assert.IsFalse(l.Equals(string.Empty));
            NUnit.Framework.Assert.AreEqual(l.GetHashCode(), new EditList().GetHashCode());
        }
        public virtual void TestEdit_LcsContainsUnique()
        {
            EditList r = Diff(T("nqnjrnjsnm"), T("AnqnjrnjsnjTnmZ"));

            NUnit.Framework.Assert.AreEqual(new Edit(0, 0, 0, 1), r[0]);
            // INSERT "A";
            NUnit.Framework.Assert.AreEqual(new Edit(9, 9, 10, 13), r[1]);
            // INSERT "jTn";
            NUnit.Framework.Assert.AreEqual(new Edit(10, 10, 14, 15), r[2]);
            // INSERT "Z";
            NUnit.Framework.Assert.AreEqual(3, r.Count);
        }
Beispiel #20
0
        public virtual void TestRemove()
        {
            Edit     e1 = new Edit(1, 2, 1, 1);
            Edit     e2 = new Edit(8, 8, 8, 12);
            EditList l  = new EditList();

            l.AddItem(e1);
            l.AddItem(e2);
            l.Remove(e1);
            NUnit.Framework.Assert.AreEqual(1, l.Count);
            NUnit.Framework.Assert.AreSame(e2, l[0]);
        }
Beispiel #21
0
        /// <summary>Compare two sequences and identify a list of edits between them.</summary>
        /// <remarks>Compare two sequences and identify a list of edits between them.</remarks>
        /// <?></?>
        /// <param name="cmp">the comparator supplying the element equivalence function.</param>
        /// <param name="a">
        /// the first (also known as old or pre-image) sequence. Edits
        /// returned by this algorithm will reference indexes using the
        /// 'A' side:
        /// <see cref="Edit.GetBeginA()">Edit.GetBeginA()</see>
        /// ,
        /// <see cref="Edit.GetEndA()">Edit.GetEndA()</see>
        /// .
        /// </param>
        /// <param name="b">
        /// the second (also known as new or post-image) sequence. Edits
        /// returned by this algorithm will reference indexes using the
        /// 'B' side:
        /// <see cref="Edit.GetBeginB()">Edit.GetBeginB()</see>
        /// ,
        /// <see cref="Edit.GetEndB()">Edit.GetEndB()</see>
        /// .
        /// </param>
        /// <returns>
        /// a modifiable edit list comparing the two sequences. If empty, the
        /// sequences are identical according to
        /// <code>cmp</code>
        /// 's rules. The
        /// result list is never null.
        /// </returns>
        public virtual EditList Diff <S>(SequenceComparator <S> cmp, S a, S b) where
        S : Sequence
        {
            Edit region = cmp.ReduceCommonStartEnd(a, b, CoverEdit(a, b));

            switch (region.GetType())
            {
            case Edit.Type.INSERT:
            case Edit.Type.DELETE:
            {
                return(EditList.Singleton(region));
            }

            case Edit.Type.REPLACE:
            {
                SubsequenceComparator <S> cs  = new SubsequenceComparator <S>(cmp);
                Subsequence <S>           @as = Subsequence <S> .A(a, region);

                Subsequence <S> bs = Subsequence <S> .B(b, region);

                EditList e = Subsequence <S> .ToBase(DiffNonCommon(cs, @as, bs), @as, bs);

                // The last insertion may need to be shifted later if it
                // inserts elements that were previously reduced out as
                // common at the end.
                //
                Edit last = e[e.Count - 1];
                if (last.GetType() == Edit.Type.INSERT)
                {
                    while (last.endB < b.Size() && cmp.Equals(b, last.beginB, b, last.endB))
                    {
                        last.beginA++;
                        last.endA++;
                        last.beginB++;
                        last.endB++;
                    }
                }
                return(e);
            }

            case Edit.Type.EMPTY:
            {
                return(new EditList(0));
            }

            default:
            {
                throw new InvalidOperationException();
            }
            }
        }
        public override EditList DiffNonCommon <S>(SequenceComparator <S> cmp, S a,
                                                   S b)
        {
            HashedSequencePair <S>       p  = new HashedSequencePair <S>(cmp, a, b);
            HashedSequenceComparator <S> hc = p.GetComparator();
            HashedSequence <S>           ha = p.GetA();
            HashedSequence <S>           hb = p.GetB();

            p = null;
            EditList res    = new EditList();
            Edit     region = new Edit(0, a.Size(), 0, b.Size());

            DiffNonCommon(res, hc, ha, hb, region);
            return(res);
        }
Beispiel #23
0
            internal virtual void DiffReplace(Edit r)
            {
                Edit lcs = new HistogramDiffIndex <S>(this._enclosing.maxChainLength, this.cmp, this
                                                      .a, this.b, r).FindLongestCommonSequence();

                if (lcs != null)
                {
                    // If we were given an edit, we can prove a result here.
                    //
                    if (lcs.IsEmpty())
                    {
                        // An empty edit indicates there is nothing in common.
                        // Replace the entire region.
                        //
                        this.edits.AddItem(r);
                    }
                    else
                    {
                        this.Diff(r.Before(lcs));
                        this.Diff(r.After(lcs));
                    }
                }
                else
                {
                    if (this._enclosing.fallback is LowLevelDiffAlgorithm)
                    {
                        LowLevelDiffAlgorithm fb = (LowLevelDiffAlgorithm)this._enclosing.fallback;
                        fb.DiffNonCommon(this.edits, this.cmp, this.a, this.b, r);
                    }
                    else
                    {
                        if (this._enclosing.fallback != null)
                        {
                            SubsequenceComparator <HashedSequence <S> > cs  = this.Subcmp();
                            Subsequence <HashedSequence <S> >           @as = Subsequence <S> .A(this.a, r);

                            Subsequence <HashedSequence <S> > bs = Subsequence <S> .B(this.b, r);

                            EditList res = this._enclosing.fallback.DiffNonCommon(cs, @as, bs);
                            Sharpen.Collections.AddAll(this.edits, Subsequence <S> .ToBase(res, @as, bs));
                        }
                        else
                        {
                            this.edits.AddItem(r);
                        }
                    }
                }
            }
Beispiel #24
0
        public virtual void TestAddOne()
        {
            Edit     e = new Edit(1, 2, 1, 1);
            EditList l = new EditList();

            Extensions.AddItem(l, e);
            NUnit.Framework.Assert.AreEqual(1, l.Count);
            NUnit.Framework.Assert.IsFalse(Extensions.IsEmpty(l));
            NUnit.Framework.Assert.AreSame(e, l[0]);
            NUnit.Framework.Assert.AreSame(e, Extensions.Iterator(l).Next());
            NUnit.Framework.Assert.AreEqual(l, l);
            NUnit.Framework.Assert.IsFalse(l.Equals(new EditList()));
            EditList l2 = new EditList();

            Extensions.AddItem(l2, e);
            NUnit.Framework.Assert.AreEqual(l2, l);
            NUnit.Framework.Assert.AreEqual(l, l2);
            NUnit.Framework.Assert.AreEqual(AList <Edit> .GetHashCode(l), AList <Edit> .GetHashCode(l2));
        }
Beispiel #25
0
        public virtual void TestAddOne()
        {
            Edit     e = new Edit(1, 2, 1, 1);
            EditList l = new EditList();

            l.AddItem(e);
            NUnit.Framework.Assert.AreEqual(1, l.Count);
            NUnit.Framework.Assert.IsFalse(l.IsEmpty());
            NUnit.Framework.Assert.AreSame(e, l[0]);
            NUnit.Framework.Assert.AreSame(e, l.Iterator().Next());
            NUnit.Framework.Assert.AreEqual(l, l);
            NUnit.Framework.Assert.IsFalse(l.Equals(new EditList()));
            EditList l2 = new EditList();

            l2.AddItem(e);
            NUnit.Framework.Assert.AreEqual(l2, l);
            NUnit.Framework.Assert.AreEqual(l, l2);
            NUnit.Framework.Assert.AreEqual(l.GetHashCode(), l2.GetHashCode());
        }
Beispiel #26
0
 /// <param name="args">two filenames specifying the contents to be diffed</param>
 public static void Main(string[] args)
 {
     if (args.Length != 2)
     {
         System.Console.Error.WriteLine(JGitText.Get().need2Arguments);
         System.Environment.Exit(1);
     }
     try
     {
         RawText  a = new RawText(new FilePath(args[0]));
         RawText  b = new RawText(new FilePath(args[1]));
         EditList r = INSTANCE.Diff(RawTextComparator.DEFAULT, a, b);
         System.Console.Out.WriteLine(r.ToString());
     }
     catch (Exception e)
     {
         Sharpen.Runtime.PrintStackTrace(e);
     }
 }
Beispiel #27
0
 public override void DiffNonCommon <S>(EditList edits, HashedSequenceComparator <S>
                                        cmp, HashedSequence <S> a, HashedSequence <S> b, Edit region)
 {
     new HistogramDiff.State <S>(this, edits, cmp, a, b).DiffReplace(region);
 }
Beispiel #28
0
 /// <summary>Construct an edit list containing a single edit.</summary>
 /// <remarks>Construct an edit list containing a single edit.</remarks>
 /// <param name="edit">the edit to return in the list.</param>
 /// <returns>
 /// list containing only
 /// <code>edit</code>
 /// .
 /// </returns>
 public static NGit.Diff.EditList Singleton(Edit edit)
 {
     NGit.Diff.EditList res = new NGit.Diff.EditList(1);
     res.AddItem(edit);
     return(res);
 }
        public virtual void TestEmptyInputs()
        {
            EditList r = Diff(T(string.Empty), T(string.Empty));

            NUnit.Framework.Assert.IsTrue(r.IsEmpty(), "is empty");
        }
 /// <summary>Compare two sequences and identify a list of edits between them.</summary>
 /// <remarks>
 /// Compare two sequences and identify a list of edits between them.
 /// This method should be invoked only after the two sequences have been
 /// proven to have no common starting or ending elements. The expected
 /// elimination of common starting and ending elements is automatically
 /// performed by the
 /// <see cref="DiffAlgorithm.Diff{S}(SequenceComparator{S}, Sequence, Sequence)">DiffAlgorithm.Diff&lt;S&gt;(SequenceComparator&lt;S&gt;, Sequence, Sequence)
 ///     </see>
 /// method, which invokes this method using
 /// <see cref="Subsequence{S}">Subsequence&lt;S&gt;</see>
 /// s.
 /// </remarks>
 /// <?></?>
 /// <param name="edits">result list to append the region's edits onto.</param>
 /// <param name="cmp">the comparator supplying the element equivalence function.</param>
 /// <param name="a">
 /// the first (also known as old or pre-image) sequence. Edits
 /// returned by this algorithm will reference indexes using the
 /// 'A' side:
 /// <see cref="Edit.GetBeginA()">Edit.GetBeginA()</see>
 /// ,
 /// <see cref="Edit.GetEndA()">Edit.GetEndA()</see>
 /// .
 /// </param>
 /// <param name="b">
 /// the second (also known as new or post-image) sequence. Edits
 /// returned by this algorithm will reference indexes using the
 /// 'B' side:
 /// <see cref="Edit.GetBeginB()">Edit.GetBeginB()</see>
 /// ,
 /// <see cref="Edit.GetEndB()">Edit.GetEndB()</see>
 /// .
 /// </param>
 /// <param name="region">the region being compared within the two sequences.</param>
 public abstract void DiffNonCommon <S>(EditList edits, HashedSequenceComparator <S>
                                        cmp, HashedSequence <S> a, HashedSequence <S> b, Edit region) where S : Sequence;