public void SortHandler_Handle_SequenceHasNumbers_ReturnsSortResultWithExpectedValues() { IStringToCollectionParser <decimal> parser = new StubIStringToCollectionParser <decimal>() { ParseStringToCollectionString = (strSeq) => new decimal[] { 2, 1, 3 } }; IStepCounter stepCounter = new StubIStepCounter() { CountSwapOperation = () => { }, CountCompareOperation = () => { } }; ISortStrategyFactory factory = new StubISortStrategyFactory() { CreateSortSortAlgorithmEnumSortTypeEnumIStepCounter = (alg, type, counter) => new StubISortStrategy() { SortIEnumerableOfDecimal = (seq) => new StubISortResult() { CompareOperationsCountGet = () => 2, SwapOperationsCountGet = () => 1, SortedNumbersGet = () => new decimal[] { 1, 2, 3 } } } }; decimal[] expectedSequence = new decimal[] { 1, 2, 3 }; int expectedCompareCount = 2; int expectedSwapCount = 1; var handler = new SortHandler(parser, factory); ISortResult sortResult = handler.Handle("2 1 3", SortAlgorithmEnum.InsertionSort, SortTypeEnum.Ascending, stepCounter); Assert.AreEqual(expectedCompareCount, sortResult.CompareOperationsCount, "Comapare operations are not equal"); Assert.AreEqual(expectedSwapCount, sortResult.SwapOperationsCount, "Swap counts are not equal"); CollectionAssert.AreEqual(expectedSequence, sortResult.SortedNumbers.ToArray(), "Sequences are not equal."); }
public void Sort(string sequence, SortAlgorithmEnum sortAlgorithm, SortTypeEnum sortType) { this.errorMessage = string.Empty; try { this.sortResult = this.sortHandler.Handle(sequence, sortAlgorithm, sortType, new StepCounter()); } catch (ValidationException) { errorMessage = "Not validating sequence"; } this.OnPropertyChanged(string.Empty); }
public ISortResult Handle(string sequence, SortAlgorithmEnum sortAlgorithm, SortTypeEnum sortType, IStepCounter stepCounter) { if (sequence == null) { throw new ArgumentNullException(nameof(sequence)); } if (stepCounter == null) { throw new ArgumentNullException(nameof(sequence)); } IEnumerable <decimal> numbersSequence = this.stringToDecimalCollectionParser.ParseStringToCollection(sequence); ISortResult sortResult = this.sortStrategyFactory.CreateSort(sortAlgorithm, sortType, stepCounter).Sort(numbersSequence); return(sortResult); }
public void ReorderChildren(IDsmElement element, ISortResult sortResult) { DsmElement parent = element as DsmElement; if (parent != null) { List <IDsmElement> clonedChildren = new List <IDsmElement>(parent.Children); foreach (IDsmElement child in clonedChildren) { parent.RemoveChild(child); } for (int i = 0; i < sortResult.GetNumberOfElements(); i++) { parent.AddChild(clonedChildren[sortResult.GetIndex(i)]); } } }
public void SortStrategy_Sort_SequenceIsEmpty_ReturnEmptySequence() { ISortType sortType = new StubISortType() { UpdateIEnumerableOfDecimal = (notUpdatedSequence) => notUpdatedSequence }; IStepCounter stepCounter = new StubIStepCounter() { CountSwapOperation = () => { }, CountCompareOperation = () => { } }; var sortStrategy = GetSortStrategy(sortType, stepCounter); var expectedSequence = new decimal[1]; ISortResult sortResult = sortStrategy.Sort((new decimal[1])); CollectionAssert.AreEqual(expectedSequence, sortResult.SortedNumbers.ToArray()); }
public void ReorderChildren(IDsmElement element, ISortResult sortResult) { _elementsDataModel.ReorderChildren(element, sortResult); }