7

C++ Stress-Test Template

 6 months ago
source link: https://codeforces.com/blog/entry/126901
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

C++ Stress-Test Template

Stress-Testing involves comparing your incorrect solution with a correct one to pinpoint where your code is going wrong. Normally, you don't have access to the correct solution, but if you can create a brute force solution that you're confident is accurate, then you're good to go.

I recommend you use a 2nd file to store this template for cleaner implementation.

Solution.cpp
stressTest.cpp ⭐

How to use it?

Let's say you have the following problem.

Max Product

You made the following solution and can't figure out why it's wrong.

Your incorrect solution

Step 1: Make a brute force solution.

Correct Brute Force solution

Step 2: Make a random test case generator

Generate a TestCase

Step 3: Make sure you uncomment // #define STRESS_TEST to enable stress testing.

Now when you run this file, you'll see an output as:

Output

Now you realize that you didn't consider multiplication of smallest 2 elements. Hence you change it, and get Accepted

Updated Solution

Here answer was verified using brute force solution. But what if there were multiple correct answers? How would you do then? What if instead of returning the product, we need to return it's index?

Even then it is simple, we need to tweak testCasePassed() function like this and our job is done.

testCasePassed() function

I used a very simple question to illustrate, but you get the point.

Why is it better than other stress-test templates?

  • It's beginner friendly and easy to use. You don't need to create 5 different files and run a bash script to test it. Most beginners are uncomfortable with terminal.
  • It clearly tells you the testcase your code fails, instead of just TestCase no, Expected Output and Received Output and then you figure out that yourself by looking at input.txt
  • It can be used even with questions that have multiple correct answers.
  • It can work with interactive problems too, but it needs some extra functions. I'll update how to do that after a while. (I'll be glad if someone in comments does it first.)

Note:

  • Keep the range of random number small in beginning. Like between 0 — 10. (If you don't find required test case, then increase its range).
  • Don't forget to uncomment // #define STRESS_TEST. This is what enables it in the first place.
  • I am using srand(time(0) as seed to generate new random numbers everytime, but you can provide your own seed everytime however you like. Or just use uniform_int_distribution.
  • Output will go to stderr, so it won't interfere with anything else. If you use File I/O, make sure to have freopen("error.txt", "w", stderr);

Do checkout my other blog C++ Debug Template!

I hope it was useful to you. Please upvote / leave a comment if you liked my blog. Thank you.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK