Testing of own code.

Problems given has examples of very small size, when you try that you get correct answer and in less time. but, when the expected range is so long how to check that one.
My question is can i get the testCase file of the code, which will be tried out by the codechef ?(Practice Section).
Problem: yes, I know when anyone has testCase file he may submit code which generate static output.

Test case generation is mostly required for hard or Challenge problem questions where testing your code is important. For easy questions , you should be able to pass in most cases by making a good algorithm and checking for simple corner cases.

However if you want to generate your own test file, you can write a simple Python or C code to generate random test cases within the given constraints.You can also impose constraints in your code to ensure that you generate specific test cases.Here is a test case generator i had written for the problem LEBAMBOO in October 2013 Long for reference.(You will need to read the question to unserstand exactly what it is doing.)

#Test case generator for LEBAMBOO, October 2013 Long

import random
l=[i for i in range(4,10)] #limit length of each test case

for qq in range(100):  #generate 100 test cases
   ans=0
   initial=[]
   a2=[]
   leng=random.choice(l)  #choose length of test case

   for i in range(leng):
     initial.append(random.choice([1,2,3]))
   for i in initial:
      a2.append(i)
   for i in range(random.choice([3,4,5,6,7,8,9,10])):
      pos=random.choice([u for u in range(leng)])
      if(initial[pos]==1):
          i-=1
      else:
         ans+=1
         for i in range(pos):
            initial[i]+=1
         initial[pos]-=1
         for i in range(pos+1,leng):
             initial[i]+=1

   cnt=0

   for i in xrange(leng):
       if(initial[i]-a2[i]!=0):
          cnt+=1
   if(leng==cnt+1 and ans%2==1):
       print a2
       print initial
       print leng,ans,cnt

(P.S. The indentation might have gone wrong during pasting so you might need to modify it)

1 Like

@kcahdog How should one proceed writing this test case file of own (i know very very little about python, so it would be great if you can give example of c/c++/java file)??

@damn_me : The approach in C/C++ will be similar to Python.Main point is to realize what are the limits of the problem and what sort of test case do you want to generate and print all this data to some file.Sample steps:

1)Print no. of test cases. You can enter this manually.

  1. Generate a list of N random numbers or you can specify that choose numbers between range 10^6 to 10^7 , something like that. You can choose N equal to the maximum value specified in the question.

The point of generating test cases is that you compute the correct answer to the question while generating the test case or you can also do it manually.You can also use some brute force solution(which will exceed time limit ) to compute the actual answer and compare it with the answer given by your code. However it is highly problem dependent and you will have to approach each question differently.

1 Like
//