How to test my answer perfectly ?

This is a very common problem with many beginners like me.
When we solve a particular question how should I test it??

Many a times we get correct answer for the test cases provided in question ( And other own test cases ) but still we get WA when submitted.

How should we solve this problem?

PS:
About Basic testing

On codechef platform testing is done by providing input from text file to STDIN and writing STDOUT onto another text file

For example if your problem is compiled and resulting execution file is test.exe

Then enter the inputs in “in.txt” and compile using

./test <in.txt> out.txt

In this case output is written to out.txt file

Also if you are using gcc compiler (c++) you can check execution time using

time ./test <in.txt> out.txt

Make sure you understand the difference in processors!!

7 Likes

First of all, keep the constraints in mind. Besides the sample test cases and a few simple ones of your own, check cases at the extreme ends. For example, if in a problem, the input string length can be less than or equal to 100 then make sure you test the program on a string of length 100.
The usual way to test is to use a less efficient but correct program to find the answer (e.g. brute force). Now generate a few random test cases along with the boundary test cases mentioned above and check it with both the brute force program and your program. Practice a lot. Solve lots of problems in the CC archives and perfect this method.

7 Likes

Please accept the answer (By clicking on the tick in the left side of the answer) if you feel it has answered your question. If not then add comments requesting clarification.

1 Like

In addition to what balajiganapath had said earlier.

Try to avoid common mistakes, your code runs correctly for some cases doesn’t really mean that you might not have made them. For instance check my submissions for the problem LUCKY2, my code 895278was running correct for 0.55s but still there was a pretty common mistake at line 38.

Everyone would have developed a particular style of programming over the years. Due to this it is possible for you make the same mistake at many different places. It is easier said than done to ‘not repeat your mistakes’. So make a checklist of the common mistakes that you frequently make, making additions whenever you discover new ones. Whenever your code is doing as expected go through this checklist and make sure you have not repeated these mistakes.

Learn to debug your program. Debugging is just a process of finding the existence of bugs in your program. You don’t need to use special debuggers like gdb for most cases. At few crucial points have something printed a text or contents of variables. Of your created inputs which include corner cases as well run your program and go through this debug trace and try to figure out information.

If no bug is found that is not the end of road for debugging, possibly your test cases were not strong enough to find the bug. Try to come up with a more stringent test cases by trying to figure out what could go wrong. This could be hard at times but still you need to try. One example is if you print some debug info at the start of some condition and in your debug trace you don’t find it anytime then it clearly indicates that your code doesn’t execute the code with that portion for any of your test case. Either look for a potential bug in the code inside that condition or try to come up with a test case which will ensure that particular piece of code runs.

If you are still not able to find any mistake then it is possible that you are not able to come with that particular case which is part of the input on the judge. In this case just try putting assert statements at various places to ensure some conditions which should be ideally true in any run of the program. Run this on the judge and if the assert(s) fail then you are that there is definitely something wrong and then you should try to look up for a possible cause of that. Caution: Don’t try this in contests with penalties for wrong submissions as it will attract a penalty of 20 mins for each wrong submission.

5 Likes

– Placing everything together and adding a bit to it.

**NOTE:**Also refer to gultus and balajiganapath answers

DataTypes & Overfloaw

Make a note of the constrains and get the maximum possible value that will be generated at any instance. And make sure your datatype will hold that value.
(Approx) 32Bit integer can hold upto 9digit values while 64Bit can hold upto 19Digits

Very Large Sets

In some cases we cannot test individual values, like for example if you are using Prime Sieve and you want to test its correctness. Put a counter and check how many primes are generated. Now compare these with values on Web. Then randomly check few values and that will do the work.

There are many online calculators which even expresses very hard mathematical answers.

Refer - WRA

This website answers questions like “primes <= (10^9)”. It is very useful for testing.

Floating Point Precision

In many problems we are asked to print only 2(or some x) digits after the decimal point.In this type of problems be very careful with what is actually asked. In few questions they ask to print only 2 digits after decimal point by rounding off and in other they ask you not to roundoff

Ex: value is 3.1476…

2 Decimals Rounded is 3.15

2 Decimals Without Rounding is 3.14

Re-Initializing

One more common error is Re-Initializing few values at the start of every test case. Ex:Counters;

Extreme Bounds

Check for working of the program at extreme values. Not just the answer but try and print every value that is calculated during the process and check for correctness of all the values. This will make sure the METHOD being used is correct.

Brute Force Testing

For testing you can always use another program which does the same work but with low efficiency and high perfection.

Direct Test

When the constrains are small print all the values and check randomly. Even for larger values print values at every interval of 50 or 100 and check those. Mainly notice that none of then are 0. Because 0 occurs due to Datatype Overflow error.

Debug

This topic is limitless. To mention a few make sure your code is first following correct execution order. Insert print statements before,inside and after ever loop/conditionals to check how it is working.
Segmentation Fault - Use a similar method and find where exactly the program is breaking.
Then Check for following exceptions - Array Out Of Bound and Null Pointer Access Violation.

Happy Coding :slight_smile:

8 Likes

Well for this particular Question there cant be one particular answer. :slight_smile:

1 Like

Ok, I am making this a community wiki.

One more small thing u need to be aware of is to check weather all the variables are initialized or not…!
Else you may get wrong output…!!

1 Like

Test with edge cases to get sure that code works fine at border cases even.

If you are using static typed language (eg. C++), your compiler is a free common bugs finder. Set bunch of compiler flags to enable all possible warnings.

g++ code.cpp -o prg -O2 -W -Wall -Wshadow -Wformat -Wsequence-point -Wunused -Wuninitialized -Wfloat-equal
1 Like

@balajiganapath , i have never tried the way of testing mentioned by you. I had a basic problem in trying out your method . I simply dont know where to put “./test <in.txt> out.txt” ! I tried putting that line at top & bottom of my c++ code but it gives error . Please tell me where to put that line ???