Esempio n. 1
0
        public void PartitionTestSubset()
        {
            int[] testArrayLengths = new int[] { 1, 10, 100, 999 };
            int[] seeds            = new int[] { 1, 54353, 6363, 756757 };

            /*
             *  Instantiate and seed random number generator within
             *  method to keep array elements consistent between tests
             */
            Random arrayNumberGenerator = new Random(2017);

            foreach (int arrayLength in testArrayLengths)
            {
                foreach (int seed in seeds)
                {
                    for (int start = 0; start < arrayLength / 4; ++start)
                    {
                        for (int end = arrayLength - arrayLength / 4; end < arrayLength; ++end)
                        {
                            Partitioning.SetSeed(seed);
                            //generate random array of given lenth
                            int[] array = new int[arrayLength];
                            for (int i = 0; i < arrayLength; ++i)
                            {
                                array[i] = arrayNumberGenerator.Next();
                            }

                            int pivot = Partitioning.Partion <int>(array, start, end);

                            for (int i = start; i < pivot; ++i)
                            {
                                Assert.IsTrue(array[i] <= array[pivot]);
                            }

                            for (int i = pivot + 1; i < end; ++i)
                            {
                                Assert.IsTrue(array[i] > array[pivot]);
                            }
                        }
                    }
                }
            }
            //Console.ReadLine();
        }
Esempio n. 2
0
        public void PartitionTestWholeArray()
        {
            int [] testArrayLengths = new int[] { 1, 10, 100, 999 };
            int[]  seeds            = new int[] { 1, 54353, 6363, 756757 };

            /*
             *  Instantiate and seed random number generator within
             *  method to keep array elements consistent between tests
             */
            Random arrayNumberGenerator = new Random(2017);

            foreach (int arrayLength in testArrayLengths)
            {
                foreach (int seed in seeds)
                {
                    Partitioning.SetSeed(seed);
                    //generate random array of given lenth
                    int[] array = new int[arrayLength];
                    for (int i = 0; i < arrayLength; ++i)
                    {
                        array[i] = arrayNumberGenerator.Next();
                    }

                    int pivot = Partitioning.Partion <int>(array, 0, array.Length);

                    Console.Out.WriteLine($"Partitioned Array = {string.Join(",",array)} and pivot = {pivot}");

                    for (int i = 0; i < pivot; ++i)
                    {
                        Assert.IsTrue(array[i] <= array[pivot]);
                    }

                    for (int i = pivot + 1; i < arrayLength; ++i)
                    {
                        Assert.IsTrue(array[i] > array[pivot]);
                    }
                }
            }
            //Console.ReadLine();
        }
        public void TestSelection()
        {
            int[] testArrayLengths = new int[] { 1, 10, 100, 999 };
            int[] seeds            = new int[] { 1, 54353, 6363, 756757 };

            /*
             *  Instantiate and seed random number generator within
             *  method to keep array elements consistent between tests
             */
            Random arrayNumberGenerator = new Random(2017);

            foreach (int arrayLength in testArrayLengths)
            {
                foreach (int seed in seeds)
                {
                    Partitioning.SetSeed(seed);
                    //generate random array of given lenth
                    int[] array = new int[arrayLength];
                    for (int i = 0; i < arrayLength; ++i)
                    {
                        array[i] = arrayNumberGenerator.Next();
                    }

                    int[] sortedArray = new int[arrayLength];

                    array.CopyTo(sortedArray, 0);

                    Array.Sort(sortedArray);

                    for (int i = 0; i < arrayLength; ++i)
                    {
                        int selectedValue = Selection.SelectKthValue(array, i, 0, arrayLength);
                        Assert.AreEqual <int>(sortedArray[i], selectedValue);
                    }
                }
            }
        }