vendredi 5 janvier 2018

Testing Several Inputs with a Pseudo Random Number Generator (PRNG)

Question Background

I already have an O(nlog(n)) solution to a given problem which I'm using as a checker to test a new faster solution (O(n)). Both methods have the same syntax, they receive an array as a parameter and return a long value. So, I'm generating the input array using the regular PRNG and I run N experiments which check both solutions give the same output. I always use the same Seed value at the begining to get the same stream of numbers from the PRNG.

I implemented it all in a single test method which (for what I know) goes beyond of what it's considered a Unit Test. In fact, I saw a similar implementation in the book Beautiful Code but I did not find some explanations about the question below. The closer I got was this 9 years old question at stackoverflow.

The Question(s)

How do you theoretically call this type of tests, I'm I coding it properly? As for what I researched I'm confused if it could be a slight variation of a Smoke Test, a Monkey Test or even none of them. Should I create a Unit Test for every failed experiment? Should I set up something else that does it automatically. What is the correct approach when you want to get as much as you can in this scenario.

Explanation of my code The code is pretty simple actually because the methods that solve the problem are irrelevant for the purpose of the question. Anyways, the main part is contained within the while loop and is basically generate the input array, call the slow solution, call the fast solution and check both are equals.

        public static int MySeed = 1000000007;
        public static Random random;

        [TestMethod()]
        public void TestSeveralInputSizes()
        {
            random = new Random(MySeed);
            int numberOfExperiments = (int)1e4;
            int maxArraySize = (int)1e2;
            while (numberOfExperiments-- > 0)
            {
                var array = GenerateRandomArray(random.Next(maxArraySize));
                long expectedAnswer = SlowSolution(array);//O(nlog(n))
                long myAnswer = FastSolution(array);//O(n)
                Assert.AreEqual(expectedAnswer, myAnswer);
            }
        }

        public long[] GenerateRandomArray(int maxArraySize)
        {
            //code to generate the array
        }

Aucun commentaire:

Enregistrer un commentaire