Exemplo n.º 1
0
        public void Uneven()
        {
            var players = new[] { 1, 10, 20, 3, 4, 7, 6, 5, 11, 13, 12, 14, 17, 16, 2, 19, 8, 9, 15, 18 };

            Sorter <int> sorter = new QuickSorter();

            sorter.Sort(players);

            // Assert
            int bestOfWeakest = 0;

            var half = players.GetLength(0) / 2 - 1;

            for (int i = 0; i < half; i++)
            {
                if (players[i] > bestOfWeakest)
                {
                    bestOfWeakest = players[i];
                }
            }

            for (int i = half + 1; i < players.GetLength(0) - 1; i++)
            {
                Assert.IsTrue(players[i] >= bestOfWeakest);
            }
        }
Exemplo n.º 2
0
        public void When_sorting(int[] input, int[] answer)
        {
            var sorter = new QuickSorter <int>();
            var sorted = sorter.Sort(input);

            Assert.Equal(sorted, answer);
        }
        public IChromosome GetSample()
        {
            //TODO: Unit tests!
            var length    = Variances.Length;
            var positions = new List <PositionValuePair <double> >();

            for (int i = 0; i < length; i++)
            {
                var mean      = Variances[i].Mean;
                var deviation = Variances[i].Deviation;

                var position = Random.GetGaussian(mean, deviation);
                positions.Add(new PositionValuePair <double>(position, i));
            }

            var quickSorter = new QuickSorter <PositionValuePair <double> >();

            quickSorter.Sort(positions, PositionValuePair <double> .ComparerByPosition.Instance, 0, length - 1);

            var genes = new int[length];

            for (int i = 0; i < length; i++)
            {
                genes[i] = positions[i].Value;
            }

            return(new PermutationChromosome(genes));
        }
Exemplo n.º 4
0
        public void ThrowInvalidOperationException_WhenCollectionIsEmpty()
        {
            var collection = new List <int>();
            var sorter     = new QuickSorter <int>();

            Assert.Throws <InvalidOperationException>(() => sorter.Sort(collection));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Sorts the specified list.
        /// </summary>
        /// <typeparam name="T">The type of item in the list.</typeparam>
        /// <param name="list">The list.</param>
        /// <param name="sortOrder">The order in which to sort the list.</param>
        public static void Sort <T>(this IList <T> list, SortOrder sortOrder)
        {
            Guard.ArgumentNotNull(list, "list");

            var sorter = new QuickSorter <T>();

            sorter.Sort(list, sortOrder);
        }
Exemplo n.º 6
0
        public void PassingOneItemCollection()
        {
            List<int> numbers = new List<int>() {1};
            QuickSorter<int> sorter = new QuickSorter<int>(numbers);
            List<int> sortedList = new List<int>(sorter.Sort());

            Assert.AreEqual(numbers[0], sortedList[0]);
        }
Exemplo n.º 7
0
        public void NaiveQuickSorter_SortStringsDescending_SortsCorrectly(IList <string> actual)
        {
            var sorter = new QuickSorter <string>();

            sorter.Sort(actual, SortOrder.Descending);

            SortAssert.IsSortedDescending(actual, Comparer <string> .Default);
        }
Exemplo n.º 8
0
        public static void Main()
        {
            List<int> numbers = new List<int>() { 1,3,4,5,3,5,7,8,4,3,23,2 };
            QuickSorter<int> quickSorter = new QuickSorter<int>(numbers);
            numbers = quickSorter.Sort();

            Console.WriteLine(string.Join(",", numbers));
        }
Exemplo n.º 9
0
        public void Sort_NullArray_ReturnArgumentException()
        {
            int[] array = null;

            var sorter = new QuickSorter <int>();

            sorter.Sort(array);
        }
Exemplo n.º 10
0
        public void QuickSortingTest(int[] heap, int[] expectedArray)
        {
            ISortingService sorter = new QuickSorter();

            sorter.Sort(heap);

            Assert.Equal(heap, expectedArray);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Sorts the specified list.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list">The list.</param>
        /// <param name="comparison">The comparison.</param>
        /// <param name="sortOrder">The order in which to sort the list.</param>
        public static void Sort <T>(this IList <T> list, Comparison <T> comparison, SortOrder sortOrder)
        {
            Guard.ArgumentNotNull(list, "list");
            Guard.ArgumentNotNull(comparison, "comparison");

            var sorter = new QuickSorter <T>();

            sorter.Sort(list, comparison, sortOrder);
        }
 public void Sort_hugeArrayTest()
 {
     int[] unsortedArray_1 = GenerateArray();
     int[] unsortedArray_2 = new int[1000];
     unsortedArray_1.CopyTo(unsortedArray_2, 0);
     Array.Sort(unsortedArray_1);
     QuickSorter.Sort(unsortedArray_2);
     CollectionAssert.AreEqual(unsortedArray_1, unsortedArray_2);
 }
Exemplo n.º 13
0
        public void SortEmptyTest()
        {
            List <int>        testCollection  = new List <int>();
            QuickSorter <int> testQuickSorter = new QuickSorter <int>();

            testQuickSorter.Sort(testCollection);

            Assert.AreEqual(testCollection.Count, 0);
        }
Exemplo n.º 14
0
        private void PopulateInvokerTree(
            RunInvokerTree tree,
            RunInvokerVertex parent,
            Type t
            )
        {
            // Try and find setup method
            MethodRunInvoker setup = null;

            if (TypeHelper.HasMethodCustomAttribute(t, typeof(SetUpAttribute)))
            {
                setup = new MethodRunInvoker(this, TypeHelper.GetAttributedMethod(t, typeof(SetUpAttribute)));
            }

            // Gather all methods, with order.
            ArrayList methods = new ArrayList();

            foreach (MethodInfo mi in
                     TypeHelper.GetAttributedMethods(t, this.AttributeType))
            {
                // get sequence attribute
                TestSequenceAttribute seq =
                    (TestSequenceAttribute)TypeHelper.GetFirstCustomAttribute(mi, typeof(TestSequenceAttribute));
                methods.Add(new OrderedMethod(mi, seq.Order));
            }

            // sort the methods
            QuickSorter sorter = new QuickSorter();

            sorter.Sort(methods);

            // Try and find teardown method
            MethodRunInvoker teardown = null;

            if (TypeHelper.HasMethodCustomAttribute(t, typeof(TearDownAttribute)))
            {
                teardown = new MethodRunInvoker(this, TypeHelper.GetAttributedMethod(t, typeof(TearDownAttribute)));
            }

            // populate execution tree.
            RunInvokerVertex child = parent;

            foreach (OrderedMethod om in methods)
            {
                if (setup != null)
                {
                    child = tree.AddChild(child, setup);
                }

                child = tree.AddChild(child, InstanceInvoker(om.Method));

                if (teardown != null)
                {
                    child = tree.AddChild(child, teardown);
                }
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// Sorts the specified list.
        /// </summary>
        /// <typeparam name="T">The type of item in the list.</typeparam>
        /// <param name="list">The list.</param>
        /// <param name="comparer">The comparer.</param>
        public static void Sort <T>(this IList <T> list, IComparer <T> comparer)
        {
            Guard.ArgumentNotNull(list, "list");
            Guard.ArgumentNotNull(comparer, "comparer");

            var sorter = new QuickSorter <T>();

            sorter.Sort(list, comparer);
        }
Exemplo n.º 16
0
        static void Main(string[] args)
        {
            int[] a = { 13, 88, 37, 44, 69, 74, 59, 23 };
            QuickSorter.Sort(a, 0, a.Length - 1);

            foreach (int element in a)
            {
                Console.Write(element + " ");
            }
        }
Exemplo n.º 17
0
        public void Sort_StringArray_ReturnSortedArray()
        {
            string[] array    = { "fgh", "a", "", "b", "fga", "" };
            string[] expected = { "", "", "a", "b", "fga", "fgh" };

            var sorter = new QuickSorter <string>();

            sorter.Sort(array);

            CollectionAssert.AreEqual(array, expected);
        }
Exemplo n.º 18
0
        public void ShouldSortNegativeNumbersCorrectly()
        {
            var array       = new int[] { -103, -166, -42, -3, -1 };
            var mergeSorter = new QuickSorter <int>();

            mergeSorter.Sort(array);

            var expected = new int[] { -166, -103, -42, -3, -1 };

            CollectionAssert.AreEqual(expected, array);
        }
Exemplo n.º 19
0
        public void ShouldSortCharactersCorrectly()
        {
            var array       = new char[] { 'a', 'c', 'b', 'z', 'a' };
            var mergeSorter = new QuickSorter <char>();

            mergeSorter.Sort(array);

            var expected = new char[] { 'a', 'a', 'b', 'c', 'z' };

            CollectionAssert.AreEqual(expected, array);
        }
Exemplo n.º 20
0
        public void Sort_IntArray_ReturnSortedArray()
        {
            int[] array    = { 1, 6, 45, 6, 5, 35 };
            int[] expected = { 1, 5, 6, 6, 35, 45 };

            var sorter = new QuickSorter <int>();

            sorter.Sort(array);

            CollectionAssert.AreEqual(array, expected);
        }
Exemplo n.º 21
0
        public void NaiveQuickSorter_EmptyList_RemainsEmptyList()
        {
            var expected = new List <string>();
            var sorter   = new QuickSorter <string>();

            var actual = new List <string>();

            sorter.Sort(actual);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 22
0
        public void PassingSortedItemCollection()
        {
            List<int> sortedNumbers = new List<int>() { 1, 1, 2, 2, 2, 3, 4, 5, 5, 7, 8, 9 };
            QuickSorter<int> sorter = new QuickSorter<int>(sortedNumbers);
            List<int> sortedList = new List<int>(sorter.Sort());

            for (int i = 0; i < sortedNumbers.Count; i++)
            {
                Assert.AreEqual(sortedNumbers[i], sortedList[i]);
            }
        }
Exemplo n.º 23
0
        public void ShouldSortNumbersCorrectly()
        {
            var array       = new int[] { 1, 4, 100, -3, 1 };
            var mergeSorter = new QuickSorter <int>();

            mergeSorter.Sort(array);

            var expected = new int[] { -3, 1, 1, 4, 100 };

            CollectionAssert.AreEqual(expected, array);
        }
Exemplo n.º 24
0
        public void SortElementsCorrectly_WhenValidArgumentsArePassed <T>(IList <T> collection)
            where T : IComparable <T>
        {
            var expected = collection.Select(x => x).ToList();

            expected.Sort();
            var sorter = new QuickSorter <T>();

            sorter.Sort(collection);

            CollectionAssert.AreEqual(expected, collection);
        }
Exemplo n.º 25
0
        public static void Main()
        {
            var elements        = new int[] { 3, 4, 18, 22, 11, 44, 33, 0, -1 };
            var selectionSorter = new SelectionSorter <int>();
            var quickSorter     = new QuickSorter <int>();
            var mergeSorter     = new MergeSorter <int>();

            //selectionSorter.Sort(elements);
            quickSorter.Sort(elements);
            //mergeSorter.Sort(elements);

            System.Console.WriteLine(string.Join(", ", elements));
        }
Exemplo n.º 26
0
        public void QuickSorter_Sort_PassNullCollection_ThrowArgumentNullException()
        {
            //
            // Arrange.
            //
            double[] array       = null;
            var      quickSorter = new QuickSorter <double>();

            //
            // Assert.
            //
            Assert.ThrowsException <ArgumentNullException>(() => quickSorter.Sort(array));
        }
Exemplo n.º 27
0
        public void SortSingleTest()
        {
            List <int> testCollection = new List <int>()
            {
                1
            };
            QuickSorter <int> testQuickSorter = new QuickSorter <int>();

            testQuickSorter.Sort(testCollection);

            Assert.AreEqual(testCollection.Count, 1);
            Assert.AreEqual(testCollection[0], 1);
        }
Exemplo n.º 28
0
        public void SortListOrderExample()
        {
            ComparisonSorter<int> sorter = new QuickSorter<int>();

            var list = new List<int> {13, 5, 77, 9, 12};

            sorter.Sort(list, SortOrder.Ascending);

            Assert.AreEqual(77, list[4]);
            Assert.AreEqual(13, list[3]);
            Assert.AreEqual(12, list[2]);
            Assert.AreEqual(9, list[1]);
            Assert.AreEqual(5, list[0]);
        }
Exemplo n.º 29
0
        public void SortListDelegateExample()
        {
            ComparisonSorter<int> sorter = new QuickSorter<int>();

            var list = new List<int> {13, 5, 77, 9, 12};

            sorter.Sort(list, IntComparison);

            Assert.AreEqual(5, list[0]);
            Assert.AreEqual(9, list[1]);
            Assert.AreEqual(12, list[2]);
            Assert.AreEqual(13, list[3]);
            Assert.AreEqual(77, list[4]);
        }
Exemplo n.º 30
0
        public void SortExample()
        {
            var sorter = new QuickSorter<int>();

            var list = new List<int> {13, 5, 77, 9, 12};

            sorter.Sort(list, Comparer<int>.Default);

            Assert.AreEqual(5, list[0]);
            Assert.AreEqual(9, list[1]);
            Assert.AreEqual(12, list[2]);
            Assert.AreEqual(13, list[3]);
            Assert.AreEqual(77, list[4]);
        }
Exemplo n.º 31
0
        /// <summary>
        /// Sorts the specified list.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <param name="sortOrder">The order in which to sort the list.</param>
        /// <param name="property">The target property to use to sort the list.</param>
        public static void Sort <T>(this IList <T> list, Expression <Func <T, IComparable> > property, SortOrder sortOrder)
        {
            Guard.ArgumentNotNull(list, "list");
            Guard.ArgumentNotNull(property, "property");

            var sorter     = new QuickSorter <T>();
            var comparison = new Comparison <T>((x, y) =>
            {
                var compile = property.Compile();
                return(compile(x).CompareTo(compile(y)));
            });

            sorter.Sort(list, comparison, sortOrder);
        }
        public static void ArraySorted([Random(0, 1000, 100, Distinct = true)] int n)
        {
            // Arrange
            var sorter      = new QuickSorter <int>();
            var intComparer = new IntComparer();

            var(correctArray, testArray) = RandomHelper.GetArrays(n);

            // Act
            sorter.Sort(testArray, intComparer);
            Array.Sort(correctArray, intComparer);

            // Assert
            Assert.AreEqual(testArray, correctArray);
        }
        public void Sort_DoesNotChange_OriginalInput()
        {
            //arrange
            const int firstElement = 5, lastElement = 3;
            var       original = new List <int> {
                firstElement, 2, 4, 6, 1, lastElement
            };
            var sorter = new QuickSorter <int>();
            //act
            var sorted = sorter.Sort(original);

            //assert
            original[0].Should().Be(firstElement);
            original[5].Should().Be(lastElement);
        }
Exemplo n.º 34
0
        public static void Main()
        {
            var numbers = NumbersGenerator.GenerateNumberArray(50);

            NumbersGenerator.ShuffleNumbers(numbers);

            Console.WriteLine(string.Join(" ", numbers));

            var sortedNumbers = QuickSorter.Sort(numbers);

            Console.WriteLine(string.Join(" ", sortedNumbers));

            //InsertionSorter.Sort(numbers);
            //Console.WriteLine(string.Join(" ", numbers));
        }
Exemplo n.º 35
0
        public void SortListDelegateExample()
        {
            ComparisonSorter <int> sorter = new QuickSorter <int>();

            var list = new List <int> {
                13, 5, 77, 9, 12
            };

            sorter.Sort(list, IntComparison);

            Assert.AreEqual(5, list[0]);
            Assert.AreEqual(9, list[1]);
            Assert.AreEqual(12, list[2]);
            Assert.AreEqual(13, list[3]);
            Assert.AreEqual(77, list[4]);
        }
Exemplo n.º 36
0
        public void SortListOrderExample()
        {
            ComparisonSorter <int> sorter = new QuickSorter <int>();

            var list = new List <int> {
                13, 5, 77, 9, 12
            };

            sorter.Sort(list, SortOrder.Ascending);

            Assert.AreEqual(77, list[4]);
            Assert.AreEqual(13, list[3]);
            Assert.AreEqual(12, list[2]);
            Assert.AreEqual(9, list[1]);
            Assert.AreEqual(5, list[0]);
        }
Exemplo n.º 37
0
 public void TestQuickSorter()
 {
     int[] s = new int[6];
     s[0] = 3;
     s[1] = 2;
     s[2] = 4;
     s[3] = 1;
     s[4] = 0;
     s[5] = 5;
     QuickSorter<int> ms = new QuickSorter<int>();
     int[] sorted_s = ms.Sort(s);
     Assert.AreEqual(sorted_s[0], 0);
     Assert.AreEqual(sorted_s[1], 1);
     Assert.AreEqual(sorted_s[2], 2);
     Assert.AreEqual(sorted_s[3], 3);
     Assert.AreEqual(sorted_s[4], 4);
     Assert.AreEqual(sorted_s[5], 5);
 }