You are not logged in. Please login at www.codechef.com to post your questions!

×

# 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%

19 Answers:
 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 ♦♦ 15.2k●347●484●504 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●224 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; }

answered 17 Mar '15, 21:05

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●37 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%
 0 i want to know why compile error at include in using c answered 31 Oct '16, 14:55 0★bino123 1 accept rate: 0%
 0 Learn gdb from here and here (vedios from CS50 by harvard).It is a commandline runtime debugging tool. It supports many languges but the tutorials are specifically for c and can be extended to c++ easily. For other languages also you can find the tutorials. It makes debuggig really faster and you dont have to change your program again and again for debugging, answered 02 Nov '16, 10:56 539●8 accept rate: 27%
 0 Answer is hidden as author is suspended. Click here to view. answered 04 Nov '16, 16:57 (suspended) accept rate: 0%
 0 import java.io.*; class firstpro{ public static void main(String args[])throws IOException{ int[] a={1,2,88,42,99}; int i; System.out.println("Input:"); for(i=0;i<5;i++){ System.out.println(a[i]); } System.out.println(); System.out.println("Output:"); for(i=0;i<5;i++){ if(a[i]==42) break; else System.out.println(a[i]); }  } } This is my program for input and output processing but codechef is giving me wrong answer. I even tested this answer on ideone and there it got successfully executed. so why codechef is giving me wrong answer?? answered 11 Jan, 16:35 1 accept rate: 0%

# include<stdio.h>

int main() {

int i,n,a,b,s=0; printf("enter the number\n"); scanf("%d",&n); for(i=1;n>0;i++) { a=n%10; b=n/10; n=b; s=s+a; } printf("sum is %d",s); return 0; }

answered 16 Apr, 12:10

1
accept rate: 0%

 0 You can check your code on https://www.codechef.com/ide...Write test case and give the custom input and check the corresponding output answered 16 Apr, 23:51 31●1 accept rate: 0%
 0 Can anyone tell me whether the time limit given for a program is for individual test cases or for the entire test cases answered 17 Apr, 10:27 2★ram_24 1 accept rate: 0%
 0 @princelegolas Excuse Me Sir/Ma'am... What about C++'s double and long double? Even they won't be able to store 100! ? I don't think so... answered 28 Apr, 17:27 1 accept rate: 0%
 0 After putting my code on ideone, ideone is giving correct output on the sample input but codechef is saying the solution is wrong.Why?? answered 26 May, 08:27 1 accept rate: 0%
 toggle preview community wiki:
Preview

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• link:[text](http://url.com/ "title")
• 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

Question tags:

×1,447
×166
×155

question asked: 21 Jun '12, 10:56

question was seen: 31,965 times

last updated: 26 May, 08:27