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 signature: they receive an array as a parameter and return a long
value. So, I'm generating several array inputs using the regular PRNG and running N
experiments which check both solutions. I always use the same Seed value at the beginning 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 question.
The Question(s)
Should I create a Unit Test for every failed experiment? Should I set up something else that does it automatically? What should I do in this scenario?
Explanation of my code
The code is actually pretty simple 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 basically generates the input array, calls the slow solution, calls the fast solution and checks if both results 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
}