Importance of Testing In Competitive Programming

Many times while solving problems, we face issues like time limit exceeds, wrong solution, runtime error, and memory limit exceeds because after designing algorithm we don’t test algorithm efficiency, correctness, time complexity and running the algorithm on a large set of inputs for testing purpose.
This is where stress testing comes for rescue. Stress testing is the common way to find the bug in the algorithm.
which involves generating a large set of random test cases and checking if a brute force algorithm and an efficient algorithm always agree with each other. Many programmers are not familiar with this kind of comprehensive testing.
stress testing helps in finding an efficient algorithm and correct algorithms to problems but also see why certain approaches do not work. In particular, it is easy to sketch intuitive greedy solutions to any problems, but such solutions often do not work in reality.
Write a Generator program which output some random input.
Step’s Involed In Stress Testing
Repeatedly
– Generate a random input.
– Run your solution on it.
– Check if the output is correct.
– if not-stop and output the test cases.

Generate small tests for faster and easier debugging especially for trivial solution. make parameters to easily tweak test size. Do not loose generality and check for special cases.
1 Stress test after manual test.
2 No Point if generator/trivial solution / checker is too complex
3 Start with very small test sizes.
4 Couple of minutes running testing program is usually enough.
5 if bug is not found, generate larger tests.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Find a Maximum pairwise product in array of Integer.
#include <bits/stdc++.h>
using namespace std;
  
// trivial solution
long long MaxPairwiseProductslow(const std::vector<int>& v)
{
    int n = v.size();
    long long max_product = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            max_product = max(max_product, (long long)(v[i]) * (v[j]));
        }
    }
  
    return max_product;
}
  
// efficient solution
long long MaxPairwiseProduct(const std::vector<int>& v)
{
    sort(v.begin(), v.end());
    int n = v.size();
    long long max_product = 0;
  
    max_product = ((long long)v[n - 2]) * v[n - 1];
  
    return max_product;
}
  
int main()
{
    // random generator
    while (true) {
        int n = rand() % 10 + 2;
        cout << n << " \n";
        vector<int> a;
        for (int i = 0; i < n; i++) {
            a.push_back(rand() % 100000);
        }
        for (int i = 0; i < n; i++) {
            cout << a[i] << " ";
        }
        cout << "\n";
  
        long long res1 = MaxPairwiseProduct(a); // result of efficient solution
        long long res2 = MaxPairwiseProductslow(a); // // result of trival solution
        if (res1 != res2) // when two outputs dont match
        {
            cout << "Wrong answer: " << res1 << res2 << "\n";
            break;
        }
        else {
            cout << "Sucess"
                 << "\n";
        }
    }
  
    int n;
    std::cin >> n;
    std::vector<int> v(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> v[i];
    }
    long long result = MaxPairwiseProduct(v);
    std::cout << result << "\n";
  
    return 0;
}

chevron_right


Input Example: 
3
1 2 3
Output: 6
Input Example:
7
7 5 6 1 3 7 10
Output:
140

Summary Test your solution before to avoid errors. Start testing with given samples. check for corner cases. special/hidden test cases. Manual cases like min-max, problem-type specific and anything you could imagine. test different part separately. If everything else fails, run stress-test

competitive-programming-img




My Personal Notes arrow_drop_up

Recommended Posts:


    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


    Article Tags :

    1


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.