How should I test my program

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?

2 Likes

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.

12 Likes

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)".

1 Like

There are a few small things that you can keep in mind while testing your code.

  1. New programmers usually forget to add a new line, testing on ideone will reveal such errors easily.
  2. 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.
  3. 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++.
  4. Try your code against boundary cases, if possible. Always check against small values like 0,1,2,3.
  5. In questions involving modular arithmetic (Ans%SomeMod), ensure that ans is not obtaining a negative value , use (ans + mod)%mod to check that.
  6. 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 :slight_smile:

9 Likes

5
5 14
6 18
6 21
7 8
7 14

i have use ideone.com to check my code its working there but when i submit my code here it shows runtime error SIGSEGV

1 Like

#include<stdio.h>
#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;
}

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!! … :slight_smile:

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;i<T;i++)
{
N[i]=Integer.parseInt(br.readLine());

		if(N[i]>0 && 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-FLOW007 Problem - CodeChef

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?

7 Likes

Use ideone(online ide) or other ide for testing.

i want to know why compile error at include<conio.h> in using c

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,

in manually u can check all the possible inputs into it and can check out. i used this method to test my last work about kerala houseboat packages. also there are a lot of testing tools are available in the market.

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??

#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;
}

You can check your code on https://www.codechef.com/ide...Write test case and give the custom input and check the corresponding output

Can anyone tell me whether the time limit given for a program is for individual test cases or for the entire test cases

@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…

After putting my code on ideone, ideone is giving correct output on the sample input but codechef is saying the solution is wrong.Why??