Пример #1
0
        public void Constructor_NullComparer_ThrowsArgumentNullException()
        {
            var collection1 = new List <string>();
            var collection2 = new List <string>();
            EqualityComparer <string> comparer = null;

            Assert.Throws <ArgumentNullException>(() => Diff2.Compare(collection1, collection2, comparer));
        }
Пример #2
0
        public void SimpleDiff_ProducesCorrectResults()
        {
            const string text1 = "This is a test of the diff implementation, with some text that is deleted.";
            const string text2 = "This is another test of the same implementation, with some more text.";

            Diff2Change[] diff = Diff2.Compare(text1, text2).ToArray();

            CollectionAssert.AreEqual(diff, new[]
            {
                new Diff2Change(true, 9, 9),   // same        "This is a"
                new Diff2Change(false, 0, 6),  // add        "nother"
                new Diff2Change(true, 13, 13), // same      " test of the "
                new Diff2Change(false, 4, 4),  // replace    "same" with "diff"
                new Diff2Change(true, 27, 27), // same      " implementation, with some "
                new Diff2Change(false, 0, 5),  // add        "more "
                new Diff2Change(true, 4, 4),   // same        "text"
                new Diff2Change(false, 16, 0), // delete    " that is deleted"
                new Diff2Change(true, 1, 1),   // same        "."
            });
        }
Пример #3
0
        private DataMatch MatchValues(DataVisitNode node1Parent, List <IDataVisitNode> nodes1, DataVisitNode node2Parent, List <IDataVisitNode> nodes2, bool expectSameCount)
        {
            if (nodes1 == null || nodes2 == null || (expectSameCount && nodes1.Count != nodes2.Count))
            {
                return(ReferenceEquals(nodes1, nodes2) ? DataMatch.Empty : UnMatched(node1Parent, node2Parent));
            }

            var match = new DataMatch();

            if (expectSameCount)
            {
                for (int i = 0; i < nodes1.Count; i++)
                {
                    match += Match((DataVisitNode)nodes1[i], (DataVisitNode)nodes2[i]);
                }
            }
            else
            {
                var alignedDiffs = Diff2.CompareAndAlign(nodes1, nodes2, modelNodeComparer, modelNodeSimilarityComparer, modelNodeCanAlign).ToList();
                foreach (var alignedDiffChange in alignedDiffs)
                {
                    switch (alignedDiffChange.Change)
                    {
                    case ChangeType.Same:
                    case ChangeType.Changed:
                        match += Match((DataVisitNode)nodes1[alignedDiffChange.Index1], (DataVisitNode)nodes2[alignedDiffChange.Index2]);
                        break;

                    case ChangeType.Added:
                        match += new DataMatch(0, nodes2[alignedDiffChange.Index2].CountNodes());
                        break;

                    case ChangeType.Deleted:
                        match += new DataMatch(0, nodes1[alignedDiffChange.Index1].CountNodes());
                        break;
                    }
                }
            }

            return(match);
        }
Пример #4
0
        //-----------------------------------------------------------------------
        //-----------------------------------------------------------------------
        static void Main()
        {
            Console.WriteLine("----------------   ETAP 1   ------------------------");
            //postaæ: wielomianu, wielomianu pochodnej1 oraz wielomianu pochodnej2

            double[] w = { -10, 5, -5, 1 };
            Pol      P = new Pol(w);

            w[0] = 4;
            w[2] = -2;
            Pol Q = new Pol(w);

            // wypisac wielomian P i jego pochodne
            Console.WriteLine(" P(x) = " + P);
            Console.WriteLine("P1(x) = " + P.Diff1());
            Console.WriteLine("P2(x) = " + P.Diff2());

            Console.WriteLine();
            // wypisac wielomian Q i jego pochodne
            Console.WriteLine(" Q(x) = " + Q);
            Console.WriteLine("Q1(x) = " + Q.Diff1());
            Console.WriteLine("Q2(x) = " + Q.Diff2());

            Console.WriteLine();
            Console.WriteLine("----------------   ETAP 2   ------------------------");
            //wartoœci dla:
            //. wielomian,
            //. pochodna1 (dok³adna i przybli¿ona)
            //. pochodna2 (dok³adna i przybli¿ona)

            // wypisac wartosci wielomianu P i jego pochodnych
            // (dokladnych i przyblizonych) dla x=2
            double x = 2;

            Console.WriteLine("  P(2) = " + P.Horner(2));
            Console.WriteLine(" P1(2) = " + P.Diff1().Horner(2));
            Console.WriteLine("~P1(2) = " + Diff1(P.Horner)(2));
            Console.WriteLine(" P2(2) = " + P.Diff2().Horner(2));
            Console.WriteLine("~P2(2) = " + Diff2(P.Horner)(2));



            Console.WriteLine();
            Console.WriteLine("----------------   ETAP 3   ------------------------");
            //zastosowania algorytmów: Bisekcja oraz Newton do znalezienia minimum funkcji
            //(przedzia³y i funkcje s¹ tak ustalone, ¿eby nie by³o problemów numerycznych)

            double   a = 2, b = 4;
            double   m_Bisection, m_Newton;
            Function p1, p2;

            p1          = P.Diff1().Horner;
            m_Bisection = Bisection(p1, a, b);

            p1       = Diff1(P.Horner);
            p2       = Diff2(P.Horner);
            m_Newton = Newton(p1, p2, a);

            // wypisac znalezione minima wielomianu P wykorzystuj¹c metody bisekcji i Newtona
            // (oraz dokladne i przyblizone pochodne)

            Console.WriteLine("m_Bisection = " + m_Bisection);
            Console.WriteLine(" P1(m_Bisection) = " + P.Diff1().Horner(m_Bisection));
            Console.WriteLine("~P1(m_Bisection) = " + Diff1(P.Horner)(m_Bisection));

            Console.WriteLine("   m_Newton = " + m_Newton);
            Console.WriteLine(" P1(m_Newton) = " + P.Diff1().Horner(m_Newton));
            Console.WriteLine("~P1(m_Newton) = " + Diff1(P.Horner)(m_Newton));



            Console.WriteLine();
            Console.WriteLine("----------------   ETAP 4   ------------------------");
            //sumy wielomianów

            Function f;
            Pol      wiel;

            double[] w1    = { 0, 0, 0, 1 };
            Pol      wiel1 = new Pol(w1);

            double[] w2    = { 0, 0, -5 };
            Pol      wiel2 = new Pol(w2);

            double[] w3    = { -10, 5 };
            Pol      wiel3 = new Pol(w3);

            wiel = wiel1 + wiel2 + wiel3;
            f    = wiel.Diff1().Horner;

            Console.WriteLine("suma operator bisekcja: " + Bisection(f, a, b));

            f = Suma(Diff1(wiel1.Horner), Diff1(wiel2.Horner), Diff1(wiel3.Horner));
            Console.WriteLine("suma funkcja bisekcja: " + Bisection(f, a, b));

            Console.WriteLine();
            Console.WriteLine("----------------   ETAP 5   ------------------------");
            //funkcje anonimowe oraz wyr. lambda

            a = 0;
            b = 4;

            f = delegate(double t) { return(2 - 4 * t + Math.Exp(t)); };
            Console.WriteLine("metoda anonimowa bisekcja: " + Bisection(Diff1(f), a, b));
            Console.WriteLine("  metoda anonimowa newton: " + Newton(Diff1(f), Diff2(f), a));

            f = t => (2 - 4 * t + Math.Exp(t));
            Console.WriteLine("wyrazenie lambda bisekcja: " + Bisection(Diff1(f), a, b));
            Console.WriteLine("  wyrazenie lambda newton: " + Newton(Diff1(f), Diff2(f), a));

            f = Suma(t => (2 - 4 * t), t => Math.Exp(t));
            Console.WriteLine("wyrazenie lambda bisekcja: " + Bisection(Diff1(f), a, b));
            Console.WriteLine("  wyrazenie lambda newton: " + Newton(Diff1(f), Diff2(f), a));
        }