public void Can_BubbleSort_By_Sums_Ascending()
        {
            int[][] actual = new int[8][];

            actual[0] = new int[] { 1, 3, 4, 5, 6 };                            // 19
            actual[1] = new int[] { -7, 7, 3, 7, 7, 12, 4 };                    // 33
            actual[2] = new int[] { 3 };                                        // 3
            actual[3] = new int[] { };
            actual[4] = new int[] { int.MaxValue, int.MinValue, int.MaxValue }; // int.MaxValue
            actual[5] = new int[] { int.MinValue, int.MaxValue, int.MinValue }; // int.MinValue
            actual[6] = null;
            actual[7] = new int[] { 4, 3, 5, 234 };                             // 246

            int[][] expected = new int[8][];

            expected[0] = actual[5];
            expected[1] = actual[2];
            expected[2] = actual[0];
            expected[3] = actual[1];
            expected[4] = actual[7];
            expected[5] = actual[4];
            expected[6] = actual[3];
            expected[7] = actual[6];

            IComparer <int[]> comparer = new SumComparer();

            actual.BubbleSort(comparer);

            CollectionAssert.AreEqual(expected, actual);
        }
        public void Can_BubbleSort_By_Sums_Ascending_Via_Func()
        {
            int[][] actual = new int[8][];

            actual[0] = new int[] { 1, 3, 4, 5, 6 };                            // 19
            actual[1] = new int[] { -7, 7, 3, 7, 7, 12, 4 };                    // 33
            actual[2] = new int[] { 3 };                                        // 3
            actual[3] = new int[] { };
            actual[4] = new int[] { int.MaxValue, int.MinValue, int.MaxValue }; // int.MaxValue
            actual[5] = new int[] { int.MinValue, int.MaxValue, int.MinValue }; // int.MinValue
            actual[6] = null;
            actual[7] = new int[] { 4, 3, 5, 234 };                             // 246

            int[][] expected = new int[8][];

            expected[0] = actual[5];
            expected[1] = actual[2];
            expected[2] = actual[0];
            expected[3] = actual[1];
            expected[4] = actual[7];
            expected[5] = actual[4];
            expected[6] = actual[3];
            expected[7] = actual[6];

            // call delegate method with interface method
            IComparer <int[]> comparer = new SumComparer();

            actual.BubbleSortFunc((a, b) => comparer.Compare(a, b));

            CollectionAssert.AreEqual(expected, actual);
        }
 public void Test_SortWthInterface_KeyMax()
 {
     double[][] a = new double[3][];
     a[0] = new double[2] { 2, 2 };
     a[1] = new double[2] { 3, 3 };
     a[2] = new double[3] { 1, 1, 1 };
     IComparer<Double[]> comparer = new SumComparer();
     SortClass.SortWithInterfaces(a, comparer);
     CollectionAssert.AreEqual(a[0], new double[3] { 1, 1, 1 });
     CollectionAssert.AreEqual(a[1], new double[2] { 2, 2 });
     CollectionAssert.AreEqual(a[2], new double[2] { 3, 3 });
 }
Exemplo n.º 4
0
        public void Sort_SumCompareAscDelegate_SuccessfulTests()
        {
            var comparer = new SumComparer();

            for (int i = 0; i < _sumSourceData.Length; i++)
            {
                Sorter.Sort(_sumSourceData[i], comparer.Compare);

                if (!MatrixAreEquals(_sumSourceData[i], _resultSum[i]))
                {
                    Assert.Fail($"Arrays of {i} index don't equal! ");
                }
            }
        }
        public void Sort_SumCompareAsc_SuccessfulTests()
        {
            IComparer <int[]> compare = new SumComparer();

            for (int i = 0; i < _sumSourceData.Length; i++)
            {
                Task1.AnotherSorter.Sort(_sumSourceData[i], compare);

                if (!MatrixAreEquals(_sumSourceData[i], _resultSum[i]))
                {
                    Assert.Fail($"Arrays of {i} index don't equal! ");
                }
            }
        }
Exemplo n.º 6
0
        public void Sort_SumCompareDec_SuccessfulTests()
        {
            IComparer <int[]> compare = new SumComparer(false);

            foreach (var subarray in _resultSum)
            {
                Array.Reverse(subarray);
            }

            for (int i = 0; i < _sumSourceData.Length; i++)
            {
                Sorter.Sort(_sumSourceData[i], compare);

                if (!MatrixAreEquals(_sumSourceData[i], _resultSum[i]))
                {
                    Assert.Fail($"Arrays of {i} index don't equal!");
                }
            }
        }
        public bool SortArrayStringsTest_SumComparing()
        {
            int[][] inputArray = new int[][] 
            {
                new int[] {1, 0, 8, 0},
                new int[] {-3, 10, 16},
                new int[] {6, 2, 5}
            };
            int[][] outputArray = new int[][] 
            {
                new int[] {1, 0, 8, 0},
                new int[] {6, 2, 5},
                new int[] {-3, 10, 16}
            };

            IComparer<int[]> comparer = new SumComparer();
            JaggedArray.SortArrayStrings(inputArray, comparer);

            return Equals(inputArray, outputArray);
        }
        /// <summary>
        /// Sort jagged array by sum of values
        /// </summary>
        /// <param name="jaggedArr">Array of arrays of integers</param>
        /// <param name="desc">Descending order</param>
        /// <exception cref="ArgumentNullException">
        /// Thrown when jagged array or item of jagged array or parameter is null
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Thrown when jagged array or item of jagged array is empty
        /// </exception>
        public static void BubbleSortBySum(int[][] jaggedArr, bool desc = false)
        {
            Func <int[], int[], int> compare = new SumComparer().Compare;

            BubbleSort(jaggedArr, (IComparer <int[]>)compare.Target, desc);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Sort jagged array by sum of values
        /// </summary>
        /// <param name="jaggedArr">Array of arrays of integers</param>
        /// <param name="desc">Descending order</param>
        /// <exception cref="ArgumentNullException">
        /// Thrown when jagged array or item of jagged array or parameter is null
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Thrown when jagged array or item of jagged array is empty
        /// </exception>
        public static void BubbleSortBySum(int[][] jaggedArr, bool desc = false)
        {
            var comparer = new SumComparer();

            BubbleSort(jaggedArr, comparer.Compare, desc);
        }