Algorithmic Toolbox week1 Stress Test

 We will learn a powerful technique called stress testing.

 By the end, you will be able to implement a stress test for your solution of an algorithmic problem,use it to find a small and easy test on which your program fails, debug your program, and make sure it works correctly afterwards high confidence.

What is stress testing?

 It is a program generates random tests in an infinite loop, and for each test, it launches your solution on this test and an alternative solution on the same test and compares the results.
 This alternative solution you also have to invent and implement yourself, but it is usually easy, because it can be any simple, slow, brute force solution, or just any other solution that you can come up with.

 Then you just wait until you find a test on which your solutions differ.
 When you find a test on which your solutions' answers differ, you can determine which one of them returns wrong answer and debug it, fix it, and then repeat the stress testing.

#include 
#include 
#include 

using std::vector;
using std::cin;
using std::cout;
using std::endl;

int main() {
    while(true) {
        int n = rand() % 10 + 2;
        cout< a;

        for(int i=0; i

 So what do we do in principle is we generate some random tests, then we launch both solutions, MaxPairwiseProduct and MaxPairwiseProductFast on this random test, and we compare the results.
 And the idea is if you have a correct solution and another correct solution and the correct answer for your problem is the only correct answer, then any two correct solutions will give the same answers for any test.
 And if you have some wrong solution and some correct solution, then on some tests, their answers will differ.
 And also if you have two wrong solutions,then probably they're wrong in a different way, and then there will also be some test, hopefully, on which their answers differ.

 First, we need to generate the test for our problem. We'll start with generating number n, the number of numbers(2~11).

  Why is it so small? Well, we first want both our solutions to work fast enough. And also if we create a big random test, it will be hard for us to debug it, so we start with some relatively small value of n.

 So we basically inserted this code in our regular program. But instead of reading numbers from the input, it will first try to find the test for which our two solutions differ.

你可能感兴趣的:(Algorithmic Toolbox week1 Stress Test)