×

# How should I test my program

 2 2 I am done writing the code. And I am not sure if my program works correctly or not. How do I go about testing it? asked 21 Jun '12, 10:56 447●10●12●17 accept rate: 0%

 13 The best way of testing your program is doing so in exactly the same way that Codechef does. Create an input file (for example, in.txt). Then run your program from the command line, using < and > to redirect the streams. For example: java test < in.txt > out.txt or test.exe < in.txt > out.txt Your output will then be in the out.txt file, so you can check if it is correct. Often people forget to print a new line between test cases, but this is easily avoided if you use this method of testing. answered 21 Jun '12, 10:58 0★admin ♦♦ 14.6k●347●483●502 accept rate: 36% Or use freopen("in.txt","r",stdin); and freopen("out.txt","w",stdout); (21 Feb '16, 00:55)
 3 There are a few small things that you can keep in mind while testing your code. New programmers usually forget to add a new line, testing on ideone will reveal such errors easily. Sometimes we forget to reinitialize variables,arrays after every testcase. So input same test case atleast twice and check if same correct output is obtained. Some people use a "solve()" function for each test case , so variables are reinitialized automatically. Always keep the constraints in mind, and know the range of the datatype you are using. I prefer to use long long int most of the times. Though some of the questions require even higher range.for ex. 100! cannot be represented in any data type in C++. Try your code against boundary cases, if possible. Always check against small values like 0,1,2,3. In questions involving modular arithmetic (Ans%SomeMod), ensure that ans is not obtaining a negative value , use (ans + mod)%mod to check that. Recently in a question in nov long (chefwords), i missed a case involving duplicate strings and wasted so many hours. So take care of "Duplicates". Keep Practicing, experience will teach you everything. This should help a bit :) answered 19 Nov '14, 18:56 235●3 accept rate: 15%
 0 Very good approach to test the code is to use ideone.com, because CodeChef and ideone use same back-end. Please be aware, that even contest rules says: As a contestant, you are responsible for making sure others don't access the code that you submit. If you're on a network or shared computer, or using an online development environment, make sure that other contestants don't have access to your code. If you use Ideone, make sure to mark your submission "private" (not secret)". answered 19 Nov '14, 15:05 16.8k●49●115●223 accept rate: 11%
 0 5 5 14 6 18 6 21 7 8 7 14 answered 10 Feb '15, 20:02 2★sajib123 1 accept rate: 0%
 0 i have use ideone.com to check my code its working there but when i submit my code here it shows runtime error SIGSEGV answered 08 Mar '15, 16:26 1 accept rate: 0%

# include<string.h>

int main() { int t,i,j,m,g=0,n,len,k,v=0,len1,z=0,h,u,x[5],l=0,h1,m1,m2; char a[1100],s[1100],c,d[1100]; scanf("%d",&t); for(i=0;i<t;i++) { g=0,z=0; gets(a); len=strlen(a); scanf("%d",&n); //printf("Pppppppp\n"); for(j=0;j<n;j++) { h=0; gets(s); len1=strlen(s); for(u=z;u<z+len1;u++) { d[u]=s[h]; h++; } d[u]='\0'; z=z+len1+1; } /for(j=0;j<z;j++) printf("%c",d[j]); printf("\n");/ k=0; for(h1=0;h1<n;h1++) { //printf("PPPPPPP\n"); //if(l==0) //l=0; m1=k; for(j=0;j<len;j++) { m2=0; while(d[k]!='\0'&&d[k]==a[j+m2]) { k++; m2++; } if(d[k]=='\0') { //printf("OOOOOOOOOOOOOOOO\n"); for(m=j;m<j+m2;m++) a[m]='9'; ++g; k=k+1; //printf("%d\n",g); //l=1; break; } else k=m1; } } x[v]=g; v++; } for(i=0;i<v;i++) if(x[i]%2!=0) printf("Teddy\n"); else printf("Tracy\n"); return 0; }

1
accept rate: 0%

 0 One of the way of testing is to generate random test cases that can be solved manually for solutions and test your code against those test cases to figure out the bug.. For Example these are some of the random cases generator for problems of March Long Contest in PYTH 2.7.x #Test case generator for DEVCLASS import random task=[0,1,2] case=["B","G"] test=100 print test for i in range(test): a='' print random.choice(task) siz=random.randrange(1,15) for j in range(siz): a+=random.choice(case) print a #------------------------------------------------# #------------------------------------------------# #Random Test case generator for SINEWAVE import random #number of test cases = 50 T=50 def task1(): print T for i in range(T): a="" s=random.randrange(0,12+1) c=random.randrange(0,12+1) k=random.randrange(1,25+1) a+=str(s)+" "+str(c)+" "+str(k) print a def task2(): print T for i in range(T): a="" s=random.randrange(0,50+1) c=0 k=random.randrange(1,50+1) a+=str(s)+" "+str(c)+" "+str(k) print a def task3(): print T for i in range(T): a="" s=random.randrange(0,50+1) c=random.randrange(0,50+1) k=1 a+=str(s)+" "+str(c)+" "+str(k) print a def task4(): print T for i in range(T): a="" s=random.randrange(0,50+1) c=random.randrange(0,50+1) k=random.randrange(1,100+1) a+=str(s)+" "+str(c)+" "+str(k) print a def forTesting(): print T for i in range(T): a="" s=random.randrange(0,10+1) c=random.randrange(0,10+1) k=random.randrange(1,10+1) a+=str(s)+" "+str(c)+" "+str(k) print a forTesting()  If you are using C/C++, add this in your code  #ifndef ONLINE_JUDGE freopen("test.in", "r" , stdin); freopen("test.out", "w", stdout); #endif  Copy random test cases in a file named test with extension .in in same directory your source code is saved and test your code or you may use ideone or codechef IDE .. Believe me you need patience but this works for 70-80% of the time... Good Luck!! .. :) answered 19 Mar '15, 00:33 1.9k●1●10●36 accept rate: 14%
 0 import java.io.*; class cc1 { public static void main(String args[])throws IOException { int N[]=new int[1000]; int r[]=new int[1000]; int T,i; InputStreamReader read=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(read); T=Integer.parseInt(br.readLine()); if(T>0 && T<1001) { for(i=0;i0 && N[i]<100001) { while( N[i] != 0 ) { r[i] = r[i] * 10; r[i] = r[i] + N[i]%10; N[i] = N[i]/10; } System.out.println(r[i]); } } } }  } please check my program and tell why code chef is giving wrong answer for it question link is-https://www.codechef.com/problems/FLOW007 answered 10 Feb '16, 13:21 1 accept rate: 0%
 0 the biggest problem I'm having is the codechef runs the code, then says 'wrong answer', but no feedback as to what was wrong with the output from my program. My program works for the presented inputs and meets (as I read the criteria) all the listed criteria. How should I proceed in such cases? answered 21 Feb '16, 00:35 0★rkwill 1●1 accept rate: 0%
 0 Use ideone(online ide) or other ide for testing. answered 21 Feb '16, 12:46 35●5 accept rate: 0%
 toggle preview community wiki

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Tags:

×1,211
×164
×138