public void LevenshteinCorrelater_BenchmarkTwoVeryLongStrings()
        {
            var string1 = Utils.GetLongString(LENGTH);
            var string2 = Utils.GetLongString(LENGTH);

            correlater.Correlate(string1, string2);
        }
        public void MyersAlgorithmCorrelater_BenchmarkTwoVeryLongStrings()
        {
            var string1 = Utils.GetLongString(LENGTH);
            var string2 = Utils.GetLongString(LENGTH);

            correlater.Correlate(string1, string2);
        }
        public void SameString_CallInnderCorrelaterWithEmptyCollection()
        {
            var s = "abcdefg";

            A.CallTo(() => innerCorrelater.Correlate(A <IEnumerable <char> > ._, A <IEnumerable <char> > ._, A <CancellationToken> ._)).
            Invokes((IEnumerable <char> collection1, IEnumerable <char> collection2, CancellationToken ct) =>
            {
                Assert.IsTrue(!collection1.Any(), $"{nameof(collection1)} was not empty");
                Assert.IsTrue(!collection2.Any(), $"{nameof(collection2)} was not empty");
            });
            correlater.Correlate(s, s);
        }
Exemplo n.º 4
0
        private List <Task <CorrelaterResult <T> > > Map(ICollectionWrapper <T> collection1, ICollectionWrapper <T> collection2, CancellationToken cancellationToken)
        {
            var resultTasks = new List <Task <CorrelaterResult <T> > >();

            for (int i = 0; i < Math.Max(collection1.Length, collection2.Length); i = i + chunkSize)
            {
                var wrappedCollection1 = new OffsetCollectionWrapper <T>(collection1, Math.Min(collection1.Length, i), Math.Min(collection1.Length, i + chunkSize));
                var wrappedCollection2 = new OffsetCollectionWrapper <T>(collection2, Math.Min(collection2.Length, i), Math.Min(collection2.Length, i + chunkSize));
                resultTasks.Add(Task.Run(() => innerCorrelater.Correlate(wrappedCollection1, wrappedCollection2, cancellationToken)));
            }

            return(resultTasks);
        }
Exemplo n.º 5
0
        public void SplitToChunksCorrelater_BenchmarkTwoVeryLongStrings()
        {
            var string1 = Utils.GetLongString(LENGTH);
            var string2 = Utils.GetLongString(LENGTH);

            correlater.Correlate(string1, string2);
        }
Exemplo n.º 6
0
        public void HuntSzymanskiCorrelater_BenchmarkTwoVeryBigAlomostSameCollections()
        {
            var collection1 = Utils.GetBigCollection(LENGTH);
            var collection2 = collection1.ToList();

            collection2[500] = collection1.ElementAt(500) == 'a' ? 'b' : 'a';

            correlater.Correlate(collection1, collection2);
        }
Exemplo n.º 7
0
        private void AssertResultsForAllCorrelatersAreTheSame(IEnumerable <char> collection1, IEnumerable <char> collection2)
        {
            var huntResult    = huntSzymanskiCorrelater.Correlate(collection1, collection2);
            var myersResult   = myersAlgorithmCorrelater.Correlate(collection1, collection2);
            var dynamicResult = dynamicLcsCorrelater.Correlate(collection1, collection2);

            AssertThatDistanceIsTheSame(nameof(dynamicResult), nameof(huntResult), dynamicResult, huntResult, string.Join("", collection1), string.Join("", collection2));
            AssertThatDistanceIsTheSame(nameof(dynamicResult), nameof(myersResult), dynamicResult, myersResult, string.Join("", collection1), string.Join("", collection2));

            AssertThatLcsIsTheSameLength(nameof(dynamicResult), nameof(huntResult), dynamicResult, huntResult, string.Join("", collection1), string.Join("", collection2));
            AssertThatLcsIsTheSameLength(nameof(dynamicResult), nameof(myersResult), dynamicResult, myersResult, string.Join("", collection1), string.Join("", collection2));
        }
Exemplo n.º 8
0
        public static void AssertProgressUpdateWasCalledRightNumberOfTimes <T>(this ICorrelater <T> correlater, T[] array1, T[] array2, int expectedProgress)
        {
            var progressUpdates = 0;

            correlater.OnProgressUpdate += (progress, outOf) =>
            {
                Assert.AreEqual(expectedProgress, outOf);
                Interlocked.Increment(ref progressUpdates);
                Assert.AreEqual(progressUpdates, progress);
            };
            correlater.Correlate(array1, array2);

            Assert.AreEqual(expectedProgress, progressUpdates);
        }
Exemplo n.º 9
0
 public static void AssetThrowsNullElementException <T>(this ICorrelater <T> correlater,
                                                        IEnumerable <T> collection1, IEnumerable <T> collection2, string badCollectionName, int nullIndex)
 {
     try
     {
         correlater.Correlate(collection1, collection2);
         Assert.Fail("Exception wasn't thrown");
     }
     catch (NullElementException e)
     {
         Assert.IsTrue(e.Message.Contains(badCollectionName));
         Assert.IsTrue(e.Message.Contains($"index {nullIndex}"));
     }
 }
        public void PatienceDiffCorrelaterCorrelater_BenchmarkTwoVeryLongStrings()
        {
            var string1 = Utils.GetLongString(LENGTH);
            var string2 = Utils.GetLongString(LENGTH);

            var asciiCharCode = 1;

            for (var i = 1000; i < string1.Length; i += 1002, asciiCharCode++)
            {
                string1 = string1.Insert(i, ((char)asciiCharCode).ToString());
                string2 = string2.Insert(i, ((char)asciiCharCode).ToString());
            }

            correlater.Correlate(string1, string2);
        }
Exemplo n.º 11
0
        public static void AsseertCancellationTokenWorks <T>(this ICorrelater <T> correlater, IEnumerable <T> collection1, IEnumerable <T> collection2)
        {
            try
            {
                var cancellationTokenSource = new CancellationTokenSource();
                var task = Task.Run(() => correlater.Correlate(collection1, collection2, cancellationTokenSource.Token));
                cancellationTokenSource.Cancel();
                task.Wait(TimeSpan.FromSeconds(1));
                Assert.Fail("The task doesn't seem to have stopped");
            }
            catch (Exception e) when(e is AggregateException)
            {
                var innerException = e.InnerException;

                while (innerException is AggregateException aggregateException)
                {
                    innerException = aggregateException.InnerException;
                }
                Assert.AreEqual(typeof(OperationCanceledException), innerException.GetType());
            }
        }
Exemplo n.º 12
0
        public CorrelaterResult <T> Correlate(IEnumerable <T> collection1, IEnumerable <T> collection2, CancellationToken cancellationToken = default)
        {
            var collection1Wrapper = collection1.ToCollectionWrapper();
            var collection2Wrapper = collection2.ToCollectionWrapper();

            var startIndex = GetFirstNotEqualIndex(collection1Wrapper, collection2Wrapper, cancellationToken);
            var endIndexes = GetLastNotEqualIndexes(collection1Wrapper, collection2Wrapper, startIndex, cancellationToken);

            if (startIndex > 0)
            {
                OnResultUpdate?.Invoke(new CorrelaterResult <T>(0, collection1Wrapper.Take(startIndex).ToArray(), collection2Wrapper.Take(startIndex).ToArray()));
            }

            var innerCorrelaterResult = innerCorrelater.Correlate(
                new OffsetCollectionWrapper <T>(collection1Wrapper, startIndex, endIndexes.Item1),
                new OffsetCollectionWrapper <T>(collection2Wrapper, startIndex, endIndexes.Item2),
                cancellationToken);

            var result = CreateResult(collection1Wrapper, collection2Wrapper, startIndex, endIndexes, innerCorrelaterResult);

            UpdateEndOfResult(startIndex, result, collection1Wrapper, endIndexes.Item1);

            return(result);
        }
Exemplo n.º 13
0
 public static void SetToRetrun <T>(this ICorrelater <T> correlater, Func <IEnumerable <T>, IEnumerable <T> > func1, Func <IEnumerable <T>, IEnumerable <T> > func2, int distance = 0)
 {
     A.CallTo(() => correlater.Correlate(A <IEnumerable <T> > ._, A <IEnumerable <T> > ._, A <CancellationToken> ._))
     .ReturnsLazily((IEnumerable <T> c1, IEnumerable <T> c2, CancellationToken ct) =>
                    new CorrelaterResult <T>(distance, func1(c1).ToArray(), func2(c2).ToArray()));
 }
Exemplo n.º 14
0
 public static void SetToRetrunInputCollection <T>(this ICorrelater <T> correlater, int distance)
 {
     A.CallTo(() => correlater.Correlate(A <IEnumerable <T> > ._, A <IEnumerable <T> > ._, A <CancellationToken> ._))
     .ReturnsLazily((IEnumerable <T> c1, IEnumerable <T> c2, CancellationToken ct) =>
                    new CorrelaterResult <T>(distance, c1.ToArray(), c2.ToArray()));
 }
Exemplo n.º 15
0
 public CorrelaterResult <T> Correlate(IEnumerable <T> collection1, IEnumerable <T> collection2, CancellationToken cancellationToken = default)
 {
     Thread.Sleep(sleepTime);
     return(innerCorrelater.Correlate(collection1, collection2));
 }
Exemplo n.º 16
0
        public static void AssertComparision <T>(this ICorrelater <T> correlater, IEnumerable <T> collection1, IEnumerable <T> collection2, CorrelaterResult <T> expectedResult)
        {
            var result = correlater.Correlate(collection1, collection2);

            AssertResultIsAsExpected(expectedResult, result);
        }