Exemplo n.º 1
0
    static void Main()
    {
        // Determine how many cores/processors there are on this machine.
        //
        int coreCount = Environment.ProcessorCount;

        Console.WriteLine("*********************************");
        Console.WriteLine("Process/core count = {0}", coreCount);

        // Get some data to work with.
        //
        double[] data = GetData();

        Stopwatch sw = Stopwatch.StartNew();

        // Process the entire data set using one thread
        // for comparison purposes.
        //
        ArrayProcessor wholeArray = new ArrayProcessor(data, 0, data.Length - 1);

        wholeArray.ComputeSum();
        sw.Stop();

        Console.WriteLine(
            "1 thread computed {0:n0} in {1:n0} ms",
            wholeArray.Sum, sw.ElapsedMilliseconds
            );

        Console.ReadKey();
    }
Exemplo n.º 2
0
        public void Test1()
        {
            ArrayProcessor arrayProcessor = new ArrayProcessor();

            int[] testArray = { -10000, 10, 0, 1000, 1000, 5544, -5000, 3322, 7777, 2222, 9999, 120000 };

            // делаем копию массива что передаем, чтобы убедится что после работы программы исходный - не затрагивали
            int[] testArrayCopy = new int[testArray.Length];
            Array.Copy(testArray, testArrayCopy, testArray.Length);

            int[] result = arrayProcessor.SortAndFilter(testArray);

            Assert.Multiple(() =>
            {
                Assert.AreEqual(testArray, testArrayCopy);

                Assert.That(result, Is.Ordered);
                Assert.That(result, Has.All.InRange(1000, 9999));
            });

            testArray = new int[0];
            result    = arrayProcessor.SortAndFilter(testArray);
            Assert.AreEqual(testArray, result);

            testArray = new int[] { 1000, 1000, 1000 };
            result    = arrayProcessor.SortAndFilter(testArray);
            Assert.AreEqual(testArray, result);

            testArray = null;
            Assert.Throws <ArgumentNullException>(() => arrayProcessor.SortAndFilter(testArray));

            Assert.Pass();
        }
Exemplo n.º 3
0
 static void Main(string[] args)
 {
     ConsoleHelper.WriteText($"Greetings!\nThis programm generates the random two-dimensional array and finds summ of even values.\n");
     int[,] array = ArrayGenerator.GenerateTwoDimensional();
     ArrayProcessor.CalculateSummOfEven(array);
     ConsoleHelper.PressAnyKey();
 }
Exemplo n.º 4
0
 static void Main(string[] args)
 {
     ConsoleHelper.WriteText($"Greetings!\nThis programm generates the random one-dimensional array and calculates sum of its non-negative values.\n");
     int[] array = ArrayGenerator.GenerateOneDimensional();
     ArrayProcessor.CalculateNonNegativeSumm(array);
     ConsoleHelper.PressAnyKey();
 }
Exemplo n.º 5
0
 static void Main(string[] args)
 {
     ConsoleHelper.WriteText($"Greetings!\nThis programm generates and sorts the random one-dimensional array and finds it min/max values.\n");
     int [] array = ArrayGenerator.GenerateOneDimensional();
     ArrayProcessor.FindMax(array);
     ArrayProcessor.FindMin(array);
     ArrayProcessor.BubbleSort(array);
     ConsoleHelper.PressAnyKey();
 }
Exemplo n.º 6
0
 static void Main(string[] args)
 {
     ConsoleHelper.WriteText($"Greetings!\nThis programm generates and sorts the random three-dimensional array and replaces all positive values with 0\n");
     int[, ,] array = ArrayGenerator.GenerateThreeDimensional();
     ConsoleHelper.WriteText($"Press any key to replace...");
     ConsoleHelper.PressAnyKey();
     ArrayProcessor.ReplaceAllPositive(array);
     ConsoleHelper.PressAnyKey();
 }
Exemplo n.º 7
0
        public void Delete_repeating_elements()
        {
            int[]          X     = { -5, -5, -5, -5, -5 };
            int[]          Y     = new int[X.Length];
            ArrayProcessor array = new ArrayProcessor();

            Y = array.SortAndFilter(X);
            Assert.AreEqual(-5, Y[0]);
        }
Exemplo n.º 8
0
        public void NoNegatives()
        {
            ArrayProcessor processor = new ArrayProcessor();

            double[] array    = new double[] { 7, 5, 123, -50 };
            double[] newArray = processor.SortAndFilter(array);

            Assert.Throws <InvalidOperationException>(() => newArray.First(item => item < 0));
        }
Exemplo n.º 9
0
        public void AllConditionsTest()
        {
            double[]       a        = { 1, -1, 2, 3, 2, 73, 6, 2 };
            double[]       expected = { 73, 6, 3, 2, 1 };
            ArrayProcessor arr      = new ArrayProcessor();

            double[] actual = arr.SortAndFilter(a);
            CollectionAssert.AreEqual(expected, actual);
        }
Exemplo n.º 10
0
        public void SortTest()
        {
            double[]       a        = { 0, 3, 7, 1 };
            double[]       expected = { 7, 3, 1, 0 };
            ArrayProcessor arr      = new ArrayProcessor();

            double[] actual = arr.SortAndFilter(a);
            CollectionAssert.AreEqual(expected, actual);
        }
Exemplo n.º 11
0
        public void IsOrdered()
        {
            ArrayProcessor processor = new ArrayProcessor();

            double[] array    = new double[] { 7, 5, 123, -50 };
            double[] newArray = processor.SortAndFilter(array);

            CollectionAssert.IsOrdered(newArray);
        }
Exemplo n.º 12
0
        public void ReplacingNegativeNumbersTest()
        {
            double[]       a        = { -76, -3, -1 };
            double[]       expected = { 76, 3, 1 };
            ArrayProcessor arr      = new ArrayProcessor();

            double[] actual = arr.SortAndFilter(a);
            CollectionAssert.AreEqual(expected, actual);
        }
Exemplo n.º 13
0
        public void Sort_elements()
        {
            int[]          X     = { -5, -2, -8, 10, 11, 45 };
            int[]          Y     = new int[X.Length];
            ArrayProcessor array = new ArrayProcessor();

            Y = array.SortAndFilter(X);
            Array.Sort(X);
            CollectionAssert.AreEqual(X, Y);
        }
Exemplo n.º 14
0
        public void NoDuplicates()
        {
            ArrayProcessor processor = new ArrayProcessor();


            double[] array    = new double[] { 7, 5, 123, -50 };
            double[] newArray = processor.SortAndFilter(array);

            CollectionAssert.AllItemsAreUnique(newArray);
        }
Exemplo n.º 15
0
        public void SortAndFilter_originArr_ArrFilteredAndSorted_()
        {
            int[] ororiginArr = new int[] { 0, -2, -5, -7, 9, -7, 12, -5, 4, 44, -2, 8 };
            int[] expectedArr = new int[] { -7, -5, -2, 0, 4, 8, 9, 12, 44 };

            ArrayProcessor arrProc = new ArrayProcessor();

            int[] resultArr = arrProc.SortAndFilter(ororiginArr);

            CollectionAssert.AreEqual(resultArr, expectedArr);
        }
Exemplo n.º 16
0
        public void SortAndFilter_originArr_originArrNotChange_()
        {
            int[] ororiginArr = new int[] { 0, -2, -5, -7, 9, -7, 12, -5, 4, 44, -2, 8 };
            int[] actualArr   = new int[] { 0, -2, -5, -7, 9, -7, 12, -5, 4, 44, -2, 8 };

            ArrayProcessor arrProc = new ArrayProcessor();

            int[] resultArr = arrProc.SortAndFilter(ororiginArr);

            CollectionAssert.AreEqual(ororiginArr, actualArr);
        }
Exemplo n.º 17
0
        public void THeOriginalOneShouldRemainTheSame()
        {
            ArrayProcessor processor = new ArrayProcessor();

            double[] array = new double[] { 7, 5, 123, -50 };
            double[] copy  = new double[] { 7, 5, 123, -50 };

            double[] newArray = processor.SortAndFilter(array);

            CollectionAssert.AreEqual(array, copy);
        }
Exemplo n.º 18
0
        public void CanSortAndFilterArrayAssertCollection()
        {
            //Arrange
            ArrayProcessor ap = new ArrayProcessor();

            int[] tmp = { -3, -10, 9, 9, 8, -3, -10, 4, 5, 1 };
            int[] fin = { -10, -3, 1, 4, 5, 8, 9, 9 };
            //Act
            tmp = ap.SortAndFilter(tmp);
            //Assert
            NUnit.Framework.CollectionAssert.AreEqual(fin, tmp);
        }
Exemplo n.º 19
0
        public void CanSortAndFilterArrayMSUnit()
        {
            //Arrange
            ArrayProcessor ap2 = new ArrayProcessor();

            int[] tmp = { -10, 9, 9, 8, -3, -10, 4, 5, 1 };
            int[] fin = { -10, -3, 1, 4, 5, 8, 9, 9 };
            //Act
            tmp = ap2.SortAndFilter(tmp);
            //Assert
            //Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(fin, tmp);
        }
Exemplo n.º 20
0
        public void Test_elements_Array()
        {
            ArrayProcessor array = new ArrayProcessor();

            int[] X = { -5, -2, -2, -2, -8, -5, 10, 11, 11, 45 };
            int[] B = { -5, -2, -8, 10, 11, 45 };
            int   a = array.SortAndFilter(X).Length;

            int[] Y = new int[a];
            Y = array.SortAndFilter(X);
            Array.Sort(B);
            CollectionAssert.AreEquivalent(B, Y);
        }
Exemplo n.º 21
0
        public void testArrayProccessor()
        {
            //arange
            int[] actual    = { -8, 5443, -20, 4559, 0, -20, 2, -1323, 10000, -1323 };
            int[] excepting = { -1323, -20, -8, 0, 2, 4559, 5443, 10000 };

            //act
            ArrayProcessor arrProc = new ArrayProcessor();

            int[] result = arrProc.SortAndFilter(actual);

            //assert
            CollectionAssert.AreEqual(excepting, result);
        }
Exemplo n.º 22
0
        public void testSameArray()
        {
            int[] actual    = { -8, 5443, -20, 4559, 0, -20, 2, -1323, 10000, -1323 };
            int[] excepting = { -8, 5443, -20, 4559, 0, -20, 2, -1323, 10000, -1323 };

            //act
            ArrayProcessor arrProc = new ArrayProcessor();

            int[] result = arrProc.SortAndFilter(actual);

            //assert
            //Проверяем, то оригинальный массив не меняется
            CollectionAssert.AreEqual(excepting, actual);
        }
Exemplo n.º 23
0
        public static void Main(string[] args)
        {
            var coresCount = Environment.ProcessorCount;
            var threads = new List<Thread>(coresCount);
            var arrayProcessors = new List<ArrayProcessor>(coresCount);

            // Build array
            var arraySize = 50000000;
            var array = GetArray(arraySize);

            var stopwatch = Stopwatch.StartNew();

            // Run N Threads to deal with N SMALLER problems
            var elementsPerCore = arraySize / coresCount;
            var elementsLeftOver = arraySize % coresCount;

            for (int i = 0; i < coresCount; i++)
            {
                var startIndex = i * elementsPerCore;
                var elementsToProcessCount = elementsPerCore;

                if (i == coresCount - 1)
                {
                    elementsToProcessCount += elementsLeftOver;
                }

                var arrayProcessor = new ArrayProcessor(array, startIndex, elementsToProcessCount);
                arrayProcessors.Add(arrayProcessor);

                var thread = new Thread(arrayProcessor.CalculateSum);
                threads.Add(thread);
                thread.Start();
            }

            // Wait for the tasks to finish and calculate the final sum
            BigInteger totalSum = 0;
            for (int i = 0; i < threads.Count; i++)
            {
                threads[i].Join();
                totalSum += arrayProcessors[i].CalculatedSum;
            }

            stopwatch.Stop();

            // Elapsed time: 1900-3300 ms
            // Sum: 1249999975000000
            Console.WriteLine($"Elapsed time: {stopwatch.Elapsed.TotalMilliseconds}");
            Console.WriteLine($"Sum: {totalSum}");
        }
Exemplo n.º 24
0
        static void Main(string[] args)
        {
            // Build array
            var arraySize = 50000000; // 50 000 000
            var array = GetArray(arraySize);

            // Run ONE THREAD to deal with ONE LARGE PROBLEM
            var stopwatch = Stopwatch.StartNew();

            var startIndex = 0;
            var elementsToProcessCount = arraySize;

            var arrayProcessor = new ArrayProcessor(array, startIndex, elementsToProcessCount);

            arrayProcessor.CalculateSum();
            var totalSum = arrayProcessor.CalculatedSum;

            stopwatch.Stop();

            // Еlapsed time: 6700-7100 ms
            // Sum: 1249999975000000
            Console.WriteLine($"Elapsed time: {stopwatch.Elapsed.TotalMilliseconds} ms");
            Console.WriteLine($"Sum: {totalSum}");
        }
Exemplo n.º 25
0
 public void Setup()
 {
     array = new ArrayProcessor();
 }
 public void Setup()
 {
     _processor = new ArrayProcessor();
 }
Exemplo n.º 27
0
    static void Main()
    {
        // Determine how many cores/processors there are on this machine.
        //
        int coreCount = Environment.ProcessorCount;

        Console.WriteLine("Process/core count = {0}", coreCount);

        // Get some data to work with.
        //
        double[] data = GetData();

        Stopwatch sw = Stopwatch.StartNew();

        // Setup same-sized arrays of references to ArraySlice
        // Thread objects; one per core/processor.
        //
        ArrayProcessor[] slices  = new ArrayProcessor[coreCount];
        Thread[]         threads = new Thread[coreCount];

        // Divide the work (roughly) evenly among the
        // number of threads we're about to start.  The last
        // thread will pickup any leftovers if the data size
        // is not evenly divisible by the core count.
        //
        int indexesPerThread = data.Length / coreCount;
        int leftOverIndexes  = data.Length % coreCount;

        for (int n = 0; n < coreCount; n++)
        {
            int firstIndex = (n * indexesPerThread);
            int lastIndex  = firstIndex + indexesPerThread - 1;

            if (n == (coreCount - 1))
            {
                lastIndex += leftOverIndexes;
            }

            // Setup the array slice that describes this
            // portion of the array.
            //
            ArrayProcessor slice = new ArrayProcessor(data, firstIndex, lastIndex);
            slices[n] = slice;

            // Start the thread that will process this slice.
            //
            threads[n] = new Thread(slice.ComputeSum);
            threads[n].Start();
        }

        // Once all the threads have been started, wait for
        // them to complete their assigned computations, then
        // add their sum to the running total.
        //
        double sum = 0;

        for (int n = 0; n < coreCount; n++)
        {
            threads[n].Join();
            sum += slices[n].Sum;
        }

        sw.Stop();

        // Display the results.
        //
        Console.WriteLine(
            "{0} threads computed {1:n0} in {2:n0} ms",
            coreCount, sum, sw.ElapsedMilliseconds
            );

        Console.ReadKey();
    }
Exemplo n.º 28
0
        public void ThrowsWIthNull()
        {
            ArrayProcessor processor = new ArrayProcessor();

            Assert.Throws <ArgumentNullException>(() => processor.SortAndFilter(null));
        }
Exemplo n.º 29
0
        public void UniqueValue(double[] expectedResult, double[] obtainedResult)
        {
            ArrayProcessor arrayProcessor = new ArrayProcessor();

            CollectionAssert.AreEqual(expectedResult, arrayProcessor.UniqueArray(obtainedResult));
        }
Exemplo n.º 30
0
        public void TestPosition(int[] expectedResult, int[] obtainedResult)
        {
            ArrayProcessor arrayProcessor = new ArrayProcessor();

            CollectionAssert.AreEqual(expectedResult, arrayProcessor.SortAndFilter(obtainedResult));
        }
Exemplo n.º 31
0
        public void Removing_excess(int[] expectedResult, int[] obtainedResult)
        {
            ArrayProcessor arrayProcessor = new ArrayProcessor();

            CollectionAssert.AreEqual(expectedResult, arrayProcessor.CreateNewArray(obtainedResult));
        }
Exemplo n.º 32
0
        public void SortTest(int[] expectedResult, int[] obtainedResult)
        {
            ArrayProcessor arrayProcessor = new ArrayProcessor();

            CollectionAssert.AreEqual(expectedResult, arrayProcessor.SortArray(obtainedResult));
        }